<div class="layui-fluid">
	<div class="layui-card">
		<div class="layui-form layui-form-pane layui-card-header layuiadmin-card-header-auto" lay-filter="LAY-demand-loglist-search">
			<div class="layui-form-item">
				<div class="layui-inline">
					<label class="layui-form-label">公司/订单</label>
					<div class="layui-input-block" id="workerdemand">
					</div>
				</div>
				<div class="layui-inline">
					<button class="layui-btn" lay-submit lay-filter="LAY-demand-loglist-search-btn">
						<i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
					</button>
					<button class="layui-btn" lay-submit lay-filter="LAY-demand-loglist-export-btn">
						导出
					</button>
				</div>
			</div>
		</div>

		<div class="layui-card-body">
			<div style="padding-bottom: 10px;">
				<button class="layui-btn layuiadmin-btn" data-type="batchdel">删除</button>
			</div>

			<table id="LAY-demand-loglist-table" lay-filter="LAY-demand-loglist-table"></table>
			<script type="text/html" id="useravatarTpl">
				{{#  if(d.user!=null && d.user.avatar != ''){ }}
				<div id="pic{{d.id}}" lay-event="pic">
					<img style="width: 100%; height: 100%;" src="{{d.user.avatar}}">
				</div>
				{{#  } }}
			</script>
			<script type="text/html" id="setTpl">
				<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
			</script>
		</div>
	</div>
</div>

<script>
	layui.config({
		base: '/static/echoui/' //静态资源所在路径
	}).extend({
		index: 'lib/index' //主入口模块
	}).use(['index', 'form', 'set', 'table', 'selectN', 'selectM'], function() {
		var $ = layui.$,
			setter = layui.setter,
			admin = layui.admin,
			form = layui.form,
			table = layui.table,
			selectN = layui.selectN,
			selectM = layui.selectM;
		form.render();

		var workerdemand = selectN({
			elem: '#workerdemand',
			search: [true, true],
			tips: ["请选择劳务公司（可搜索）", "请选择招聘信息（可搜索）"],
			data: {$workerlist|raw},
			selected: [0, 0],
			field: { idName: 'id', titleName: 'title', childName: 'demand' }
		});

		table.render({
			elem: '#LAY-demand-loglist-table',
			url: setter.baseAdminUrl + 'demand/listlog',
			cols: [
				[
					{ type: 'checkbox', fixed: 'left' },
					{ field: 'id', width: 80, title: '表ID', sort: true },
					{ field: 'gworkertitle', title: '接单公司名称', width: 300, templet: '<div>{{d.gworker.title||""}}</div>' },
					{ field: 'gworkerrealname', title: '接单公司联系人', width: 150, templet: '<div>{{d.gworker.realname||""}}</div>' },
					{ field: 'gworkermobile', title: '接单公司电话', width: 150, templet: '<div>{{d.gworker.mobile||""}}</div>' },
					{ field: 'workerid', title: '发单公司', toolbar: '<div>{{d.worker.title||""}}</div>' },
					{ field: 'demandid', title: '订单标题', width: 200, toolbar: '<div>{{d.demand.title||""}}</div>' },
					{ field: 'createtime', title: '接单时间', width: 170 },
					{ title: '操作', width: 100, align: 'center', fixed: 'right', toolbar: '#setTpl' }
				]
			],
			page: true,
			limit: 50,
			cellMinWidth: 200,
			text: '对不起，加载出现异常！'
		});

		form.on('submit(LAY-demand-loglist-search-btn)', function(data) {
			table.reload('LAY-demand-loglist-table', {
				where: data.field,
				page: {
					curr: 1
				}
			});
		});
		form.on('submit(LAY-demand-loglist-export-btn)', function(data) {
			var url = setter.baseAdminUrl + 'demand/exportlog?';
			for (var item in data.field) {
				url = url + '&' + item + '=' + data.field[item];
			}
			window.location.href = url;
		});

		table.on('edit(LAY-demand-loglist-table)', function(obj) {
			var id = obj.data.id,
				field = obj.field,
				value = obj.value;
			admin.req({
				url: setter.baseAdminUrl + 'demand/fieldlog',
				data: { id: id, field: field, value: value },
				done: function(res) {
					layer.msg('修改成功');
					layui.table.reload('LAY-demand-loglist-table');
				}
			});
		});

		var active = {
			batchdel: function() {
				var checkStatus = table.checkStatus('LAY-demand-loglist-table'),
					checkData = checkStatus.data; //得到选中的数据
				if (checkData.length === 0) {
					return layer.msg('请选择数据');
				}
				var idarr = [];
				for (var i = 0; i < checkData.length; i++) {
					idarr.push(checkData[i].id);
				}
				layer.confirm('确定删除此报名记录吗？', function(index) {
					admin.req({
						url: setter.baseAdminUrl + 'demand/dellog',
						data: {
							idarr: idarr
						},
						done: function(res) {
							table.reload('LAY-demand-loglist-table');
							layer.msg('已删除');
						}
					});
					layer.close(index);
				});
			}
		};

		table.on('tool(LAY-demand-loglist-table)', function(obj) {
			var data = obj.data;
			if (obj.event === 'del') {
				layer.confirm('确定删除此报名记录吗？', function(index) {
					admin.req({
						url: setter.baseAdminUrl + 'demand/dellog',
						data: {
							idarr: [data.id]
						},
						done: function(res) {
							obj.del();
							layer.msg('已删除');
						}
					});
					layer.close(index);
				});
			} else if (obj.event === 'pic') {
				layer.photos({
					photos: '#pic' + data.id,
					anim: 5,
				});
			}
		});

		$('.layui-btn.layuiadmin-btn').on('click', function() {
			var type = $(this).data('type');
			active[type] ? active[type].call(this) : '';
		});

	});
</script>
