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();
  }
};

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

export const feeTypeDic = [
  { label: '加油', value: '加油' },
  { label: '充电', value: '充电' },
];

export const option = {
  height: 'auto',
  calcHeight: 32,
  dialogWidth: 980,
  labelPosition: 'right',
  tip: false,
  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: 'cardNo',
      minWidth: 150,
      rules: [{ max: 30, message: '最多 30 个字符', trigger: 'blur' }],
    },
    {
      label: '交易时间',
      prop: 'transactionTime',
      sortable: true,
      type: 'datetime',
      format: 'YYYY-MM-DD HH:mm',
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      minWidth: 170,
      rules: [{ required: true, message: '请选择交易时间', trigger: 'click' }],
    },
    {
      label: '车船类型',
      prop: 'vehicleType',
      type: 'radio',
      dicData: vehicleTypeDic,
      value: '车辆',
      search: true,
      minWidth: 110,
      rules: [{ required: true, message: '请选择车船类型', trigger: 'change' }],
    },
    {
      label: '费用类型',
      prop: 'feeType',
      type: 'select',
      dicData: feeTypeDic,
      search: true,
      minWidth: 120,
      rules: [{ required: true, message: '请选择费用类型', trigger: 'change' }],
    },
    {
      label: '油品',
      prop: 'oilProduct',
      minWidth: 160,
      rules: [{ max: 50, message: '最多 50 个字符', trigger: 'blur' }],
    },
    {
      label: '车牌号/船号',
      prop: 'vehicleNo',
      slot: true,
      formslot: true,
      search: true,
      minWidth: 150,
      placeholder: '输入车牌号/船号查询选择',
      rules: [
        { required: true, message: '请输入车牌号/船号', trigger: 'blur' },
        { max: 30, message: '最多 30 个字符', trigger: 'blur' },
      ],
    },
    {
      label: '持卡人',
      prop: 'cardHolder',
      minWidth: 120,
      rules: [{ max: 20, message: '最多 20 个字符', trigger: 'blur' }],
    },
    {
      label: '数据来源',
      prop: 'dataSource',
      value: '手工录入',
      minWidth: 120,
      addDisplay: false,
      editDisplay: false,
    },
    {
      label: '数量',
      prop: 'quantity',
      type: 'input',
      formslot: true,
      minWidth: 110,
      rules: [{ validator: validateNonNegative, trigger: 'blur' }],
    },
    {
      label: '单价',
      prop: 'unitPrice',
      type: 'input',
      formslot: true,
      minWidth: 120,
      rules: [{ validator: validateNonNegative, trigger: 'blur' }],
    },
    {
      label: '交易金额',
      prop: 'transactionAmount',
      type: 'input',
      formslot: true,
      minWidth: 130,
      rules: [
        { required: true, message: '请输入交易金额', trigger: 'blur' },
        { validator: validateNonNegative, trigger: 'blur' },
      ],
    },
    {
      label: '余额',
      prop: 'balance',
      type: 'input',
      formslot: true,
      minWidth: 110,
      rules: [{ validator: validateNonNegative, trigger: 'blur' }],
    },
    {
      label: '站点',
      prop: 'station',
      minWidth: 150,
      rules: [{ max: 50, message: '最多 50 个字符', trigger: 'blur' }],
    },
    {
      label: '备注',
      prop: 'remark',
      type: 'textarea',
      minRows: 4,
      span: 24,
      minWidth: 180,
      overHidden: true,
      rules: [{ max: 200, message: '最多 200 个字符', trigger: 'blur' }],
    },
    {
      label: '附件',
      prop: 'attachments',
      span: 24,
      minWidth: 100,
      slot: true,
      formslot: true,
    },
    {
      label: '交易开始时间',
      prop: 'transactionTimeStart',
      type: 'datetime',
      format: 'YYYY-MM-DD HH:mm:ss',
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      search: true,
      hide: true,
      addDisplay: false,
      editDisplay: false,
      viewDisplay: false,
    },
    {
      label: '交易结束时间',
      prop: 'transactionTimeEnd',
      type: 'datetime',
      format: 'YYYY-MM-DD HH:mm:ss',
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      search: true,
      hide: true,
      addDisplay: false,
      editDisplay: false,
      viewDisplay: false,
    },
    {
      label: '消费金额开始值',
      prop: 'transactionAmountStart',
      type: 'number',
      precision: 2,
      search: true,
      hide: true,
      addDisplay: false,
      editDisplay: false,
      viewDisplay: false,
    },
    {
      label: '消费金额结束值',
      prop: 'transactionAmountEnd',
      type: 'number',
      precision: 2,
      search: true,
      hide: true,
      addDisplay: false,
      editDisplay: false,
      viewDisplay: false,
    },
    {
      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: 'createTimeRange',
      type: 'date',
      format: 'YYYY-MM-DD',
      valueFormat: 'YYYY-MM-DD',
      hide: true,
      addDisplay: false,
      editDisplay: false,
      viewDisplay: false,
    },
    {
      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: '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/oil-electric-record/import-oil-electric-record',
    },
    {
      label: '模板下载',
      prop: 'excelTemplate',
      formslot: true,
      span: 24,
    },
  ],
};
