import moment from "moment";
import store from "@/store";
import API from "../../api/default";
import {
  genWarehouseAuthSearchModal,
  genCargoownerAuthSearchModal,
  genWarehouseLocatorSearchModal,
  genSelfWarehouseLocator,
} from "@/views/kzyWMS/SearchModal";
const grid = { span: 8, labelCol: { span: 6 } };
import { upcloud_base_inventory, upcloud_base_strategy, upcloud_base_wh_control } from "@/config/serviceSettings";

let rows = null;
let warehouseId = store.state.user.warehouse.warehouseId; //仓库id;
let cargoOwnerId = null; //货主id;
let itemId = null; //商品id;
let warehouseAreaId = null; // 源库区id
let destWarehouseAreaId = null; // 目的库区id
let batchId = null; //商品批次ID
let locatorId = null; // 货位id
let toWhLocatorId = null; // 目标货位id
let itemHealthCategory = null;
let warehouseAreaCategory = null;

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,
        });
      }
    },
    { span: 8, labelCol: { span: 6 } }
  ),
  // {
  //   ...grid,
  //   type: 'select',
  //   name: 'char',
  //   value: '',
  //   label: '是否允许超补',
  //   options: [
  //       { label: "是", value: 1 },
  //       { label: "否", value: 2 },
  //   ],
  // }
  /* { name: 'warehouseId', label: '仓库ID', hidden: true,value: store.state.user.warehouse.warehouseId },
    {
      type: 'search',
      name: 'warehouseName',
      label: '仓库',
	  value:store.state.user.warehouse.warehouseName,
      destroyOnClose:true,
      required: true,
      targetClick: true,
	  rowKey: 'warehouseId',
	  targetValueKey: 'warehouseName',
      ...grid,
      toolBar: {
        items: [
          {
            type: 'input',
            name: 'warehouseName',
            label: '仓库名称',
            ...grid,
            value: ''
          },
          {
            type: 'input',
            name: 'warehouseCode',
            label: '仓库编码',
            ...grid,
            value: ''
          }
        ]
      },
      tableConfig: {
        rowKey: 'warehouseName',
        rowSelectType: 1,
        hasRowSelectCheck: true,
        columns: [
          { dataIndex: 'warehouseName', title: '仓库名称', key: 'warehouseName' },
          { dataIndex: 'warehouseCode', title: '仓库编码', key: 'warehouseCode' },
          { dataIndex: 'warehouseId', title: '仓库ID', key: 'warehouseId' }
        ]
      },
      remoteUrl: API.warehouse,
      on: {
        ok(event, value, rowData, form) {
			warehouseId = rowData[0].warehouseId ;
			console.log("warehouseId:",warehouseId);
          form.setFieldsValue({
            warehouseId: rowData[0].warehouseId,
          })
        },
        change(event, value, rowData, form) {
          if (!value) {
            form.setFieldsValue({
              warehouseId: '',
            })
          }
        }
      }
  }, 
  {
    type: 'input',
    name: 'customBillNum',
    label: '手动单号',
	required:true,
  		...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: false, dataIndex: "ids", customRender: (text, rows, index) => (index + 1).toString(), align: "center" },
    // 货主(与账户权限关联)
    {
      ...genCargoownerAuthSearchModal(
        2,
        true,
        false,
        function (vn, p) {
          console.log("remoteBefore参数回调：", vn, p);
        },
        function (modalRowData, index, value, record) {
          console.log("货主表格change：", modalRowData, index, value, record);
          cargoOwnerId = modalRowData[0].companyId; //货主id
          record.cargoOwnerId = modalRowData[0].companyId; //货主id
          record.cargoOwnerName = modalRowData[0].companyName; //货主id
          record.cargoOwnerCode = modalRowData[0].companyCode; //货主id
          console.log("仓库id:", warehouseId);
          console.log("货主id:", cargoOwnerId);

          // _this.columns[2].disabled = false;
          /* for(let item  in  _this.columns) {
					 if(_this.columns[item].dataIndex=="itemName"){
						 _this.columns[item].disabled = false;
						 return
					 }
				 } */
          // 清除后面重选
          record.itemName = ""; // 2、商品名称
          itemId = ""; // 2、商品名称
          record.locatorName = ""; // 3、货位名称
          locatorId = ""; // 3、货位名称
          record.destWarehouseAreaName = ""; // 4、目的库区
          record.warehouseLocatorName = ""; // 5、目的货位
          toWhLocatorId = ""; //5、目的货位
          record.caseConversionRate = ""; // 6、包装数量
        },
        null
      ),
      dataIndex: "companyName",
      targetValueKey: "companyName",
      // width:200,
    },

    //紧急补货  分页查询商品库存列表 get
    {
      title: "商品名称",
      type: "searchModal",
      required: true,
      dataIndex: "itemName",
      // width: 200,
      // disabled:true,
      targetClick: true,
      targetValueKey: "itemName",
      remoteUrl: { url: upcloud_base_inventory + "/batch-number/v1/item-page", method: "get" },
      toolBar: {
        items: [
          { type: "input", name: "itemAssistantCode", label: "助记码", value: "" },
          { type: "input", name: "itemCode", label: "商品编码", value: "" },
        ],
      },
      tableConfig: {
        rowKey: "itemId",
        // remoteParams:remoteParams,
        remoteBefore: (vn, p) => {
          p.params.warehouseId = warehouseId;
          p.params.cargoOwnerId = cargoOwnerId;
          console.log("vn,p:", vn, p);
          return p;
        },
        columns: [
          { dataIndex: "itemId", title: "itemId", hidden: true },
          { dataIndex: "itemName", title: "商品名称", ellipsis: true },
          { dataIndex: "itemCode", title: "商品编码", ellipsis: true },
          { dataIndex: "pieceConversionRate", title: "规格", ellipsis: true },
          { dataIndex: "caseUom", title: "包装单位", ellipsis: true },
          { dataIndex: "caseConversionRate", title: "包装数量", ellipsis: true },
          { dataIndex: "manufacturerName", title: "生产厂家", ellipsis: true },
        ],
      },
      modalOpenChange: (open) => {
        let record = _this.$refs.tableInstancy.getCurrentRow().obj;
        if (record.cargoOwnerName) {
          return true;
        } else {
          _this.$message.warning("请先选择货主！");
          return false;
        }
      },
      on: {
        /* search(v1,v2,v3,v4,v5){
					rows = v5;
				}, */
        change: ({ event, name, value, modalRowData, record, index }) => {
          console.log("当前行数据", event, name, value, modalRowData, record, index);
          console.log(modalRowData);
          itemId = modalRowData[0].itemId;
          record.itemId = modalRowData[0].itemId;
          record.itemName = modalRowData[0].itemName;
          record.itemHealthCategory = modalRowData[0].itemName;
          record.itemCode = modalRowData[0].itemCode;
          record.caseConversionRate = modalRowData[0].caseConversionRate;
          console.log("商品名称record前:", record);

          // 清除后面重选
          // record.locatorName="";	// 3、货位名称
          // 		locatorId = "";	// 3、货位名称
          // record.destWarehouseAreaName="";	// 4、目的库区
          // record.warehouseLocatorName="";	// 5、目的货位
          // 			toWhLocatorId = ""; //5、目的货位
        },
      },
    },

    // 库存/获取商品库存列表
    {
      title: "源货位名称",
      type: "searchModal",
      required: true,
      dataIndex: "locatorName",
      // width: 200,
      targetClick: true,
      targetValueKey: "locatorName",
      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: "" },
        ],
      },
      tableConfig: {
        // remoteParams:remoteParams,
        remoteBefore: (vn, p) => {
          p.params.warehouseId = warehouseId;
          p.params.cargoOwnerId = cargoOwnerId;
          p.params.itemId = itemId;
          p.params.status = "1"; //补货状态，因定1合格
          console.log("vn,p:", vn, p);
          return p;
        },
        columns: [
          { dataIndex: "locatorName", title: "货位名称", ellipsis: true },
          { dataIndex: "quantity", title: "数量" },
          { dataIndex: "availableQuantity", title: "可用数量" },
          { dataIndex: "batchNum", title: "批号", ellipsis: true },
          { dataIndex: "warehouseAreaName", title: "库区名称", ellipsis: true },
          { dataIndex: "warehouseAreaId", title: "库区id", ellipsis: true },
        ],
      },
      modalOpenChange: (open) => {
        let record = _this.$refs.tableInstancy.getCurrentRow().obj;
        if (record.itemName) {
          return true;
        } else {
          _this.$message.warning("请先选择商品！");
          return false;
        }
      },
      on: {
        change: ({ event, name, value, modalRowData, record, index }) => {
          console.log("货位行数据", event, name, value, modalRowData, record, index);
          // record.itemCode=modalRowData[0].itemCode;
          warehouseAreaId = modalRowData[0].warehouseAreaId; //源库区id
          batchId = modalRowData[0].batchId; //商品批次ID
          record.batchId = modalRowData[0].batchId; //商品批次ID
          locatorId = modalRowData[0].locatorId; //货位ID
          record.whLocatorId = modalRowData[0].locatorId; //货位ID

          // 清除后面重选
          record.destWarehouseAreaName = ""; // 4、目的库区
          record.warehouseLocatorName = ""; // 5、目的货位
          toWhLocatorId = ""; //5、目的货位
        },
      },
    },
    // 补货规则/查询规则 库存/获取目的库区名称
    {
      title: "目的库区名称",
      type: "searchModal",
      required: true,
      dataIndex: "destWarehouseAreaName",
      // width: 200,
      targetClick: true,
      targetValueKey: "destWarehouseAreaName",
      remoteUrl: { url: upcloud_base_strategy + "/rule/replenish/v1/list", method: "get" },
      toolBar: {
        items: [
          { type: "input", name: "destWarehouseAreaName", label: "目的库区名称", value: "" },
          { type: "input", name: "srcWarehouseAreaName", label: "源库区名称", value: "" },
        ],
      },
      tableConfig: {
        rowKey: "replenishRuleId",
        // remoteParams:remoteParams,
        remoteBefore: (vn, p) => {
          p.params.warehouseId = warehouseId;
          p.params.srcWarehouseAreaId = warehouseAreaId; //源库区ID
          console.log("目的库区名称vn,p:", vn, p);
          return p;
        },
        columns: [
          { dataIndex: "destWarehouseAreaName", title: "目的库区名称", width: 80 },
          { dataIndex: "srcWarehouseAreaName", title: "源库区名称", width: 80 },
          { dataIndex: "warehouseName", title: "仓库名称", width: 80 },
        ],
      },
      modalOpenChange: (open) => {
        let record = _this.$refs.tableInstancy.getCurrentRow().obj;
        if (record.locatorName) {
          return true;
        } else {
          _this.$message.warning("请先选择源货位！");
          return false;
        }
      },
      on: {
        change: ({ event, name, value, modalRowData, record, index }) => {
          console.log("目的库区名称", event, name, value, modalRowData, record, index);
          // record.itemCode=modalRowData[0].itemCode;
          console.log(modalRowData);
          destWarehouseAreaId = modalRowData[0].destWarehouseAreaId;
          warehouseAreaCategory = modalRowData[0].warehouseAreaCategory;

          // 清除后面重选
          record.warehouseLocatorName = ""; // 5、目的货位
          toWhLocatorId = ""; //5、目的货位
        },
      },
    },
    // 根据上面的目标库区id查目的货位id
    {
      ...genSelfWarehouseLocator(
        2,
        true,
        false,
        function (vn, p) {
          p.params.warehouseAreaId = destWarehouseAreaId; //目标库区id

          p.params.cargoOwnerId = cargoOwnerId;
          p.params.itemId = itemId;
          p.params.warehouseId = warehouseId;
          p.params.itemHealthCategory = itemHealthCategory;
          p.params.warehouseAreaCategory = warehouseAreaCategory;

          console.log("remoteBefore参数回调：", vn, p);
        },
        function (modalRowData, index, value, record) {
          console.log("目标货位：", modalRowData, index, value, record);
          toWhLocatorId = modalRowData[0].warehouseLocatorId; //目标货位id
          record.toWhLocatorId = modalRowData[0].warehouseLocatorId; //目标货位id
          console.log("目标货位id:", toWhLocatorId);
        },
        null
      ),
      // width:200,
      remoteUrl: { url: upcloud_base_wh_control + "/item-locator/v2/locator-limit-inbound", method: "get" },
      title: "目的货位",
      dataIndex: "warehouseLocatorName",
      modalOpenChange: (open) => {
        let record = _this.$refs.tableInstancy.getCurrentRow().obj;
        if (record.destWarehouseAreaName) {
          return true;
        } else {
          _this.$message.warning("请先选择目的库区！");
          return false;
        }
      },
    },
    { type: "txt", dataIndex: "caseConversionRate", title: "包装数量", width: 120 },
    {
      type: "number",
      dataIndex: "pieceNum",
      title: "补货件数",
      key: "pieceNum",
      on: {
        change(e) {
          rows = e.record;
          console.log("补货件数", rows);
          if (!e.record.caseConversionRate) {
            e.record.pieceNum = 0;
            _this.$message.warning("包装数量不能为空！", 5);
          } else {
            e.record.pieceQuantity = e.record.pieceNum * e.record.caseConversionRate;
          }
        },
        blur(e) {
          console.log("blur:", e);
          // if(!e.record.caseConversionRate){
          // 	e.record.pieceNum = 0;
          // }
          /* if(rows.pieceQuantity % rows.caseConversionRate){
							rows.pieceQuantity = '';
							_this.$message.warning('补货数量需为包装数量倍数!',5);
						} */
        },
      },
    },
    {
      type: "txt",
      dataIndex: "pieceQuantity",
      title: "补货数量",
      /* on:{
			change(e){
				rows = e.record;
				console.log("rows",rows);
				if(!e.record.caseConversionRate){
					_this.$message.warning('包装数量不能为空！',5);
					e.record.pieceQuantity = '';
					return ;
				}
			},
			blur(e){
				if(rows.pieceQuantity % rows.caseConversionRate){
					rows.pieceQuantity = '';
					_this.$message.warning('补货数量需为包装数量倍数!',5);
				}
				
			}  
	  }, */
    },
  ];
};

export { items, columns };
