<!doctype html>
<html>
	<head>
		<meta charset="utf-8"><meta http-equiv="Expires" content="0"><meta http-equiv="Pragma" content="no-cache"><meta http-equiv="Cache-control" content="no-cache"><meta http-equiv="Cache" content="no-cache">
		<title>表单+明细模板1</title>
		
		<script type="text/javascript">
			var pageVue = null;
			function loadJsCss(callback) {
				var jscss_urls = [];
				window.top.initJsCss(document, jscss_urls, callback);
			};
			
			function initVue() {
				new FormVue({
					el: "#vbody",
					data: {
						dataname: "fee_joint",
						table_dataname: "",
						title: "表单+明细模板1",
						formAttr: {
							istitle: false,
							title: "表单名称",
							columnnumber: 2,
							labelwidth: "140px",
							labelposition: "left",//"left",// right//top
							size: "mini",
							border: "3px solid #c6c6c600"
						},
						
						default_formFields: [
							{isshow: "T", field: "biz_date", name: "订单日期", type: "date", formatter: "date", notvalunit: true},
							{isshow: "T", field: "customer_name", name: "客户名称", type: "popup", required: true, notvalunit: true},
							{isshow: "T", field: "customer_consignee_address", name: "收货地址", required: true, type: "select",isfilterable:true, isrefresh:true, options: [], props:{value: "id", label: "address", checkStrictly: true}, notvalunit: true},
							{isshow: "T", field: "customer_consignee_user_name", name: "联系人", required: true, notvalunit: true},
							{isshow: "T", field: "customer_consignee_phone", name: "联系电话", required: true, notvalunit: true},
							{isshow: "T", field: "operate_emp_name", name: "业务员名称", disabled: true, notvalunit: true},
							{isshow: "T", field: "business_depart_name", name: "业务部门", type: "popup", required: true, notvalunit: true},
							{isshow: "T", field: "province_name", name: "区域", width: "120", type: "popup", required: true, notvalunit: true},
							{isshow: "T", field: "settle_type", name: "结算方式", type: "select", options: [{label: "现金",value: "cash"}, {label: "信用付款",value: "credit"}], required: true, notvalunit: true},
							{isshow: "T", field: "emp_depart_name", name: "销售部门", disabled: true, defaultval: "", notvalunit: true},
							{isshow: "T", field: "contract_no", name: "合同号", notvalunit: true},
							{isshow: "T", field: "amt_credit", name: "授信额度", disabled: true, formatter: "money", notvalunit: true},
							{isshow: "F", field: "iscreditsuccess", name: "资信检查结果", formatter: "TF3", notvalunit: true},
							{isshow: "T", field: "amt", name: "订单总金额", disabled: true, formatter: "money", align: "right", notvalunit: true},
							{isshow: "T", field: "amt_receipts", name: "折扣后金额", disabled: true, formatter: "money", align: "right", notvalunit: true},
							{isshow: "T", field: "amt_discount", name: "折扣总金额", disabled: true, formatter: "money", align: "right", notvalunit: true},
						],
						formFields: [],
						
						newformData: {
							//id: uuid_short(),
							// code: createCode("BA"),
							create_time: createDatetime(),
							state_code: "Draft",
							state_name: "草稿",
							creator_name: window.top.vue.userinfo.name,
							type_code: "",
							type_name: "",
						},
						formData: {},
						
						isRefresh: true,
						default_tableFields: [
							{isshow: "T",field: "create_time", name: "创建日期", width: "130",formatter: "formatter_date"},
							{isshow: "T", field: "delivery_part_name", name: "工厂名称", width: "180"},
							{isshow: "T", field: "product_code", name: "产品编号", width: "120"},
							{isshow: "T", field: "product_name", name: "产品名称", width: "400", type: "popup", align: "left"},
							{isshow: "T", field: "cnt", name: "数量", width: "80", type: "input", istablesum: true},
							{isshow: "T", field: "rebate_amt_sku", name: "折扣金额(专项)", defaultval: 0, align: "right", width: "120", istablesum: true},
							{isshow: "T", field: "rebate_amt_ty", name: "折扣金额(通用)", defaultval: 0, align: "right", width: "120", type: "popup"},
							{isshow: "T", field: "price_type", name: "价格类型", width: "120", type: "popup"},
							{isshow: "T", field: "product_price", name: "单价", width: "80", formatter: "formatter_money", align: "right"},
							{isshow: "T", field: "tax_rate", name: "税率", width: "110", type: "select", options: dataRoot.database.taxrate},
							{isshow: "T", field: "product_conversion_rate", name: "换算率", width: "80"},
							{isshow: "T", field: "standrad_cnt", name: "ä»¶æ•°", width: "80", istablesum: true},
							{isshow: "T", field: "stock", name: "库存", width: "80", istablesum: true},
							{isshow: "T", field: "amt", name: "金额", align: "right", width: "120", formatter: "formatter_money", istablesum: true},
							{isshow: "T", field: "rebate_amt", name: "返利金额", align: "right", width: "100", formatter: "formatter_money", istablesum: true},
							{isshow: "T", field: "sales_type", name: "销售类型", width: "120", type: "select", options: dataRoot.database.saletype},
							{isshow: "T", field: "qty_return", name: "退货数量", width: "160", align: "right"},
							{isshow: "T", field: "remark", name: "备注", type: "input", width: "180"}, 
						],
						tableFields: [],
						newTableData: {
							create_time: createDatetime(),
							creator_name: window.top.vue.userinfo.name,
						},
						tableData: [],
						
						//按键权限设置
						isedit: false,//提交前编辑,保存/提交
						isrefuseedit: false,//拒绝后编辑,保存/再次提交
						isapproval: false,//审批,同意/拒绝/转办/退回
						
						iscommit: false,//提交标记
						
						//弹窗参数
						popupParames: {},
						//字段设置
						tablefieldClick: {},
						formfieldClick: {},
					},
					created() {
						this.popupParames = clone(Root.popupParames);
						this.title = this.popupParames.title || this.popupParames.text
						if (this.popupParames.data) {
							this.formData = clone(this.popupParames.data);
						}
						
						if (this.popupParames.sceneCode) {
							if (this.popupParames.sceneCode == "add") {//新增
								if (this.newformData) {
									let formData_ = clone(this.formData);
									
									for (var k in this.newformData) {
										formData_[k] = this.newformData[k];
									}
									this.formData = formData_;
								}
								
								if (this.newTableData) {
									this.tableData.push(clone(this.newTableData));
								}
								
								this.isedit = true;
							}
							else if (this.popupParames.sceneCode == "browse") {//只读
								this.formAttr.disabled = true;
							}
							else if (this.popupParames.sceneCode == "edit") {//编辑
								this.isedit = true;
							}
							else if (this.popupParames.sceneCode == "approval") {//审批
								this.formAttr.disabled = true;
								this.isapproval = true;
							}
							else if (this.popupParames.sceneCode == "refuseedit") {//拒绝后的编辑
								this.isrefuseedit = true;
							}
						}
					},
					
					mounted() {
						var me = this;
						//预加载数据
						if (this.dataRequest && this.dataRequest.length) {
							var result = {};
							this.loadRequestData(this.dataRequest, result, function(data) {
								me.dataRequestObj = data;
								//预加载数据后给哪些字段设置options或formatterjson
								
								me.initData();
							});
						}
						else {
							this.initData();
						}
						
						// 以服务的方式调用的 Loading 需要异步关闭
						this.$nextTick(() => { 
							hideLoading();
							
							//重新设置弹窗宽高
							this.$nextTick(function(){
								//let w_ = this.$refs.popup_body.offsetWidth + "px";
								let w_ = "900px";
								let h_ = this.$refs.popup_body.offsetHeight + "px";
								Root.setPopupWH(w_, h_);
							})
						});
					},
					
					methods:{
						//关闭弹窗
						closeDialog() {
							var me = this;
							if (me.popupParames.totab){
								Root.tab.removeItem(Root.tab.selected);
								Root.tab.open(me.popupParames.parentOption, false); 
							}
							else {
								Root.hidePopup();
							}
						},
						//关闭前调回调
						saveAfter() {
							var me = this;
							if(this.popupParames.callback) {
								let obj = {
									//row: this.formData
								}
								this.popupParames.callback(obj, function() {
									me.closeDialog();
								});
							}
							else {
								me.closeDialog();
							}
						},
						
						initData() {
							let me = this
							var id_ = null;
							if (this.formData.id) {
								id_ = this.formData.id;
							}
							this.getRowDataById(id_, function(result) {//查询后的回调,用于获取字段的
								if (result.meta && result.meta[me.dataname] && result.meta[me.dataname].fields) {
									var metas = clone(result.meta[me.dataname].fields);
									var table_dataname_ = "";
									for (var dataname_ in result.meta) {
										if (dataname_ != me.dataname && !table_dataname_) {
											table_dataname_ = dataname_;
										}
									}
									var table_metas = [];
									if (table_dataname_) {
										me.table_dataname = table_dataname_;
										table_metas = clone(result.meta[table_dataname_].fields);
									}
									
									var formFields_ = [];
									var tableFields_ = [];
									metas.map(f=>{
										f.isshow = "T";
										formFields_.push(clone(f));
									})
									table_metas.map(f=>{
										f.isshow = "T";
										tableFields_.push(clone(f));
									})
									
									if (!me.formFields || (me.formFields && me.formFields.length == 0)) {
										me.formFields = clone(formFields_);
										me.tableFields = clone(tableFields_);
										
										//字段数组转字段obj,目的为了筛选时获取字段属性
										me.fieldsToFieldsObj();
										
										//设置字段事件
										me.tableFieldClick();
									}
								}
								
								if (me.rowData[me.dataname]) {
									me.formData = me.rowData[me.dataname];
								}
								if (me.rowData[me.table_dataname]) {
									me.tableData = me.rowData[me.table_dataname];
								}
							})
						},
						
						tableFieldClick() {
							var me = this;
							//表单字段事件设置
							this.formfieldClick = {
								type_code: {//字段事件设置
									val: {//有值时的点击事件
										onclick: function(obj) {//数据值点击事件
											Root.message({
												type: 'success',
												message: obj.val + '点击事件'
											}); 
										},
										onchange: function(obj) {//数据修改事件
											Root.message({
												type: 'success',
												message: '数据修改事件'
											});
										}
									},
									popup: {
										onclick: function(obj) {//弹窗点击事件
											Root.message({
												type: 'success',
												message: '弹窗点击事件'
											});
										}
									},
									visible: {
										onchange: function(fieldObj, row, callback) {//下拉展开事件
											Root.message({
												type: 'success',
												message: '下拉展开事件'
											});
											
											//重新设置选择项
											if (callback) {
												var obj_ = {
													options: []
												};
												callback(obj_);
											}
										}
									},
									select: {
										onchange: function(obj) {//下拉更改事件
											obj.data.type_name = clone(obj.selectoption.label);
										}
									},
									input: {
										onchange: function(obj) {//输入更改事件
											Root.message({
												type: 'success',
												message: '输入更改事件'
											});
										}
									},
									button: {
										onclick: function(obj) {//按键点击事件
											Root.message({
												type: 'success',
												message: '按键点击事件'
											});
										}
									},
									buttonarray: {
										onclick: function(obj) {//按键组点击事件,需要根据按键的code来判断具体事件
											Root.message({
												type: 'success',
												message: '按键组点击事件'
											});
										}
									},
								},
								customer_name: {
									popup: {
										onclick: function(obj) {//弹窗点击事件
											//打开
											me.open_customerlist()
										}
									},
								},
								terminal_name: {
									popup: {
										onclick: function(obj) {//弹窗点击事件
											//打开
											me.open_terminallist()
										}
									},
								},
								product_name: {
									popup: {
										onclick: function(obj) {//弹窗点击事件
											//打开
											me.open_productlist()
										}
									},
								},
							};
							
							//表格字段事件设置
							this.tablefieldClick = {
								product_name: {//字段事件设置
									val: {//有值时的点击事件
										notclick_val: "",//不可点击的值,1、是数组["11","22"];2、以“;”分隔的字符串"111;222"
										notclick_bindfield: [],//当该字段值等于指定字段值时不可点击["filterfield": "111"]
										onclick: function(obj) {//数据值点击事件
											Root.message({
												type: 'success',
												message: obj.val + '点击事件'
											}); 
										},
									},
									// defaultval: {
									// 	val: "查看",//空值时的显示值
									// 	onclick: function(obj) {//默认值点击事件,此事件需要设置val才有效
									// 		Root.message({
									// 			type: 'success',
									// 			message: '默认值点击事件'
									// 		});
									// 	}
									// },
									suffixval: {
										// val: "详情",//有值时的后缀,此后缀和单位后缀不同,如:详情,更多,查看等
										// onclick: function(obj) {//有值时的后缀点击事件,此事件需要设置val才有效
										// 	//打开
										// 	me.opentest()
										// }
									},
									popup: {
										onclick: function(obj) {//弹窗点击事件
											Root.message({
												type: 'success',
												message: '弹窗点击事件'
											});
										}
									},
									visible: {
										onchange: function(obj) {//下拉展开事件
											Root.message({
												type: 'success',
												message: '下拉展开事件'
											});
										}
									},
									select: {
										onchange: function(obj) {//下拉更改事件
											Root.message({
												type: 'success',
												message: '下拉更改事件'
											});
										}
									},
									input: {
										onchange: function(obj) {//输入更改事件
											Root.message({
												type: 'success',
												message: '输入更改事件'
											});
										}
									},
								},
								
								customer_name: {
									popup: {
										onclick: function(obj) {//弹窗点击事件
											//打开
											me.tablepopup_customerlist()
										}
									},
								},
								terminal_name: {
									popup: {
										onclick: function(obj) {//弹窗点击事件
											//打开
											me.tablepopup_terminallist()
										}
									},
								},
								product_name: {
									popup: {
										onclick: function(obj) {//弹窗点击事件
											//打开
											me.tablepopup_productlist()
										}
									},
								},
							};
						
						},
						
						open_customerlist(obj) {
							var me = this;
							Root.showPopup({
								url: "../md/customer/popup/customer_list.html",
								width: 800,
								height: 550,
								data: this.oldObj,
								dataname: "customer",
								callback: function(obj, callback) {
									let formData_ = clone(me.formData);
									formData_.customer_name = obj.row.md_description;
									formData_.customer_code = obj.row.md_code;
									formData_.customer_id = obj.row.id;
									
									me.formData = formData_;
									if (callback) {
										callback();
									}
								}
							});
						},
						open_terminallist(obj) {
							var me = this;
							Root.showPopup({
								url: "../md/customer/popup/customer_list.html",
								width: 800,
								height: 550,
								data: this.oldObj,
								dataname: "customer",
								callback: function(obj, callback) {
									let formData_ = clone(me.formData);
									formData_.customer_name = obj.row.md_description;
									formData_.customer_code = obj.row.md_code;
									formData_.customer_id = obj.row.id;
									
									me.formData = formData_;
									if (callback) {
										callback();
									}
								}
							});
						},
						open_productlist(obj) {
							var me = this;
							Root.showPopup({
								url: "../md/product/popup/sku_list.html",
								width: 800,
								height: 550,
								callback: function(obj, callback) {
									let formData_ = clone(me.formData);
									
									formData_.product_id = obj.row.id;
									formData_.product_code = obj.row.md_code;
									formData_.product_name = obj.row.md_description;
									//formData_.spec = obj.row.pack_specification;
									
									me.formData = formData_;
									if (callback) {
										callback();
									}
								}
							});
						},
						
						tablepopup_customerlist(obj) {
							var me = this;
							Root.showPopup({
								url: "../md/customer/popup/customer_list.html",
								width: 800,
								height: 550,
								data: this.oldObj,
								dataname: "customer",
								callback: function(callbackobj, callback) {
									obj.row.customer_code = callbackobj.row.md_code
									obj.row.customer_name = callbackobj.row.md_description
									
									if (callback) {
										callback();
									}
								}
							});
						},
						tablepopup_terminallist(obj) {
							var me = this;
							Root.showPopup({
								url: "../md/customer/popup/customer_list.html",
								width: 800,
								height: 550,
								data: this.oldObj,
								dataname: "customer",
								callback: function(callbackobj, callback) {
									obj.row.terminal_id = callbackobj.row.id
									obj.row.terminal_name = callbackobj.row.md_description
									
									if (callback) {
										callback();
									}
								}
							});
						},
						tablepopup_productlist(obj) {
							var me = this;
							Root.showPopup({
								url: "../md/product/popup/sku_list.html",
								width: 800,
								height: 550,
								callback: function(callbackobj, callback) {
									obj.row.product_id = callbackobj.row.id;
									obj.row.product_code = callbackobj.row.md_code;
									obj.row.product_name = callbackobj.row.md_description;
									obj.row.spec = callbackobj.row.pack_specification;
									obj.row.pack = callbackobj.row.unit;
									
									if (callback) {
										callback();
									}
								}
							});
						},
						
						addTableData() {
							var table_row = clone(this.newTableData);
							this.rowChange(table_row, "add", this.table_dataname);
						},
						
						delData(scope) {
							let me = this;
							let row = scope.row;
							let index_ = scope.$index;
							
							Root.confirm('确定删除数据【' + JSON.stringify(row) + '】吗?', '删除提示', {
							  confirmButtonText: '删除',
							  cancelButtonText: '取消',
							  type: 'warning'
							}).then(() => {
								me.rowChange(row, "del", me.table_dataname);
							}).catch(() => {
								Root.message({
									type: 'info',
									message: '已取消删除'
								});          
							});
						},
						
						rowChange(row, type, tablename) {
							var me = this;
							if (type == "add") {
								this.tableData.unshift(row);
							}
							else if (type == "del") {
								if (row.id) {
									let param = {
										dataname: tablename,
										id: row.id
									}
									
									Server.call("root/data/deleteEntity", param, function(result) {
										console.log(result);
										if (result && result.data) {
											me.tableData.remove(row);
											
											Root.message({
												type: 'success',
												message: '删除成功!'
											});
										}
									});
								}
								else {
									this.tableData.remove(row);
								}
							}
							else {
							
							}
						},
						
						//提交
						submitRowTable() {
							this.iscommit = true;
							this.saveRowTable();
						},
						
						//保存
						saveRowTable() {
							var me = this;
							var operator_ = "save";//保存
							if(me.iscommit) {
								operator_ = "commit";//提交
							}
							
							var entity_ = clone(this.formData);
							var entity = {};
							for (var r in entity_) {
								if (entity_[r] || entity_[r] == "" || entity_[r] == false || entity_[r] == 0) {
									entity[r] = entity_[r];
								}
							}
							
							var tableData_ = [];
							this.tableData.map(r=>{
								var row_ = {};
								for (var k in r) {
									if (r[k]) {
										row_[k] = r[k];
									}
								}
								tableData_.push(row_);
							})
							
							if (tableData_.length == 0) {
								Root.message({
									type: 'warning',
									message: '请先添加明细数据'
								});
								return
							}
							
							let param = {
								dataname: this.dataname,
								// operator: operator_,
								data: {},
							}
							param.data[this.dataname] = entity;
							param.data[this.table_dataname] = tableData_;
							
							Server.call("root/data/saveEntity", param, function(result) {
								console.log(result);
								if (result.success) {
									if(me.iscommit){
										me.iscommit = false;
										Root.message({
											type: 'success',
											message: '提交成功'
										}); 
										me.saveAfter();
									}
									else {
										Root.message({
											type: 'success',
											message: '保存成功'
										}); 
										me.saveAfter();
									}
								}
							});
						},
						
					}
				});
			};
			
			loadJsCss(function () {
				initVue();
			});
		</script>
		<style>
			/*  在vue.js中 v-cloak 这个指令是防止页面加载时出现 vuejs 的变量名而设计的 */
			[v-cloak] {
				display: none !important;
			}
		</style>
		
	</head>
	
	<body style="margin: 0px;">
		<div v-cloak id="vbody">
			<div id="page_root">
				<div ref="popup_body" style="padding: 0 20px;">
					<div class="el-dialog__header">
						<div class="dialog-title">
						  <i class="iconfont icon-customermanagement"></i>
						  <span> {{title}}</span>
						</div>
					</div>	
					<div style="height: 550px; overflow-y: auto;">
						<div class="el-dialog__body">
							<h-form
								ref="form1"
								:form-attr="formAttr"
								:table-fields="formFields"
								:form-data="formData"
								:table-field-click="formfieldClick"
							>
							</h-form>
							
							<div class="h_dialog__body">
								<div v-if="isedit || isrefuseedit" style=" text-align: right; ">
									<!-- 工具栏 -->
									<div  style=" display: inline-block; width: 90px;">
										<el-button-group>
											<el-button @click="addTableData">新增明细</el-button>
										</el-button-group>
									</div>
								</div>
								<h-table
									v-if="isRefresh && tableFields.length"
									ref="table1"
									:table-fields="tableFields" 
									:table-data="tableData" 
									:is-edit-table-data="isedit || isrefuseedit"
									:is-within-edit-table-data="isedit || isrefuseedit"
									:is-pagination="false"
									:table-field-click="tablefieldClick"
									:is-show-index="tableData.length ? true : false"
									:edit-table-button="editTableButton"
									
									v-on:get-data="getData"
									v-on:del-data="delData"
								>
								</h-table>
							</div>
							
							
						</div>
					</div>
					<div class="el-dialog__footer">
						<el-button size="small" type="default" @click="closeDialog">取 消</el-button>
						<el-button size="small" v-if="isedit || isrefuseedit" type="primary" @click="saveRowTable":icon="buttonsconfig.save.icon">{{buttonsconfig.save.name}}</el-button>
						<el-button size="small" v-if="isedit" type="success" @click="submitRowTable":icon="buttonsconfig.submit.icon">{{buttonsconfig.submit.name}}</el-button>
						<el-button size="small" v-if="isrefuseedit" type="success" @click="">再次提交</el-button>
						
						<el-button size="small" v-if="isapproval" type="primary" @click="">通 过</el-button>
						<el-button size="small" v-if="isapproval" type="danger" @click="">拒 绝</el-button>
						<el-button size="small" v-if="isapproval" type="primary" @click="">转 办</el-button>
						<el-button size="small" v-if="isapproval" type="success" @click="">退 回</el-button>
					</div>
				</div>
			</div>
		</div>
		
		<div id="page_loading" style="position: absolute; top:0px; width: 100vw; height: 50vh;">
			<div class="spinner">
			  <div class="cube1"></div>
			  <div class="cube2"></div>
			</div>
		</div>
	</body>
</html>