import CONFIG from "@/config";
import { iunitprice } from "@/utils/expression";

export default function useColumns() {
  const TableColumns = [
    {
      item: { key: "priceCode", title: "价格编码" },
      attr: { is: "el-input", disabled: true, readonly: true },
    },
    {
      item: { key: "supplierName", title: "供应商", required: true },
      attr: {
        is: "el-popover-select-v2",
        valueKey: "name",
        referName: "SUPPLIER_PARAM",
        dataMapping: {
          supplier: "id",
          supplierCode: "code",
          supplierName: "name",
        },
      },
    },
    {
      item: { key: "puOrgName", title: "采购组织", required: true },
      attr: {
        is: "el-popover-select-v2",
        valueKey: "name",
        referName: "ORG_PARAM",
        dataMapping: { puOrg: "id", puOrgCode: "code", puOrgName: "name" },
      },
    },
    {
      item: { key: "currencyName", title: "币种", required: true },
      attr: {
        is: "el-popover-select-v2",
        valueKey: "name",
        referName: "CURRENCY_PARAM",
        dataMapping: {
          currency: "id",
          currencyCode: "code",
          currencyName: "name",
        },
      },
    },
    {
      item: { key: "explainStr", title: "价格合理性说明", required: true },
      attr: { is: "el-input" },
    },
    {
      item: { key: "buyerName", title: "采购员", required: true },
      attr: {
        is: "el-popover-select-v2",
        valueKey: "name",
        referName: "CONTACTS_PARAM",
        dataMapping: { buyer: "code", buyerName: "name" },
      },
    },
    {
      item: { key: "puDeptName", title: "采购部门", required: 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: "isEffective", title: "是否已推价格" },
      attr: {
        is: "el-select",
        dictName: "is_effective",
        value: "N",
        disabled: true,
      },
    },
    {
      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",
      },
    },
    {
      item: { key: "approveTime", title: "单据申请日期" },
      attr: { is: "el-input", disabled: true, readonly: true },
    },
    {
      item: { key: "file", title: "附件", span: 24 },
      attr: { is: "el-file-upload" },
    },
  ].map(({ item, attr }) => ({
    attr,
    item: { ...item, hidden: true, span: item.span || 6 },
  }));

  const TabColumns = [
    {
      item: {
        title: "物料信息表",
        key: "priceApplyItems",
      },
      attr: {
        value: [],
      },
      TableColumns: [
        {
          item: { key: "materialCode", title: "物料编码", required: true },
          attr: {
            is: "el-input",
            clearable: true,
          },
        },
        {
          item: { key: "materialName", title: "物料名称", required: true },
          attr: {
            is: "el-popover-select-v2",
            checkbox: true,
            clearable: true,
            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: "specification", title: "规格" }, attr: {} },
        { item: { key: "model", title: "型号" }, attr: {} },
        { item: { key: "manufacturerName", 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: "采购单位", hidden: false },
        //   attr: {
        //     is: "el-popover-select-v2",
        //     valueKey: "name",
        //     referName: "UNIT_PARAM",
        //     dataMapping: {
        //       puUnit: "id",
        //       puUnitCode: "code",
        //       puUnitName: "name",
        //     },
        //   },
        // },
        {
          item: { key: "customerName", title: "客户" },
          attr: {
            is: "el-popover-select-v2",
            valueKey: "name",
            referName: "CUSTOMER_PARAM",
            dataMapping: {
              customer: "id",
              customerCode: "code",
              customerName: "name",
            },
          },
        },
        {
          item: { key: "taxPrice", title: "含税单价", required: true },
          attr: {
            is: "el-input-number",
            min: 0.00000001,
          },
        },
        {
          item: { key: "tax", title: "税率%", required: true },
          attr: {
            is: "el-popover-select-v2",
            valueKey: "ntaxrate",
            referName: "TAX_RATE_PARAM",
            dataMapping: { tax: "ntaxrate" },
            disabled: true,
          },
        },
        {
          item: { key: "price", title: "无税单价", required: true },
          attr: {
            formatter: (prop) => {
              const { tax = 0, taxPrice = 0 } = prop;
              return (prop.price = iunitprice(tax, taxPrice));
            },
          },
        },
        // {
        //   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: "价格有效期（起）",
            required: true,
          },
          attr: {
            is: "el-date-picker",
            valueFormat: "yyyy-MM-dd",
            value: new Date(),
          },
        },
        {
          item: { key: "periodEnd", title: "价格有效期（止）", required: 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: "recentlyPrice", title: "最近价格" },
          attr: {
            is: "el-input-number",
            min: 0,
            precision: CONFIG.precision,
            disabled: true,
          },
        },

        {
          item: {
            key: "recentlyPriceDate",
            title: "最近价格日期",
          },
          attr: {
            is: "el-date-picker",
            valueFormat: "yyyy-MM-dd",
          },
        },

        {
          item: { key: "isApprovalFirst", title: "首次报批" },
          attr: {
            is: "el-select",
            dictName: "is_effective",
            disabled: true,
          },
        },
        {
          item: { key: "isPriceAdjustmentName", title: "调价类型" },
          attr: {
            value: '无',
            formatter: (prop) => {
              const { priceDiffer } = prop;
              return (prop.isPriceAdjustment = priceDiffer > 0 ? '涨价' : priceDiffer < 0 ? '降价' : '无');
            },
          },
        },
        {
          item: { width: 100, key: "priceDiffer", title: "单价差" },
          attr: {
            formatter: (prop) => {
              const { taxPrice = 0, recentlyPrice = 0 } = prop;
              return (prop.priceDiffer = Number(recentlyPrice) !== 0 ? Number(taxPrice) - Number(recentlyPrice) : 0);
            },
          },
        },

        {
          item: { width: 100, key: "increase", title: "涨幅（%）" },
          attr: {
            formatter: (prop) => {
              const { priceDiffer = 0, recentlyPrice = 0 } = prop;
              return (prop.increase = recentlyPrice ? ((Number(priceDiffer) / Number(recentlyPrice)) * 100).toFixed(1) : 0);
            },
          },
        },

        {
          item: { width: 100, key: "yPurchaseQuantity", title: "预计年采购量" },
          attr: {
            // is: "el-computed-input-v2",
            formatter: (prop) => {
              return prop.yPurchaseQuantity
            },
          },
        },
        {
          item: { width: 100, key: "yPurchaseVolume", title: "预计年采购额" },
          attr: {
            formatter: (prop) => {
              const { taxPrice = 0, yPurchaseQuantity = 0 } = prop;
              return (prop.yPurchaseVolume = (Number(taxPrice) * Number(yPurchaseQuantity)).toFixed(2));
            },
          },
        },
        {
          item: { width: 100, key: "yAffectedAmount", title: "预计年影响金额" },
          attr: {
            formatter: (prop) => {
              const { priceDiffer = 0, yPurchaseQuantity = 0 } = prop;
              return (prop.yAffectedAmount = (
                Number(priceDiffer) * Number(yPurchaseQuantity)
              ));
            },
          },
        },
        {
          item: { key: "priceType", title: "价格类型", required: true },
          attr: {
            is: "el-select",
            dictName: "sys_price_type",
            value: "order",
          },
        },
        {
          item: { key: "isDistributionPrice", title: "配送价", required: true },
          attr: {
            is: "el-select",
            dictName: "is_effective",
            value: "N",
          },
        },

        {
          item: { width: 100, key: "isChannel", title: "是否渠道变更" },
          attr: {
            is: "el-select",
            dictName: "is_effective",
            value: "N",
          },
        },
        {
          item: { width: 100, key: "brandReplacement", title: "是否品牌替换" },
          attr: {
            is: "el-select",
            dictName: "is_effective",
            value: "N",
          },
        },
        {
          item: { width: 150, key: "supplierName", title: "供应商名称1" },
          attr: { is: "el-input" },
        },
        {
          item: { width: 150, key: "bidPrice", title: "中标价1" },
          attr: {
            is: "el-input-number",
            min: 0,
            precision: CONFIG.precision,
          },
        },
        {
          item: { width: 150, key: "unitPrice", title: "单价1" },
          attr: {
            is: "el-input-number",
            min: 0,
            precision: CONFIG.precision,
          },
        },
        {
          item: { width: 150, key: "profit", title: "利润比1" },
          attr: {
            is: "el-input-number",
            min: 0,
            precision: CONFIG.precision,
          },
        },

        {
          item: { width: 150, key: "supplierName1", title: "供应商名称2" },
          attr: { is: "el-input" },
        },
        {
          item: { width: 150, key: "bidPrice1", title: "中标价2" },
          attr: {
            is: "el-input-number",
            min: 0,
            precision: CONFIG.precision,
          },
        },
        {
          item: { width: 150, key: "unitPrice1", title: "单价2" },
          attr: {
            is: "el-input-number",
            min: 0,
            precision: CONFIG.precision,
          },
        },
        {
          item: { width: 150, key: "profit1", title: "利润比2" },
          attr: {
            is: "el-input-number",
            min: 0,
            precision: CONFIG.precision,
          },
        },

        {
          item: { width: 150, key: "supplierName2", title: "供应商名称3" },
          attr: { is: "el-input" },
        },
        {
          item: { width: 150, key: "bidPrice2", title: "中标价3" },
          attr: {
            is: "el-input-number",
            min: 0,
            precision: CONFIG.precision,
          },
        },
        {
          item: { width: 150, key: "unitPrice2", title: "单价3" },
          attr: {
            is: "el-input-number",
            min: 0,
            precision: CONFIG.precision,
          },
        },
        {
          item: { width: 150, key: "profit2", title: "利润比3" },
          attr: {
            is: "el-input-number",
            min: 0,
            precision: CONFIG.precision,
          },
        },
        // { item: { key: "createByName", title: "创建人名称" }, attr: {} },
        // { item: { key: "updateByName", title: "更新人名称" }, attr: {} },
        {
          item: { key: "isPriceAdjustment", title: "", width: 10 },
          attr: {
            value: 'no',
            formatter: (prop) => {
              const { priceDiffer } = prop;
              return (prop.isPriceAdjustment = priceDiffer > 0 ? 'up' : priceDiffer < 0 ? 'down' : 'no');
            },
          },
        },
        // 采购换算率隐藏
        {
          item: { key: "conversionRate", title: "采购换算率", required: true, width: 5 },
          attr: {
            is: "el-input-number",
            min: 0,
            precision: CONFIG.precision,
            value: 1,
            disabled: true,
          },
        },
      ],
    },
  ];

  return { TableColumns, TabColumns };
}
