const typeLevelDic = [
  { label: '一级货物类型', value: 1 },
  { label: '二级货物类型', value: 2 },
];

const validateCargoName = (rule, value, callback) => {
  const v = String(value || '').trim();
  if (!v) {
    callback(new Error('请输入货物类型名称'));
  } else if (v.length > 50) {
    callback(new Error('货物类型名称不能超过50字符'));
  } else {
    callback();
  }
};

const validateRemark = (rule, value, callback) => {
  if (String(value || '').length > 200) {
    callback(new Error('备注不能超过200字'));
  } else {
    callback();
  }
};

export const getCargoTypeOption = ctx => ({
  height: 'auto',
  calcHeight: 22,
  dialogWidth: 760,
  labelPosition: 'right',
  labelWidth: 'auto',
  tip: false,
  searchBtnText: '查询',
  emptyBtnText: '重置',
  saveBtnText: '提交',
  updateBtnText: '提交',
  searchShow: true,
  searchIcon: true,
  searchLabelWidth: 50,
  searchMenuSpan: 10,
  searchIndex: 4,
  searchMenuPosition: 'right',
  border: true,
  index: true,
  indexLabel: '序号',
  indexWidth: 70,
  viewBtn: false,
  delBtn: false,
  editBtn: false,
  selection: true,
  dialogClickModal: false,
  menuWidth: 180,
  column: [
    {
      label: '类型',
      prop: 'typeLevel',
      type: 'radio',
      dataType: 'number',
      dicData: typeLevelDic,
      search: false,
      value: 2,
      span: 24,
      order: 100,
      hide: true,
      minWidth: 120,
      editDisabled: true,
      rules: [{ required: true, message: '请选择货物类型级别', trigger: 'change' }],
      change: ({ value }) => ctx.handleTypeLevelChange(value),
    },
    {
      label: '货物类型',
      prop: 'cargoName',
      minWidth: 150,
      search: true,
      searchPlaceholder: '请输入',
      searchSpan: 6,
      searchOrder: 2,
      order: 70,
      placeholder: '请输入',
      maxlength: 50,
      showWordLimit: true,
      showOverflowTooltip: true,
      rules: [
        { required: true, message: '请输入货物类型名称', trigger: 'blur' },
        { validator: validateCargoName, trigger: 'blur' },
      ],
    },
    {
      label: '货物类型编码',
      prop: 'cargoCode',
      minWidth: 140,
      search: true,
      searchPlaceholder: '请输入',
      searchSpan: 6,
      searchOrder: 1,
      maxlength: 4,
      order: 60,
      placeholder: '请输入',
      editDisabled: true,
      rules: [
        { required: true, message: '请输入货物类型编码', trigger: 'blur' },
        {
          validator: (rule, value, callback) => {
            const form = ctx.form || {};
            const code = String(value || '').trim();
            const pattern = form.typeLevel === 1 ? /^\d{2}$/ : /^\d{4}$/;
            if (!code) {
              callback(new Error('请输入货物类型编码'));
            } else if (!pattern.test(code)) {
              callback(new Error('货物类型编码格式不正确'));
            } else if (
              form.typeLevel === 2 &&
              form.parentCargoCode &&
              !code.startsWith(form.parentCargoCode)
            ) {
              callback(new Error('二级编码前2位必须与上级货物类型编码一致'));
            } else {
              callback();
            }
          },
          trigger: 'blur',
        },
      ],
      change: ({ value }) => ctx.handleCargoCodeChange(value),
    },
    {
      label: '上级货物类型',
      prop: 'parentId',
      formslot: true,
      hide: true,
      display: false,
      span: 12,
      order: 90,
      placeholder: '请输入',
      rules: [{ required: true, message: '请选择上级货物类型', trigger: 'change' }],
    },
    {
      label: '上级货物类型',
      prop: 'parentCargoName',
      slot: true,
      addDisplay: false,
      editDisplay: false,
      display: false,
      showOverflowTooltip: true,
      minWidth: 160,
    },
    {
      label: '上级货物类型编码',
      prop: 'parentCargoCode',
      slot: true,
      readonly: true,
      editDisabled: true,
      addDisabled: true,
      addDisplay: false,
      editDisplay: false,
      display: false,
      span: 12,
      order: 80,
      minWidth: 160,
      placeholder: '请输入',
      rules: [{ required: true, message: '请选择上级货物类型', trigger: 'change' }],
    },
    {
      label: '创建人',
      prop: 'createUserName',
      slot: true,
      addDisplay: false,
      editDisplay: false,
      display: false,
      minWidth: 120,
    },
    {
      label: '备注',
      prop: 'remark',
      type: 'textarea',
      minRows: 4,
      span: 24,
      minWidth: 180,
      placeholder: '请输入',
      maxlength: 200,
      showWordLimit: true,
      overHidden: true,
      showOverflowTooltip: true,
      rules: [{ validator: validateRemark, trigger: 'blur' }],
    },
    {
      label: '更新时间',
      prop: 'updateTime',
      type: 'datetime',
      format: 'YYYY-MM-DD',
      valueFormat: 'YYYY-MM-DD',
      addDisplay: false,
      editDisplay: false,
      display: false,
      sortable: true,
      minWidth: 160,
    },
    {
      label: '创建时间',
      prop: 'createTime',
      type: 'datetime',
      format: 'YYYY-MM-DD',
      valueFormat: 'YYYY-MM-DD',
      addDisplay: false,
      editDisplay: false,
      display: false,
      sortable: true,
      minWidth: 160,
    },
  ],
});
