import {
  genItemSearchModalcuring,
  genWarehouseAuthSearchModal,
  genCargoownerAuthSearchModal,
  genItemSearchModal,
} from "@/views/kzyWMS/SearchModal";
import qs from "qs";
import request from "@/utils/request";
import store from "@/store";
import { changecargoOwnerfun } from "@/views/kzyWMS/utils/setCargoOwner";
function sorter(columns) {
  // 添加排序
  columns.forEach((t) => {
    //去掉序号列
    if (["_row_count", "action", "operation"].includes(t.dataIndex) || !!t.scopedSlots) {
      return;
    }
    t.sorter = function (a, b) {
      let a1 = a[t.dataIndex] || "";
      let b1 = b[t.dataIndex] || "";
      if (typeof a1 === "number") {
        return a1 - b1;
      }
      return a1.localeCompare(b1);
    };
  });
  return columns;
}
const span8 = { span: 8, labelCol: { span: 6 } };
let dcId = null;
let warehouseId = store.state.user.warehouse.warehouseId || null;
let bldgId = null;
let warehouseRoomId = null;
let warehouseAreaId = null;
let warehouseLogicId = null;
let cargoOwnerId = null;
import { upcloud_base_strategy, upcloud_base_gsp, SERVICE_DATA_API_BASE } from "@/config/serviceSettings";

function gettype(parameter) {
  //  批量录入销毁信息 POST
  return request({
    method: "get",
    url: upcloud_base_strategy + "/registry/by-code",
    params: parameter,
  });
}
let a = [
  { label: "待审核", value: "10" },
  { label: "通过", value: "20" },
  { label: "驳回", value: "30" },
];
let b = [
  { label: "待审核", value: "10" },
  { label: "通过", value: "20" },
];

