layui.config({
	base: "../../../admin/js/layuiPlugins/"
}).use(['form', 'layer', 'jquery', 'laypage', 'element', 'table', 'laydate', 'cascade'], function() {
	var form = layui.form,
		layer = layui.layer,
		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.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,
			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;
		if (obj.field.dateRange) {
			_this.searchParameter.startDate = obj.field.dateRange.substring(0, 10);
			_this.searchParameter.endDate = obj.field.dateRange.substring(13, 23);
		}else{
			_this.searchParameter.startDate = "";
			_this.searchParameter.endDate = "";
		}
		delete _this.searchParameter.dateRange;
		_this.reload();
	};

	//渲染时间选择器
	vm.prototype.showTimeSelect = function() {
		var _this = this;
		laydate.render({
			elem: _this.datePicker,
			range: true
		});
	};
	//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);
	};
	// 表格列表
	var tableVm = new vm({
		dataId: 'purchaseOrderTableData',
		elem: '#purchaseOrderTableData',
		url: 'purchaseOrderTableUrl',
		dateTime: {
			year: 1,
			day: 1,
		},
		page: true,
		cols: [
				[{
					field: 'purchaseOrderNumber',
					title: '采购订单号',
					minWidth: 220,
					align: 'center',
					fixed: 'left',
				}, {
					field: 'createTime',
					title: '下单时间',
					align: 'center',
					minWidth: 180
				}, {
					field: 'orderAmount',
					title: '订单金额（元）',
					align: 'center',
					minWidth:140
				}, {
					field: 'payWay',
					title: '支付方式',
					align: 'center',
					templet: "<span>{{ d.payWay == 1 ? '微信支付' : '其它' }}</span>",
					minWidth:120
				}, {
					field: 'realPay',
					title: '支付金额（元）',
					align: 'center',
					minWidth:140
				}, {
					field: 'payNumber',
					title: '支付订单编号',
					align: 'center',
					minWidth:220

				}, {
					field: 'name',
					title: '用户名',
					align: 'center',
					minWidth:120
				}, {
					field: 'status',
					title: '订单状态',
					align: 'center',
					templet: '#orderStatusTpl',
					minWidth:80
				}, {
					field: 'operating',
					title: '操作',
					toolbar: "#operatingTool",
					align: 'center',
					fixed: 'right',
					minWidth: 80
				}]
			] //设置表头
			,
		done: function(res) {
			setTimeout(function() {
				table.resize('purchaseOrderTableData');
			}, 100)
			if (res.data.length === 0) {
				flowOrderRender()
			} else {
				flowOrderRender(res.data[0].id)
			}
		}
	});
	//初始化事件监听
	tableVm.formEvent = ['purchaseOrderTableSearch'];
	tableVm.eventMonitor();
	tableVm.$event();
	tableVm.datePicker = '#datePicker'; //时间容器
	tableVm.showTimeSelect();

	function flowOrderRender(id) {
		table.render({
			id: 'flowOrderTableData',
			elem: '#flowOrderTableData',
			data:[],
			page: false,
			cols: [
				[{
					field: 'waterNumber',
					title: '流转订单',
					align: 'center',
					minWidth:60,
					fixed: 'left',
				}, {
					field: 'productName',
					title: '商品名称',
					align: 'center',
					minWidth:160,
					fixed: 'left',
				}, {
					field: 'specification',
					title: '规格',
					align: 'center',
					minWidth:60
				}, {
					field: 'amount',
					title: '数量',
					align: 'center',
					minWidth:60
				}, {
					field: 'price',
					title: '金额（元）',
					align: 'center',
					minWidth:80
				}, {
					field: 'sellerName',
					title: '供应商',
					align: 'center',
					minWidth:140
				}, {
					field: 'customerNote',
					title: '客户备注',
					align: 'center',
					minWidth:140
				}, {
					field: 'status',
					title: '备货状态',
					align: 'center',
					templet: '#flowerOrderStatusTpl',
					minWidth:140,
					fixed: 'right',
				}]
			]
		});
	}

	//监听行单击事件
	table.on('row(' + tableVm.dataId + ')', function(obj) {
		obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
		table.reload('flowOrderTableData',{
			url:'flowOrderTableUrl?orderId=' + obj.data.id
		});
	});
	//注册按钮事件
	$('.layui-btn[data-type]').on('click', function() {
		var type = $(this).data('type');
		activeByType(type);
	});
	//激活事件
	var activeByType = function(type, arg) {
		if (arguments.length === 2) {
			active[type] ? active[type].call(this, arg) : '';
		} else {
			active[type] ? active[type].call(this) : '';
		}
	}
	//定义事件集合
	var active = {
		add: function() {
			layer.open({
				type: 2,
				title: "新增",
				area: ['600px', '500px'],
				maxmin: true,
				content: 'add_change.html',
				btn: ['确定', '取消'],
				yes: function(index, layero) {
					//点击确认触发 iframe 内容中的按钮提交
					var submit = layero.find('iframe').contents().find("#formSubmit");
					submit.click();
				},
			});
		},
		updateRow: function(obj) {
			var oldData = table.cache[layTableId];
			for (var i = 0, row; i < oldData.length; i++) {
				row = oldData[i];
				if (row.tempId == obj.tempId) {
					$.extend(oldData[i], obj);
					return;
				}
			}
			tableIns.reload({
				data: oldData
			});
		}
	}
	//可用性修改
	form.on('switch(isUserble)', function(data) {
		console.log('sdhahdiehie', data.elem.title)
		var field = {
			id: data.elem.id,
			isUserble: '',
			isRecommend: parseInt(data.elem.title)
		}
		if (data.elem.checked == false) {
			field.isUserble = 0
		} else {
			field.isUserble = 1
		}
		$.ajax({
			type: 'post',
			url: 'addImgTextContent',
			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 === 'detail') {
			let index = layer.open({
				type: 2,
				title: "详情",
				area: ['1200px', '700px'],
				maxmin: true,
				content: 'detail.html?id=' + obj.data.id,
				//btn: ['确定', '取消'],
				yes: function(index, layero) {
					var submit = layero.find('iframe').contents().find("#formSubmit");
					submit.click();
				},
			});
			layer.full(index);
		}
	}
	//监听事件条
	table.on('tool(' + tableVm.dataId + ')', function(obj) {
		toolOperating(tableVm.dataId, obj);
	});
});
