import {
  auditColumns,
  businessTypeOptions,
  createCrudOption,
  effectiveTypeOptions,
  nonNegativeRule,
  projectApprovalStatusOptions,
  projectSourceOptions,
  projectTypeOptions,
  selectRule,
  settlementModeOptions,
  textRule,
} from './common';

const amountRules = label => [
  { required: true, message: `请输入${label}`, trigger: 'blur' },
  nonNegativeRule(label),
];

const userDicFormatter = res => {
  const list = Array.isArray(res)
    ? res
    : Array.isArray(res.data)
    ? res.data
    : res.data?.records || [];
  return list.map(user => ({
    ...user,
    realName: user.realName || user.name || user.account,
  }));
};

const formatUserRealName = (row, role) =>
  row[`${role}RealName`] ||
  row[`${role}UserRealName`] ||
  row[`${role}User`]?.realName ||
  row[role]?.realName ||
  row[`${role}UserName`] ||
  '';

export const config = {
  title: '项目管理',
  permission: 'project_apply',
  exportUrl: '/blade-transport/project-apply/export-project-apply',
  exportName: '项目立项',
  enableAllDept: false,
  statusProp: 'approvalStatus',
  statusTextProp: 'approvalStatusName',
  deleteStatus: ['draft', 'withdrawn', 'rejected'],
  editStatus: ['draft', 'withdrawn', 'rejected', 'change_rejected'],
  operations: [
    {
      action: 'submitApproval',
      label: '提交',
      statusProp: 'approvalStatus',
      status: ['draft'],
      permission: 'project_apply_submit',
    },
    {
      action: 'withdraw',
      label: '撤回',
      statusProp: 'approvalStatus',
      status: ['reviewing'],
      permission: 'project_apply_withdraw',
    },
    {
      action: 'approve',
      label: '通过',
      type: 'success',
      statusProp: 'approvalStatus',
      status: ['reviewing', 'change_reviewing'],
      permission: 'project_apply_approve',
    },
    {
      action: 'reject',
      label: '审批驳回',
      type: 'warning',
      statusProp: 'approvalStatus',
      status: ['reviewing', 'change_reviewing'],
      permission: 'project_apply_reject',
    },
    {
      action: 'startChange',
      label: '变更',
      statusProp: 'approvalStatus',
      status: ['approved'],
      permission: 'project_apply_change',
    },
    {
      action: 'voidProject',
      label: '作废',
      type: 'danger',
      statusProp: 'approvalStatus',
      status: ['approved', 'change_approved'],
      permission: 'project_apply_void',
      prompt: '请输入作废原因',
    },
  ],
};

