import moment  from 'moment';
import store  from '@/store';
import { 	genWarehouseAuthSearchModal,
					genCargoownerAuthSearchModal,
					genWarehouseLocatorSearchModal,
		}  from '@/views/kzyWMS/SearchModal';
import { upcloud_base_inventory,upcloud_base_strategy } from "@/config/serviceSettings";

let rows = null ;	// on search获取当前行数据
let warehouseId= store.getters.warehouse.warehouseId;	//仓库id;
let cargoOwnerId= null; //货主id;
let itemId= null; //商品id;
let warehouseAreaId=null;  // 源库区id
let srcWarehouseCategory =null;  // 源库别编码
let warehouseAreaCategory =null;  // 目的库别编码

let destWarehouseAreaId=null;  // 目的库区id
let batchId=null;	//商品批次ID
let locatorId=null; // 货位id
let toWhLocatorId=null; // 目标货位id
let quantityMode = {} // 包装属性
let paramData = {}

const grid = { span: 8, labelCol: { span: 6 } };


const items = [
	// 仓库
	...genWarehouseAuthSearchModal(1, true, false,null, 
		function (rowData, form) {
			if (!rowData) {
				warehouseId = '';
				form.setFieldsValue({
					companyId: '',
					companyCode:'',
					companyName:''
				})
			} else {
				warehouseId = rowData[0].companyId;
				form.setFieldsValue({
					warehouseId: rowData[0].companyId,
					warehouseCode: rowData[0].companyCode,
					warehouseName: rowData[0].companyName,
				})
			}
		},
		{...grid },
	),
	
	// 货主(与账户权限关联)
	...genCargoownerAuthSearchModal(1, true, false,
			function(vn,p){
				console.log('remoteBefore参数回调：',vn,p);
			}, 
			function (rowData, form) {
				if(rowData){
					console.log('货主表格rowData：',rowData);
					cargoOwnerId =rowData[0].companyId; //货主id
					console.log("仓库id:",warehouseId);
					console.log("货主id:",cargoOwnerId);
				} else {
					console.log('货主表格rowData：',rowData);
					cargoOwnerId = null;
				}
						
			},
			{...grid},
	),
	{ type:'input',name:'erpBillId',label:'ERP移库单ID',value:'', ...grid },
	{ type:'input',name:'erpBillNo',label:'ERP移库单号',value:'', ...grid },
	{ type:'date',name:'iarDate',label:'单据日期',value:'',format:'YYYY-MM-DD',disabled:true,...grid},

	
	// { type:'input',name:'warehouseName1',label:'自动单号',value:'',disabled:true,...grid },
	{ name:'warehouseWorkerId',label:'操作员id',value:store.getters.userInfo.userId,hidden:true,...grid},
	{ type:'input',name:'warehouseWorkerName',label:'操作员',value:store.getters.userInfo.username,disabled:true,...grid},
	{ type:'input',name:'enterpriseName',label:'企业',value:store.getters.userInfo.enterpriseName,disabled:true,...grid },
	// { type:'input',name:'warehouseLevel',label:'移货位类型',value:'移库',disabled:true,...grid },
	// { type:'input',name:'platStatus',label:'来源类型',value:'',...grid },
	// 部门
		{ type:'input',name:'departmentName',label:'部门',value:store.getters.userInfo.organizations?store.getters.userInfo.organizations[0]?store.getters.userInfo.organizations[0].name:'':'',disabled:true,...grid },
		{ type:'input',name:'departmentId',label:'部门ID',hidden:true,value:store.getters.userInfo.organizations?store.getters.userInfo.organizations[0]?store.getters.userInfo.organizations[0].id:'':'',disabled:true,...grid },
	
	{ type:'input',name:'comments',label:'备注',...grid },
	/*{
  		...grid,
  		type: 'buttonGroup',
  		style: { textAlign: 'center' },
  		options: [
  		   {value: '保存',icon: 'save',btnType: 'primary',style: 'margin-right:16px',htmlType: 'submit'},
  		   {value: '重置',name: 'ccz',icon: 'redo',
  			on: {
  			  click(event, form) {
  				form.resetFields();
  				 form.setFieldsValue({
  				  	warehouseName: '',
  					warehouseId:'',
  				 });
  					}
  				}
  			}
		]
	}*/
]


