import { genSupplierSearchModal, genItemRelatedSearchModal } from "../../../SearchModal";
import { upcloud_base_common } from "@/config/serviceSettings";
import { getConsignorItems, getBranchItems } from "./formItems";

const grid = {
  labelCol: {span: 6}, wrapperCol: {span: 14}, span: 6,
};
const remote = (type) => {
  return {
    remoteUrl: {
      url: upcloud_base_common + `/dict/listbytype/?dicttypeCodes=${type}`,
      method: "post",
    },
    replaceFields: {
      label: "dictName",
      value: "dictCode",
    },
    autoLoad: true,
  };
};

// 主界面查询表单
export function items(self) {
  return [
    // 仓库
    ...getBranchItems((event, targetName, selectRecords, form) => {
      const selectRecord = selectRecords[0] || {};
      const { companyCode = "", companyName = "", companyId = "" } = selectRecord;
      form.setFieldsValue({ branchcode: companyCode, branchname: companyName, branchid: companyId });
    }),
    // 货主
    ...getConsignorItems((event, targetName, selectRecords, form) => {
      const selectRecord = selectRecords[0] || {};
      const { companyCode = "", companyName = "", companyId = "" } = selectRecord;
      form.setFieldsValue({ consignorcode: companyCode, consignorname: companyName, consignorid: companyId });
    }),
    ...genSupplierSearchModal(
      1,
      false,
      false,
      function (vnode, p) {
        let vals = self.$refs.formTarget.form.getFieldsValue();
        p.data.cargoOwnerId = vals.cargoOwnerId;
        return p;
      },
      null,
      grid
    ),
    {
      type: "select",
      name: "billtype",
      label: "单据类型",
      value: "",
      autoLoad: true,
      remoteUrl: { url: upcloud_base_common + "/dict/listbytype?dicttypeCodes=oms_asn_bill_type", method: "post" },
      doneBefore: (data) => {
        console.log(data);
        data.data = data.data.filter((_) => _.isEnable);
        return data;
      },
      replaceFields: { value: "dictCode", label: "dictName" },
      ...grid,
    },
    {
      type: "select",
      name: "priorityLevel",
      label: "紧急程度",
      value: "",
      autoLoad: true,
      remoteUrl: { url: upcloud_base_common + "/dict/listbytype?dicttypeCodes=ASN_PRIORITY_LEVEL", method: "post" },
      replaceFields: { value: "dictCode", label: "dictName" },
      ...grid,
    },
    // {
    //   type: "select",
    //   name: "status",
    //   label: "订单状态",
    //   value: "",
    //   autoLoad: true,
    //   remoteUrl: { url: upcloud_base_common + "/dict/listbytype?dicttypeCodes=po_billh_status", method: "post" },
    //   replaceFields: { value: "dictCode", label: "dictName" },
    //   ...grid,
    // },
    // 供应商
 
    { type: "input", name: "asnAutoBillNum", label: "单据编号", value: "", ...grid },
    { type: "input", name: "refBillno", label: "源单号", value: "", ...grid },

    // { type: "input", name: "asnAutoBillNum", label: "入库单号", value: "", ...grid },
    // { type: "input", name: "billno", label: "关联单号", value: "", ...grid },
    {
      type: "select",
      name: "arrivalMode",
      label: "到货方式",
      value: "",
      autoLoad: true,
      remoteUrl: { url: upcloud_base_common + "/dict/listbytype?dicttypeCodes=ASN_ARRIVAL_MODE", method: "post" },
      replaceFields: { value: "dictCode", label: "dictName" },
      ...grid,
    },
    { type: "input", name: "buyerName", label: "采购员", value: "", ...grid },
    {
      type: "select",
      name: "shipTerm",
      label: "运输条件",
      value: "",
      autoLoad: true,
      remoteUrl: { url: upcloud_base_common + "/dict/listbytype?dicttypeCodes=ASN_SHIP_TERM", method: "post" },
      replaceFields: { value: "dictCode", label: "dictName" },
      ...grid,
    },
    { type: "input", name: "buyerNam12", label: "订单日期", value: "", ...grid },

    { hidden: true, type: "input", name: "remark", label: "备注", value: "", ...grid },
    { hidden: true, type: "input", name: "sAttainDate", label: "预定到货日期 开始", value: "", ...grid },
    { hidden: true, type: "input", name: "eAttainDate", label: "预定到货日期 结束", value: "", ...grid },
    {
      type: "date",
      name: "attainDate",
      label: "预计到货时间",
      value: null,
      showTime: true,
      className: "date-range",
      //   placeholder: ["起始预计到货时间", "结束预计到货时间"],
      ...grid,
      format: "YYYY-MM-DD HH:mm:ss",
      on: {
        change(date, dateString) {
          self.$refs.formTarget.form.setFieldsValue({
            sAttainDate: dateString[0],
            eAttainDate: dateString[1],
          });
        },
      },
    },
    { hidden: true, type: "input", name: "sdate", label: "开始日期", value: "", ...grid },
    { hidden: true, type: "input", name: "edate", label: "结束日期", value: "", ...grid },
    {
      type: "date",
      name: "billDate",
      label: "开票时间",
      value: null,
      //   showTime: true,
      className: "date-range",
      //   placeholder: ["起始采购订单日期", "结束采购订单日期"],
      ...grid,
      format: "YYYY-MM-DD HH:mm:ss",
      on: {
        change(date, dateString) {
          console.log(date, dateString, "date, dateString");
          //   self.$refs.formTarget.form.setFieldsValue({
          //     sdate: dateString[0],
          //     edate: dateString[1],
          //   });
        },
      },
    },

    {
      type: "buttonGroup",
      span:6,
      offset:12,
      style:{textAlign:'center'},
      options: [
        {
          value: "查询",
          icon: "search",
          btnType: "primary",
          // style: "margin-left:10px",
          htmlType: "submit",
        },
        {
          value: "重置",
          icon: "redo",
          style: "margin-left:10px",
          on: {
            click: () => {
              self.$refs.formTarget.form.resetFields();
            },
          },
        },
      ],
    },
  ];
}

