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

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

export const vehicleTypeDic = [
  { label: '车辆', value: '车辆' },
  { label: '船舶', value: '船舶' },
];

export const insuranceTypeDic = [
  { label: '交强险', value: '交强险' },
  { label: '商业险', value: '商业险' },
  { label: '承运人责任险', value: '承运人责任险' },
  { label: '货运险', value: '货运险' },
  { label: '船舶险', value: '船舶险' },
];

export const ocrTemplateDic = [
  { label: '紫金-机动车交强险', value: '紫金-机动车交强险' },
  { label: '机动车商业险', value: '机动车商业险' },
  { label: '船舶保险', value: '船舶保险' },
];

export const option = {
  height: 'auto',
  calcHeight: 32,
  dialogWidth: 1100,
  labelPosition: 'right',
  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,
  column: [
    {
      label: '车船类型',
      prop: 'vehicleType',
      type: 'radio',
      minWidth: 110,
      dicData: vehicleTypeDic,
      value: '车辆',
      search: true,
      rules: [{ required: true, message: '请选择车船类型', trigger: 'change' }],
    },
    {
      label: 'OCR识别模板',
      prop: 'ocrTemplate',
      type: 'select',
      dicData: ocrTemplateDic,
      value: '紫金-机动车交强险',
      hide: true,
      span: 12,
    },
    {
      label: '上传文件',
      prop: 'policyFile',
      formslot: true,
      hide: true,
      span: 24,
    },
    {
      label: '车牌号/船号',
      prop: 'vehicleNo',
      slot: true,
      formslot: true,
      search: true,
      minWidth: 140,
      rules: [
        { required: true, message: '请输入车牌号/船号', trigger: 'blur' },
        { max: 50, message: '最多 50 个字符', trigger: 'blur' },
      ],
    },
    {
      label: '保险类型',
      prop: 'insuranceType',
      type: 'select',
      dicData: insuranceTypeDic,
      search: true,
      minWidth: 130,
      rules: [{ required: true, message: '请选择保险类型', trigger: 'change' }],
    },
    {
      label: '保单号',
      prop: 'policyNo',
      minWidth: 170,
      rules: [
        { required: true, message: '请输入保单号', trigger: 'blur' },
        { max: 80, message: '最多 80 个字符', trigger: 'blur' },
      ],
    },
    {
      label: '开始日期',
      prop: 'startDate',
      sortable: true,
      type: 'date',
      format: 'YYYY-MM-DD',
      valueFormat: 'YYYY-MM-DD',
      minWidth: 120,
      rules: [{ required: true, message: '请选择开始日期', trigger: 'click' }],
    },
    {
      label: '结束日期',
      prop: 'endDate',
      sortable: true,
      type: 'date',
      format: 'YYYY-MM-DD',
      valueFormat: 'YYYY-MM-DD',
      minWidth: 120,
      rules: [{ required: true, message: '请选择结束日期', trigger: 'click' }],
    },
    {
      label: '保额',
      prop: 'insuredAmount',
      type: 'input',
      formslot: true,
      append: '元',
      minWidth: 120,
      rules: [{ validator: validateNonNegative, trigger: 'blur' }],
    },
    {
      label: '保费',
      prop: 'premium',
      type: 'input',
      formslot: true,
      append: '元',
      minWidth: 120,
      rules: [
        { required: true, message: '请输入保费', trigger: 'blur' },
        { validator: validateNonNegative, trigger: 'blur' },
      ],
    },
    {
      label: '发票号',
      prop: 'invoiceNo',
      minWidth: 150,
      rules: [{ max: 80, message: '最多 80 个字符', trigger: 'blur' }],
    },
    {
      label: '开票日期',
      prop: 'invoiceDate',
      type: 'date',
      format: 'YYYY-MM-DD',
      valueFormat: 'YYYY-MM-DD',
      minWidth: 120,
    },
    {
      label: '备注',
      prop: 'remark',
      type: 'textarea',
      minRows: 3,
      span: 24,
      hide: true,
      rules: [{ max: 200, message: '备注最多 200 个字', trigger: 'blur' }],
    },
    {
      label: '创建时间',
      prop: 'createTime',
      sortable: true,
      type: 'date',
      format: 'YYYY-MM-DD HH:mm:ss',
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      addDisplay: false,
      editDisplay: false,
      viewDisplay: false,
      display: false,
      minWidth: 170,
    },
    {
      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: 'createTimeRange',
      type: 'date',
      format: 'YYYY-MM-DD',
      valueFormat: 'YYYY-MM-DD',
      searchRange: true,
      search: true,
      hide: true,
      addDisplay: false,
      editDisplay: false,
      viewDisplay: false,
    },
    {
      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/insurance-record/import-insurance-record',
    },
    {
      label: '模板下载',
      prop: 'excelTemplate',
      formslot: true,
      span: 24,
    },
  ],
};