const columns = function(_this){
	return	[
		{ hidden: true, dataIndex: "key",customRender:(text,rows,index)=>index.toString() },
		{type:'txt',dataIndex:'batchId',title:'批次ID',hidden:true},
		{type:'txt',dataIndex:'cargoOwnerId',title:'货主id',hidden:true},
		{type:'txt',dataIndex:'erpBillId',title:'ERP移库单ID',hidden:true},
		{type:'txt',dataIndex:'erpBillNo',title:'ERP移库单编号',hidden:true},
		{type:'txt',dataIndex:'erpLineId',title:'ERP明细ID',hidden:true},
		{type:'txt',dataIndex:'inContainerCode',title:'移入容器ID',hidden:true},
		{type:'txt',dataIndex:'inGoodsCaseCode',title:'移入库位ID',hidden:true},
		{type:'txt',dataIndex:'inLocatorId',title:'移入货格',hidden:true},
		{type:'txt',dataIndex:'outContainerCode',title:'移出容器ID',hidden:true},
		{type:'txt',dataIndex:'outGoodsCaseCode',title:'移出货格',hidden:true},
		{type:'txt',dataIndex:'outLocatorId',title:'移出库位ID',hidden:true},
		{type:'txt',dataIndex:'warehouseId',title:'仓库ID',hidden:true},
		{type:'txt',dataIndex:'cargoOwnerCode',title:'货主编号',hidden:true},
		{type:'txt',dataIndex:'cargoOwnerId',title:'货主id',hidden:true},
		{type:'txt',dataIndex:'cargoOwnerName',title:'货主名称',hidden:true},
		{type:'txt',dataIndex:'warehouseWorkerId',title:'库工ID',hidden:true},
		{type:'txt',dataIndex:'itemId',title:'商品ID',hidden:true},
		
		{ type:'txt',dataIndex:'itemCode',title:'商品编码',width:120 },
		{ type:'txt',dataIndex:'itemName',title:'商品名称',width:160 },
		// { type:'txt',dataIndex:'caseQuantity',title:'整件数',width:80},
		// { type:'txt',dataIndex:'oddPieceQuantity',title:'零散数',width:80},
		{ type:'txt',dataIndex:'erpPlanQuantity',title:'ERP计划数量',width:80},
		// { type:'txt',dataIndex:'uom',title:'包装单位',width:80},
		{ type:'txt',dataIndex:'caseConvertionRate',title:'包装数量',width:80 },
		{ type:'txt',dataIndex: "isCaseOrPiece",title: "存储属性",width:100,customRender:(text,rows,index)=>{
			switch(text){
				case 0:
					return '整散合一';
					break;
				case 1:
					return '整散分离';
					break;
			}
		}},
		{ type:'txt',dataIndex:'manufacturerLotNum',title:'批号',width:80},
		{
			title: "移出货位编码",
			type: "searchModal",
			width:100,
			required: true,
			dataIndex: "locatorId",
			targetClick: true,
			destroyOnClose: true,
			targetValueKey: "locatorCode",
			remoteUrl: { url: upcloud_base_inventory+'/stock/v1/qty/avl', method: 'get' },
			toolBar: {
			  items: [
				{type: "input",name: "locatorName",label: "货位名称",value: "",},
				{type: "input",name: "locatorCode",label: "货位编码",value: "",},
			  ],
			},
			modalOpenChange: open => {
				let  bool = false;
				_this.$refs.search.form.validateFields((err,values)=>{
					  if (err) {
						_this.$message.warn('请选择货主！');
						bool = false;
					  } else {
						bool = true;
					  }
				})
				return  bool;
			},
			tableConfig: {
			  rowKey: "id",
			  // remoteParams:remoteParams,
			  remoteBefore:(vn,p)=>{
				  console.log("当前行数据：",rows);
						p.params.warehouseId = warehouseId;
						p.params.cargoOwnerId = cargoOwnerId;
						p.params.itemId = rows.itemId;
						p.params.outStockStatus = rows.outStockStatus;	//下架库存状态,列表暂无此字段
						p.params.manufacturerLotNum = rows.manufacturerLotNum;	//生产商批号
						return p;
					},
			  columns: [
				{dataIndex: "locatorName",title: "货位名称"},
				{dataIndex: "locatorCode",title: "货位编码"},
				{dataIndex: "quantity",title: "数量"},
				{dataIndex: "availableQuantity",title: "可用数量"},
				{dataIndex: "batchNum",title: "批号",ellipsis:true},
				{dataIndex: "warehouseAreaName",title: "库区名称",ellipsis:true},
				{dataIndex: "warehouseAreaCategoryName",title: "库别名称",ellipsis:true},
				{dataIndex: "warehouseAreaCategory",title: "库别"},
			  ],
			},
			on: {
				search:(val1,val2,val3,val4,val5,val6,val7,val8,val9)=>{
					console.log(arguments,val1,val2,val3,val4,val5,val6,val7,val8,val9)
					rows = val5;	//当前行数据
				},
				change:({ event, name, value, modalRowData, record, index})=>{
						console.log("货位行数据",event, name, value, modalRowData, record, index);
						record.warehouseAreaCategoryName = modalRowData[0].warehouseAreaCategoryName ;
						// record.batchNum = modalRowData[0].batchNum ;  //批号
						// record.availableQuantity = modalRowData[0].availableQuantity ;  //可用库存数量
						// record.quantity = modalRowData[0].quantity ;  //数量
						record.batchProductionDate = modalRowData[0].batchProductionDate ;  //生产日期
						record.batchExpiredDate = modalRowData[0].batchExpiredDate ;  //有效期至
						record.batchProductionDateShow = modalRowData[0].batchProductionDateShow ;  //显示生产日期
						record.batchExpiredDateShow = modalRowData[0].batchExpiredDateShow ;  //显示有效期至
						// warehouseAreaId =modalRowData[0].warehouseAreaId;	//源库区id
						// locatorId = modalRowData[0].locatorId;		//货位ID
						srcWarehouseCategory =  modalRowData[0].warehouseAreaCategory; //源库别编码
						
						//最新保存接口需字段 
						record.batchId = modalRowData[0].batchId  //批次ID
						record.outContainerCode = modalRowData[0].containerCode  //移出容器ID
						record.outGoodsCaseCode = modalRowData[0].locatorCode  //移出货格
						record.outLocatorId = modalRowData[0].locatorId  //移出货格
						record.warehouseWorkerId = store.getters.userInfo.userId  //当前操作员
						
						
						// 清除后面重选
						// record.destWarehouseAreaName="";	// 4、目的库区
						// record.warehouseLocatorName="";	// 5、目的货位
						// 			toWhLocatorId = ""; //5、目的货位
					  },
			},
		  },
		  
		{ type:'txt',dataIndex:'outGoodsCaseCode',title:'移出货位',width:80 },
		{ type:'txt',dataIndex:'warehouseAreaCategoryName',title:'移出库别',width:80},
		  
		  
	  // 补货规则/查询规则 库存/获取目的库区名称
	  {
	  	title: "移入库别",
	  	type: "searchModal",
	  	required: true,
		width:120,
	  	dataIndex: "destWarehouseCategoryName",
	  	targetClick: true,
	  	targetValueKey: "destWarehouseCategoryName",
	  	remoteUrl: { url: upcloud_base_strategy+'/rule/move/v1/list', method: 'get' },
	  	toolBar: {
	  	  items: [
	  		{type: "input",name: "srcWarehouseCategoryName",label: "源库别",value: "",},
	  		{type: "input",name: "destWarehouseCategoryName",label: "目标库别",value: "",},
	  	  ],
	  	},
	  	tableConfig: {
			rowKey:"moveRuleId",
	  	  // remoteParams:remoteParams,
	  	  remoteBefore:(vn,p)=>{
	  				p.params.warehouseId = warehouseId;
	  				p.params.srcWarehouseCategory = srcWarehouseCategory;	//源库别编码
	  				return p;
	  			},
	  	  columns: [
	  		{dataIndex: "srcWarehouseCategoryName",title: "源库别"},
	  		{dataIndex: "destWarehouseCategoryName",title: "目标库别"},
	  		{dataIndex: "quantityMode",title: "包装属性",customRender:(text,rows,index)=>{
				switch(text){
					case 1:
						return '整件';
						break;
					case 2:
						return '零散';
						break;
					case 3:
						return '整散';
						break;
				}
			}},
	  		{dataIndex: "warehouseName",title: "仓库名称"},
				],
				
	  	},
			on: {
				ok(event, value, rowData, name, index, tableRowData) {
          quantityMode[index] = rowData[0].quantityMode
        },
	  		change:({ event, name, value, modalRowData, record, index})=>{
	  				console.log("移入库别",event, name, value, modalRowData, record, index);
	  			    // record.quantityMode=modalRowData[0].quantityMode;  // 包装属性
	  			    warehouseAreaCategory =modalRowData[0].destWarehouseCategory; //目的库别编码
					
	  			  },
	  	},
	    },
	// 根据上面的目标库区id查目的货位id
	
	
    {
		...genWarehouseLocatorSearchModal(2, true, false, function (res, p) {
		  p.params.warehouseId = warehouseId;
		  p.params.cargoOwnerId = paramData.cargoOwnerId;
		  p.params.itemId = paramData.itemId;
		  p.params.warehouseAreaCategory = paramData.warehouseAreaCategory; //目的库别编码
		  // p.params.itemCategoryCode = itemCategoryCode;
		  return p;
		}, function (modalRowData, index) {
		  console.log(modalRowData, index, 'indexindexindex');
		  if (!modalRowData) {
			warehouseId = null;
			cargoOwnerId = null;
			itemId = null;
			// itemCategoryCode = null;
		  } else {
			console.log(modalRowData, 'modalRowDatamodalRowData');
			paramData.inGoodsCaseCode =  modalRowData[0].warehouseLocatorCode;  // 移入货位
			paramData.inLocatorId =  modalRowData[0].warehouseLocatorId;  // 移入库位ID
			console.log(paramData,'paramData');
		  }
		},null, {
		  search(p1, p2, p3, index, record) {
			console.log(p1, p2, p3, index, record,'p1, p2, p3, index, record');
			paramData = record
		  },
		}
		),
		title: '移入货位编码',
		width: 140,
	  },
	  
		// {type:'txt',dataIndex:'warehouseLocatorCode',title:'warehouseLocatorCode',width:120},
		// { 	title:'移入货位编码',
		// 	width:120,
		// 	dataIndex:'warehouseLocatorCode',
		// 	targetClick: true,
		// 	targetValueKey: "warehouseLocatorName",
		// 	...genWarehouseLocatorSearchModal(2, true, false,
		// 		function(vn,p){
		// 			p.params.warehouseAreaCategory = warehouseAreaCategory; //目的库别编码
		// 			p.params.warehouseId = warehouseId; //仓库id
		// 			console.log('remoteBefore参数回调：',vn,p);
		// 		}, 
		// 		function (modalRowData, index, value, record) {
		// 			console.log('移入货位：',modalRowData, index, value, record);
		// 			// record.inContainerCode=modalRowData[0].;  // 移入容器ID,找不到
		// 			record.inGoodsCaseCode=modalRowData[0].warehouseLocatorCode;  // 移入货位
		// 			record.inLocatorId=modalRowData[0].warehouseLocatorId;  // 移入库位ID
		// 			 // toWhLocatorId =modalRowData[0].warehouseLocatorId; //目标货位id
		// 			 // record.toWhLocatorId =modalRowData[0].warehouseLocatorId; //目标货位id
		// 		},
		// 		null,
		// 	),
		// },
		/* {type:'select',dataIndex:'outStockStatus',title:'下架库存状态',options:[
			{label:'合格',value:'1'},
			{label:'不合格',value:'2'},
			{label:'待检',value:'3'},
			{label:'退货',value:'4'},
			] },
		{type:'select',dataIndex:'inStockStatus',title:'上架库存状态',options:[
			{label:'合格',value:'1'},
			{label:'不合格',value:'2'},
			{label:'待检',value:'3'},
			{label:'退货',value:'4'},
			] }, */
		{type:'select',dataIndex:'reason',title:'调整原因',options:[
				{label:'合格',value:'合格'},
				{label:'不合格',value:'不合格'},
				{label:'待检',value:'待检'},
				{label:'退货',value:'退货'}, 	
			], width: 100,
		},
				    {
      dataIndex: 'caseQuantity',
      title: '件数',
      width: 100,
      key: 'caseQuantity',
      type: 'number',
      on: {
        keydown(event) {
          console.log(event, 'eventeventevent')
          let index = event.target.parentNode.parentNode.dataset.rowIndex
          if (quantityMode[index] == '2') {
            event.stopPropagation()
            event.preventDefault()
            event.target.value = 0
            _this.$message.warn('当前行移入库别禁止输入整件数！')
            return false
          } else {
            return true
          }
        },
        change({ event, value, name, index, record }) {
          console.log(arguments, 'argumentsargumentsarguments')
          record.numData = record.caseConversionRate * value + Number(record.oddPieceQuantity)
          if (record.availableQuantity < record.numData) {
            _this.$message.warn('可用库存数量不足')
            record.caseQuantity = 0
            return
          }
        }
      }
    },
    {
      dataIndex: 'oddPieceQuantity',
      width: 100,
      title: '零散数',
      key: 'oddPieceQuantity',
      type: 'number',
      on: {
        keydown(event) {
          console.log(event, 'eventeventevent')
          let index = event.target.parentNode.parentNode.dataset.rowIndex
          if (quantityMode[index] == '1') {
            event.stopPropagation()
            event.preventDefault()
            event.target.value = '0'
            _this.$message.warn('当前行移入库别禁止输入零散数！')
            return false
          }
        },
        change({ event, value, name, index, record }) {
          console.log(arguments, 'argumentsargumentsarguments')
          record.numData = record.caseConversionRate * Number(record.caseQuantity) + Number(value)
          if (record.availableQuantity < record.numData) {
            _this.$message.warn('可用库存数量不足')
            record.oddPieceQuantity = 0
            return
          }
        }
      }
    },
		{type:'txt',dataIndex:'batchProductionDate',title:'生产日期',width:120},
		{type:'txt',dataIndex:'batchExpiredDate',title:'有效期至',width:120},
		{type:'txt',dataIndex:'batchProductionDateShow',title:'显示生产日期',width:120},
		{type:'txt',dataIndex:'batchExpiredDateShow',title:'显示有效期至',width:120},
		{type:'txt',dataIndex:'grade',title:'等级',width:60},
		{type:'txt',dataIndex:'manufacturerId',title:'生产厂家',width:200},
		{type:'txt',dataIndex:'productionPlace',title:'产地',width:200},
		{type:'txt',dataIndex:'opertion',title:'操作',scopedSlots:{customRender:'opertion'},fixed:'right',width:80,align:'center'}
		/* {type:'edit',dataIndex:'pieceQuantity',title:'补货数量',required:true,
			on:{
				change(e){
					if(!e.record.caseConvertionRate){
						_this.$message.warning('箱数量不能为空！',5);
						e.record.pieceQuantity = '';
						return ;
					}
				},
				blur(e){
					if(e.target.value % _this.data[e.target.dataset.rowIndex].caseConvertionRate){
						_this.data[e.target.dataset.rowIndex].pieceQuantity = '';
						_this.$message.warning('补货数量需为箱数量倍数!',5);
					}
					
				}  
			},
		}, */
  ]}
  
 const columns2 = function(_this){
  	return	[
		{ hidden: true, dataIndex: "key",customRender:(text,rows,index)=>index.toString() },
		{type:'txt',dataIndex:'batchId',title:'批次ID',hidden:true},
		{type:'txt',dataIndex:'cargoOwnerId',title:'货主id',hidden:true},
		{type:'txt',dataIndex:'billId',title:'ERP移库单ID',hidden:true},
		{type:'txt',dataIndex:'billNo',title:'ERP移库单编号',hidden:true},
		{type:'txt',dataIndex:'lineId',title:'ERP明细ID',hidden:true},
		{type:'txt',dataIndex:'inContainerCode',title:'移入容器ID',hidden:true},
		{type:'txt',dataIndex:'inGoodsCaseCode',title:'移入库位ID',hidden:true},
		{type:'txt',dataIndex:'inLocatorId',title:'移入货格',hidden:true},
		{type:'txt',dataIndex:'outContainerCode',title:'移出容器ID',hidden:true},
		{type:'txt',dataIndex:'outGoodsCaseCode',title:'移出货格',hidden:true},
		{type:'txt',dataIndex:'outLocatorId',title:'移出库位ID',hidden:true},
		{type:'txt',dataIndex:'warehouseId',title:'仓库ID',hidden:true},
		{type:'txt',dataIndex:'cargoOwnerCode',title:'货主编号',hidden:true},
		{type:'txt',dataIndex:'cargoOwnerId',title:'货主id',hidden:true},
		{type:'txt',dataIndex:'cargoOwnerName',title:'货主名称',hidden:true},
		{type:'txt',dataIndex:'warehouseWorkerId',title:'库工ID',hidden:true},
		{type:'txt',dataIndex:'itemId',title:'商品ID',hidden:true},
		
		{type:'txt',dataIndex:'itemCode',title:'商品编码'},
		{type:'txt',dataIndex:'itemName',title:'商品名称'},
  		{type:'number',dataIndex:'caseQuantity',title:'整件数',width:100},
  		{type:'number',dataIndex:'oddPieceQuantity',title:'零散数',width:100},
		{type:'txt',dataIndex:'erpPlanQuantity',title:'ERP计划数量'},
		{type:'txt',dataIndex:'caseUom',title:'包装单位'},
		{ type:'txt',dataIndex:'caseConvertionRate',title:'包装数' },
		{ type:'txt',dataIndex: "isCaseOrPiece",title: "存储属性",customRender:(text,rows,index)=>{
			switch(text){
				case 0:
					return '整散合一';
					break;
				case 1:
					return '整散分离';
					break;
			}
		}},
		
		{ type:'txt',dataIndex:'batchNum',title:'批号'},
		{ type:'txt',dataIndex:'locatorCode',title:'移出货位编码'},
		{ type:'txt',dataIndex:'warehouseAreaCategory',title:'移出库别编码',hidden:true },
		{ type:'txt',dataIndex:'warehouseAreaCategoryName',title:'移出库别'},
		{ type:'txt',dataIndex:'destWarehouseCategoryName',title:'移入库别'},
		{ type:'txt',dataIndex:'warehouseLocatorName',title:'移入货位编码'},
  		{type:'txt',dataIndex:'inStockStatus',title:'上架库存状态'},
  		{type:'txt',dataIndex:'outStockStatus',title:'下架库存状态'},
  		{type:'txt',dataIndex:'erpReason',title:'ERP移库原因' },
  		{type:'txt',dataIndex:'batchProductionDate',title:'生产日期'},
  		{type:'txt',dataIndex:'batchExpiredDate',title:'有效日期'},
  		{type:'txt',dataIndex:'batchProductionDateShow',title:'显示生产日期'},
  		{type:'txt',dataIndex:'batchExpiredDateShow',title:'显示有效日期'},
  		{type:'txt',dataIndex:'manufacturerId',title:'生产厂家'},
    ]}
  
  
const columns3	= function(_this){
	return [
		{ dataIndex:'cargoOwnerId',title:'货主id',hidden:true },
		{ dataIndex:'cargoOwnerName',title:'货主名称'},
		{ dataIndex:'billId',title:'ERP移库单ID'},
		{ dataIndex:'billNo',title:'ERP移库单编号'},
		{ dataIndex:'iarDate',title:'单据日期'},
	]
}


 export { items,columns,columns2,columns3 }
