<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <?php $shoname_name = D('Home/Front')->get_config_by_name('shoname'); ?>
  <title><?php echo $shoname; ?></title>
  <link rel="shortcut icon" href="" />
        
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">

  <link rel="stylesheet" href="/layuiadmin/style/admin.css" media="all">
  <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
  <!--[if lt IE 9]>
  <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
  <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
  <![endif]-->

  <link href="./resource/css/bootstrap.min.css?v=201903260001" rel="stylesheet">
  <link href="./resource/css/common.css?v=201903260001" rel="stylesheet">
  <script type="text/javascript">
      window.sysinfo = {
        <?php if (!empty($_W['uniacid']) ){ ?>'uniacid': '{$_W['uniacid']}',<?php } ?>
        <?php if( !empty($_W['acid'])  ){ ?>'acid': '{$_W['acid']}',<?php } ?>
        <?php if (!empty($_W['openid']) ) { ?>'openid': '{$_W['openid']}',<?php } ?>
        <?php if( !empty($_W['uid']) ) {  ?>'uid': '{$_W['uid']}',<?php } ?>
        'isfounder': <?php if (!empty($_W['isfounder']) ) { ?>1<?php  }else{  ?>0<?php } ?>,
        'siteroot': '{$_W['siteroot']}',
        'siteurl': '{$_W['siteurl']}',
        'attachurl': '{$_W['attachurl']}',
        'attachurl_local': '{$_W['attachurl_local']}',
        'attachurl_remote': '{$_W['attachurl_remote']}',
        'module': {'url' : '<?php if( defined('MODULE_URL') ) { ?>{MODULE_URL}<?php } ?>', 'name' : '<?php if (defined('IN_MODULE') ) { ?>{IN_MODULE}<?php } ?>'},
        'cookie': {'pre': ''},
        'account': {:json_encode($_W['account'])},
      };
  </script>
		
  <script type="text/javascript" src="./resource/js/lib/jquery-1.11.1.min.js"></script>
  <script type="text/javascript" src="./resource/js/lib/bootstrap.min.js"></script>
  <script type="text/javascript" src="./resource/js/app/util.js?v=201903260001"></script>
  <script type="text/javascript" src="./resource/js/app/common.min.js?v=201903260001"></script>
  <script type="text/javascript" src="./resource/js/require.js?v=201903260001"></script>
  <script type="text/javascript" src="./resource/js/lib/jquery.nice-select.js?v=201903260001"></script>
  <link rel="stylesheet" href="/layuiadmin/layui/css/layui.css" media="all">
  <link href="/static/css/snailfish.css" rel="stylesheet">

  <style type="text/css">
  	.input-group .input-group-addon { padding: 0 5px; }
  </style>
</head>
<body layadmin-themealias="default">

