;
layui.config({
	base: "../../../admin/js/layuiPlugins/"
}).use(['form', 'layer', 'jquery', 'laypage', 'element', 'table', 'laydate', 'laytpl', 'cascade'], function() {
	var form = layui.form,
		layer = layui.layer,
		laytpl = layui.laytpl,
		laypage = layui.laypage,
		$ = layui.jquery,
		table = layui.table,
		element = layui.element,
		laydate = layui.laydate,
		cascade = layui.cascade;
	//自定义验证
	form.verify({
		namelength: [
			/^.{0,18}$/,
			"长度限制在0-18个字符噢"
		]
	});
	//监听一级Tab切换
	element.on('tab(productTabBrief)', function(obj) {
		switch (obj.index) {
			case 0:
				tableVm.reload();
				break;
		}
	});
	var vm = function(obj) {
		var _this = this;
		_this.dataId = obj.dataId || 'dataListId'; //数据表ID
		_this.elem = obj.elem || 'dataListBox'; //数据表容器
		_this.toolbar = obj.toolbar || '';
		_this.url = obj.url || ''; //数据表接口URL
		_this.page = obj.page; //分页
		_this.limit = obj.limit;
		_this.cols = obj.cols || []; //表头
		_this.done = obj.done || ''; //表头
		_this.formObj = {}; //当前表单对象
		_this.searchParameter = {}; //搜索参数
		_this.config = {};
		_this.where = obj.where;
		_this.config.dateTime = obj.dateTime;
		_this.dateTime = {
			year: 365 * 1 * 24 * 3600 * 1000,
			month: 30 * 1 * 24 * 3600 * 1000,
			day: 24 * 3600 * 1000,
		}
		_this.main();
	};
	//入口函数
	vm.prototype.main = function() {
		var _this = this;
		_this.initializationData()
	};
	//初始化表格
	vm.prototype.initializationData = function() {
		var _this = this;
		_this.dataListObj = table.render({
			id: _this.dataId,
			elem: _this.elem,
			toolbar: _this.toolbar,
			defaultToolbar: ['filter'],
			url: _this.url,
			page: _this.page,
			cols: _this.cols,
			done: _this.done,
			where: $.extend(_this.where, _this.searchParameter),
			limit: _this.limit,
		});
	};
	//重新加载数据表
	vm.prototype.reload = function() {
		var _this = this;
		_this.dataListObj = table.reload(_this.dataId, { //执行重载数据表
			where: _this.searchParameter,
			page: {
				curr: 1
			}
		});
	};
	//初始化组件
	vm.prototype.initializationModule = function(obj) {
		var _this = this;
		$.ajax({
			url: obj.url,
			data: obj.data,
			success: function(res) {
				var html = '<option value="">' + obj.prompt + '</option>';
				for (var i in res.data) {
					html += '<option value="' + res.data[i].id + '">' + res.data[i].name +
						'</option>';
				}
				$(obj.elem).html(html);
				form.render();
			}
		});
	};
	//初始化事件监听
	vm.prototype.eventMonitor = function(id) {
		var _this = this;
		//监听搜索表单事件
		if (typeof(_this.formEvent) == "object") {
			form.on('submit(' + _this.formEvent + ')', function(obj) {
				_this.formInfoSearch(_this.formEvent, obj);
				return false;
			});
		}
		return _this;
	};
	//搜索处理函数
	vm.prototype.formInfoSearch = function(id, obj) {
		var _this = this;
		_this.searchParameter = obj.field;
		_this.reload();
	};

	//渲染时间选择器
	vm.prototype.showTimeSelect = function(obj) {
		laydate.render({
			elem: obj.elem,
			type: "month",
			value: ((new Date().getMonth() + 1) < 10) ? new Date().getFullYear() + '-0' + (new Date().getMonth() + 1) : new Date()
				.getFullYear() + '-' + (new Date().getMonth() + 1)
		});
	};
	//jquer事件绑定
	vm.prototype.$event = function() {
		var _this = this;
		$(_this.dataExportBtn).on("click", function(e) {
			_this.dataExport();
		});
	};
	//数据导出处理函数
	vm.prototype.dataExport = function() {
		var _this = this;
		_this.downloadParameter = '?';
		for (var i in _this.dataListObj.config.where) {
			_this.downloadParameter += "&" + i + '=' + _this.dataListObj.config.where[i];
		}
		_this.startDownload();
	};
	//启动下载
	vm.prototype.startDownload = function() {
		var _this = this;
		window.open(window.hywa.config.href + window.hywa.config.port[_this.dataDownload] + _this.downloadParameter);
	};

	//模板引擎
	function typeTpl(obj) {
		laytpl(selectTpl.innerHTML).render(obj, function(html) {
			$(obj.elem).html(html);
			form.render();
		});
	}

	// 表格列表
	let tableId = "tableData";
	var tableVm = new vm({
		dataId: 'tableData',
		elem: '#tableData',
		url: 'productTableUrl',
		toolbar: '#toolbar',
		page: true,
		cols: [
			[{
				title: '产品图片',
				field: 'imgUrl',
				width: 120,
				height: 60,
				align: 'center',
				templet: "<span><img width='50%' src=" + window.hywa.config.href + '/{{d.imgUrl}}' + "/></span>"
			}, {
				field: 'name',
				title: '产品名称'
			}, {
				field: 'commonName',
				title: '常用名',
			}, {
				field: 'productType',
				title: '小类',
				align: 'center'
			}, {
				field: 'productBigType',
				title: '大类',
				align: 'center'
			}, {
				field: 'from',
				title: '来源',
				align: 'center',
				templet: '<div>{{d.source == 0? "自建" : "数据同步"}}</div>'
			}, {
				field: 'enable',
				title: '是否可用',
				align: 'center',
				sort: true,
				templet: '<div>{{# d.checked = ((d.enable == 0)? "checked" : ""); }}<input type="checkbox" id="{{d.id}}" name="sex" value="{{d.enable}}" lay-skin="switch" lay-text="是|否" lay-filter="enable" {{d.checked}}></div>',
				width: 100
			}, {
				field: 'operating',
				title: '操作',
				toolbar: "#operatingTool",
				align: 'center',
				fixed: 'right',
				width: 150
			}]
		],
		done: function() {
			if (!layui.data('auth').data.add) {
				$('.add-btn').hide();
			}
		}
	});
	//初始化事件监听
	tableVm.formEvent = ['tableSearch'];
	tableVm.eventMonitor();
	tableVm.$event();
	// 获取商品大类别
	$.ajax({
		type: 'get',
		url: 'productBigTypeUrl',
		data: {
			type: 'productType'
		},
		success: function(res) {
			typeTpl({
				elem: "#bigTypeSelect",
				name: "大类别",
				list: res.data
			});
		}
	});
	//商品小类别
	form.on('select(bigType)', function(data) {
		$.ajax({
			type: 'get',
			url: 'productSmallTypeUrl',
			data: {
				bigTypeId: data.value
			},
			success: function(res) {
				typeTpl({
					elem: "#smallTypeSelect",
					name: "小类别",
					list: res.data
				});
			}
		});
	});
	//监听头部工具栏
	table.on('toolbar(' + tableId + ')', function(obj) {
		switch (obj.event) {
			case 'add':
				layer.open({
					type: 2,
					title: "新增",
					area: ['700px', '500px'],
					maxmin: true,
					content: 'add_change.html',
					btn: ['确定', '取消'],
					yes: function(index, layero) {
						var submit = layero.find('iframe').contents().find("#formSubmit");
						submit.click();
					},
				});
				break;
		};
	});
	//可用性修改
	form.on('switch(enable)', function(data) {
		var field = {
			id: data.elem.id,
			enable: data.value == 0 ? 1 : 0,
		}
		$.ajax({
			type: 'post',
			url: 'productStatusUrl',
			data: JSON.stringify(field),
			contentType: 'application/json',
			success: function(res) {
				if (!(res.code - 0)) {
					layer.msg(res.msg, {
						icon: 1
					});
					tableVm.reload();
				} else {
					data.elem.checked = !data.elem.checked;
					form.render();
				}
			}
		});
	});
	//工具条处理
	function toolOperating(name, obj) {
		if (obj.event === 'del') { //删除
			layer.confirm('真的删除吗？', function(index) {
				layer.close(index);
				var index = layer.msg('删除中，请稍候', {
					icon: 16,
					time: false,
					shade: 0.8
				});
				$.ajax({
					type: 'delete',
					url: 'del_productTableData?id=' + obj.data.id,
					success: function(res) {
						if (!(res.code - 0)) {
							obj.del();
							layer.close(index);
							layer.msg(res.msg, {
								icon: 1
							});
							table.reload(name);
						} else {
							layer.msg(res.msg, {
								icon: 2
							});
						}
					}
				});
			});
		} else if (obj.event === 'edit') { //修改
			layer.open({
				type: 2,
				title: "修改",
				area: ['700px', '500px'],
				maxmin: true,
				content: 'add_change.html?id=' + obj.data.id,
				btn: ['确定', '取消'],
				yes: function(index, layero) {
					var submit = layero.find('iframe').contents().find("#formSubmit");
					submit.click();
					// table.reload(name);
				},
			});
		}
	}
	//监听事件条
	table.on('tool(' + tableId + ')', function(obj) {
		toolOperating(tableId, obj);
	});
});
