import {
  genItemSearchModal,
  genItemSearchModalcuring,
  genWarehouseAuthSearchModal,
  genBldgSearchModal,
  genCargoownerAuthSearchModal,
  genWarehouseLocatorSearchModal,
} from "@/views/kzyWMS/SearchModal";
import qs from "qs";
import moment from "moment";
import store from "@/store";
import { sorter } from "../../../utils/sort";
import { upcloud_base_common, upcloud_base_gsp, SERVICE_DATA_API_BASE } from "@/config/serviceSettings";

const remote = (type) => {
  return {
    remoteUrl: {
      url: upcloud_base_common + `/dict/listbytype/?dicttypeCodes=${type}`,
      method: "post",
    },
    replaceFields: {
      label: "dictName",
      value: "dictCode",
    },
    autoLoad: true,
  };
};
console.log(store.state.user.warehouse);
const baseUrl = upcloud_base_gsp;
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;

const tablecolumns = {
  gridform: {
    ref: "form",
    items: [
      { type: "input", name: "id", label: "id", hidden: true, value: "592360607987142656" },

      // 仓库
      ...genWarehouseAuthSearchModal(
        1,
        true,
        false,
        null,
        function (rowData, form) {
          console.log(form);
          form.setFieldsValue({
            cargoOwnerCode: null,
            cargoOwnerId: null,
            cargoOwnerName: null,
          });
          console.log(rowData);
          if (!rowData) {
            warehouseId = null;
          } else {
            warehouseId = rowData[0].companyId;
          }
        },
        {
          span: 6,
          labelCol: { span: 6 },
          wrapperCol: { span: 14 },
        }
      ),
      ...genCargoownerAuthSearchModal(
        1,
        false,
        false,
        null,
        function (rowData, form) {
          console.log(rowData);
          if (!rowData) {
            cargoOwnerId = null;
          } else {
            cargoOwnerId = rowData[0].companyId;
          }
        },
        {
          span: 6,
          labelCol: { span: 6 },
          wrapperCol: { span: 14 },
        }
      ),
      ...genBldgSearchModal(
        1,
        false,
        false,
        function (res, p) {
          console.log(warehouseId);
          p.params.warehouseId = warehouseId;
          return p;
        },
        function (rowData, form) {
          bldgId = rowData[0].bldgId;
        },
        {
          span: 6,
          labelCol: { span: 6 },
          wrapperCol: { span: 14 },
        }
      ),
      {
        type: "input",
        name: "advanceDays",
        label: "提前日期(天)",
        span: 6,
        value: "",
      },
      {
        type: "select",
        name: "itemCategoryCode",
        label: "商品大类",
        span: 6,
        value: "",
        ...remote("item_health_category"),
      },
      {
        type: "input",
        name: "lotNum",
        label: "批号",
        span: 6,
        value: "",
      },
      {
        type: "input",
        name: "containerCode",
        label: "托盘号",
        span: 6,
        value: "",
      },
      // 货位
      ...genWarehouseLocatorSearchModal(
        1,
        false,
        false,
        function (res, p) {
          p.params.warehouseId = warehouseId;
          p.params.cargoOwnerId = cargoOwnerId;
          return p;
        },
        null,
        { span: 6, labelCol: { span: 6 }, wrapperCol: { span: 14 } }
      ),
      // 商品
      ...genItemSearchModalcuring(
        1,
        false,
        false,
        function (res, p) {
          p.data.cargoOwnerId = cargoOwnerId;
          p.data.warehouseId = warehouseId;
          return p;
        },
        null,
        { span: 6, labelCol: { span: 6 }, wrapperCol: { span: 14 } }
      ),
      {
        span: 6,
        type: "buttonGroup",
        style: { textAlign: "left" },
        options: [
          {
            value: "查询",
            icon: "search",
            btnType: "primary",
            style: "margin-right:10px;margin-left:30px;",
            htmlType: "submit",
          },
          {
            value: "重置",
            name: "ccz",
            icon: "redo",
            on: {
              click(event, form) {
                form.resetFields();
              },
            },
          },
        ],
      },
    ],
  },
  //主表
  MainTable: {
    scroll: { x: 2500 },
    edit: false,
    // edit: true,
    title: "不合格品单",
    // /strategy/v1/page
    remoteUrl: {
      url: SERVICE_DATA_API_BASE + "/search/page",
      method: "post",
    },
    doneBefore(data) {
      console.log(data);
      if (data.data == null) {
        data.data = {};
        data.data.list = [];
        return data;
      }
      data.data.list.forEach((item) => {
        item.id = item.batchId + item.locatorId + Math.random();
      });
      return data;
    },
    remoteBefore(vn, params) {
      let p = { ...params };

      p.data = {
        num: "592360607987142656",
        pageNum: params.data.pageNum,
        pageSize: params.data.pageSize,
        params: params.data,
      };
      console.log(p, params, "====");
      return p;
    },
    ref: "MainTable",
    rowkey: "id",
    columns: sorter([
      {
        title: "仓库",
        dataIndex: "warehouseName",
        width: 150,
        ellipsis: true,
        slots: { title: "warehouseName" },
      },
      {
        title: "建筑物",
        dataIndex: "bldgName",
        width: 150,
      },
      {
        title: "货主",
        dataIndex: "cargoOwnerName",
        ellipsis: true,
        width: 150,
      },
      {
        title: "货位",
        ellipsis: true,
        width: 150,
        dataIndex: "locatorName",
      },
      {
        // hidden: true,
        title: "库别名称",
        ellipsis: true,
        align: "center",
        width: 100,
        dataIndex: "warehouseareacategoryname",
        type: "txt",
      },

      {
        hidden: true,
        title: "库别编码",
        ellipsis: true,
        align: "center",
        width: 100,
        dataIndex: "warehouseAreaCategory",
        type: "txt",
      },
      {
        title: "库区",
        ellipsis: true,
        align: "center",
        width: 100,
        dataIndex: "warehouseAreaName",
        type: "txt",
      },
      {
        title: "托盘号",
        ellipsis: true,
        width: 150,
        dataIndex: "containerCode",
      },
      {
        title: "库区",
        dataIndex: "areaName",
        width: 150,
        ellipsis: true,
        hidden: true,
      },
      {
        title: "库存数量",
        dataIndex: "stockQty",
        width: 150,
      },
      {
        title: "商品编号",
        ellipsis: true,
        dataIndex: "itemCode",
        width: 150,
      },
      {
        title: "商品名称",
        dataIndex: "itemName",
        ellipsis: true,
        width: 150,
      },
      {
        title: "商品大类",
        dataIndex: "itemHealthCategoryName",
        width: 150,
        ellipsis: true,
      },
      {
        title: "商品规格",
        dataIndex: "specification",
        width: 150,
      },
      {
        title: "生产厂家",
        dataIndex: "manufacturerName",
        width: 150,
        ellipsis: true,
      },
      {
        title: "产地",
        dataIndex: "countryOriginName",
        width: 150,
        ellipsis: true,
      },
      {
        title: "包装数量",
        dataIndex: "caseConversionRate",
        width: 150,
      },
      {
        title: "包装单位",
        dataIndex: "caseUom",
        width: 150,
      },
      {
        title: "批号",
        dataIndex: "lotNum",
        width: 150,
        ellipsis: true,
      },

      {
        title: "生产日期",
        dataIndex: "pdateFrom",
        width: 150,
        ellipsis: true,
        customRender(text) {
          if (text) {
            return moment(text).format("YYYY-MM-DD");
          }
        },
      },
      {
        title: "有效期至",
        width: 150,
        dataIndex: "validUntil",
        ellipsis: true,
        customRender(text) {
          if (text) {
            return moment(text).format("YYYY-MM-DD");
          }
        },
      },
      {
        title: "存储条件",
        dataIndex: "storeTemperature",
        width: 150,
      },
      {
        title: "质量状况",
        dataIndex: "qualityStatus",
        width: 150,
        hidden: true,
      },
      {
        title: "批准文号",
        ellipsis: true,
        width: 150,
        dataIndex: "itemHealthApprovalNbr",
      },
      {
        title: "上市许可持有人",
        ellipsis: true,
        width: 150,
        dataIndex: "itemHealthListedLicensor",
      },
      {
        title: "注册证号",
        ellipsis: true,
        width: 150,
        dataIndex: "itemHealthRegisterNbr",
      },
      {
        title: "备注",
        ellipsis: true,
        width: 150,
        dataIndex: "remark",
      },
    ]),
  },
  //弹窗
  modalItems: {
    rowkey: "locatorId",
    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;
    },
    title: "提取商品",
    gridformref: "gridformModelFormType",
    ref: "modelFormType",
    items: [
      //   ...genwarehouse({
      //     span: 6,
      //     required: true
      //   }),
      ...genWarehouseAuthSearchModal(
        1,
        true,
        false,
        null,
        function (rowData, form) {
          form.setFieldsValue({
            cargoOwnerCode: null,
            cargoOwnerId: null,
            cargoOwnerName: null,
          });
          console.log(rowData);
          if (!rowData) {
            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) {
            form.cargoownerId = null;
          } else {
            form.cargoownerId = rowData[0].companyId;
          }
        },
        {
          span: 6,
          labelCol: { span: 6 },
          wrapperCol: { span: 14 },
        }
      ),
      {
        type: "select",
        span: 6,
        name: "refBillType",
        label: "不合格品来源",
        value: "",
        options: [
          { label: "所有", value: "" },
          { label: "入库", value: "1" },
          { label: "移库", value: "2" },
        ],
      },
      {
        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:10px;",
            htmlType: "submit",
          },
          {
            value: "重置",
            name: "ccz",
            icon: "redo",
            on: {
              click(event, form) {
                form.resetFields();
              },
            },
          },
        ],
      },
    ],
    columns: [
      {
        title: "日期",
        dataIndex: "refBillDate",
        ellipsis: true,
      },
      {
        title: "原单据编号",
        dataIndex: "refBillId",
        ellipsis: true,
      },
      {
        title: "不合格品来源",
        dataIndex: "refBillType",
        ellipsis: true,
        width: 100,
      },
      {
        title: "不合格原因",
        dataIndex: "unqualifiedReason",
        ellipsis: true,
      },
      {
        title: "不合格数量",
        dataIndex: "unqualifiedQty",
        hidden: true,
      },

      {
        title: "数量",
        dataIndex: "lossQty",
        width: 100,
        scopedSlots: { customRender: "number" },
      },
      {
        title: "商品编码",
        ellipsis: true,
        dataIndex: "itemCode",
      },
      {
        title: "商品名称",
        ellipsis: true,
        dataIndex: "itemName",
      },
      {
        title: "货位",
        ellipsis: true,
        dataIndex: "locatorCode",
      },
      {
        title: "货位ID",
        hidden: true,
        ellipsis: true,
        dataIndex: "locatorId",
      },
      {
        title: "批号",
        key: "lotNum",
        ellipsis: true,
        dataIndex: "lotNum",
      },
      {
        title: "助记码",
        ellipsis: true,
        dataIndex: "itemAssistantCode",
      },
      {
        title: "生产厂家",
        ellipsis: true,
        dataIndex: "manufacturerName",
      },

      {
        title: "是否监管",
        ellipsis: true,
        dataIndex: "itemHealthSuperviseflag",
      },
      {
        title: "商品规格",
        ellipsis: true,
        dataIndex: "specification",
      },
      {
        title: "产地",
        ellipsis: true,
        dataIndex: "countryOriginName",
      },

      {
        title: "上市许可持有人",
        ellipsis: true,
        width: 150,
        dataIndex: "itemHealthListedLicensor",
      },
      {
        title: "包装量",
        ellipsis: true,
        dataIndex: "caseConversionRate",
      },
      {
        title: "包装单位",
        ellipsis: true,
        dataIndex: "caseUom",
      },
    ],
    ModalItemsOpinion: {
      ref: "Opinion",
      items: [
        {
          type: "input",
          span: 12,
          name: "opinions",
          label: "所在部门意见",
          value: "",
        },
      ],
    },
  },
};

export default tablecolumns;
