<!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 type="text/javascript">
			var pageVue = null;
			function loadJsCss(callback) {
				var jscss_urls = [];
				window.top.initJsCss(document, jscss_urls, callback);
			};
			
			function initVue() {
				pageVue = new TabTableVue({
					el: "#vbody",
					data: {
						title: "库存调整产品选择",
						dataRequest: [
							{
								name: "org_product",
								dataname: "md_prod_sku",//授权产品分类
								//url: "root/front/getOneDictionary",
								//paramsobj: { code: "OrgType", },
								filter: "parent_id='" + window.top.vue.userinfo.org.id + "'",
								isnotoption: false, //true:不是选项
								code:"product_category_name",//是下拉选项时设置
								label:"product_category_name",//是下拉选项时设置
							},
							{
								name: "wm_warehouse",
								dataname: "wm_warehouse",//授权产品分类
								//url: "root/front/getOneDictionary",
								//paramsobj: { code: "OrgType", },
								filter: "org_id='" + window.top.vue.userinfo.org.id + "' and is_active='T'",
								isnotoption: false, //true:不是选项
								code:"code",//是下拉选项时设置
								label:"name",//是下拉选项时设置
							},

							{
								name: "org_biz_regist_scope",
								dataname: "md_org_data.md_org_biz_regist_scope",//授权范围分类
								//url: "root/front/getOneDictionary",
								//paramsobj: { code: "OrgType", },
								filter: "parent_id='" + window.top.vue.userinfo.org.id + "'",
								isnotoption: true, //true:不是选项
							},
						],
						dataRequestObj: {},
						//Tab项设置
						tabs_d: [
							{code: "wm_detail", name: "库存明细", dataname: "wm_book_detail", selectedrows: [], filterTxt: "1=1", orderby: "id", lazy: true},
							{code: "product", name: "产品列表", dataname: "md_prod_sku", selectedrows: [], filterTxt: "1=1", orderby: "id", lazy: false},
						],
						
						d_tabs: {
							wm_detail: true,
							product: true,
						},
						
						tabs: [],
						tabsobj: {},
						popupParames: {},
						formData: {},
						filterTxt: "",
						paramObjBydefault: {},
						isedit: false,
						
						filterData: {},
						rowkey: "id",
						lazy: true,
						selectedparentrow: {},
						iscustomer: false,
						
					},
					created() {
						var me = this;
						if (window.top.vue.userinfo && window.top.vue.userinfo.org && window.top.vue.userinfo.org.is_account) {
							this.iscustomer = true;
						}
						
						this.tabs = this.tabs_d;
						
						this.popupParames = clone(Root.popupParames);
						this.title = this.popupParames.title || this.popupParames.text
						if (this.popupParames.filter) {
						    this.tabs[0].filterTxt = this.popupParames.filter
						}
						
						if (this.popupParames.data) {
							this.formData = this.popupParames.data;
						}
						if (this.popupParames.delta) {
							this.filterData = this.popupParames.delta;
						}
						
						if (this.popupParames.sceneCode) {
						    if (this.popupParames.sceneCode == "add") {//新增
						        this.isedit = 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
								if (!me.formData.salesgroup_code) {
									me.formData.salesgroup_code = "0001";
								}
								var filter = " salesgroup_code='"+me.formData.salesgroup_code+"' and enablestate='2'" ;
								var skufilter_= " 1=1";
								
								//过滤商业的授权产品
								var product_ = "";
								if (me.dataRequestObj.org_product) {
									me.dataRequestObj.org_product.map(p=>{
										if (product_ == "") {
											product_ = p.product_category_id;
										}
										else {
											product_ += "','" + p.product_category_id;
										}
									})
									if (product_) {
										product_ = " and authorize_category_id in ('" + product_ + "')";
									}
								}
								else {
									product_ = " and 1<>1"
								}
								skufilter_ += product_;
								
								//过滤商业的授权范围md_org_biz_regist_scope
								var scope_ = "";
								if (me.dataRequestObj.org_biz_regist_scope) {
									me.dataRequestObj.org_biz_regist_scope.map(p=>{
										var do_scope_ = [];
										if (p.is_2002) {
											do_scope_.push("â…¢-6877");
										}
										if (p.is_2017) {
											do_scope_.push("â…¢-03");
										}
										if (p.is_13) {
											do_scope_.push("â…¢-13");
										}
										
										do_scope_.map(s=>{
											if (scope_ == "") {
												scope_ = s
											}
											else {
												scope_ += "','" + s;
											}
										})
									})
									if (scope_) {
										scope_ = " and type_code in ('" + scope_ + "')";
									}
								}
								else {
									scope_ = " and 1<>1"
								}
								filter += scope_;
								
								me.tabs[1].filterTxt = filter;
								me.tabs[1].paramObjBydefault = {
									categoryfilter: skufilter_
								}
								
								if(me.tabs.length>0){
									me.activeTabName = me.tabs[0].code;
								}
								
								//初始化Tabs
								me.initTabsPage(function() {
									//获取数据
									me.initData();
								});
								
								me.$nextTick(() => { // 以服务的方式调用的 Loading 需要异步关闭
									hideLoading();
									
									//重新设置弹窗宽高
									me.$nextTick(function(){
										//let w_ = this.$refs.popup_body.offsetWidth + "px";
										let w_ = "1000px";
										let h_ = "650px";
										// let h_ = this.$refs.popup_body.offsetHeight + "px";
										Root.setPopupWH(w_, h_);
									})
								});
							});
						}
						else {
							if(me.tabs.length>0){
								me.activeTabName = me.tabs[0].code;
							}
							
							//初始化Tabs
							me.initTabsPage(function() {
								//获取数据
								me.initData();
							});
							
							me.$nextTick(() => { // 以服务的方式调用的 Loading 需要异步关闭
								hideLoading();
								//重新设置弹窗宽高
								me.$nextTick(function(){
									//let w_ = this.$refs.popup_body.offsetWidth + "px";
									let w_ = "1000px";
									// let h_ = this.$refs.popup_body.offsetHeight + "px";
									let h_ = '607px';
									Root.setPopupWH(w_, h_);
								})
							});
						}
					},
					
					methods:{
						//关闭弹窗
						closeDialog() {
							this.docloseDialog(true);
						},
						
						//关闭前调回调
						saveAfter() {
							var me = this;
							if(this.popupParames.callback) {
								let obj = {
									type: this.selectTabObj.code,
									row: clone(this.selectTabObj.selectedrow)
								}
								this.popupParames.callback(obj, function() {
									me.docloseDialog();
								});
							}
							else {
								me.docloseDialog();
							}
						},

						//多选保存
						saveListAfter() {
							let me = this;
							//获取购物车的数据
							var rows =  this.$refs[this.selectTabObj.code].getShoppingcart();
							
							if (rows && rows.length) {
								let obj = {
									type: this.selectTabObj.code,
									rowlist: clone(rows)
								}
								this.popupParames.callback(obj, function() {
									me.docloseDialog();
								});
							}
							else {
								Root.message({
									type: 'errer',
									message: '请先选取库存明细'
								}); 
							}
						},

						copyListAfter() {
							var me = this;
							//获取购物车的数据
							var rows =  this.$refs[this.selectTabObj.code].getShoppingcart();
							let copytimes = 1;
							var config = {
								totab: false,
								width: "600",
								height: '400',
								icon: "icon-product",
								text: "批量条数",
								url: "module/inventory/page/popup/copytimes.html",
								sceneCode: "add",
								data: {},
								callback: function(obj, callback) {
									if (obj.count) {
										copytimes = obj.count*1;
									}
									for(i = 1; i < copytimes; i ++) {
										rows.push(rows[0]);
									}
									
									if (rows && rows.length) {
										let obj = {
											type: me.selectTabObj.code,
											rowlist: clone(rows)
										}
										me.popupParames.callback(obj, function() {
											me.docloseDialog();
										});
									}
									else {
										Root.message({
											type: 'errer',
											message: '请先选取库存明细'
										}); 
									}

									if (callback) {
										callback();
									}
								}
							};
							me.doPopupByPublic(config);

						},
						
						onhandleClick() {
							this.initData();
						},
						
						initData() {
							var me = this;
							this.doQueryByTab(function(result, callback) {//查询后的回调,用于获取字段的
								if (result.meta && result.meta[me.selectTabObj.dataname] && result.meta[me.selectTabObj.dataname].fields) {
									var metas = clone(result.meta[me.selectTabObj.dataname].fields);
									var filterFields_ = [];
									var tableFields_ = [];
									if (me.selectTabObj.code == "wm_detail") {
										metas.map(f=>{
											f.isshow = "T";

											//Brooke
											// if(f.field == "org_code" || f.field == "org_name"){
											// 	if(me.iscustomer){
											// 		f.isfilter = false;
											// 	}
											// }
											if(f.field == "org_code" || f.field == "org_name" || f.field == "price" || f.field == "amt"){
												if(me.iscustomer){
													f.isfilter = false;
													f.isshow = "F";
												}
											}
											if(f.field == "category_code"){
												f.isfilter = false;
												f.isshow = "F";
											}
											
											if (f.field == "warehouse_name") {//仓库名称
												f.isfilter = false;
												// f.options = me.dataRequestObj.wm_warehouse;
											}

											//others
											if (f.isfilter) {
												if(f.field == "category_name") {//授权产品
													f.options = me.dataRequestObj.org_product;
												}
												
												filterFields_.push(clone(f));
											}
											else {
												var filter_f = clone(f);
												filter_f.isshow = "F";
												filterFields_.push(filter_f);
											}
											if (f.field == "cpid" || f.field == "is_active") {
												
											}
											else {
												tableFields_.push(clone(f));
											}
										})
										
										me.onQueryAfter();
										// var tableData_ = clone(me.selectTabObj.tableData);
										// tableData_.map(stock=>{
										// 	//rowkey
										// 	var key_ = stock.product_code + "_" + stock.batch_no
										// 	if (stock.batch_sn) {//有序列号
										// 		key_ = stock.product_code + "_" + stock.batch_no + "_" + stock.batch_sn
										// 	}
										// 	if (me.filterData[key_]) {
										// 		// stock.cssname = "z_notrowclick";
										// 		stock.h_classname = "z_notrowclick";
										// 	}
											
										// })
										// me.selectTabObj.tableData = clone(tableData_);
									}
									else if (me.selectTabObj.code == "product") {
										metas.map(f=>{
											f.isshow = "T";
											
											if (f.isfilter) {
												if(f.field == "authorize_category_name") {//授权产品
													f.options = me.dataRequestObj.org_product;
												}
												if (f.field == "warehouse_name") {//仓库名称
													f.options = me.dataRequestObj.wm_warehouse;
												}
												
												filterFields_.push(clone(f));
											}
											else {
												var filter_f = clone(f);
												filter_f.isshow = "F";
												filterFields_.push(filter_f);
											}
											if (f.field == "cpid" || f.field == "is_active") {
												
											}
											else {
												tableFields_.push(clone(f));
											}
										})
									}
									
									if (!me.selectTabObj.filterFields || (me.selectTabObj.filterFields && me.selectTabObj.filterFields.length == 0)) {
										me.selectTabObj.filterFields = clone(filterFields_);
										me.selectTabObj.tableFields = clone(tableFields_);
										
										//字段数组转字段obj,目的为了筛选时获取字段属性
										me.fieldsToFieldsObj();
										
										//设置字段事件
										me.tableFieldClick();
									}
								}
								
								if (callback) {
									callback();
								}
							});
						},
						
						tableFieldClick() {
							var me = this;
							var tablefieldClick = {};
							//表格字段事件设置
							tablefieldClick.input = {};
							tablefieldClick.all = {};
							
							this.selectTabObj.tablefieldClick = tablefieldClick[this.activeTabName];
						},
						
						doQueryByTab(callback) {
							let me = this;
							if (!this.selectTabObj) {
								this.selectTabObj = {}
							}
							var thistab = this.selectTabObj;
							
							let filter_ = "1=1";
							this.filterList = [];
							this.selectedrow = {};
							thistab.selectedrow = {};
							for(var k in thistab.filterObj) {
								let k_val = thistab.filterObj[k];
								
								if (k_val && typeof(k_val) == "string") {
									//去除两端的空格
									thistab.filterObj[k] = k_val = k_val.replace(/(^\s*)|(\s*$)/g, "");
									
									//去除两端的Tab
									thistab.filterObj[k] = k_val = k_val.replace(/(^\t*)|(\t*$)/g, "");
								}
								
								let fieldObj_ = thistab.filterFieldsObj[k];
								
								if (!fieldObj_) {
									continue
								}
								if (!fieldObj_) {
									fieldObj_ = {
										field: k
									}
								}
								
								let type_ = fieldObj_.type;
								let valfield_ = fieldObj_.valfield;
								if (type_ == "popup" && valfield_) {
									k_val = thistab.filterObj[valfield_];
								}
								
								let field_ = fieldObj_.field;
								
								let filteroperator_ = "equal";
								
								let isnull = false;
								let ornullval_ = fieldObj_.ornullval;
								if (ornullval_) {
									var nullvals_ = ornullval_.split(";");
									nullvals_.map(v=>{
										if (v == k_val) {
											isnull = true;
										}
									});
								}
								
								if (fieldObj_.filterfield) {
									field_ = fieldObj_.filterfield
								}
								if (fieldObj_.filteroperator) {
									filteroperator_ = fieldObj_.filteroperator
								}
								
								//传条件数组由后端拼接筛选数据
								/* let filterListObj_ = {
									field: field_,
									val: k_val,
									type: filteroperator_
								}
								
								this.filterList.push(filterListObj_);
								 */
								
								//传条件数据有前端拼接
								if (k_val && k_val != "" && k_val.length) { filter_ = getFilterByFilterType(filter_, type_, field_,  k_val, filteroperator_, isnull, fieldObj_); }
								
							}
							
							if (thistab.filterObjBydefault != {}) {
								for(var k in thistab.filterObjBydefault) {
									let k_val = thistab.filterObjBydefault[k];
									if (k_val.indexOf(',') != -1) {//包含英文","用in
										filter_ += " and " + k + " in ('" + k_val +"')";
									}
									else {
										filter_ += " and " + k + " ='" + k_val +"'";
									}
								}
							}
							
							if (thistab.filterTxt && thistab.code == "wm_detail") {
								filter_ += " and " + thistab.filterTxt;
							}
							
							if (me.filterTxt) {
								filter_ += " and " + me.filterTxt;
							}
							
							var attachmeta_ = false;
							if (!thistab.tableFields || (thistab.tableFields && thistab.tableFields.length == 0)) {
								attachmeta_ = true;
							}
							
							let param_ = {
								filterList: this.filterList,
								page: {
									pageno: thistab.pagenum,
									pagesize: thistab.pagesize
								},
								attachMeta: attachmeta_
							}
							
							if (thistab.code == "wm_detail") {
								param_.filter = filter_;
							}
							else if (thistab.code == "product") {
								param_.filter = thistab.filterTxt;
							}
							if (thistab.orderby) {
								param_.orderby = thistab.orderby;
							}
							if (thistab.dataname) {
								param_.dataname = thistab.dataname;
							}
							
							if (thistab.paramObjBydefault != {}) {
								for(var k in thistab.paramObjBydefault) {
									let k_val = thistab.paramObjBydefault[k];
									if (k == "categoryfilter") {
										k_val += " and " + filter_
									}
									param_[k] = k_val;
								}
							}
							if (this.paramObjBydefault != {}) {
								for(var k in this.paramObjBydefault) {
									let k_val = this.paramObjBydefault[k];
									param_[k] = k_val;
								}
							}
							
							thistab.tableData = [];
							//me.total = 0;					
							me.tableloading = true;
							Server.call(thistab.dataurl, param_, function(result) {
								console.log(result);
								me.tableloading = false;
								if (result && result.meta) {
									me.data_tablename_map = result.meta
								}
								
								if (result && result.data && result.data.entityset) {
									if (result.data.page && result.data.page.recordcount && thistab.pagenum > 1 && result.data.page.recordcount < (thistab.pagenum - 1) * thistab.pagesize) {
										thistab.pagenum = 1;
										me.doQueryByTab(callback);
										return;
									}
									
									var data_ = result.data.entityset;
									
									thistab.tableData = data_;
									thistab.total = result.data.page ? result.data.page.recordcount : 0;
								}
								else {
									thistab.total = 0;
								}
								me.onQueryAfter();
								if (callback) {
									callback(result, function() {
										me.tableDataAfter();
									});
								}
								
							}, function(){
								me.tableloading = false;
							});
						},
						
						onQueryAfter() {
							var me = this;
							
							if (me.selectTabObj.code == "wm_detail") {
								var tableData_ = clone(me.selectTabObj.tableData);
								tableData_.map(stock=>{
									//rowkey
									var key_ = stock.product_code + "_" + stock.batch_no
									if (stock.batch_sn) {//有序列号
										key_ = stock.product_code + "_" + stock.batch_no + "_" + stock.batch_sn
									}
									if (me.filterData[key_]) {
										// stock.cssname = "z_notrowclick";
										stock.h_classname = "z_notrowclick";
									}
									
								})
								me.selectTabObj.tableData = clone(tableData_);
							}
						},
						
						loadClick(tree, treeNode, resolve) {
							var me = this;
							var body_ = [];
							if (!this.selectedparentrow.id) {
								Root.message({
									type: 'warning',
									message: '未获取到物料'
								}); 
								resolve([])
								return
							}
							var param = {
								dataname: "wm_sn",
								filter: "book_detail_id='" + this.selectedparentrow.id + "'"
							}
							Server.call("root/data/getEntitySet", param, function(result) {
								console.log(result);
								var stockList = [];
								if (result.data && result.data.entityset) {
									var natpnumobj_ = {}
									result.data.entityset.map(stock=>{
										//rowkey
										var row_ = clone(me.selectedparentrow);
										var key_ =  me.selectedparentrow.product_code +"_"+ me.selectedparentrow.batch_no +"_"+ stock.batch_sn;
										row_.id = key_;
										// row_.nonhandnum = 1;
										row_.qty = 1;
										row_.batch_sn = stock.batch_sn;
										// row_.is_sn_active = null;
										row_.h_classname = null;
										row_.haschildren = null;
										if (me.filterData[key_]) {
											row_.h_classname = "z_notrowclick";
										}
										stockList.push(row_);
									})
									
								}
								resolve(stockList)
							}, function(errorncc) {
								console.log("获取库存失败", errorncc);
								Root.message({
									type: 'warning',
									message: '未获取到该库存的现存'
								}); 
								resolve([])
							})
						},
						// rowClick(obj) {
						// 	this.selectedrow = {};
						// 	this.selectedparentrow = {};
						// 	if (this.selectTabObj.code == "wm_detail") {
						// 		// if (obj.row.is_sn_active) {//有序列号
						// 		// 	this.$refs.table1.setCurrentRow()
						// 		// 	this.selectedparentrow = obj.row;
						// 		// 	if (obj.event.currentTarget.firstElementChild.firstElementChild.firstElementChild.tagName == 'DIV') {
						// 		// 		obj.event.currentTarget.firstElementChild.firstElementChild.firstElementChild.click();
						// 		// 	} else {
						// 		// 		obj.event.currentTarget.firstElementChild.firstElementChild.firstElementChild.nextElementSibling.click();
						// 		// 	}
						// 		// 	return;
						// 		// }
						// 		if (this.filterData[obj.row.vsncode]) {
						// 			this.$refs.table1.setCurrentRow()
						// 			return;
						// 		}
						// 		if (this.filterData[obj.row.materialcode]) {
						// 			this.$refs.table1.setCurrentRow()
						// 			return;
						// 		}
						// 	}
							
						// 	this.selectTabObj.selectedrow = obj.row;
						// },
						
						// rowDblclick(obj) {
						// 	if (this.selectTabObj.code == "wm_detail") {
						// 		// if (obj.row.is_sn_active) {//有序列号
						// 		// 	this.$refs.table1.setCurrentRow()
						// 		// 	return;
						// 		// }
								
						// 		if (this.filterData[obj.row.vsncode]) {
						// 			this.$refs.table1.setCurrentRow()
						// 			return;
						// 		}
								
						// 		if (this.filterData[obj.row.materialcode]) {
						// 			this.$refs.table1.setCurrentRow()
						// 			return;
						// 		}
						// 	}
							
						// 	this.selectTabObj.selectedrow = obj.row;
						// 	if (this.isedit) {
						// 		this.saveAfter();
						// 	}
						// },
						
						

						selectionChange(obj){
							var me = this;
							me.selectTabObj.selectedrows = obj;
						},
						saveShoppingcart(list) {
							var me = this;
							me.selectTabObj.selectedrows = list;
						},
					}
				});
			};
			
			loadJsCss(function () {
				initVue();
			});
		</script>
		<style>
			/* 在vue.js中 v-cloak 这个指令是防止页面加载时出现 vuejs 的变量名而设计的 */
			[v-cloak] {
				display: none !important;
			}
			.el-table__body tr.z_notrowclick>td{
				background-color: #aaa !important;
			}
			.el-table__body tr.z_notrowclick>td:hover {
				cursor: not-allowed;
			}
			.el-tabs__nav-scroll {
			    overflow: hidden;
			    position: relative;
			    padding-left: 20px;
			}
		</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="popup_topbar">
						<span>{{title}}</span>
						
						
					</div>
					<div>
						<el-tabs v-model="activeTabName" @tab-click="handleClick">
							<el-tab-pane v-for="(tab, k) in tabs" :label="tab.name" :name="tab.code" :key="k"></el-tab-pane>
						</el-tabs>
					</div>
				
					<div class="topbar-line">
						<div class="query-bar">
							<h-form-filter v-if="selectTabObj.filterFields" ref="form1" 
								:form-attr="filterAttr" 
								:table-fields="selectTabObj.filterFields" 
								:form-data="selectTabObj.filterObj" 
								:table-field-click="selectTabObj.filterfieldClick"
								:isdraggableorder="true"
								
								v-on:on-query="onQuery"
								v-on:on-init-query="onInitFilter"
								v-on:on-edit-query="onEditFilter"
								v-on:order-fields="orderFilterFields"
								>
							</h-form-filter>
						</div>
					</div>
				
					<div class="h_dialog__body">
						<h-table
							v-if="isRefresh && selectTabObj.tableFields && selectTabObj.tableFields.length"
							:ref="selectTabObj.code"
							:table-fields="selectTabObj.tableFields"
							:table-data="selectTabObj.tableData" 
							:is-edit-table-data="selectTabObj.isEditTableData"
							:pagesize="selectTabObj.pagesize"
							:pagenum="selectTabObj.pagenum"
							:total="selectTabObj.total"
							:table-height="selectTabObj.tableHeight"
							:table-field-click="selectTabObj.tablefieldClick"
							:is-highlight-row="isedit"
							
							:is-pagination="true"
							:rowkey="rowkey"
							:lazy="selectTabObj.lazy"
							
							v-on:get-data="getData"
							v-on:load-click="loadClick"

							:select-table-data="selectTabObj.selectedrows"
							:is-selection="true"
							:isshow-shoppingcart="true"
							v-on:selection-change="selectionChange"
							v-on:save-shoppingcart="saveShoppingcart"
						>
						</h-table>
						
					</div>
					
					<!-- 底部工具栏 -->
					<div class="el-dialog__footer">
						<el-button size="small" type="default" @click="closeDialog">å…³ é—­</el-button>
						<el-button v-if="isedit && selectTabObj.code == 'product'" size="small" :disabled = "!(selectTabObj.selectedrows && selectTabObj.selectedrows.length == 1)" type="primary" @click="copyListAfter">批量添加</el-button>
						<el-button v-if="isedit" size="small" :disabled = "!(selectTabObj.selectedrows && selectTabObj.selectedrows.length)" type="primary" @click="saveListAfter">确 定</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>