import CONFIG from "@/config";

export default function useColumns() {
  const TableColumns = [
    {
      item: { key: "priceCode", title: "价格编码" },
      attr: { is: "el-input", disabled: true, readonly: true },
    },
    {
      item: { key: "supplierName", title: "供应商", require: true },
      attr: {
        is: "el-popover-select-v2",
        valueKey: "name",
        referName: "SUPPLIER_PARAM",
        dataMapping: {
          supplier: "id",
          supplierCode: "code",
          supplierName: "name",
        },
      },
    },
    {
      item: { key: "puOrgName", title: "采购组织", require: true },
      attr: {
        is: "el-popover-select-v2",
        valueKey: "name",
        referName: "ORG_PARAM",
        dataMapping: { puOrg: "id", puOrgCode: "code", puOrgName: "name" },
      },
    },
    {
      item: { key: "currencyName", title: "币种", require: true },
      attr: {
        is: "el-popover-select-v2",
        valueKey: "name",
        referName: "CURRENCY_PARAM",
        dataMapping: {
          currency: "id",
          currencyCode: "code",
          currencyName: "name",
        },
      },
    },
    {
      item: { key: "explainStr", title: "价格合理性说明", require: true },
      attr: { is: "el-input" },
    },
    {
      item: { key: "buyerName", title: "采购员", require: true },
      attr: {
        is: "el-popover-select-v2",
        valueKey: "name",
        referName: "CONTACTS_PARAM",
        dataMapping: { buyer: "code", buyerName: "name" },
      },
    },
    {
      item: { key: "puDeptName", title: "采购部门", require: true },
      attr: {
        is: "el-popover-select-v2",
        valueKey: "name",
        referName: "DEPT_PARAM",
        dataMapping: { puDept: "id", puDeptCode: "code", puDeptName: "name" },
      },
    },
    {
      item: { key: "createByName", title: "创建人" },
      attr: { is: "el-input", disabled: true, readonly: true },
    },
    {
      item: { key: "source", title: "来源单据号" },
      attr: { is: "el-input", disabled: true, readonly: true },
    },
    {
      item: { key: "isEffective", title: "是否已推价格" },
      attr: {
        is: "el-select",
        dictName: "is_effective",
        value: "N",
      },
    },
    {
      item: { key: "effectiveDate", title: "生效日期" },
      attr: { is: "el-input", disabled: true, readonly: true },
    },
    {
      item: { key: "file", title: "附件", require: true, span: 24 },
      attr: { is: "el-file-upload" },
    },
    {
      item: { key: "sourceType", title: "来源单据类型" },
      attr: { is: "el-input", disabled: true, readonly: true },
    },
    {
      item: { key: "status", title: "单据状态" },
      attr: {
        is: "el-select",
        dictName: "sys_status",
        disabled: true,
        readonly: true,
        value: "0",
      },
    },
  ].map(({ item, attr }) => ({
    attr,
    item: { ...item, hidden: true, span: item.span || 6 },
  }));

  const TabColumns = [
    {
      item: {
        title: "物料信息表",
        key: "priceApplyItems",
      },
      attr: {
        value: [],
      },
      TableColumns: [
        {
          item: { key: "materialName", title: "物料名称", require: true },
          attr: {
            is: "el-popover-select-v2",
            valueKey: "name",
            referName: "MATERIAL_PARAM",
            dataMapping: {
              model: "model",
              material: "id",
              materialCode: "code",
              materialName: "name",
              unitName: "unitIdName",
              puUnitName: "unitIdName",
              specification: "specification",
              manufacturer: "manufacturerId",
              manufacturerName: "manufacturerIdName",
              tax: "materialRateName",
            },
          },
        },
        {
          item: { key: "materialCode", title: "物料编码", require: true },
          attr: {},
        },
        { item: { key: "manufacturerName", title: "生产厂家" }, attr: {} },
        { item: { key: "specification", title: "规格" }, attr: {} },
        { item: { key: "model", title: "型号" }, attr: {} },
        {
          item: { key: "unitName", title: "单位" },
          attr: {
            is: "el-popover-select-v2",
            valueKey: "name",
            referName: "UNIT_PARAM",
            dataMapping: { unit: "id", unitCode: "code", unitName: "name" },
          },
        },
        {
          item: { key: "puUnitName", title: "采购单位" },
          attr: {
            is: "el-popover-select-v2",
            valueKey: "name",
            referName: "UNIT_PARAM",
            dataMapping: {
              puUnit: "id",
              puUnitCode: "code",
              puUnitName: "name",
            },
          },
        },
        {
          item: { key: "conversionRate", title: "采购换算率", require: true },
          attr: {
            is: "el-input-number",
            precision: CONFIG.precision,
          },
        },
        {
          item: { key: "tax", title: "税率%", require: true },
          attr: {
            is: "el-popover-select-v2",
            valueKey: "ntaxrate",
            referName: "TAX_RATE_PARAM",
            dataMapping: { tax: "ntaxrate" },
          },
        },
        {
          item: { key: "taxPrice", title: "含税单价", require: true },
          attr: {
            is: "el-input-number",
            precision: CONFIG.precision,
          },
        },
        {
          item: { key: "price", title: "无税单价", require: true },
          attr: {
            formatter: (prop) => {
              const { tax = 0, taxPrice = 0 } = prop;
              prop.price = ((taxPrice / (tax / 100 + 1)) * 1).toFixed(
                CONFIG.precision
              );
              return prop.price;
            },
          },
        },
        {
          item: { key: "currencyName", title: "币种" },
          attr: {
            is: "el-popover-select-v2",
            valueKey: "name",
            referName: "CURRENCY_PARAM",
            dataMapping: {
              currency: "id",
              currencyCode: "code",
              currencyName: "name",
            },
          },
        },
        {
          item: {
            key: "periodBegin",
            title: "价格有效期（起）",
            require: true,
          },
          attr: {
            is: "el-date-picker",
            valueFormat: "yyyy-MM-dd",
            value: new Date(),
          },
        },
        {
          item: { key: "periodEnd", title: "价格有效期（止）", require: true },
          attr: {
            is: "el-date-picker",
            valueFormat: "yyyy-MM-dd",
            pickerOptions: {
              disabledDate(time) {
                return time.getTime() < Date.now() + 3600 * 1000 * 24 * 365;
              },
            },
            value: new Date(new Date().getTime() + 3600 * 1000 * 24 * 366),
          },
        },
        {
          item: { key: "customerName", title: "客户" },
          attr: {
            is: "el-popover-select-v2",
            valueKey: "name",
            referName: "CUSTOMER_PARAM",
            dataMapping: {
              customer: "id",
              customerCode: "code",
              customerName: "name",
            },
          },
        },
        { item: { key: "recentlyPrice", title: "最近价格" }, attr: {} },
        {
          item: { key: "isApprovalFirst", title: "首次报批" },
          attr: {
            is: "el-select",
            dictName: "is_effective",
          },
        },
        {
          item: { key: "isPriceAdjustment", title: "价格调整" },
          attr: {
            is: "el-select",
            dictName: "is_effective",
          },
        },
        {
          item: { key: "priceType", title: "价格类型", require: true },
          attr: {
            is: "el-select",
            dictName: "sys_price_type",
            value: "order",
          },
        },
        {
          item: { key: "isDistributionPrice", title: "配送价", require: true },
          attr: {
            is: "el-select",
            dictName: "is_effective",
            value: "N",
          },
        },
        { item: { key: "createByName", title: "创建人名称" }, attr: {} },
        { item: { key: "updateByName", title: "更新人名称" }, attr: {} },
      ],
    },
  ];

  return { TableColumns, TabColumns };
}
