import { formatUpdateUserName } from '@/utils/audit';

const isEmpty = value => value === undefined || value === null || value === '';

const validateNonNegative = (rule, value, callback) => {
  if (isEmpty(value)) {
    callback();
  } else if (Number(value) < 0) {
    callback(new Error('不能小于 0'));
  } else {
    callback();
  }
};

const maskEtcCardNo = value => {
  if (!value) return '';
  const cardNo = String(value);
  if (cardNo.length <= 8) return cardNo.replace(/.(?=.{4})/g, '*');
  return `${cardNo.slice(0, 4)}**** ****${cardNo.slice(-8)}`;
};

export const option = {
  height: 'auto',
  calcHeight: 32,
  dialogWidth: 960,
  labelPosition: 'right',
  labelWidth: 'auto',
  searchLabelWidth: 160,
  tip: false,
  searchBtnText: '查询',
  emptyBtnText: '重置',
  saveBtnText: '提交',
  updateBtnText: '提交',
  searchShow: true,
  searchMenuSpan: 24,
  searchIcon: true,
  searchIndex: 4,
  searchMenuPosition: 'right',
  border: true,
  index: true,
  indexLabel: '序号',
  viewBtn: true,
  selection: true,
  dialogClickModal: false,
  menuWidth: 180,
  menuFixed: 'right',
  column: [
    {
      label: '车牌号',
      prop: 'vehicleNo',
      slot: true,
      formslot: true,
      search: true,
      minWidth: 140,
      span: 12,
      placeholder: '输入车牌号模糊查询选择',
      rules: [
        { required: true, message: '请输入车牌号', trigger: 'blur' },
        { max: 30, message: '最多 30 个字符', trigger: 'blur' },
      ],
    },
    {
      label: '入口时间',
      prop: 'entryTime',
      sortable: true,
      type: 'datetime',
      format: 'YYYY-MM-DD HH:mm',
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      minWidth: 170,
      span: 12,
      placeholder: '请选择，日期选择器',
    },
    {
      label: 'ETC卡号',
      prop: 'etcCardNo',
      formatter: row => maskEtcCardNo(row.etcCardNo),
      search: true,
      minWidth: 220,
      span: 12,
      placeholder: '请输入',
      rules: [
        { required: true, message: '请输入ETC卡号', trigger: 'blur' },
        { max: 30, message: '最多 30 个字符', trigger: 'blur' },
      ],
    },
    {
      label: '出口时间',
      prop: 'exitTime',
      sortable: true,
      type: 'datetime',
      format: 'YYYY-MM-DD HH:mm',
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      minWidth: 170,
      span: 12,
      placeholder: '请选择，日期选择器',
      rules: [{ required: true, message: '请选择出口时间', trigger: 'click' }],
    },
    {
      label: '入口站',
      prop: 'entryStation',
      minWidth: 150,
      span: 12,
      placeholder: '请输入',
      rules: [{ max: 50, message: '最多 50 个字符', trigger: 'blur' }],
    },
    {
      label: '交易金额',
      prop: 'transactionAmount',
      type: 'input',
      formslot: true,
      minWidth: 130,
      span: 12,
      placeholder: '请输入',
      rules: [
        { required: true, message: '请输入交易金额', trigger: 'blur' },
        { validator: validateNonNegative, trigger: 'blur' },
      ],
    },
    {
      label: '出口站',
      prop: 'exitStation',
      minWidth: 150,
      span: 12,
      placeholder: '请输入',
      rules: [{ max: 50, message: '最多 50 个字符', trigger: 'blur' }],
    },
    {
      label: '数据来源',
      prop: 'dataSource',
      value: '手工录入',
      minWidth: 120,
      addDisplay: false,
      editDisplay: false,
      viewDisplay: false,
    },
    {
      label: '备注',
      prop: 'remark',
      type: 'textarea',
      minRows: 4,
      span: 24,
      minWidth: 180,
      overHidden: true,
      placeholder: '请输入，200字以内',
      rules: [{ max: 200, message: '最多 200 个字符', trigger: 'blur' }],
    },
    {
      label: '附件',
      prop: 'attachments',
      span: 24,
      minWidth: 100,
      slot: true,
      formslot: true,
    },
    {
      label: '组织',
      prop: 'createDept',
      type: 'tree',
      dicData: [],
      props: {
        label: 'title',
        value: 'id',
      },
      checkStrictly: true,
      search: true,
      hide: true,
      addDisplay: false,
      editDisplay: false,
      viewDisplay: false,
    },
    {
      label: '创建时间',
      prop: 'createTime',
      sortable: true,
      type: 'datetime',
      format: 'YYYY-MM-DD HH:mm:ss',
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      addDisplay: false,
      editDisplay: false,
      viewDisplay: false,
      minWidth: 170,
    },
    {
      label: '更新人',
      prop: 'updateUserName',
      formatter: formatUpdateUserName,
      addDisplay: false,
      editDisplay: false,
      display: false,
      minWidth: 180,
    },
    {
      label: '更新时间',
      prop: 'updateTime',
      sortable: true,
      type: 'datetime',
      format: 'YYYY-MM-DD HH:mm:ss',
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      addDisplay: false,
      editDisplay: false,
      display: false,
      minWidth: 170,
    },
  ],
};

export const excelOption = {
  labelPosition: 'right',
  submitBtn: false,
  emptyBtn: false,
  column: [
    {
      label: '模板上传',
      prop: 'excelFile',
      type: 'upload',
      drag: true,
      loadText: '模板上传中，请稍等',
      span: 24,
      propsHttp: {
        res: 'data',
      },
      tip: '请上传 .xls,.xlsx 标准格式文件',
      action: '/blade-transport/etc-record/import-etc-record',
    },
    {
      label: '模板下载',
      prop: 'excelTemplate',
      formslot: true,
      span: 24,
    },
  ],
};
