<!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>发货单列表弹出窗体</title>
	<script src="../../../../js/vue/vue.js"></script>
	<script src="../../../../js/vue/element-ui/lib/index.js"></script>
	<script src="../../../../js/myelement.js?v=20220615"></script>
	<script src="../../../../js/vue/page.js?v=20220615"></script>
	
	<link href="../../../../js/vue/element-ui/lib/theme-chalk/index.css" rel="stylesheet">
	<link href="../../../../css/myelement.css?v=20220425" rel="stylesheet">
	<link href="../../../../css/iconfont.css" rel="stylesheet">
	<link href="../../../../css/page.css?v=20220425" rel="stylesheet">
	<link href="//at.alicdn.com/t/font_2374495_13ltsxm2eor.css" rel="stylesheet">	
</head>
<body>
	<div id="vbody">
		<div id="page_root">
			<div ref="popup_body" style="padding-right: 20px;">
				<div class="el-dialog__header">
					<div class="dialog-title">
						<i class="iconfont icon-customermanagement"></i>
						<span>选择发货单</span>
					</div>
				</div>	
				<div style=" overflow-y: auto;">
					<div class="el-dialog__body">
						<div class="topbar-line" v-if="isshow">
							<div class="query-icon">
								<i class="iconfont icon-query"></i>
							</div>
							<div class="query-bar" style="width: 85%;">
								<h-form-filter ref="form1"
									:form-attr="filterAttr" 
									:table-fields="filterFields" 
									:form-data="filterObj" 
									:isbuttonquery="true"
									v-on:on-formchange="onQuery" 
									v-on:show-popup="onShowFilterPopup"
									v-on:on-query="onQuery"
									v-on:on-init-query="onInitFilter"
									>
								</h-form-filter>
							</div>
						</div>
						<h-table
							v-if="isRefresh"
							ref="table1"
							:table-fields="tableFields_order" 
							:table-data="tableData" 
							:pagesize="pagesize"
							:pagenum="pagenum"
							:total="total"
							:tableloading="tableloading"
							:table-height="tableHeight2"
							:is-within-edit-table-data= "true"
							:is-highlight-row="true"
							:is-selection="isSelection"
							v-on:row-click="rowClick"
							v-on:get-data="getData"
							v-on:cell-input-change="cellInputChange"
							v-on:selection-change="handleSelectionChange"
							v-on:row-dblclick="rowDblClick"
						>
						</h-table>
					</div>
				</div>
				<div class="el-dialog__footer">
					<el-button type="default" @click="close">取 消</el-button>
					<el-button type="primary" @click="saveFormValByField_order">保 存</el-button>
				</div>
			</div>
		</div>
		<div id="page_loading" style="position: absolute; top:0px; width: 100vw; height: 100vh;">
			<div class="spinner">
			  <div class="cube1"></div>
			  <div class="cube2"></div>
			</div>
		</div>
	</div>
	<script type="text/javascript">
		new ListVue({
			el: "#vbody",
			data: {
				dataname: "getDeliveryOrder",
				pageAttr: {
				      heightType: "page"
				     },
				filterAttr: {
					columnnumber: 3,
					labelwidth: "100px",
					labelposition: "right",
					size: "medium",
					border: "3px solid #c6c6c600"
				},
				filterFields:[],
				filterFieldsObj: {},
				filterObj: {},
				
				isRefresh: true,
				tableFields_order: [],
				tableData: [],
				pagesize: 10,
				pagenum: 1,
				total: 0,
				tableHeight2: 330,
				isshow:true,
				isSelection:true,
				dataRequest: [
					{
						isClientMode: false,
						code: "code",
						label: "name",
						name: "delivery_part_code",
						dataname: "deliveryOrganization",
					},
				],
				selectionList: [],
				orderby: "parentid",
			},
			
			created() {
				this.popupParames = clone(Root.popupParames);
				if(this.popupParames.type == "delivery" || this.popupParames.type == "return")
					this.isSelection = false;
			},
			
			mounted() {
				let me = this;
				if (this.popupParames.type == "delivery" && this.dataRequest.length) {
					this.tableFields_order = dataRootFields.tableFields.chooseDeliveryedOrder_;
					this.isshow = false;
					//me.filterFields = dataRootFields.filterFields.chooseDeliveryedOrder_;
					var result = {};
					this.loadRequestData(this.dataRequest, result, function(data) {
						me.dataRequestObj = data;
						if (me.filterFields.length) {
							var formFields_part_ = clone(me.filterFields);
							formFields_part_.map(e=>{
								if(e.field == "delivery_part_code") {
									e.options = me.dataRequestObj.delivery_part_code;
								}
							});
							
							me.filterFields = formFields_part_;
						}
					});
				}
				this.onQuery();
				this.$nextTick(() => { // 以服务的方式调用的 Loading 需要异步关闭
					document.getElementById('page_root').style.display =  "block";
					document.getElementById('page_loading').style.display =  "none";
					
					//重新设置弹窗宽高
					this.$nextTick(function(){
						document.getElementById('page_root').classList.add("z_popup-page");
						let w_ = this.$refs.popup_body.offsetWidth + "px";
						let h_ = this.$refs.popup_body.offsetHeight + "px";
						Root.setPopupWH(w_, h_);
						this.tableHeight = this.setpageHeight("table1","form1");
						
					})
				});
				
			},
			
			methods:{
				onQuery(){
					this.pagenum = 1;
					this.doQuery();
					this.isRefresh = true
				},
				
				doQuery() {
					let me = this;
					let filter_ = "deo.order_type = 1";
					
					this.tableloading = true;
					if (this.popupParames.filter) {
						filter_ = this.popupParames.filter;
					}
					if (this.popupParames.type) {
						if (this.popupParames.type == "delivery") {
							this.dataname = "getDeliveryOrderDelivery";
							if(this.popupParames.delivery_part_code) {
								filter_ += " sales_order.delivery_part_code = '" + this.popupParames.delivery_part_code + "'" +"and "
							}
							filter_ += "sales_order.code is not null GROUP BY delivery_part_code"
						}
						else if (this.popupParames.type == "return") {
							this.tableFields_order = dataRootFields.tableFields.chooseReturnOrder_;
							me.filterFields = dataRootFields.filterFields.chooseOrder_;
							//this.dataname = "salesOrderDetailWithDelivery";
							filter_ += " and delivery_order.withdraw <> 1"
							this.dataname = "getReturnorderAndDetail";
							
							
							
						}
						else if (this.popupParames.type == "invoice") {
							this.tableFields_order = dataRootFields.tableFields.chooseSinvoiceOrder_;
							this.dataname = "getinvoiceorder";
						}
					}
					me.selectionList = this.selectionList;
					if(me.filterFields.length > 0) {
						for(var i=0; i < me.filterFields.length; i++) {
							let fieldObj_ = me.filterFields[i];
							
							me.filterFieldsObj[fieldObj_.field] = fieldObj_;
						}
					}	
					for(var k in this.filterObj) {
						let k_val = this.filterObj[k];
						let fieldObj_ = this.filterFieldsObj[k];
						
						let type_ = fieldObj_.type;
						let field_ = fieldObj_.field;
						let fieldtype_ = "equal";
						if (field_ == "product_name" && this.dataname == "getReturnorderAndDetail") {
							field_ = "sales_order_detail.product_name"
						}
						else if (field_ == "product_code" && this.dataname == "getReturnorderAndDetail") {
							field_ = "sales_order_detail.product_code"
						}
						else if (field_ == "biz_date" && this.dataname == "getReturnorderAndDetail") {
							field_ = "sales_order.biz_date"
						}
						
						else if (fieldObj_.fieldname) {
							field_ = fieldObj_.fieldname
						}
						if (fieldObj_.fieldtype) {
							fieldtype_ = fieldObj_.fieldtype
						}
						if (fieldtype_ == "like") {
							filter_ += " and " + field_ + " like '%" + k_val +"%'";
						}else if (type_ == "daterange" && k_val.length) {//期间筛选
							//
							var k_val_a = k_val[0];
							var k_val_b = k_val[1];
							
							if (fieldtype_ == "inScope") {//期间之内,不包含两端
								filter_ += " and (" + field_ + ">'" + k_val_a + "' and " + field_ + "<'" + k_val_b + "')";
							}
							else if (fieldtype_ == "outScope") {//期间之外,不包含两端
								filter_ += " and (" + field_ + "<'" + k_val_a + "' or " + field_ + ">'" + k_val_b + "')";
							}
							else if (fieldtype_ == "inScopeInclude") {//期间之内,包含两端
								filter_ += " and ((" + field_ + ">'" + k_val_a + "' and " + field_ + "<'" + k_val_b + "') or (" + field_ + "='" + k_val_a + "' or " + field_ + "='" + k_val_b + "'))";
							}
						}
						else {
							filter_ += " and " + field_ + " = '" + k_val +"'";
						}
					}
					
					let param_table = {
						isClientMode: false,
						dataname: this.dataname,
						filter: filter_,
						page: {
							no: this.pagenum,
							pagesize: this.pagesize
						},
						orderby:"biz_date desc"
					}
					if(this.popupParames.trans_customer_code && this.popupParames.type == "return") {
						param_table.trans_customer_code = this.popupParames.trans_customer_code;
					}
					Server.call("root/data/getEntitySet", param_table, function(result) {
						me.total = 0;
						me.tableData = [];
						me.tableloading = false
						console.log(result)
						if (result && result.data) {
							var aa_ = result.data.entityset;
							me.total = result.data.page.recordcount;
							for (var i = 0; i < aa_.length; i++) {
								aa_[i].qty_canDelivery = aa_[i].cnt - aa_[i].delivery_qty;
								aa_[i].qty_needDelivery = aa_[i].qty_canDelivery; 
								aa_[i].return_qty = aa_[i].return_qty?aa_[i].return_qty:"0"; 
								
								//aa_[i].qty_canReturn = aa_[i].checkout_cnt - (aa_[i].return_qty?aa_[i].return_qty:0) + "";
								aa_[i].qty_canReturn = aa_[i].can_return_qty?aa_[i].can_return_qty:"0";
								aa_[i].qty_needReturn = aa_[i].qty_canReturn;
								/* aa_[i].qty_canInvoice = aa_[i].delivery_qty - aa_[i].invoice_qty;
								aa_[i].qty_needInvoice = aa_[i].qty_canInvoice; */
								/*if(me.popupParames.type == "return") {
									if(aa_[i].qty_canReturn > 0) {
										me.tableData.push(aa_[i])
									}
								}else { */
									me.tableData.push(aa_[i])
								/*} */
								
							}
						}
					});
				},
				onServerInitData(data) {
					
				},
				
				setpageHeight(table_ref,form_ref) {
					var tableHeight_ = 0;
					if(!this.$refs[table_ref] || !this.$refs[form_ref]) {
						return;
					}
					let table_el = this.$refs[table_ref].$el;
					let form_el = this.$refs[form_ref].$el;
					let clientHeight = document.getElementsByClassName('el-dialog__body').length ? document.getElementsByClassName('el-dialog__body')[0].offsetHeight : 0;
					
					let pagination_height = 0;
					if (table_el.getElementsByClassName('z_table_pagination').length) {
						pagination_height = table_el.getElementsByClassName('z_table_pagination')[0].offsetHeight;
					}
					tableHeight_ = clientHeight - pagination_height - form_el.offsetHeight - 50;
					return tableHeight_;
				},
				
				onShowFilterPopup(filter) {
					var me = this;
					var fieldObj_ = filter.obj;
					var filterValue = clone(me.filterObj);
					var field_ = fieldObj_.field;
					
					if ("customername" == field_) {
						Root.showPopup({
							 
							url: "../md/customer/popup/customer_list.html",
							width: 800,
							height: 550,
							callback: function(result, callback) {
								filterValue[field_] = result.row.md_description;
								me.filterObj = filterValue;
								me.onQuery();
								if (callback) {
									callback();
								}
							}
						});
					}
				},
				rowDblClick(event) {
					if (this.popupParames.type == "delivery" || this.popupParames.type == "return") {
						this.saveFormValByField_order();
					}
				},
				close: function() {
					Root.hidePopup();
				},
				rowClick(event) {
					this.selectcusrow = event.row;
					var tabdata = new Array();;
					tabdata[0] = event.row
					this.$refs.table1.toggleSelection(tabdata);
				},
				saveFormValByField_order() {
					if (this.popupParames.type == "delivery") {
						let obj = {
							row: this.selectcusrow
						}
						document.getElementById('page_loading').style.display = "block"
						this.popupParames.callback(obj, function() {
							Root.hidePopup();
						});
					}
					else if(this.popupParames.callback) {
						let array =[];
						if(this.selectionList.length > 0)
							array = clone(this.selectionList)
						else {
							if(this.selectcusrow.code)
								array.push(this.selectcusrow)
							else
								array = clone(this.tableData)
						}
						this.popupParames.callback(array, function() {
							Root.hidePopup();
						});
					}
					else {
						alert("该按键事件缺失!")
					}
				},
				
				handleSelectionChange(selection) {
					if (selection.length > 0) {
						var doccode_ = selection[0].doccode;
						var customerid_ = selection[0].customerid;
						if (this.popupParames.type == "invoice") {
							for (var i = 0; i < selection.length; i++) {
								if (selection[i].customerid != customerid_) {
									Root.message({
										type: 'warning',
										message: '只可选择同一订单'
									})
									this.onInitFilter();
									return;
								}
								this.selectionList = selection;
							}
						}
						else {
							for (var i = 0; i < selection.length; i++) {
								if (selection[i].doccode != doccode_) {
									Root.message({
										type: 'warning',
										message: '只可选择同一订单'
									})
									this.onInitFilter();
									return;
								}
								this.selectionList = selection;
							}
						}
					}
				},
				
				cellInputChange(scope) {
					let row = scope.row;
					let field = scope.column.property;
					if (row.qty_needDelivery > (row.cnt - row.delivery_qty)) {
						Root.message({
							type: 'warning',
							message: '超过可发货数量'
						})
						row.qty_needDelivery = 0 ;
						return;
					}
					else if (parseInt(row.qty_needReturn)  > parseInt(row.qty_canReturn)) {
						Root.message({
							type: 'warning',
							message: '超过可退货数量'
						})
						row.qty_needReturn = 0 ;
						return;
					}
					else if (row.qty_needInvoice > (row.qty_order - row.qty_invoice)) {
						Root.message({
							type: 'warning',
							message: '超过可开票数量'
						})
						row.qty_needReturn = 0 ;
						return;
					}
				},
			}
		});
			
	</script>	
	
	<!-- <div id="vbody">
		<div class="topbar-line">
			<div ref="popup_body" style="padding-right: 20px;">
				<div class="query-icon">
					<i class="iconfont icon-query"></i>
				</div>
				<div class="query-bar" style="width: 90%;">
					<h-form-filter ref="form1" 
						:form-attr="filterAttr" 
						:table-fields="filterFields" 
						:form-data="filterObj" 
						
						v-on:show-popup="onShowFilterPopup"
						v-on:on-query="onQuery"
						v-on:on-init-query="onInitFilter"
						>
					</h-form-filter>
				</div>
				<h-table
					v-if="isRefresh"
					ref="table_order"
					:table-fields="tableFields_order" 
					:table-data="tableData_order2" 
					:pagesize="pagesize"
					:pagenum="pagenum"
					:total="total"
					:table-height="tableHeight_order"
					:is-within-edit-table-data= "true"
					:is-highlight-row="true"
					:is-selection="true"
					v-on:row-click="rowClick"
					v-on:cell-input-change="cellInputChange"
					v-on:selection-change="handleSelectionChange"
					v-on:row-dblclick="rowDblClick"
				>
				</h-table>
			</div>
			<div class="el-dialog__footer">
				<el-button type="default" @click="close">取 消</el-button>
				<el-button type="primary" @click="saveFormValByField_order">确 定</el-button>
			</div>		
		</div>
	</div> -->
	</body>
</html>