import {
  auditColumns,
  createCrudOption,
  packageOptions,
  priceUnitOptions,
  textRule,
} from './common';

const listDicFormatter = res => {
  const data = res?.data || res;
  if (Array.isArray(data)) return data;
  if (Array.isArray(data?.records)) return data.records;
  if (Array.isArray(data?.data)) return data.data;
  if (Array.isArray(data?.data?.records)) return data.data.records;
  return [];
};

export const config = {
  title: '常用货物',
  permission: 'common_cargo',
  exportUrl: '/blade-transport/common-cargo/export-common-cargo',
  exportName: '常用货物',
  importUrl: '/blade-transport/common-cargo/import-common-cargo',
  templateUrl: '/blade-transport/common-cargo/export-template',
  enableAllDept: false,
};

export const option = createCrudOption([
  {
    label: '货物名称',
    prop: 'cargoName',
    minWidth: 150,
    placeholder: '请输入',
    rules: textRule('货物名称', 100, true),
  },
  {
    label: '货物编号',
    prop: 'cargoCode',
    formslot: true,
    minWidth: 120,
    maxlength: 20,
    showWordLimit: true,
    rules: [
      { required: true, message: '请输入货物编号', trigger: 'blur' },
      { max: 20, message: '货物编号不能超过20个字符', trigger: 'blur' },
    ],
  },
  {
    label: '一级货物类型',
    prop: 'firstCargoTypeName',
    formslot: true,
    minWidth: 150,
    editDisabled: true,
    rules: [{ required: true, message: '请选择一级货物类型', trigger: 'change' }],
  },
  {
    label: '一级货物类型编码',
    prop: 'firstCargoTypeCode',
    hide: true,
    display: false,
    addDisplay: false,
    editDisabled: true,
  },
  {
    label: '二级货物类型',
    prop: 'secondCargoTypeName',
    formslot: true,
    minWidth: 150,
    editDisabled: true,
    rules: [{ required: true, message: '请选择二级货物类型', trigger: 'change' }],
  },
  {
    label: '二级货物类型编码',
    prop: 'secondCargoTypeCode',
    display: false,
    addDisplay: false,
    editDisabled: true,
  },
  // 搜索字段与表格、表单字段独立，避免筛选配置影响数据展示和编辑布局。
  {
    label: '货物名称',
    prop: 'searchCargoName',
    searchProp: 'cargoName',
    search: true,
    searchOrder: 5,
    searchPlaceholder: '请输入',
    hide: true,
    display: false,
    addDisplay: false,
    editDisplay: false,
    viewDisplay: false,
  },
  {
    label: '货物编号',
    prop: 'searchCargoCode',
    searchProp: 'cargoCode',
    search: true,
    searchOrder: 4,
    searchPlaceholder: '请输入',
    hide: true,
    display: false,
    addDisplay: false,
    editDisplay: false,
    viewDisplay: false,
  },
  {
    label: '一级货物类型',
    prop: 'searchFirstCargoTypeName',
    searchProp: 'firstCargoTypeName',
    type: 'select',
    searchType: 'select',
    search: true,
    searchOrder: 3,
    searchPlaceholder: '请选择',
    dicUrl: '/blade-system/cargo-type/list?current=1&size=9999&typeLevel=1',
    dicFormatter: listDicFormatter,
    props: {
      label: 'cargoName',
      value: 'cargoName',
    },
    filterable: true,
    hide: true,
    display: false,
    addDisplay: false,
    editDisplay: false,
    viewDisplay: false,
  },
  {
    label: '二级货物类型',
    prop: 'searchSecondCargoTypeName',
    searchProp: 'secondCargoTypeName',
    type: 'select',
    searchType: 'select',
    search: true,
    searchOrder: 2,
    searchPlaceholder: '请选择',
    dicUrl: '/blade-system/cargo-type/list?current=1&size=9999&typeLevel=2',
    dicFormatter: listDicFormatter,
    props: {
      label: 'cargoName',
      value: 'cargoName',
    },
    filterable: true,
    hide: true,
    display: false,
    addDisplay: false,
    editDisplay: false,
    viewDisplay: false,
  },
  {
    label: '组织',
    prop: 'searchDeptId',
    searchProp: 'deptId',
    type: 'select',
    searchType: 'select',
    search: true,
    searchOrder: 1,
    searchPlaceholder: '请选择',
    dicData: [],
    props: {
      label: 'label',
      value: 'value',
    },
    filterable: true,
    hide: true,
    display: false,
    addDisplay: false,
    editDisplay: false,
    viewDisplay: false,
  },
  {
    label: '包装品牌',
    prop: 'packageBrand',
    slot: true,
    minWidth: 150,
    addDisplay: false,
    editDisplay: false,
    viewDisplay: false,
  },
  {
    label: '品牌',
    prop: 'brand',
    search: false,
    hide: true,
    minWidth: 120,
    placeholder: '请输入',
    rules: textRule('品牌', 50),
  },
  {
    label: '包装',
    prop: 'packageType',
    type: 'select',
    dicData: packageOptions,
    hide: true,
    minWidth: 100,
  },
  {
    label: '规格',
    prop: 'specification',
    minWidth: 120,
    placeholder: '请输入',
    rules: textRule('规格', 50),
  },
  {
    label: '型号',
    prop: 'model',
    minWidth: 120,
    placeholder: '请输入',
    rules: textRule('型号', 50),
  },
  {
    label: '单价',
    prop: 'cargoValue',
    formslot: true,
    minWidth: 120,
    rules: [
      {
        validator: (rule, value, callback) => {
          if (value === undefined || value === null || value === '') {
            callback();
            return;
          }
          if (!/^\d+(\.\d{1,2})?$/.test(String(value))) {
            callback(new Error('货值只能输入数字，最多保留2位小数'));
            return;
          }
          callback();
        },
        trigger: 'blur',
      },
    ],
  },
  {
    label: '计价单位',
    prop: 'priceUnit',
    type: 'select',
    dicData: priceUnitOptions,
    display: false,
    minWidth: 120,
  },
  {
    label: '尺寸',
    prop: 'sizeText',
    minWidth: 120,
    addDisplay: false,
    editDisplay: false,
    rules: textRule('尺寸', 100),
  },
  {
    label: '其他说明1',
    prop: 'descriptionOne',
    minWidth: 160,
    placeholder: '请输入',
    rules: textRule('其他说明1', 100),
  },
  {
    label: '其他说明2',
    prop: 'descriptionTwo',
    minWidth: 160,
    placeholder: '请输入',
    rules: textRule('其他说明2', 100),
  },
  {
    label: '备注',
    prop: 'remark',
    type: 'textarea',
    minRows: 4,
    span: 24,
    maxlength: 200,
    showWordLimit: true,
    placeholder: '请输入',
    rules: textRule('备注', 200),
  },
  {
    label: '组织',
    prop: 'deptName',
    minWidth: 150,
    addDisplay: false,
    editDisplay: false,
  },
  ...auditColumns
    .filter(column => ['updateTime', 'createTime'].includes(column.prop))
    .sort(
      (first, second) =>
        ['updateTime', 'createTime'].indexOf(first.prop) -
        ['updateTime', 'createTime'].indexOf(second.prop)
    ),
]);

export const excelOption = {
  submitBtn: false,
  emptyBtn: false,
  column: [
    {
      label: '导入模板',
      prop: 'excelTemplate',
      formslot: true,
      span: 24,
    },
    {
      label: 'Excel文件',
      prop: 'excelFile',
      type: 'upload',
      drag: true,
      loadText: '模板上传中，请稍等',
      span: 24,
      propsHttp: {
        res: 'data',
      },
      tip: '请上传 .xls,.xlsx 标准格式文件',
    },
  ],
};
