<template>
	<view class="pop-container">
		<uni-popup ref="popup" type="dialog" >
			<uni-popup-dialog 
				:title="title" 
				message="" 
				:duration="2000" 
				:before-close="true" 
				@close="handleCancel" 
				@confirm="handleOk">
				<view class="dialog-content">
					<view class="header-container">
						<uni-row class="demo-uni-row">
							<uni-col :span="5" style="padding-left: 10px;text-align: center" >
								<view  class="gjz-title">关键字</view>
							</uni-col>
							<uni-col :span="19">
								<uni-search-bar
									v-model="keyWord"
									 @input="input"
									 @cancel="cancel" 
									 @clear="clear" 
									  cancelButton="none"
								 />
							</uni-col>
						</uni-row>
						<uni-row class="demo-uni-row">
							<uni-col :span="24" style="text-align: center;">
								<button class="mini-btn" type="primary" size="mini"  @click="search">查询</button>
							</uni-col>
						</uni-row>
					</view> 
					<view style="height: 200px">
						 <zb-table
						 ref="zbTable"
						 :show-header="true"
						 :stripe="false"
						 :columns="column"
						 :data="data"
						 :highlight="true"
						 @currentChange="currentChange"
						 :border="true"
					    >
						 
						</zb-table> 
					</view>
				</view>
				<!-- <view slot="footer">
				  <button type="primary" @click="handleOk">确定</button>
				  <button @click="handleCancel">取消</button>
				</view> -->
			</uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
	import { getDicts } from "@/api/system/dict/data"; // 字典
	import { listStockMove, ListStockMoveHead, listTaskMove, listRwbm, listDepartment, listEmployee, listExtend, listWarehouse, listLocation, listUser, listPallet, listPackage, listCurrent, listCustomer } from "@/api/wms/stockMove.js";
	
	export default {
		props: {
		  title: {
		    type: String,
		    required: true
		  },
		  idxFlag:{
			type: String,
			required: true
		  },
		  formData:{
			type: Object,
			require: true
		  }
		},
		mounted() {
			
		},
		created: function(option) {
			
		},
		data() {
			return {
				currentFlag: '',
				keyWord: '', // 输入查询的值
				column: [],	 // 表头数据
				data: [], // 表体数据
				currentSelectData: {},  //当前选中数据行
			};
		},
		methods:{
			handleOk() {
				let returnData = {
					currentFlag: this.currentFlag,
					selectData: this.currentSelectData
				}
				this.$emit("sendData", returnData);
			      // 点击确定按钮的处理逻辑
				this.currentSelectData = {}
			    this.$refs.popup.close()
			},
			handleCancel() {
			  // 点击取消按钮的处理逻辑
			  this.$refs.popup.close()
			},
			showPopup() {
				// 传递的哪个跳转标志位
				this.currentFlag = this.idxFlag;
				let that = this;
				let cFlag = that.idxFlag;
				switch (cFlag){
					case "rwbm": //任务编码
							// 出库类型
							let inTypeObj = {}
							getDicts("out_type").then(response => { 
								  // that.inTypeOption = response.data;
								  let tmpArr = response.data;
								  if(tmpArr.length > 0){
									tmpArr.forEach((item,index)=>{
										inTypeObj[item.dictValue] = item.dictLabel;
									})
								  }
							});
							// 任务编码表头
							that.column = [
							  { name: 'taskCode', label:'任务编号', width:170,align:'center', emptyString:'--' },
							  { name: 'taskDate', label:'任务日期', width:120,align:'center', emptyString:'--'},
							];
							that.getRwbmList();
						break;
					case "bmOut": // 部门
					case "bmIn":
						// 部门表头
						that.column = [
						  { name: 'deptCode', label:'部门编码', width:130,align:'center', emptyString:'--' },
						  { name: 'deptName', label:'部门名称', width:120,align:'center', emptyString:'--'},
						];
						that.getBmList();
						break;
					case "zyOut": // 移出职员
						// 职员表头
						that.column = [
						  { name: 'employeeCode', label:'职员编码', width:120,align:'center', emptyString:'--' },
						  { name: 'employeeName', label:'职员名称', width:100,align:'center', emptyString:'--'},
						];
						that.getZyOutList();
						break;
					case "zyIn": // 移入职员
						// 职员表头
						that.column = [
						  { name: 'employeeCode', label:'职员编码', width:120,align:'center', emptyString:'--' },
						  { name: 'employeeName', label:'职员名称', width:100,align:'center', emptyString:'--'},
						];
						that.getZyInList();
						break;
					// case "zyIn":
					// 	// 职员表头
					// 	that.column = [
					// 	  { name: 'employeeCode', label:'职员编码', width:120,align:'center', emptyString:'--' },
					// 	  { name: 'employeeName', label:'职员名称', width:100,align:'center', emptyString:'--'},
					// 	];
					// 	that.getZyList();
					// 	break;
					case "sflbOut": // 出库类别
						that.column = [
							{ name: 'extendClassName', label:'类', width:60, align:'center', emptyString:'--' },
							{ name: 'extendCode', label:'编码', width:100, align:'center', emptyString:'--'},
							{ name: 'extendName', label: '名称', width:150, align:'center', emptyString:'--'},
						];
						that.getSflbOutList();
						break;
					case "sflbIn": // 入库类别
						that.column = [
							{ name: 'extendClassName', label:'类', width:60, align:'center', emptyString:'--' },
							{ name: 'extendCode', label:'编码', width:100, align:'center', emptyString:'--'},
							{ name: 'extendName', label: '名称', width:150, align:'center', emptyString:'--'},
						];
						that.getSflbInList();
						break;
					case "khIn": // 入客户
						that.column = [
						  { name: 'customerCode', label:'客户编码', width:100,align:'center', emptyString:'--' },
						  { name: 'customerName', label:'客户名称', width:300,align:'center', emptyString:'--'},
						];
						that.getKhList();
						break;
					case "tpIn": // 托盘
						// 托盘表头
						that.column = [
						  { name: 'palletCode', label:'托盘编码', width:100, align:'center', emptyString:'--'},
						  { name: 'palletName', label: '托盘名称', width:130, align:'center', emptyString:'--'},
						];
						that.getTpList();
						break;
					case "bzIn": // 包装
						// 包装表头
						that.column = [
						  { name: 'packageCode', label:'包装编码', width:100, align:'center', emptyString:'--'},
						  { name: 'packageName', label: '包装名称', width:130, align:'center', emptyString:'--'},
						];
						that.getBzList();
						break;
					case "wareOut": // 仓库
					case "wareIn":
						// 仓库表头
						that.column = [
						  { name: 'warehouseCode', label:'仓库编码', width:110, align:'center', emptyString:'--'},
						  { name: 'warehouseName', label: '仓库名称', width:140, align:'center', emptyString:'--'},
						];
						that.getWareList();
						break;
					case "locaOut": //移出货位选库存
						that.column = [
						  { name: 'locationCode', label:'货位编码', width:140, align:'center', emptyString:'--'},
						  { name: 'stockQty', label: '数量', width:120, align:'center', emptyString:'--'},
						];
						that.getCurrList();
						break;
					case "locaIn": //移入货位
						that.column = [
						  { name: 'locationCode', label:'货位编码', width:140, align:'center', emptyString:'--'},
						  { name: 'locationName', label: '货位名称', width:180, align:'center', emptyString:'--'},
						];
						that.getLocaList();
						break;
					case "shr": // 审核人
						that.column = [
						  { name: 'userId', label:'编码', width:120,align:'center', emptyString:'--' },
						  { name: 'nickName', label:'名称', width:100,align:'center', emptyString:'--'},
						];
						that.getShrList();
						break;
					case "stock": // 单据
						that.column = [
						  { name: 'stockCode', label:'单据编号', width:150, align:'center', emptyString:'--'},
						  { name: 'deptName', label: '部门', width:130, align:'center', emptyString:'--'},
						  { name: 'warehouseName', label: '仓库', width:150, align:'center', emptyString:'--'},
						  { name: 'rowCount', label: '行数', width:90, align:'center', emptyString:'--'},
						];
						that.getStockList();
						break;
					default:
						break;
				}
			
			},
			hidePopup() {
				this.$refs.popup.close();
			},
			// 单选当前行
			currentChange(row,index){
				this.currentSelectData = row;
		    },
			// searchBar
			input(value) {// uniSearchBar 的 value 改变时触发事件，返回参数为uniSearchBar的 value	e=value
				this.keyWord = value;
			},
			clear(res) {
				this.keyWord = "";
			},
			cancel(res) {
				this.keyWord = "";
			},
			// 查询按钮点击事件
			search(){
				let that = this;
				let cFlag = that.idxFlag;
				switch (cFlag){
					case "rwbm": // 任务编码
						this.getRwbmList();
						break;
					case "bmOut": // 部门
					case "bmIn":
						that.getBmList();
						break;
					case "zyOut": // 移出职员
						this.getZyOutList();
						break;
					case "zyIn": // 移入职员
						this.getZyInList();
						break;
					case "sflbOut": // 出库类别
						this.getSflbOutList();
						break;
					case "sflbIn": //入库类别
						this.getSflbInList();
						break;
					case "khIn": //入客户
						this.getKhList();
						break;
					case "tpIn": //托盘
						this.getTpList();
						break;
					case "bzIn": //包装
						that.getBzList();
						break;
					case "wareOut": // 仓库
					case "wareIn":
						this.getWareList();
						break;
					case "locaOut": // 货位
						this.getCurrList();
						break;
					case "locaIn":
						this.getLocaList();
						break;
					case "shr": // 审核人
						this.getShrList();
						break;
					case "stock": //单据
						this.getStockList();
						break;
					default:
						break;
				}
			},
			// 审核人
			getShrList(){
				let that = this;
				let params = {
					status: '0', //(固定)
					params: {
						"userids": that.formData.userids,
						"keyWord":that.keyWord,
						"limit":50
					},
				}
				listUser(params).then(res => {
					if(res.code === 200){
						that.data = res.rows;
						this.$refs.popup.open();
					}
				});
			},
			// 仓库数据
			getWareList(){
				let that = this;
				let params = {
					status: '0', //(固定)
					params: {
						"keyWord":that.keyWord,
						"limit":50
					},
				}
				
				listWarehouse(params).then(res => {
					if(res.code === 200){
						that.data = res.rows;
						this.$refs.popup.open();
					}
				});
			},
			// 现存量
			getCurrList(){
				let that = this;
				let params = {
					params: {
						"limit":50
					},
					labelCode: that.formData.labelCode,
					warehouseCode: that.formData.warehouseCodeOut,
				}
				
				listCurrent(params).then(res => {
					if(res.code === 200){
						that.data = res.rows;
						this.$refs.popup.open();
					}
				});
			},
			// 货位数据
			getLocaList(){
				let that = this;
				let params = {
					status: '0', //(固定)
					warehouseCode: that.formData.warehouseCodeIn,
					params: {
						"keyWord":that.keyWord,
						"limit":50
					},
				}
				
				listLocation(params).then(res => {
					if(res.code === 200){
						that.data = res.rows;
						this.$refs.popup.open();
					}
				});
			},
			// 获取任务编码数据
			getRwbmList(){
				let that = this;
				let params = {
					billTypeCode: that.formData.taskBillTypeCode,
					isControlTask: that.formData.isControlTask,
					params: {
						"keyWord":that.keyWord,
						"limit":50
					},
				}
				listTaskMove(params).then(res => {
					if(res.code === 200){
						console.log(res)
						that.data = res.rows.map(item => {
							let a1 = item.wmsTaskMoves;
							let a2 = item;
						    return Object.assign({}, a2, a1);
						});
						this.$refs.popup.open();
					}
				});
			},
			// 获取部门数据
			getBmList(){
				let that = this;
				let params = {
					status: '0', //(固定)
					params: {
						"keyWord":that.keyWord,
						"limit":50
					},
				}
				listDepartment(params).then(res => {
					if(res.code === 200){
						that.data = res.rows;
						this.$refs.popup.open();
					}
				});
				
			},
			// 移出职员
			getZyOutList(){
				let that = this;
				if(that.formData.deptCode === ""){
					uni.showToast({
						title: "请先选择部门",
						icon: "none",
					})
					return; 
				}
				
				let params = {
					status: '0', //(固定)
					deptCode: that.formData.deptCodeOut,
					params: {
						"keyWord":that.keyWord,
						"limit":50
					},
				}
				
				listEmployee(params).then(res => {
					if(res.code === 200){
						that.data = res.rows;
						this.$refs.popup.open();
					}
				});
			},
			// 移入职员
			getZyInList(){
				let that = this;
				if(that.formData.deptCode === ""){
					uni.showToast({
						title: "请先选择部门",
						icon: "none",
					})
					return; 
				}
				
				let params = {
					status: '0', //(固定)
					deptCode: that.formData.deptCodeIn,
					params: {
						"keyWord":that.keyWord,
						"limit":50
					},
				}
				
				listEmployee(params).then(res => {
					if(res.code === 200){
						that.data = res.rows;
						this.$refs.popup.open();
					}
				});
			},
			// 获取收发类别数据
			getSflbOutList(){
				let that = this;
				let params = {
					extendClassCode: "01.02",
					params: {
						"keyWord":that.keyWord,
						"limit":50
					},
				}
				
				listExtend(params).then(res => {
					if(res.code === 200){
						that.data = res.rows;
						this.$refs.popup.open();
					}
				});
			},
			getSflbInList(){
				let that = this;
				let params = {
					extendClassCode: "01.01",
					params: {
						"keyWord":that.keyWord,
						"limit":50
					},
				}
				
				listExtend(params).then(res => {
					if(res.code === 200){
						that.data = res.rows;
						this.$refs.popup.open();
					}
				});
			},
			// 获取客户数据
			getKhList(){
				let that = this;
				let params = {
					status: '0', //(固定)
					params: {
						"keyWord":that.keyWord,
						"limit":50
					},
				}
				listCustomer(params).then(res => {
					if(res.code === 200){
						that.data = res.rows;
						this.$refs.popup.open();
					}
				});
				
			},
			getTpList(){
				let that = this;
				
				let params = {
					status: '0',
					params: {
						"keyWord":that.keyWord,
						"limit":50
					},
				}
				
				listPallet(params).then(res => {
					if(res.code === 200){
						that.data = res.rows;
						this.$refs.popup.open();
					}
				});
			},
			getBzList(){
				let that = this;
				let params = {
					status: '0',
					params: {
						"keyWord":that.keyWord,
						"limit":50
					},
				}
				
				listPackage(params).then(res => {
					if(res.code === 200){
						that.data = res.rows;
						this.$refs.popup.open();
					}
				});
			},
			getStockList(){
				let that = this;
				let params = {
					params: {
						"keyWord":that.keyWord,
						"limit":50
					},
				}
				ListStockMoveHead(params).then(res => {
					if(res.code === 200){
						that.data = res.rows;
						this.$refs.popup.open();
					}
				});
			},
		},
	}
</script>

<style>
	@import "@/static/scss/commonpopup.css"
</style>
