{echo:JS::import('form');}
{echo:JS::import('date');}
{echo:JS::import('editor');}
{echo:JS::import('dialog?skin=brief');}
{echo:JS::import('dialogtools');}
<script>
    var editor;
    KindEditor.ready(function(K) {
      editor = K.create('textarea[name="description"]', {
        uploadJson : '{url:/admin/upload_image}'
      });
    });
</script>

<h1 class="page_title">编辑商品促销活动</h1>
<form action="{url:/marketing/prom_goods_save}" method="post" callback="check_invalid" >
{if:isset($id)}<input type="hidden" name="id" id="objId" value="{$id}">{/if}
<div id="obj_form" class="form2 tab">
    <!-- tab 头 start -->
    <ul class="tab-head">
      <li>基本信息</li>
      <li class="current">活动描述</li>
    </ul>
    <!-- tab 头 end -->
    <!-- tab body start -->
    <div class="tab-body">
      <!-- 基本信息 start -->
      <div>
      <dl class="lineD">
        <dt><b class="red">*</b> 促销活动名称：</dt>
        <dd>
          <input name="name" type="text" pattern="required" value="{$name}" class="big" />
          <label> </label>
        </dd>
        </dl>
        <dl class="lineD">
        <dt><b class="red">*</b>促销活动类型：</dt>
        <dd>
          <select id="prom_type" name="type">
            <option value="0">直接打折</option>
            <option value="1">减价优惠</option>
            <option value="2">固定金额出售</option>
            <option value="3">买就赠代金券</option>
            <option value="4">买M件送N件</option>
          </select>
        </dd>
        </dl>
        <dl class="lineD" id="expression">
        <dt><b class="red">*</b>折扣：</dt> <dd> <input name="expression" type="text" class="small"  pattern="int" value=""> <label>% 折扣值(1-100 如果打9折，请输入90)</label> </dd>
        </dl>
        <dl class="lineD">
        <dt><b class="red">*</b>开始时间：</dt>
        <dd>
          <input name="start_time" type="text" class="Wdate"   pattern="required"  onChange="" value="{$start_time|date('Y-m-d H:i:s',strtotime($start_time))|''}" onfocus="WdatePicker({minDate:'{echo:date('Y-m-d H:i:s')}',dateFmt:'yyyy-MM-dd HH:mm:ss'})">
          <label></label>
        </dd>
        </dl>
        <dl class="lineD">
        <dt><b class="red">*</b>结束时间：</dt>
        <dd>
          <input name="end_time" class="Wdate" type="text" pattern="required" onChange="" value="{$end_time|date('Y-m-d H:i:s',strtotime($end_time))|''}"  onfocus="WdatePicker({minDate:'{echo:date('Y-m-d H:i:s')}',dateFmt:'yyyy-MM-dd HH:mm:ss'})">
          <label></label>
        </dd>
        </dl>
        <dl class="lineD">
        <dt><b class="red">*</b>适合用户群：</dt>
        <dd>
          <label><input name="group[]" value="0" type="checkbox">默认会员</label>
          {query:name=grade} <label><input name="group[]" value="{$item['id']}" type="checkbox">{$item["name"]}</label>{/query}
        </dd>
        </dl>
        <dl class="lineD">
        <dt><b class="red">*</b>选择商品：</dt>
        <dd><a class="button" href="javascript:;" onclick="goods_select()">选择商品 ...</a><label class="red">&nbsp;&nbsp;注：任何商品只能参加一种活动！</label></dd>
        </dl>
        <div class="mt10">
            <table class="default" id="goods_list" >
            <tr><th style="width:0">选择</th><th>商品名称</th><th style="width:100px;">价格</th><th style="width:100px;">库存</th><th style="width:40px;">操作</th></tr>
            {if:isset($id)}
              {query:name=goods where=prom_id eq $id}
              <tr><td><input type="checkbox" name="goods_id[]" value="{$item['id']}" checked="checked"></td><td>{$item['name']}</td><td>{$item['sell_price']}</td><td>{$item['store_nums']}</td><td><a href="javascript:;" class="icon-close" onclick="goods_del(this)"></a></td></tr>
              {/query}
            {/if}
          </table>
         </div>
      </div>
      <!--基本信息 end -->
      <!--活动描述 start-->
      <div>
        <dl class="lineD">
        <dt><b class="red">*</b>活动描述：</dt>
        <dd>
          <textarea id="content" pattern="required" name="description" style="width:700px;height:360px;visibility:hidden;">{$description}</textarea>
          <label></label>
        </dd>
        </dl>
      </div>
      <!--活动描述 end-->
    </div>
      <div style="text-align:center"><input type="submit" value="提交" class="button">&nbsp;&nbsp;&nbsp;&nbsp;<input type="reset" value="重置" class="button"></div>

  </div>
</form>
<script type="text/javascript">
var form =  new Form();
form.setValue('parent_id','{$parent_id}');
form.setValue('type_id','{$type_id}');
$("#prom_type").on("change",function(){
  var type = parseInt($("#prom_type").val());
  var expression = '';
  switch(type){

    case 0:{
      expression = '<dt><b class="red">*</b>折扣：</dt> <dd> <input name="expression" type="text" class="small"  pattern="int" value=""> <label>% 折扣值(1-100 如果打9折，请输入90)</label> </dd>';
      break;
    }
    case 1:{
      expression = '<dt><b class="red">*</b>立减金额：</dt> <dd> <input name="expression" type="text" class="small"  pattern="float" value=""> <label>立减金额（元）</label> </dd>';
      break;
    }
    case 2:{
      expression = '<dt><b class="red">*</b>出售金额：</dt> <dd> <input name="expression" type="text" class="small"  pattern="float" value=""> <label>出售金额（元）</label> </dd>';
      break;
    }
    case 3:{
      expression = '<dt><b class="red">*</b>代金券：</dt> <dd><select name="expression">{query:name=voucher_template}<option value="{$item["id"]}">{$item["name"]}</option>{/query}</select></dd>';
      break;
    }
    case 4:{
      expression = '<dt><b class="red">*</b>买M送N：</dt> <dd> <input name="expression" type="text" class="small"  pattern="\\d+\/\\d+" value=""> <label>买几件送几件（如买3件送1件: 3/1）</label> </dd>';
      break;
    }
  }
  $("#expression").html(expression);
});
function goods_select(){
  var params = $("input[name='goods_id[]']").serialize()+"&id={$id}";
  var url = '{url:/marketing/goods_select}';
  if(url.indexOf('?')==-1) url = url+'?'+params;
  else url = url + '&'+params;
  art.dialog.open(url,{id:'goods_select',title:'选择商品：',width:900,height:500,padding: '10px'});
}

function goods_del(id){
  $(id).parent().parent().remove();
}

//表单验证回调处理tab定位
function check_invalid(e){
  if(e==null){
    return true;
  }else{
    var index = $('.tab-body > *').has(e).index();
    if(index!=-1){
      tabs_select(0,index);
      return false;
    }
    return false;
  }
}
var form = new Form();
form.setValue('type', {$type|0});
FireEvent(document.getElementById('prom_type'),'change');
form.setValue('expression', '{$expression}');
form.setValue('group[]', '{$group}');
</script>