// 主界面订单列表-列配置
export const masterColumns = [
  { hidden: true, title: "supplierId", dataIndex: "supplierId", width: 120 },
  { hidden: true, title: "supplierName", dataIndex: "supplierName", width: 120 },
  { hidden: true, title: "supplierCode", dataIndex: "supplierCode", width: 120 },
  { hidden: true, title: "shipperid", dataIndex: "shipperid", width: 120 },
  //   { hidden: true, title: "shippername", dataIndex: "shippername", width: 120 },
  { hidden: true, title: "shippercode", dataIndex: "shippercode", width: 120 },
  { title: "订单编号", dataIndex: "billno", width: 120 },
  { title: "订单日期", dataIndex: "createdate", width: 120 },
  { title: "收货仓库", dataIndex: "branchname", width: 120 },
  { hidden: true,  title: "状态", dataIndex: "status", width: 120 },
  { title: "预计到货时间", dataIndex: "attainDate", width: 120 },
  { title: "货主名称", dataIndex: "consignorname", width: 120 },
  { title: "单位名称", dataIndex: "shippername", width: 120 },
  { title: "到货方式", dataIndex: "arrivalMode", width: 120 },
  { title: "紧急程度", dataIndex: "priorityLevel", width: 120 },
  { title: "总数量", dataIndex: "totQty", width: 120 },
  { title: "运输条件", dataIndex: "shipTerm", width: 120, ...remote("ASN_SHIP_TERM") },
  { title: "开票员", dataIndex: "buyerName", width: 120 },
  { title: "录入员", dataIndex: "createby", width: 120 },
  //   {title: '单位', dataIndex: 'shippername', width: 120},
  //   {title: '订单状态', dataIndex: 'statusName', width: 120},
  //   {title: '备注', dataIndex: 'remark', width: 120},
  //   {
  //     dataIndex: 'operation',
  //     key: 'operation',
  //     title: '操作',
  //     width: 120,
  //     align: 'center',
  //     fixed: 'right',
  //     scopedSlots: {customRender: 'action'}
  //   }
];

