{extend name="base"}

{block name="content"}
<div class="row">
	<div class="col-sm-12">
		<div class="card-box">
			<div class="row">
				<div class="col-sm-6">
					<form class="form-inline" role="form" action="" method="get">
						<div class="form-group">
							<select name="cate_id" class="form-control">
								<option value="" {if $Think.get.cate_id=='' }selected{/if}>全部分类 </option> <!-- {foreach $categorys as $v} -->
								<option value="{$v.id}" {if $Think.get.cate_id==$v.id}selected{/if}>{$v.name} </option> <!-- {/foreach} -->
							</select> </div>
						<div class="form-group">
							<select name="goods_id" class="form-control">
								<option value="" {if $Think.get.goods_id=='' }selected{/if}>全部商品 </option> <!-- {foreach $goodsList as $v} -->
								<option value="{$v.id}" {if $Think.get.goods_id==$v.id}selected{/if}>{$v.name} </option> <!-- {/foreach} -->
							</select> </div>
						<div class="form-group">
							<select name="status" class="form-control">
								<option value="" {if $Think.get.status=='' }selected{/if}>全部状态 </option> <option value="1" {if
								 $Think.get.status=='1' }selected{/if}>未售出 </option> <option value="2" {if $Think.get.status=='2' }selected{/if}>已售出
								 </option> </select> </div> {if $Think.get.status==2} <div class="form-group">
									<input name="trade_no" class="form-control" placeholder="请输入订单号" value="{$Think.get.trade_no}" />
						</div>
						<div class="form-group">
							<input name="contact" class="form-control" placeholder="请输入联系方式" value="{$Think.get.contact}" />
						</div>
						{/if}
						<button type="submit" class="btn btn-purple waves-effect waves-light"><i class="fa fa-search"></i> 搜索</button>
					</form>
				</div>
				<div class="col-sm-6">
					<a href="javascript:;" data-toggle="modal" data-target="#exportCard" class="pull-right btn btn-info waves-effect waves-light">导出卡密</a>
					<a href="{:url('add')}" class="pull-right btn btn-purple waves-effect waves-light" style="margin-right: 10px;">添加虚拟卡</a>
					<a href="javascript:batch_del();" class="pull-right btn btn-danger" style="margin-right: 10px;">批量删除</a>
				</div>
			</div>
			<!-- end row -->
			<div class="row">
				<table class="table table-striped m-0">
					<thead>
						<tr>
							<th><input type="checkbox" id="select_all"></th>
							<th>商品分类</th>
							<th>商品名称</th>
							<th>商品价格</th>
							<th>卡号</th>
							<th>卡密</th>
							<th>状态</th>
							<th>创建时间</th>
							<!-- {if $Think.get.status==2} -->
							<th>售出时间</th>
							<th>取卡密码</th>
							<th>联系方式</th>
							<!-- {/if} -->
							<th>操作</th>
						</tr>
					</thead>
					<tbody>
						{foreach $cards as $v}
						<tr>
							<td><input type="checkbox" value="{$v.id}"></td>
							<td>{$v.goods.category.name}</td>
							<td>{$v.goods.name}</td>
							<td>{$v.goods.price}</td>
							<td>{$v.number}</td>
							<td>{$v.secret}</td>
							<td>
								{if $v.status==2}
								<font color="green">已售出</font>
								{else/}
								未售出
								{/if}
							</td>
							<td>{$v.create_at|date="Y-m-d H:i:s",###}</td>
							<!-- {if $Think.get.status==2} -->
							<!-- <?php $orderCard = \think\Db::name('order_card')->where(['card_id' => $v['id']])->find(); ?> -->
							<!-- <?php $order = \think\Db::name('order')->where(['id' => $orderCard['order_id']])->find(); ?> -->
							<td>
								<!-- {if $v.status==2} -->
								<!-- {if $v.sell_time} -->
								{$v.sell_time|date="Y-m-d H:i:s",###}
								<!-- {else/} -->
								-
								<!-- {/if} -->
								<!-- {else/} -->
								未售出
								<!-- {/if} -->
							</td>
							<td>
								<!-- {if $v.status==2} -->
								{$order['take_card_password']}
								<!-- {else/} -->
								未售出
								<!-- {/if} -->
							</td>
							<td>
								<!-- {if $v.status==2} -->
								{$order['contact']}
								<!-- {else/} -->
								未售出
								<!-- {/if} -->
							</td>
							<!-- {/if} -->
							<td>
								{if $v.status==1}
								<a href="javascript:void(0);" onclick="del(this,{$v.id})">删除</a>
								{/if}
							</td>
						</tr>
						{/foreach}
					</tbody>
				</table>

			</div>
			<!-- end row -->
		</div>
		{$page}
	</div>
</div>
<div class="modal fade" id="exportCard" tabindex="-1" role="dialog">
	<div class="modal-dialog" role="document">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
				<h4 class="modal-title">导出卡密</h4>
			</div>
			<div class="modal-body">
				<form id="export_form" class="form-horizontal" method="POST" action="/merchant/goods_card/dumpCards" onsubmit="return dump()"
				 target="_blank">
					<div class="form-group">
						<div class="col-sm-2">
							<label class="control-label">指定分类</label>
						</div>
						<div class="col-sm-6">
							<select class="form-control goods_category">
								<option value="">请选择商品分类</option>
								{foreach $categorys as $v}
								<option value="{$v.id}">{$v.name}</option>
								{/foreach}
							</select>
						</div>
					</div>
					<div class="form-group">
						<div class="col-sm-2">
							<label class="control-label">指定商品</label>
						</div>
						<div class="col-sm-6">
							<select name="goods_id" class="form-control get_goods">
							</select>
						</div>
					</div>
					<div class="form-group">
						<div class="col-sm-2">
							<label class="control-label">卡密状态</label>
						</div>
						<div class="col-sm-6">
							<label class="radio-inline">
								<input type="radio" name="status" value="1,2" {if $Think.get.status=='' }checked{/if}> 全部 </label> <label class="radio-inline">
								<input type="radio" name="status" value="1" {if $Think.get.status=='1' }checked{/if}> 未售出 </label> <label class="radio-inline">
								<input type="radio" name="status" value="2" {if $Think.get.status=='2' }checked{/if}> 已售出 </label> </div> </div>
								 <div class="form-group">
								<div class="col-sm-2">
									<label class="control-label">导出范围</label>
								</div>
								<div class="col-sm-6">
									<label class="radio-inline">
										<input type="radio" name="range" value="0" checked> 全部库存的卡密
									</label>
									<label class="radio-inline">
										<input type="radio" name="range" value="1"> 导出指定的数量
									</label>
								</div>
						</div>
						<div class="form-group" id="exportNUm" style="display:none">
							<div class="col-sm-2">
								<label class="control-label">导出数量</label>
							</div>
							<div class="col-sm-6">
								<input name="number" type="number" class="form-control" placeholder="请输入正整数">
							</div>
						</div>
						<div class="form-group">
							<div class="col-sm-2">
								<label class="control-label">是否删除</label>
							</div>
							<div class="col-sm-6">
								<label class="radio-inline">
									<input type="radio" name="del" value="0" checked> 仅导出不做删除
								</label>
								<label class="radio-inline">
									<input type="radio" name="del" value="1"> 导出并删除卡密
								</label>
							</div>
						</div>
						<div class="form-group">
							<div class="col-sm-2">
								<label class="control-label">商品名称</label>
							</div>
							<div class="col-sm-6">
								<label class="radio-inline">
									<input type="radio" name="need_goods_name" value="1" checked> 导出商品名
								</label>
								<label class="radio-inline">
									<input type="radio" name="need_goods_name" value="0"> 不需要商品名
								</label>
							</div>
						</div>
						<div class="form-group">
							<div class="col-sm-2">
								<label class="control-label">导出格式</label>
							</div>
							<div class="col-sm-6">
								<label class="radio-inline">
									<input type="radio" name="file_type" value="0" checked> EXCEL
								</label>
								<label class="radio-inline">
									<input type="radio" name="file_type" value="1"> TXT
								</label>
							</div>
						</div>
				</form>
			</div>
			<div class="modal-footer">
				<button type="submit" class="btn btn-primary" id="export">确定</button>
				<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>

			</div>
		</div><!-- /.modal-content -->
	</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
{/block}

{block name="appendJS"}

<script>
	$('#select_all').click(function () {
		if ($(this).is(':checked')) {
			$('tbody').find('input[type="checkbox"]').each(function () {
				$(this).prop("checked", true)
			})
		} else {
			$('tbody').find('input[type="checkbox"]').each(function () {
				$(this).prop("checked", false)
			})
		}
	})

	function del(obj, id) {
		swal({
				title: "确定删除吗？",
				text: "你可以在回收站恢复该操作",
				type: "warning",
				showCancelButton: true,
				confirmButtonColor: "#DD6B55",
				confirmButtonText: "确定删除！",
				closeOnConfirm: false
			},
			function () {
				$.post('/merchant/goods_card/del', {
					id: id
				}, function (res) {
					if (res.code != 0) {
						swal('删除失败', res.msg, "error");
					} else {
						swal('删除成功', '', "success");
						setTimeout(function () {
							location.reload();
						}, 200);
					}
				});
			});
	}

	function batch_del() {
		var ids = new Array();
		$('tbody').find('input[type="checkbox"]').each(function () {
			if ($(this).is(':checked')) {
				ids.push($(this).val());
			}
		})
		if (ids.length == 0) {
			swal('选择要删除的数据', '', "error");
			return false;
		}
		$.post('/merchant/goods_card/batch_del', {
			ids: ids
		}, function (res) {
			if (res.code != 0) {
				swal('删除失败', res.msg, "error");
			} else {
				swal('删除成功', '', "success");
				setTimeout(function () {
					location.reload();
				}, 200);
			}
		});
		console.log(ids);
	}
	$(document).ready(function () {
		$("input[name='range']").change(function () {
			var selectedvalue = $("input[name='range']:checked").val();
			if (selectedvalue == 1) {
				$('#exportNUm').show();
			} else {
				$('#exportNUm').hide();
			}
		});

		var $sel1 = $(".goods_category");
		var $sel2 = $(".get_goods");
		var option = "<option value='0'>选择商品</option>";

		function createOption(value, text) {
			var $option = $("<option></option>");
			$option.attr("value", value);
			$option.text(text);
			return $option;
		}

		function ajaxSelect($select, id) {
			if (id == "0") {
				$sel2.html(option);
				return;
			}
			var index = '';
			$.ajax({
				type: "GET",
				url: "/merchant/goods_card/ajax_get_category_goods",
				data: {
					"cid": id
				},
				dataType: "json",
				beforeSend: function () {
					index = layer.load(1, {
						shade: [0.1, '#fff'] //0.1透明度的白色背景
					});
				},
				success: function (data) {
					layer.close(index);
					$select.html(option);
					for (var k in data) {
						$select.append(createOption(data[k].id, data[k].name));
					}
				},
				complete: function () {
					layer.close(index);
				},
				error: function () {
					layer.close(index);
				}
			});
		}
		ajaxSelect($sel2, "0");
		$sel1.change(function () {
			var id = $sel1.val();
			if (id == "0") {
				$sel2.html(option);
			} else {
				ajaxSelect($sel2, id);
			}
		});

		$('#export').click(function () {
			var category = $('.goods_category').val();
			if (!category) {
				swal('请选择商品分类', '', "error");
				return false;
			}
			var goods = $('.get_goods').val();
			if (!goods) {
				swal('请选择商品', '', "error");
				return false;
			}
			var range = $("input[name='range']");
			var number = $("input[name='number']");
			if (range == 1 && !number) {
				swal('请输入导出数量', '', "error");
				return false;
			}
			$('#export_form').submit();
		});
	});

	function dump() {
		setTimeout(function () {
			location.reload();
		}, 1000)

		return true;
	}
</script>

{/block}