const tablecolumns = function (that) {
  return {
    gridform: {
      ref: "form",
      items: [
        //genWarehouseAuthSearchModal 仓库

        ...genWarehouseAuthSearchModal(1, true, false, null, changecargoOwnerfun, {
          span: 6,
          labelCol: { span: 6 },
          wrapperCol: { span: 14 },
        }),
        ...genCargoownerAuthSearchModal(1, true, false, null, null, {
          span: 6,
          labelCol: { span: 6 },
          wrapperCol: { span: 14 },
        }),

        {
          type: "radio",
          span: 6,
          name: "status",
          label: "状态",
          value: "10", //初始化值
          options: [
            { label: "待审核", value: "10" },
            { label: "通过", value: "20" },
            { label: "驳回", value: "30" },
          ],
        },
        {
          span: 6,
          type: "buttonGroup",
          style: { textAlign: "left" },
          options: [
            {
              value: "查询",
              icon: "search",
              btnType: "primary",
              style: "margin-right:10px;margin-left:10px;",
              htmlType: "submit",
            },
            {
              value: "重置",
              name: "ccz",
              btnType: "primary",
              icon: "redo",
              on: {
                click(event, form) {
                  form.resetFields();
                },
              },
            },
          ],
        },
      ],
    },

    //主表
    MainTable: {
      edit: false,
      // edit: true,
      title: "明细列表",
      // /strategy/v1/page
      remoteUrl: {
        url: upcloud_base_gsp + "/gspUnqualifiedLoss/listPage",
        headers: {
          "Content-Type": "application/JSON",
        },
        method: "get",
      },
      ref: "MainTable",
      rowkey: "id",
      columns: sorter([
        {
          title: "单据编号",
          dataIndex: "code",
          key: "code",
        },

        {
          title: "仓库",
          dataIndex: "warehouseName",
          key: "warehouseName",
          slots: { title: "warehouseName" },
        },
        {
          title: "货主",
          dataIndex: "cargoOwnerName",
          key: "cargoOwnerName",
        },
        {
          title: "状态",
          dataIndex: "status",
          key: "status",
          scopedSlots: { customRender: "status" },
          customRender(text) {
            console.log(text, "=====");
            if (text) {
              switch (text) {
                case "10":
                  text = "待审核";
                  break;
                case "20":
                  text = "已审批";
                  break;
                case "30":
                  text = "已驳回";
                  break;
              }
              return text;
            } else return;
          },
        },
        {
          title: "申请人",
          key: "creatorName",
          dataIndex: "creatorName",
        },
        {
          title: "申请时间",
          key: "gmtCreate",
          dataIndex: "gmtCreate",
        },
        {
          title: "意见",
          key: "opinion",
          dataIndex: "opinion",
        },

        {
          title: "审核时间",
          key: "auditDate",
          dataIndex: "auditDate",
        },
        {
          title: "审核意见",
          key: "auditText",
          dataIndex: "auditText",
        },

        //   {
        //     title: '操作',
        //     key: 'operation',
        //     scopedSlots: { customRender: 'operation' },
        //     align: 'center'
        //   }
      ]),
    },
    //   详情表
    SubTable: {
      title: "明细列表",
      remoteUrl: {
        url: upcloud_base_gsp + "/gspUnqualifiedLoss/findDetailByMstId	",
        headers: {
          "Content-Type": "application/JSON",
        },
        method: "get",
      },
      ref: "rule",
      rowkey: "id",
      columns: sorter([
        {
          title: "商品编码",
          key: "itemCode",
          ellipsis: true,
          dataIndex: "itemCode",
        },
        {
          title: "商品名称",
          key: "itemName",
          ellipsis: true,
          dataIndex: "itemName",
        },
        {
          title: "商品大类",
          key: "itemHealthCategory",
          ellipsis: true,
          dataIndex: "itemHealthCategory",
        },
        {
          title: "商品规格",
          key: "specification",
          ellipsis: true,
          dataIndex: "specification",
        },
        {
          title: "生产厂家",
          key: "manufacturerName",
          ellipsis: true,
          dataIndex: "manufacturerName",
        },
        {
          title: "产地",
          key: "countryOriginName",
          ellipsis: true,
          dataIndex: "countryOriginName",
        },
        {
          title: "包装数量",
          key: "caseConversionRate",
          ellipsis: true,
          dataIndex: "caseConversionRate",
        },
        {
          title: "包装单位",
          key: "caseUom",
          ellipsis: true,
          dataIndex: "caseUom",
        },
        {
          title: "是否追溯",
          key: "itemHealthSuperviseflag",
          ellipsis: true,
          dataIndex: "itemHealthSuperviseflag",
          customRender(text, record, index) {
            text ? (text = "是") : (text = "否");
            return text;
          },
        },
        {
          title: "货位",
          key: "locatorCode",
          ellipsis: true,
          dataIndex: "locatorCode",
        },

        {
          title: "数量",
          key: "lossQty",
          dataIndex: "lossQty",
          width: 100,
          scopedSlots: { customRender: "number" },
        },
        {
          title: "不合格数量",
          dataIndex: "unqualifiedQty",
          key: "unqualifiedQty",
          hidden: true,
        },
        {
          title: "批号",
          key: "lotNum",
          ellipsis: true,
          dataIndex: "lotNum",
        },

        {
          title: "日期",
          dataIndex: "refBillDate",
          hidden: true,
          ellipsis: true,
          key: "refBillDate",
        },
        {
          title: "原单据编号",
          dataIndex: "refBillId",
          hidden: true,
          ellipsis: true,
          key: "refBillId",
        },
        {
          title: "不合格品来源",
          dataIndex: "refBillType",
          hidden: true,
          ellipsis: true,
          key: "refBillType",
          width: 100,
          customRender(text, record, index) {
            console.log(text, "============");
            if (text == 1) {
              text = "入库";
            }
            if (text == 2) {
              text = "移库";
            }
            return text;
          },
        },
        {
          title: "不合格原因",
          dataIndex: "unqualifiedReason",
          hidden: true,
          ellipsis: true,
          key: "unqualifiedReason",
        },

        {
          title: "助记码",
          key: "itemAssistantCode",
          ellipsis: true,
          dataIndex: "itemAssistantCode",
        },

        {
          title: "货位ID",
          key: "locatorId",
          hidden: true,
          ellipsis: true,
          dataIndex: "locatorId",
        },

        {
          title: "上市许可持有人",
          key: "itemHealthListedLicensor",
          ellipsis: true,
          width: 150,
          dataIndex: "itemHealthListedLicensor",
        },

        //   {
        //     title: '操作',
        //     key: 'operation',
        //     align: 'center',
        //     scopedSlots: { customRender: 'operation' }
        //   }
      ]),
    },
    modalItems: {
      rowkey: "id",
      remoteUrl: {
        url: SERVICE_DATA_API_BASE + "/search/page",
        method: "post",
      },
      remoteBefore(vn, params) {
        let p = { ...params };
        p.data = {
          num: "589130005049643008",
          pageNum: params.data.pageNum,
          pageSize: params.data.pageSize,
          params: params.data,
        };
        return p;
      },
      doneBefore(res) {
        // locatorId batchId    itemId
        console.log(res);
        if (res.data == null) {
          res.data = {};
          res.data.list = [];
        }
        let list = res.data.list;
        list.forEach((item) => {
          item.id = item.locatorId + item.batchId + item.itemId + Math.random();
          if (item.lossQty) {
            item.lossQty1 = item.lossQty;
          }
        });
        console.log(res.data.list);
        return res;
      },
      title: "提取商品",
      gridformref: "form",
      ref: "modelFormType",
      items: [
        //genWarehouseAuthSearchModal 仓库

        ...genWarehouseAuthSearchModal(
          1,
          true,
          false,
          null,
          function (rowData, form) {
            form.setFieldsValue({
              cargoOwnerCode: null,
              cargoOwnerId: null,
              cargoOwnerName: null,
            });
            console.log(rowData);
            if (rowData == null) {
              warehouseId = null;
            } else {
              warehouseId = rowData[0].companyId;
            }
          },
          {
            span: 6,
            labelCol: { span: 6 },
            wrapperCol: { span: 14 },
          }
        ),
        ...genCargoownerAuthSearchModal(
          1,
          true,
          false,
          null,
          function (rowData, form) {
            console.log(rowData);
            if (rowData == null) {
              cargoOwnerId = null;
            } else {
              cargoOwnerId = rowData[0].companyId;
            }
          },
          {
            span: 6,
            labelCol: { span: 6 },
            wrapperCol: { span: 14 },
          }
        ),

        {
          type: "select",
          span: 6,
          name: "refBillType",
          label: "不合格品来源",
          hidden: true,
          value: "", //初始化值
          options: [
            { label: "所有", value: "" },
            { label: "入库", value: "1" },
            { label: "移库", value: "2" },
          ],
        },

        ...genItemSearchModalcuring(
          1,
          false,
          false,
          function (res, p) {
            p.data.cargoOwnerId = cargoOwnerId;
            p.data.warehouseId = warehouseId;
            console.log(p, "====");
            return p;
          },
          null,
          { span: 6, labelCol: { span: 6 }, wrapperCol: { span: 14 } }
        ),
        // {
        //   type: 'input',
        //   span: 6,
        //   name: 'itemId',
        //   label: '商品',
        //   placeholder: '请输入产品名称或助记码或产品编',
        //   value: '' //初始化值
        // },
        {
          span: 24,
          type: "buttonGroup",
          style: { textAlign: "right" },
          options: [
            {
              value: "查询",
              icon: "search",
              btnType: "primary",
              style: "margin-right:10px;margin-left:50px;",
              htmlType: "submit",
            },
            {
              value: "重置",
              name: "ccz",

              style: "margin-right:50px;",
              icon: "redo",
              on: {
                click(event, form) {
                  form.resetFields();
                },
              },
            },
          ],
        },
      ],
      columns: sorter([
        {
          title: "商品编码",
          key: "itemCode",
          width: 150,
          ellipsis: true,
          dataIndex: "itemCode",
        },
        {
          title: "商品名称",
          key: "itemName",
          width: 150,
          ellipsis: true,
          dataIndex: "itemName",
        },
        {
          title: "商品大类",
          key: "itemHealthCategoryName",
          ellipsis: true,
          width: 150,
          dataIndex: "itemHealthCategoryName",
        },

        {
          title: "商品规格",
          width: 150,
          key: "specification",
          ellipsis: true,
          dataIndex: "specification",
        },
        {
          title: "生产厂家",
          width: 150,
          key: "manufacturerName",
          ellipsis: true,
          dataIndex: "manufacturerName",
        },

        {
          title: "产地",
          key: "countryOriginName",
          width: 150,
          ellipsis: true,
          dataIndex: "countryOriginName",
        },
        {
          title: "包装数量",
          key: "caseConversionRate",
          width: 150,
          ellipsis: true,
          dataIndex: "caseConversionRate",
        },
        {
          title: "包装单位",
          key: "caseUom",
          width: 150,
          ellipsis: true,
          dataIndex: "caseUom",
        },

        {
          title: "是否追溯",
          key: "itemHealthSuperviseflag",
          width: 150,
          ellipsis: true,
          dataIndex: "itemHealthSuperviseflag",
          customRender(text, record, index) {
            text ? (text = "是") : (text = "否");
            return text;
          },
        },
      
        {
          title: "库别",
          key: "warehouseAreaCategoryName",
          width: 150,
          ellipsis: true,
          dataIndex: "warehouseAreaCategoryName",
        },
        {
          title: "库区",
          key: "warehouseAreaName",
          width: 150,
          ellipsis: true,
          dataIndex: "warehouseAreaName",
        },
        {
          title: "货位",
          key: "locatorCode",
          width: 150,
          ellipsis: true,
          dataIndex: "locatorCode",
        },
        {
          title: "货位ID",
          key: "locatorId",
          hidden: true,
          ellipsis: true,
          dataIndex: "locatorId",
        },

        {
          title: "库存数量",
          width: 150,
          key: "unqualifiedQty",
          ellipsis: true,
          dataIndex: "unqualifiedQty",
        },
        {
          title: "报损数量",
          key: "lossQty",
          width: 150,
          ellipsis: true,
          dataIndex: "lossQty",
          scopedSlots: { customRender: "number" },
        },

        {
          title: "批号",
          key: "manufacturerLotNum",
          width: 150,
          ellipsis: true,
          dataIndex: "manufacturerLotNum",
        },
        {
          title: "生产日期",
          key: "batchProductionDateShow",
          ellipsis: true,
          width: 150,
          dataIndex: "batchProductionDateShow",
        },
        {
          title: "有效期至",
          key: "batchExpiredDateShow",
          ellipsis: true,
          width: 150,
          dataIndex: "batchExpiredDateShow",
        },
        {
          title: "所在部门意见",
          key: "opinion",
          ellipsis: true,
          width: 100,
          dataIndex: "opinion",
          scopedSlots: { customRender: "opinion" },
        },

        {
          title: "批准文号",
          key: "itemHealthApprovalNbr",
          ellipsis: true,
          width: 150,
          dataIndex: "itemHealthApprovalNbr",
        },
        {
          title: "上市许可持有人",
          key: "itemHealthListedLicensor",
          ellipsis: true,
          width: 150,
          dataIndex: "itemHealthListedLicensor",
        },

        {
          title: "注册证号",
          key: "itemHealthRegisterNbr",
          ellipsis: true,
          width: 150,
          dataIndex: "itemHealthRegisterNbr",
        },

        {
          title: "日期",
          dataIndex: "refBillDate",
          ellipsis: true,
          hidden: true,
          key: "refBillDate",
        },
        {
          title: "原单据编号",
          dataIndex: "refBillId",
          hidden: true,
          width: 100,
          ellipsis: true,
          key: "refBillId",
        },
        {
          title: "不合格品来源",
          dataIndex: "refBillType",
          hidden: true,
          ellipsis: true,
          key: "refBillType",
          width: 100,
          customRender(text, record, index) {
            console.log(text, "============");
            // text == 1 ? ((text = '入库') ? text == 2 : (text = '移库')) : ''
            if (text == 1) {
              text = "入库";
            }
            if (text == 2) {
              text = "移库";
            }
            console.log(text);
            return text;
          },
        },
        {
          title: "不合格原因",
          dataIndex: "unqualifiedReason",
          hidden: true,
          width: 100,
          ellipsis: true,
          key: "unqualifiedReason",
        },
        // {
        //   title: '不合格数量',
        //   dataIndex: 'unqualifiedQty',
        //   key: 'unqualifiedQty',
        //   hidden: true
        // },

        // {
        //   title: '数量',
        //   key: 'lossQty',
        //   dataIndex: 'lossQty',
        //   width: 100,
        //   scopedSlots: { customRender: 'number' }
        // },

        {
          title: "助记码",
          key: "itemAssistantCode",
          ellipsis: true,
          dataIndex: "itemAssistantCode",
        },
      ]),
      ModalItemsOpinion: {
        ref: "Opinion",
        items: [
          {
            type: "input",
            span: 12,
            name: "opinion",
            label: "意见",
            value: "", //初始化值
          },
        ],
      },
    },
  };
};

export default tablecolumns;