<div class="layui-fluid">
	<div class="layui-card">
		<div class="layui-card-header layui-elem-quote">当前位置：<span class="line-text"><?php if( !empty($item['id'])){ ?>编辑<?php  }else{  ?>添加<?php } ?>优惠券 <small><?php if( !empty($item['id'])){ ?>修改【{$item['voucher_title']}】<?php } ?></small></span></div>
		<div class="layui-card-body" style="padding:15px;">
			<form action="" method="post" class="layui-form" enctype="multipart/form-data" >
				<input type="hidden" name="id" value="{$item['id']}">
				<div class="layui-form-item">
					<label class="layui-form-label">排序</label>
					<div class="layui-input-block">
						<input type="text" name="displayorder" class="form-control" value="{$item['displayorder']}"  />
						<span class='help-block'>数字越大越靠前</span>
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label must"> 优惠券名称</label>
					<div class="layui-input-block">
						<input type="text" name="voucher_title" class="form-control" value="{$item['voucher_title']}" data-rule-required="true"  />
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">分类</label>
					<div class="layui-input-block">
						<select name='catid' class="layui-input layui-unselect">
							<option value=''></option>
							<?php foreach( $category as  $k => $c  ){ ?>
							<option value='{$k}' <?php if( $item['catid']==$k){ ?>selected<?php } ?>>{$c['name']}</option>
							<?php } ?>
						</select>
					</div>
				</div>
				<div class="layui-form-item" style="display:none;">
					<label class="layui-form-label">缩略图</label>
					<div class="layui-input-block">
						{:tpl_form_field_image2('thumb', $item['thumb'])}
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label must">使用条件</label>
					<div class="layui-input-block">
						<input type="text" name="limit_money" lay-verify="require" class="form-control" value="<?php if( empty($item)){ ?><?php  }else{  ?>{$item['limit_money']}<?php } ?>"  />
						<span class='help-block' >消费满多少可用, 空或0 不限制</span>
					</div>
				</div>

				<div class="layui-form-item">
					<label class="layui-form-label">使用时间限制</label>
					<div class="layui-input-block" style="width: 520px;">
						<div class='input-group'>
							<span class='input-group-addon'>
								<label class="radio-inline" style='margin-top:-5px;' ><input type="radio" name="timelimit" value="0" <?php if( $item['timelimit']==0){ ?>checked<?php } ?> title="获得后"></label>
							</span>
							<input type='text' class='form-control' name='get_over_hour' value="<?php if( empty($item) ){ ?>1<?php  }else{  ?>{$item['get_over_hour']}<?php } ?>" />
							<span class='input-group-addon'>天内有效(空为不限时间使用)</span>
						</div>
					</div>
				</div>

				<div class="layui-form-item">
					<label class="layui-form-label"></label>
					<div class="layui-input-block" style="width: 364px;">
						<div class='input-group'>
							<span class='input-group-addon'>
								<label class="radio-inline" style='margin-top:-5px;' ><input type="radio" name="timelimit" value="1" <?php if( $item['timelimit']==1){ ?>checked<?php } ?> title="在日期"></label>
							</span>
							{:tpl_form_field_daterange('time', array('starttime'=>date('Y-m-d', $starttime),'endtime'=>date('Y-m-d', $endtime)));}
							<span class='input-group-addon'>内有效</span>
						</div>
					</div>
				</div>
				

				<!--
				 <div class="layui-form-item">
					<label class="layui-form-label">首页弹窗</label>
					<div class="layui-input-block" >
						<input type="radio" name="is_index_alert" value="1" <?php if(!empty($item) && $item['is_index_alert'] == 1){ ?>checked="true"<?php } ?> title="是" />
						<input type="radio" name="is_index_alert" value="0" <?php if(empty($item) || $item['is_index_alert'] == 0){ ?>checked="true"<?php } ?> title="否" />
					</div>
				</div>
				
				<div class="layui-form-item">
					<label class="layui-form-label">首页领取</label>
					<div class="layui-input-block" >
						<input type="radio" name="is_index_show" value="1" <?php if( empty($item) || $item['is_index_show'] == 1){ ?>checked="true"<?php } ?> title="是" />
						<input type="radio" name="is_index_show" value="0" <?php if( !empty($item) && $item['is_index_show'] == 0){ ?>checked="true"<?php } ?> title="否" />
						
					</div>
				</div>
				-->
				 <div class="layui-form-item">
					<label class="layui-form-label">领取方式</label>
					<div class="layui-input-block" >


						<input type="checkbox" id="is_index_alert" lay-skin="primary" value="1" name="is_index_alert" lay-filter="is_index_alert" title="首页弹窗领取"
							<?php if( !empty($item) && $item['is_index_alert'] == 1 ){ ?>checked="true"<?php } ?>
						>
						<input type="checkbox" id="is_index_show" lay-skin="primary" value="1" name="is_index_show" lay-filter="is_index_show" title="首页领取"
							<?php if( !empty($item) && $item['is_index_show'] == 1 ){ ?>checked="true"<?php } ?>
						>
						<span class='help-block'>首页弹窗领取和首页领取两个同时选中的时候，优先使用首页弹窗领取，首页弹窗领取为优惠券首页弹窗用户默认直接领取，首页领取需要用户在首页优惠券展示的地方进行点击领取</span>
						<!--
						<input type="radio" name="coupon_collection_method" value="0" <?php if( empty($item) || $item['coupon_collection_method'] == 0 ){ ?>checked="true"<?php } ?> title="首页弹窗领取" />
						<input type="radio" name="coupon_collection_method" value="1" <?php if( !empty($item) && $item['coupon_collection_method'] == 1 ){ ?>checked="true"<?php } ?> title="首页领取" />
						<span class='help-block' >首页弹窗领取为优惠券首页弹窗用户默认直接领取，首页领取需要用户在首页优惠券展示的地方进行点击领取</span>-->
					</div>
				</div>
				
				
				<div class="layui-form-item">
					<label class="layui-form-label">新人券</label>
					<div class="layui-input-block" >
						<input type="radio" name="is_new_man" value="0" <?php if( empty($item) || $item['is_new_man'] == 0 ){ ?>checked="true"<?php } ?> title="否" />
						<input type="radio" name="is_new_man" value="1" <?php if( !empty($item) && $item['is_new_man'] == 1 ){ ?>checked="true"<?php } ?> title="是" />
						<span class='help-block' >商城没有进行支付过的用户可领取，领取后在设置的有效期内可使用</span>
					</div>
				</div>
				
				<div class="layui-form-item">
	                <label class="layui-form-label">优惠方式</label>
	                <div class="layui-input-block">
						<input type="hidden" name="coupontype" value="0"/>
	                    <input type="radio" name="backtype" checked title="立减">
	                </div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label"></label>
					<div class="layui-input-block backtype backtype0" style="width: 300px;" >
						<div class='input-group fixsingle-input-group'>
							<span class='input-group-addon'>立减</span>
							<input type='text' class='form-control' name='credit' value="<?php if( empty($item)){ ?>1<?php  }else{  ?>{$item['credit']}<?php } ?>"/>
							<span class='input-group-addon'>元</span>
						 </div>
					</div>
	             </div>
				 

				 
				 
				 <div class="layui-form-item">
					<label class="layui-form-label">每人限领</label>
					<div class="layui-input-block">
						<input type="text" name="person_limit_count" class="form-control" value="<?php if( empty($item)){ ?>1<?php  }else{  ?>{$item['person_limit_count']}<?php } ?>"  />
						<span class='help-block' >限制每人“首页领取”和“首页弹窗”领取数量，后台手动发送不受此限制，发多少到多少。设置0为不限。</span>
					</div>
				 </div>
				 <div class="layui-form-item">
					<label class="layui-form-label">发放总数</label>
					<div class="layui-input-block">
						<input type="text" name="total_count" class="form-control" value="<?php if( empty($item)){ ?>1<?php  }else{  ?>{$item['total_count']}<?php } ?>"  />
						<span class='help-block' >优惠券总数量，没有不能领取或发放,-1 为不限制张数</span>
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">可使用商品</label>
					<div class="layui-input-block" >
						<input type="radio" lay-filter="is_limit_goodsbuy" name="is_limit_goodsbuy" value="0" <?php if(empty($item) || $item['is_limit_goods_buy'] == 0){ ?>checked="true"<?php } ?> title="商城所有商品" />
						<input type="radio" lay-filter="is_limit_goodsbuy" name="is_limit_goodsbuy" value="1" <?php if(!empty($item) && $item['is_limit_goods_buy'] == 1){ ?>checked="true"<?php } ?> title="指定商品" />
						<input type="radio" lay-filter="is_limit_goodsbuy" name="is_limit_goodsbuy" value="2" <?php if(!empty($item) && $item['is_limit_goods_buy'] == 2){ ?>checked="true"<?php } ?> title="商品分类" />
					</div>
				</div>
				<div class="layui-form-item" id="goods_form_item" style="<?php if(!empty($item) && $item['is_limit_goods_buy'] == 1){ ?><?php }else{ ?>display:none;<?php } ?>">
					<label class="layui-form-label">选择商品</label>
					<div class="layui-input-block">
						<div class="input-group " style="margin: 0;">
							<input type="text" disabled value="" class="form-control valid" name="" placeholder="" id="agent_id">
							<span class="input-group-btn">
								<span data-input="#agent_id" id="chose_agent_id"  class="btn btn-default">选择商品</span>
							</span>
						</div>
						<?php if(!empty($limit_goods)){ ?>
						<?php foreach( $limit_goods as $goods ){ ?>
						<div class="input-group mult_choose_goodsid" data-gid="{$goods['gid']}" style="border-radius: 0;float: left;margin: 10px;margin-left:0px;width: 22%;">	
							<div class="layadmin-text-center choose_user">		
								<img style="" src="{$goods['image']}">		
								<div class="layadmin-maillist-img" style="">{$goods['goodsname']}</div>		
								<button type="button" class="layui-btn layui-btn-sm" onclick="cancle_bind(this)">
									<i class="layui-icon"></i>
								</button>	
							</div>
						</div>
						<?php }} ?>
					</div>
				</div>
				<div class="layui-form-item" id="goods_form_category" style="<?php if(!empty($item) && $item['is_limit_goods_buy'] == 2){ ?><?php }else{ ?>display:none;<?php } ?>">
					<label class="layui-form-label">商品分类</label>
					<div class="layui-input-block">
						<select id="cates"   name='goodscates'  class="form-control select2"  >
							
							<?php foreach($goods_category as $c){ ?>
							<option value="{$c['id']}" <?php if(!empty($item['goodscates']) &&  $c['id']==$item['goodscates']){ ?>selected<?php } ?> >{$c['name']}</option>
							<?php } ?>
						</select>
						
					</div>
				</div>
				
				<div class="layui-form-item">
					<label class="layui-form-label"></label>
					<div class="layui-input-block">
						<input type="submit" name="submit" value="提交" lay-submit lay-filter="formDemo" class="btn btn-primary col-lg-1"  />
						
					</div>
				</div>
			</form>
		</div>
	</div>