export const option = createCrudOption([
  {
    label: '立项申请单号',
    prop: 'applyNo',
    search: true,
    searchPlaceholder: '请输入',
    searchOrder: 7,
    minWidth: 220,
    addDisplay: false,
    editDisabled: true,
  },
  {
    label: '项目编号',
    prop: 'projectCode',
    minWidth: 150,
    addDisplay: false,
    editDisabled: true,
  },
  {
    label: '项目名称',
    prop: 'projectName',
    search: true,
    searchPlaceholder: '请输入',
    searchOrder: 6,
    minWidth: 180,
    maxlength: 50,
    rules: textRule('项目名称', 50, true),
  },
  {
    label: '项目简称',
    prop: 'projectShortName',
    hide: true,
    minWidth: 140,
    maxlength: 20,
    rules: textRule('项目简称', 20, true),
  },
  {
    label: '项目类型',
    prop: 'projectType',
    type: 'select',
    search: true,
    searchPlaceholder: '请选择',
    searchOrder: 3,
    dicData: projectTypeOptions,
    minWidth: 120,
    rules: selectRule('项目类型'),
  },
  {
    label: '客户名称',
    prop: 'customerNames',
    minWidth: 180,
    rules: textRule('客户名称', 255, true),
  },
  {
    label: '下游承运商',
    prop: 'carrierNames',
    minWidth: 180,
    rules: textRule('下游承运商', 255, true),
  },
  {
    label: '业务部门',
    prop: 'businessDeptName',
    hide: true,
    minWidth: 150,
    rules: textRule('业务部门', 50, true),
  },
  {
    label: '承办部门',
    prop: 'undertakeDeptName',
    search: true,
    searchPlaceholder: '请输入',
    searchOrder: 5,
    minWidth: 150,
    rules: textRule('承办部门', 50, true),
  },
  {
    label: '承办部门ID',
    prop: 'undertakeDeptId',
    hide: true,
  },
  {
    label: '项目负责人',
    prop: 'principalUserName',
    minWidth: 130,
    formatter: row => formatUserRealName(row, 'principal'),
    rules: textRule('项目负责人', 50, true),
  },
  {
    label: '项目负责人',
    prop: 'principalUserId',
    type: 'select',
    search: true,
    searchPlaceholder: '请选择',
    searchOrder: 4,
    filterable: true,
    dicUrl: '/blade-system/user/user-list',
    dicFormatter: userDicFormatter,
    props: {
      label: 'realName',
      value: 'id',
    },
    hide: true,
    display: false,
  },
  {
    label: '项目经办人',
    prop: 'handlerUserName',
    hide: true,
    minWidth: 130,
    formatter: row => formatUserRealName(row, 'handler'),
    rules: textRule('项目经办人', 50, true),
  },
  {
    label: '项目由来',
    prop: 'projectSource',
    type: 'select',
    dicData: projectSourceOptions,
    value: '商务洽谈',
    hide: true,
    minWidth: 120,
  },
  {
    label: '项目资金使用额度(万元)',
    prop: 'fundLimit',
    type: 'number',
    precision: 2,
    hide: true,
    minWidth: 190,
    rules: amountRules('项目资金使用额度'),
  },
  {
    label: '项目应收账款额度(万元)',
    prop: 'receivableLimit',
    type: 'number',
    precision: 2,
    hide: true,
    minWidth: 200,
    rules: amountRules('项目应收账款额度'),
  },
  {
    label: '应收账款回款期限(天)',
    prop: 'receivableDays',
    type: 'number',
    hide: true,
    minWidth: 190,
    rules: [{ required: true, message: '请输入应收账款回款期限', trigger: 'blur' }],
  },
  {
    label: '回款账期(天)',
    prop: 'paymentDays',
    type: 'number',
    hide: true,
    minWidth: 140,
    rules: [{ required: true, message: '请输入回款账期', trigger: 'blur' }],
  },
  {
    label: '货物类型',
    prop: 'cargoType',
    hide: true,
    minWidth: 120,
  },
  {
    label: '预估货物数量',
    prop: 'cargoQuantity',
    hide: true,
    minWidth: 150,
    maxlength: 100,
  },
  {
    label: '业务周期开始',
    prop: 'businessStartDate',
    type: 'date',
    format: 'YYYY-MM-DD',
    valueFormat: 'YYYY-MM-DD',
    hide: true,
    minWidth: 130,
  },
  {
    label: '业务周期结束',
    prop: 'businessEndDate',
    type: 'date',
    format: 'YYYY-MM-DD',
    valueFormat: 'YYYY-MM-DD',
    hide: true,
    minWidth: 130,
  },
  {
    label: '运输线路',
    prop: 'transportRoute',
    hide: true,
    minWidth: 180,
    maxlength: 100,
  },
  {
    label: '运输类型',
    prop: 'transportType',
    type: 'select',
    dicUrl: '/blade-system/dict-biz/dictionary?code=transport_type',
    props: {
      label: 'dictValue',
      value: 'dictKey',
    },
    hide: true,
    minWidth: 120,
  },
  {
    label: '业务类型',
    prop: 'businessType',
    type: 'select',
    dicData: businessTypeOptions,
    hide: true,
    minWidth: 130,
  },
  {
    label: '项目规模(万元)',
    prop: 'projectScale',
    type: 'number',
    precision: 2,
    hide: true,
    minWidth: 150,
    rules: [nonNegativeRule('项目规模')],
  },
  {
    label: '预计利润(万元)',
    prop: 'estimatedProfit',
    type: 'number',
    precision: 2,
    hide: true,
    minWidth: 150,
    rules: [nonNegativeRule('预计利润')],
  },
  {
    label: '资金需求(万元)',
    prop: 'fundDemand',
    type: 'number',
    precision: 2,
    hide: true,
    minWidth: 150,
    rules: [nonNegativeRule('资金需求')],
  },
  {
    label: '结算方式',
    prop: 'settlementMode',
    type: 'select',
    dicData: settlementModeOptions,
    hide: true,
    minWidth: 120,
  },
  {
    label: '审批状态',
    prop: 'approvalStatus',
    type: 'select',
    search: true,
    searchPlaceholder: '请选择',
    searchOrder: 2,
    slot: true,
    dicData: projectApprovalStatusOptions,
    minWidth: 130,
    addDisplay: false,
    editDisplay: false,
  },
  {
    label: '当前节点',
    prop: 'currentNode',
    minWidth: 140,
    addDisplay: false,
    editDisplay: false,
  },
  {
    label: '当前处理人',
    prop: 'currentProcessor',
    minWidth: 130,
    addDisplay: false,
    editDisplay: false,
  },
  {
    label: '生效类型',
    prop: 'effectiveType',
    type: 'select',
    search: true,
    searchPlaceholder: '请选择',
    searchOrder: 1,
    dicData: effectiveTypeOptions,
    value: 'temporary',
    minWidth: 110,
  },
  {
    label: '审核通过时间',
    prop: 'approvedTime',
    sortable: true,
    type: 'datetime',
    format: 'YYYY-MM-DD HH:mm:ss',
    valueFormat: 'YYYY-MM-DD HH:mm:ss',
    minWidth: 170,
    addDisplay: false,
    editDisplay: false,
  },
  {
    label: '项目由来说明',
    prop: 'sourceRemark',
    type: 'textarea',
    span: 24,
    hide: true,
    maxlength: 300,
    showWordLimit: true,
  },
  {
    label: '项目情况说明',
    prop: 'situationRemark',
    type: 'textarea',
    span: 24,
    hide: true,
    maxlength: 1000,
    showWordLimit: true,
  },
  {
    label: '项目附件JSON',
    prop: 'attachmentsJson',
    type: 'textarea',
    span: 24,
    hide: true,
  },
  ...auditColumns.map(column => ({ ...column, hide: true })),
]);