// 主界面明细-列配置
export const detailsColumns = [
  { hidden: true, title: "收货仓库", dataIndex: "branchid", width: 120 },
  { title: "商品编码", dataIndex: "itemcode", width: 120 },
  //   { title: "商品大类", dataIndex: "categoryName", width: 120 },
  { title: "商品名称", dataIndex: "itemname", width: 120 },
  { title: "商品规格", dataIndex: "sizeCode", width: 120 },
  { title: "包装数量", dataIndex: "packSize", width: 120 },
  { title: "包装单位", dataIndex: "packUnits", width: 120 },
  { title: "件数", dataIndex: "itemPcs", width: 120 },
  { title: "零散数", dataIndex: "itemOdd", width: 120 },
  { title: "数量", dataIndex: "itemQty", width: 120 },
  { title: "批号", dataIndex: "lotNo", width: 120 },
  { title: "生产日期", dataIndex: "pdateFrom", width: 120 },
  { title: "有效期至", dataIndex: "validUntil", width: 120 },
  { hidden: true, title: "加工类型", dataIndex: "machType", width: 120 },

  { title: "备注行", dataIndex: "lineRemark", width: 120 },
  { title: "最后修改日期", dataIndex: "lastmodifydate", width: 120 },
  //   { title: "上市许可证", dataIndex: "itemHealthProduceLicense", width: 120 },
  //   { title: "批准文号", dataIndex: "approvalNumber", width: 120 },
  //   { title: "注册证号", dataIndex: "itemHealthRegisterNbr", width: 120 },
];

// 新增界面-表单配置
export function detailItems(self) {
  return [
    // 仓库
    ...getBranchItems((event, targetName, selectRecords, form) => {
      const selectRecord = selectRecords[0] || {};
      const { companyCode = "", companyName = "", companyId = "" } = selectRecord;
      form.setFieldsValue({ branchcode: companyCode, branchname: companyName, branchid: companyId });
    }),
    // 货主
    ...getConsignorItems((event, targetName, selectRecords, form) => {
      const selectRecord = selectRecords[0] || {};
      const { companyCode = "", companyName = "", companyId = "" } = selectRecord;
      form.setFieldsValue({ consignorcode: companyCode, consignorname: companyName, consignorid: companyId });
    }),
    {
      type: "select",
      name: "billtype",
      label: "订单类型",
      value: "",
      autoLoad: true,
      required: true,
      remoteUrl: { url: upcloud_base_common + "/dict/listbytype?dicttypeCodes=oms_asn_bill_type", method: "post" },
      replaceFields: { value: "dictCode", label: "dictName" },
      //   doneBefore: (data) => {
      //     data.data = data.data.filter((_) => ["16", "17", "18", "19", "20"].includes(_.dictCode));
      //     return data;
      //   },
      ...grid,
    },
    // 单位
    ...genSupplierSearchModal(
      1,
      true,
      false,
      function (vnode, p) {
        let vals = self.$refs.formTarget.form.getFieldsValue();
        p.data.cargoOwnerId = vals.cargoOwnerId;
        return p;
      },
      null,
      grid
    ),
    {
      type: "date",
      name: "billDate",
      required: true,
      label: "开票时间",
      value: null,
      showTime: true,
      ...grid,
      placeholder: "开票时间",
      format: "YYYY-MM-DD",
      on: {
        change(date, dateString) {
          console.log(dateString);
        },
      },
    },
    { type: "input", name: "buyerName", label: "开票员", value: "", ...grid, required: true },
    {
      type: "date",
      name: "attainDate",
      label: "预计到货时间",
      value: null,
      showTime: true,
      ...grid,
      placeholder: "预计到货时间",
      format: "YYYY-MM-DD",
      on: {
        change(date, dateString) {
          self.$refs.formTarget.form.setFieldsValue({
            sAttainDate: dateString[0],
            eAttainDate: dateString[1],
          });
        },
      },
    },
    {
      type: "select",
      name: "status",
      label: "订单状态",
      value: "",
      autoLoad: true,
      disabled: true,
      remoteUrl: { url: upcloud_base_common + "/dict/listbytype?dicttypeCodes=po_billh_status", method: "post" },
      replaceFields: { value: "dictCode", label: "dictName" },
      ...grid,
    },
    { type: "input", name: "asnAutoBillNum", label: "入库单号", value: "", ...grid, disabled: true },
    { type: "input", name: "billno", label: "关联单号", value: "", ...grid },
    { type: "input", name: "remark", label: "备注", value: "", ...grid },
  ];
}