</div>

<script src="/layuiadmin/layui/layui.js"></script>

<script>
	layui.config({
		base: '/layuiadmin/' //静态资源所在路径
	}).extend({
		index: 'lib/index' //主入口模块
	}).use('index');
</script>

<script>
//由于模块都一次性加载，因此不用执行 layui.use() 来加载对应模块，直接使用即可：
var layer = layui.layer;
var $;
var cur_open_div;

layui.use(['jquery', 'layer','form'], function(){ 
  $ = layui.$;
  var form = layui.form;
  
  form.on('radio(is_limit_goodsbuy)', function(data){
	 
		if(data.value == 1)
		{
			$('#goods_form_item').show();
			$('#goods_form_category').hide();
		}else if(data.value == 2)
		{
			$('#goods_form_category').show();
			$('#goods_form_item').hide();
		}
		else{
			$('#goods_form_item').hide();
			$('#goods_form_category').hide();
		}
	});  

	$('#chose_member_id').click(function(){ 
		cur_open_div = $(this).attr('data-input');
		$.post("{:U('user/query', array('is_quan' => 1))}", {}, function(shtml){
		 layer.open({
			type: 1,
			area: '930px',
			content: shtml //注意，如果str是object，那么需要字符拼接。
		  });
		});
	})
	$('#chose_agent_id').click(function(){
		cur_open_div = $(this).attr('data-input');
		$.post("{:U('goods/query_normal', array('template' => 'mult'))}", {}, function(shtml){
		 layer.open({
			type: 1,
			area: '930px',
			content: shtml //注意，如果str是object，那么需要字符拼接。
		  });
		});
	})
	$('#chose_link').click(function(){
		cur_open_div = $(this).attr('data-input');
		$.post("{:U('util/selecturl', array('ok' => 1))}", {}, function(shtml){
		 layer.open({
			type: 1,
			area: '930px',
			content: shtml //注意，如果str是object，那么需要字符拼接。
		  });
		});
	})
	
	
  //监听提交
  form.on('submit(formDemo)', function(data){
	
	var gd_ar = [];
	var gd_str = '';
	$('.mult_choose_goodsid').each(function(){
		gd_ar.push( $(this).attr('data-gid') );
	})
	gd_str = gd_ar.join(',');

		var is_index_alert = 0;
	  	var is_index_show = 0;
	 	if($('input[name="is_index_alert"]').is(':checked')){
			is_index_alert = 1;
	 	}
	  if($('input[name="is_index_show"]').is(':checked')){
		  is_index_show = 1;
	  }
	  data.field.is_index_alert = is_index_alert;
	  data.field.is_index_show = is_index_show;

	data.field.limit_goods_list = gd_str;
	  console.log(data.field);
	 $.ajax({
		url: data.form.action,
		type: data.form.method,
		data: data.field,
		dataType:'json',
		success: function (info) {
		  
			if(info.status == 0)
			{
				layer.msg(info.result.message,{icon: 1,time: 2000});
			}else if(info.status == 1){
				var go_url = location.href;
				if( info.result.hasOwnProperty("url") )
				{
					go_url = info.result.url;
				}
				
				layer.msg('操作成功',{time: 1000,
					end:function(){
						location.href = info.result.url;
						
						var backurl = "{:U('marketing/coupon',array('ok'=>'1'))}";
						location.href = backurl;
						// location.href = info.result.url;
					}
				}); 
			}
		}
	});
	
    return false;
  });
})
function cancle_bind(obj,sdiv)
{
	$('#'+sdiv).val('');
	$(obj).parent().parent().remove();
}

</script>  
</body>
</html>

