;
layui.config({
	base: "../../js/layuiPlugins/"
}).use(['form', 'layer', 'jquery', 'table', 'laytpl'], function () {
	var form = layui.form,
		layer = layui.layer,
		laytpl = layui.laytpl,
		$ = layui.jquery,
		table = layui.table;
	var funcName = "qrcode";
	form.verify({
		namelength: [
			/^.{0,12}$/,
			"长度限制在0-12个字符噢"
		]
	});
	//工具条处理
	function toolOperating(obj) {
		var data = obj.data;
		if (obj.event === 'del') {
			layer.confirm('真的删除吗？', function (index) {
				layer.close(index);
				var index1 = layer.msg('删除中，请稍候', {
					icon: 16,
					time: false,
					shade: 0.8
				});
				$.ajax({
					type: "get",
					url: 'delete_' + funcName + '?guid=' + obj.data.guid,
					success: function (res) {
						layer.close(index1);
						if (!(res.code - 0)) {
							table.reload(funcName);
						}
					}
				});
			});
		} else if (obj.event === 'viewdetail') { //查看
			layer.open({
				type: 2,
				anim: 0,
				title: "溯源码预览",
				area: ['900px', '560px'],
				fixed: true,
				scrollbar: true,
				maxmin: true,
				content: 'add_change.html?event=view&guid=' + data.guid,
				end: function () {
				}
			});
		} else if (obj.event === 'drop') { //废弃
			layer.confirm('确定废弃吗？', function (index) {
				layer.close(index);
				var index1 = layer.msg('处理中，请稍候', {
					icon: 16,
					time: false,
					shade: 0.8
				});
				$.ajax({
					type: "get",
					url: 'drop_' + funcName + '?guid=' + data.guid,
					success: function (res) {
						layer.close(index1);
						if (!(res.code - 0)) {
							table.reload(funcName);
						}
					}
				});
			});
		} else if (obj.event === 'export') { //导出
			var userInfo = layui.sessionData(window.hywa.sessionTable.tableName)
			window.open(window.hywa.config.href + window.hywa.config.port.export_qrcode + "?guid=" + data.guid + "&accessToken=" + userInfo.token)
		} else if (obj.event === 'view') { //预览
			top.layer.open({
				type: 2,
				anim: 0,
				title: "溯源码预览",
				area: ['900px', '812px'],
				fixed: true,
				scrollbar: false,
				maxmin: false,
				shadeClose: true,
				content: '../forstpages/qrcode/preview-code.html?guid=' + data.guid,
				end: function () {
				}
			});
		} else if (obj.event === 'print') {//打印
			layer.prompt({
				formType: 2,
				value: '',
				title: '请输入打印份数',
				area: ['180px', '25px']
			}, function (value, index, elem) {
				if (/^[0-9]*[1-9][0-9]*$/.test(value)) {
					let params = {
						"fkBatchRecordGuid": data.guid,
						"pageSize": value
					}
					$.ajax({
						url: 'print_qrcode',
						type: 'POST',
						data: JSON.stringify(params),
						contentType: 'application/json',
						success: function (res) {
							for (let i = 0; i < res.data.length; i++) {
								console.log(res.data[i])
								print(res.data[i])
							}
						}
					});
					layer.close(index);
				} else {
					layer.msg('请输入纯数字!');
					return false;
				}

			});
		}
	}
	//打印组件
	function print(data) {
		console.log(data)
		let LODOP = getLodop(document.getElementById('LODOP_OB'), document.getElementById('LODOP_EM')),
			hPos = 10, // 小票上边距
			pageWidth = 620, // 小票宽度
			rowHeight = 18 // 小票行距
		// 初始化
		LODOP.PRINT_INIT('打印初始化中')
		LODOP.SET_PRINT_STYLEA(0, 'FontName', '黑体')
		LODOP.SET_PRINT_STYLEA(0, 'FontSize', 10)
		LODOP.SET_PRINT_STYLEA(0, 'Bold', 1)
		LODOP.ADD_PRINT_TEXT(hPos, 80, pageWidth, rowHeight, '企业名称:')
		LODOP.ADD_PRINT_TEXT(hPos, 140, pageWidth, rowHeight, data.orgName)
		hPos += rowHeight
		LODOP.ADD_PRINT_TEXT(hPos, 80, pageWidth, rowHeight, '企业电话:')
		LODOP.ADD_PRINT_TEXT(hPos, 140, pageWidth, rowHeight, data.companyTel)
		hPos += rowHeight
		LODOP.ADD_PRINT_TEXT(hPos, 80, pageWidth, rowHeight, '地址:')
		LODOP.ADD_PRINT_TEXT(hPos, 110, pageWidth, rowHeight, data.orgAddress)
		hPos += rowHeight
		LODOP.ADD_PRINT_IMAGE(hPos, 110, 150, 150, '<img width="150" height="150" src="' + window.hywa.config.href + "/" + data.qrImgUrl + '">'); hPos += rowHeight
		hPos += 135
		LODOP.ADD_PRINT_TEXT(hPos, 80, pageWidth, rowHeight, '防伪码:')
		LODOP.ADD_PRINT_TEXT(hPos, 130, pageWidth, rowHeight, data.qrcodeToken)
		// LODOP.PREVIEW()
		LODOP.PRINT()
		var printArred = [];
		printArred.push(data.guid)
		setPrintDone(printArred)
	}
	//像服务器发送已启用打印的二维码
	function setPrintDone(data) {
		$.ajax({
			url: 'setPrint_done',
			type: 'POST',
			data: JSON.stringify(data),
			contentType: 'application/json',
			success: function (res) {
				console.log(res)
			}
		});
	}
	//列表
	var DataList = {
		id: funcName,
		elem: '#DataTable',
		url: funcName + 'List',
		page: true,
		method: "post",
		contentType: 'application/json',
		loading: true,
		cellMinWidth: 80,
		cols: [
			[ //标题栏
				{
					field: 'goodsBatchNo',
					title: '批次编码',
					align: 'left',
					width: 130
				}, {
					field: 'goodsName',
					title: '产品名称',
					align: 'left',
					width: 180
				}, {
					field: 'goodsParentQrcode',
					title: '溯源码父码',
					align: 'left',
					width: 120
				}, {
					field: 'qrcodeRecordTime',
					title: '生成时间',
					align: 'center',
					width: 140
				}, {
					field: 'originNum',
					title: '溯源码数量',
					align: 'left',
					width: 100
				}, {
					field: 'cancelNum',
					title: '废弃数',
					align: 'left',
					width: 100
				}, {
					field: 'usedNum',
					title: '已使用数',
					align: 'left',
					width: 100
				}, {
					field: 'scanNum',
					title: '累计扫码数',
					align: 'left',
					width: 100
				}, {
					field: 'scanNum',
					title: '累计评论数',
					align: 'left',
					width: 100
				}, {
					field: 'notPrintNum',
					title: '未打印数量',
					align: 'left',
					width: 100
				}, {
					field: 'printNum',
					title: '打印数量',
					align: 'left',
					width: 100
				}, {
					field: 'operating',
					title: '操作',
					toolbar: '#operatingTool',
					align: 'center',
					fixed: 'right',
					width: 330
				}
			]
		] //设置表头
		, done: function (res) {
			resetButton(layui.sessionData(window.hywa.sessionTable.tableName).userData.menus.subMenuList, $);
			//res.button.add ? $('.add-btn-0').show() : $('.add-btn-0').hide();
		}
	};

	//执行默认渲染表格
	table.render(DataList);

	//监听产品工具事件条
	table.on('tool(DataTable)', function (obj) {
		toolOperating(obj);
	});
	//搜索响应函数
	var infoSearch = function (obj) {

		table.reload(funcName, { //执行重载
			where: obj.field
		});
		return false;
	};
	//监听搜索表单事件
	form.on("submit(DataSearch)", infoSearch);

	$("#addBtn").on("click", function () {
		layer.open({
			type: 2,
			anim: 0,
			title: "新增",
			area: ['800px', '560px'],
			fixed: true,
			scrollbar: true,
			maxmin: true,
			content: 'add_change.html',
			end: function () {
				table.reload(funcName);
			}
		});
	});

});