// 新增界面-编辑表格-列配置
export const columns = function (self) {
  return [
    { hidden: true, title: "lineid", dataIndex: "lineid", width: 120 },
    { hidden: true, title: "billid", dataIndex: "billid", width: 120 },
    { hidden: true, title: "billno", dataIndex: "billno", width: 120 },
    { hidden: true, title: "itemid", dataIndex: "itemid", width: 120 },
    { hidden: true, title: "itemcode", dataIndex: "itemcode", width: 120 },
    { hidden: true, title: "itemname", dataIndex: "itemname", width: 120 },
    { hidden: true, type: "txt", title: "货主id", dataIndex: "consignorid", width: 150 },
    { hidden: true, type: "txt", title: "货主code", dataIndex: "consignorcode", width: 150 },
    { type: "txt", title: "商品编码", dataIndex: "itemCode", width: 120 },
    { type: "txt", title: "商品大类", dataIndex: "itemHealthCategoryCode", width: 120 },
    {
      ...genItemRelatedSearchModal(
        2,
        true,
        false,
        function (vnode, p) {
          let vals = self.$refs.formTarget.form.getFieldsValue();
          p.data.cargoOwnerId = vals.cargoOwnerId;
          p.data.warehouseId = vals.warehouseId;
          return p;
        },
        function (rowData, index, value) {
          let itemData = self.tableData[index];
          itemData.itemCode = rowData[0].itemCode;
          itemData.sizeCode = rowData[0].specification;
          itemData.manufacturerName = rowData[0].manufacturerName;
          itemData.packSize = rowData[0].caseConversionRate;
          itemData.packUnits = rowData[0].caseUom;
          itemData.itemid = rowData[0].itemId;
          itemData.itemname = rowData[0].itemName;
          itemData.itemcode = rowData[0].itemCode;
          itemData.itemHealthCategoryCode = rowData[0].itemHealthCategoryCode;
          itemData.itemQty = +itemData.itemPcs * +itemData.packSize + +itemData.itemOdd;
          console.log(rowData, rowData[0], itemData, "value");
        },
        null,
        {
          search(p1, p2, p3, index, record) {
            console.log(record, "record");
          },
        }
      ),
      width: 150,
    },
    { type: "txt", title: "商品规格", dataIndex: "sizeCode", width: 120 },
    { type: "txt", title: "包装单位", dataIndex: "packUnits", width: 120 },
    { type: "txt", title: "包装数量", dataIndex: "packSize", width: 120 },
    { type: "txt", title: "数量", dataIndex: "itemQty", width: 120 },
    {
      type: "number",
      title: "件数",
      dataIndex: "itemPcs",
      width: 120,
      min: 0,
      on: {
        change({ record, value }) {
          //数量 = 件数*包装数 + 零散数
          record.itemQty = value * record.packSize + record.itemOdd;
        },
      },
    },
    {
      type: "number",
      title: "零散数",
      dataIndex: "itemOdd",
      width: 120,
      min: 0,
      on: {
        change({ record, value }) {
          record.itemQty = record.itemPcs * record.packSize + value;
        },
      },
    },
    { type: "edit", title: "批号", dataIndex: "lotNo", width: 120 },
    { type: "date", title: "生产日期", dataIndex: "pdateFrom", width: 120 },
    { type: "date", title: "有效期至", dataIndex: "validUntil", width: 120 },
    { type: "txt", title: "生产厂家", dataIndex: "manufacturerName", width: 120 },
    { type: "txt", title: "产地", dataIndex: "productionPlace", width: 120 },
    { type: "txt", title: "上市许可证", dataIndex: "itemHealthProduceLicense", width: 120 },
    { type: "txt", title: "批准文号", dataIndex: "approvalNumber", width: 120 },
    { type: "txt", title: "注册证号", dataIndex: "itemHealthRegisterNbr", width: 120 },
    {
      dataIndex: "operation",
      key: "operation",
      title: "操作",
      width: 120,
      align: "center",
      fixed: "right",
      scopedSlots: { customRender: "action" },
    },
  ];
};
