import {
  approvalStatusOptions,
  auditColumns,
  createCrudOption,
  nonNegativeRule,
  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 [];
};

const userDicFormatter = res =>
  listDicFormatter(res).map(user => ({
    ...user,
    realName: user.realName || user.name || user.account,
  }));

export const config = {
  title: '临时额度管理',
  permission: 'temporary_credit_limit',
  exportUrl: '/blade-transport/temporary-credit-limit/export-temporary-credit-limit',
  exportName: '临时额度申请',
  enableAllDept: false,
  enableProjectSelect: true,
  projectQueryParams: {
    temporaryCreditLimitSelectable: true,
  },
  enableAttachmentTable: true,
  enableDraftSave: true,
  saveAsDraft: true,
  defaultForm: {
    usedFundLimit: 0,
    remainingFundLimit: 0,
  },
  statusProp: 'approvalStatus',
  statusTextProp: 'approvalStatusName',
  deleteStatus: ['draft', 'withdrawn', 'rejected', '草稿'],
  editStatus: ['draft', 'withdrawn', 'rejected', '草稿', '已撤回'],
  ignoreReadonlyEditStatus: ['draft', 'withdrawn', '草稿', '已撤回'],
  ignoreReadonlyDeleteStatus: ['draft', '草稿'],
  operations: [
    {
      action: 'submitApproval',
      label: '提交',
      statusProp: 'approvalStatus',
      status: ['draft', 'withdrawn', 'rejected'],
      permission: 'temporary_credit_limit_submit',
    },
    {
      action: 'flow',
      label: '流程',
      statusProp: 'approvalStatus',
      status: ['reviewing'],
      permission: 'temporary_credit_limit_view',
    },
    {
      action: 'withdraw',
      label: '撤回',
      statusProp: 'approvalStatus',
      status: ['reviewing'],
      permission: 'temporary_credit_limit_withdraw',
    },
    {
      action: 'approve',
      label: '审批通过',
      type: 'success',
      statusProp: 'approvalStatus',
      status: ['reviewing'],
      permission: 'temporary_credit_limit_approve',
    },
    {
      action: 'reject',
      label: '审批驳回',
      type: 'warning',
      statusProp: 'approvalStatus',
      status: ['reviewing'],
      permission: 'temporary_credit_limit_reject',
    },
  ],
};

export const option = {
  ...createCrudOption([
    {
      label: '申请单号',
      prop: 'applicationNo',
      search: true,
      searchOrder: 5,
      minWidth: 160,
      addDisplay: false,
      editDisplay: false,
      editDisabled: true,
    },
    {
      label: '项目名称',
      prop: 'projectName',
      search: true,
      searchOrder: 4,
      searchType: 'select',
      formslot: true,
      dicUrl: '/blade-transport/project-apply/list?current=1&size=9999',
      dicFormatter: listDicFormatter,
      props: {
        label: 'projectName',
        value: 'projectName',
      },
      filterable: true,
      span: 12,
      minWidth: 180,
      rules: textRule('项目名称', 100, true),
    },
    {
      label: '项目ID',
      prop: 'projectId',
      hide: true,
      display: false,
    },
    {
      label: '项目编号',
      prop: 'projectCode',
      hide: true,
      disabled: true,
      span: 12,
      minWidth: 150,
      rules: textRule('项目编号', 50),
    },
    {
      label: '承办部门',
      prop: 'undertakeDeptName',
      hide: true,
      disabled: true,
      span: 12,
      minWidth: 150,
      rules: textRule('承办部门', 50, true),
    },
    {
      label: '项目资金使用额度（万元）',
      prop: 'projectFundLimit',
      hide: true,
      disabled: true,
      span: 12,
      minWidth: 200,
      rules: [nonNegativeRule('项目资金使用额度')],
    },
    {
      label: '已使用项目资金使用额度（万元）',
      prop: 'usedFundLimit',
      hide: true,
      disabled: true,
      span: 12,
      minWidth: 220,
      value: 0,
      rules: [nonNegativeRule('已使用项目资金额度')],
    },
    {
      label: '剩余项目资金使用额度（万元）',
      prop: 'remainingFundLimit',
      hide: true,
      disabled: true,
      span: 12,
      minWidth: 230,
      value: 0,
      rules: [nonNegativeRule('剩余项目资金使用额度')],
    },
    {
      label: '申请临时额度（万元）',
      prop: 'applyLimit',
      formslot: true,
      span: 12,
      minWidth: 180,
      rules: [
        { required: true, message: '请输入申请临时额度', trigger: 'blur' },
        nonNegativeRule('申请临时额度'),
      ],
    },
    {
      label: '申请有效期至',
      prop: 'validUntil',
      type: 'date',
      format: 'YYYY-MM-DD',
      valueFormat: 'YYYY-MM-DD',
      span: 12,
      minWidth: 140,
      rules: [{ required: true, message: '请选择申请有效期至', trigger: 'change' }],
    },
    {
      label: '申请部门',
      prop: 'applyDeptName',
      search: true,
      searchOrder: 2,
      searchType: 'select',
      dicUrl: '/blade-system/dept/select',
      props: {
        label: 'deptName',
        value: 'deptName',
      },
      filterable: true,
      minWidth: 150,
      addDisplay: false,
      editDisplay: false,
      editDisabled: true,
    },
    {
      label: '申请人',
      prop: 'applicantName',
      search: true,
      searchOrder: 3,
      searchType: 'select',
      dicUrl: '/blade-system/user/user-list',
      dicFormatter: userDicFormatter,
      props: {
        label: 'realName',
        value: 'realName',
      },
      filterable: true,
      minWidth: 120,
      addDisplay: false,
      editDisplay: false,
      editDisabled: true,
    },
    {
      label: '审批状态',
      searchLabel: '状态',
      prop: 'approvalStatus',
      type: 'select',
      search: true,
      searchOrder: 1,
      slot: true,
      dicData: approvalStatusOptions,
      minWidth: 130,
      addDisplay: false,
      editDisplay: false,
      fixed: 'right',
    },
    {
      label: '当前节点',
      prop: 'currentNode',
      minWidth: 140,
      addDisplay: false,
      editDisplay: false,
    },
    {
      label: '当前处理人',
      prop: 'currentProcessor',
      minWidth: 130,
      addDisplay: false,
      editDisplay: false,
    },
    {
      label: '备注',
      prop: 'remark',
      type: 'textarea',
      span: 24,
      hide: true,
      maxlength: 200,
      showWordLimit: true,
      rules: textRule('备注', 200),
    },
    {
      label: '附件',
      prop: 'attachmentsJson',
      formslot: true,
      span: 24,
      hide: true,
    },
    ...auditColumns.map(column => ({ ...column, hide: true })),
  ]),
  dialogWidth: 1010,
  index: false,
};
