import {
  auditColumns,
  contractApprovalStatusOptions,
  contractCategoryOptions,
  contractStageOptions,
  createCrudOption,
  effectiveTypeOptions,
  selectRule,
  signTypeOptions,
  textRule,
} from './common';

const contractSettlementModeOptions = [
  { label: '现结', value: '现结' },
  { label: '日结', value: '日结' },
  { label: '周结', value: '周结' },
  { label: '月结', value: '月结' },
  { label: '按固定天数周期', value: '按固定天数周期' },
];

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 [];
};

export const config = {
  title: '合同管理',
  permission: 'contract_manage',
  exportUrl: '/blade-transport/contract-manage/export-contract-manage',
  exportName: '合同管理',
  defaultForm: {
    contractCategory: '客户合同',
  },
  projectPlaceholder: '请选择',
  organizationPlaceholder: '请选择',
  enableAllDept: true,
  allDeptReadonly: false,
  batchDelete: false,
  enableContractForm: true,
  enableProjectSelect: true,
  enableContractPeriod: true,
  enableContractFileUpload: true,
  enableCurrentUserHandler: true,
  enableBillingPlan: true,
  enableSettlementRule: true,
  enableReconciliation: true,
  enableAttachmentTable: true,
  enableChangeRecord: true,
  enableOrganizationSelect: true,
  enableContractCreateActions: true,
  enableContractExpiryTags: true,
  actions: ['copy'],
  statusProp: 'approvalStatus',
  statusTextProp: 'approvalStatusName',
  customMenuActions: true,
  editLabelStatusMap: {
    change_rejected: '编辑变更内容',
  },
  deleteStatus: ['draft'],
  deleteStage: ['draft'],
  editStatus: ['draft', 'rejected', 'change_rejected'],
  operations: [
    {
      action: 'flow',
      label: '查看流程',
      statusProp: 'approvalStatus',
      status: ['reviewing', 'change_reviewing'],
      stage: ['temporary', 'formal'],
      permission: 'contract_manage_view',
    },
    {
      action: 'withdraw',
      label: '撤回',
      statusProp: 'approvalStatus',
      status: ['reviewing'],
      stage: ['temporary', 'formal'],
      permission: 'contract_manage_withdraw',
    },
    {
      action: 'approve',
      label: '通过',
      statusProp: 'approvalStatus',
      status: ['reviewing'],
      stage: ['temporary', 'formal'],
      permission: 'contract_manage_approve',
    },
    {
      action: 'reject',
      label: '驳回',
      statusProp: 'approvalStatus',
      status: ['reviewing'],
      stage: ['temporary', 'formal'],
      permission: 'contract_manage_reject',
    },
    {
      action: 'submitFormal',
      label: '重新提交转正',
      statusProp: 'approvalStatus',
      status: ['rejected'],
      stage: ['temporary'],
      permission: 'contract_manage_formal',
    },
    {
      action: 'startChange',
      label: '发起变更',
      statusProp: 'approvalStatus',
      status: ['approved'],
      stage: ['formal'],
      permission: 'contract_manage_change',
      prompt: '请输入变更内容与原因',
    },
    {
      action: 'startChange',
      label: '再次发起变更',
      statusProp: 'approvalStatus',
      status: ['change_approved'],
      stage: ['formal'],
      permission: 'contract_manage_change',
      prompt: '请输入变更内容与原因',
    },
    {
      action: 'attachmentUpload',
      label: '附件上传',
      statusProp: 'approvalStatus',
      status: ['approved', 'change_approved'],
      stage: ['formal'],
      permission: 'contract_manage_edit',
    },
    {
      action: 'flow',
      label: '查看流程',
      statusProp: 'approvalStatus',
      status: ['approved', 'change_approved'],
      stage: ['formal', 'terminated'],
      permission: 'contract_manage_view',
    },
    {
      action: 'withdraw',
      label: '撤回变更',
      statusProp: 'approvalStatus',
      status: ['change_reviewing'],
      stage: ['formal'],
      permission: 'contract_manage_withdraw',
    },
    {
      action: 'startChange',
      label: '重新提交变更',
      statusProp: 'approvalStatus',
      status: ['change_rejected'],
      stage: ['formal'],
      permission: 'contract_manage_change',
      prompt: '请输入变更内容与原因',
    },
  ],
};

export const option = createCrudOption([
  {
    label: '',
    prop: 'basicInfoTitle',
    formslot: true,
    span: 24,
    order: 200,
    hide: true,
    labelWidth: 0,
  },
  {
    label: '合同编号',
    prop: 'contractNo',
    search: true,
    searchOrder: 6,
    searchPlaceholder: '请输入',
    order: 185,
    disabled: true,
    placeholder: '不填写，则系统自动生成',
    minWidth: 200,
  },
  {
    label: '合同名称',
    prop: 'contractName',
    search: true,
    searchOrder: 7,
    searchPlaceholder: '请输入',
    order: 180,
    minWidth: 180,
    rules: textRule('合同名称', 100, true),
  },
  {
    label: '所属项目',
    prop: 'projectName',
    search: true,
    searchOrder: 8,
    searchType: 'select',
    searchPlaceholder: '请选择',
    formslot: true,
    order: 140,
    dicUrl: '/blade-transport/project-apply/list?current=1&size=9999',
    dicFormatter: listDicFormatter,
    props: {
      label: 'projectName',
      value: 'projectName',
    },
    filterable: true,
    minWidth: 160,
    rules: selectRule('所属项目'),
  },
  {
    label: '所属项目ID',
    prop: 'projectId',
    hide: true,
    display: false,
  },
  {
    label: '所属组织',
    prop: 'organizationName',
    search: true,
    searchOrder: 3,
    searchType: 'cascader',
    searchPlaceholder: '全部',
    formslot: true,
    order: 120,
    dicData: [],
    props: {
      label: 'label',
      value: 'id',
      children: 'children',
    },
    checkStrictly: true,
    emitPath: false,
    showAllLevels: false,
    filterable: true,
    minWidth: 150,
    rules: selectRule('所属组织'),
  },
  {
    label: '合同类别',
    prop: 'contractCategory',
    type: 'select',
    search: true,
    searchOrder: 9,
    searchPlaceholder: '请选择',
    order: 170,
    dicData: contractCategoryOptions,
    minWidth: 130,
    rules: selectRule('合同类别'),
  },
  {
    label: '签约类型',
    prop: 'signType',
    type: 'select',
    search: false,
    span: 6,
    order: 190,
    dicData: signTypeOptions,
    minWidth: 120,
    rules: selectRule('签约类型'),
  },
  {
    label: '生效类型',
    prop: 'effectiveType',
    type: 'select',
    search: true,
    searchOrder: 5,
    searchPlaceholder: '请选择',
    dicData: effectiveTypeOptions,
    hide: true,
    display: false,
  },
  {
    label: '甲方',
    prop: 'partyA',
    type: 'select',
    order: 160,
    dicUrl:
      '/blade-transport/customer-archive/list?current=1&size=9999&customerType=客户&approvalStatus=approved&status=1',
    dicFormatter: listDicFormatter,
    props: {
      label: 'fullName',
      value: 'fullName',
    },
    filterable: true,
    minWidth: 160,
    rules: selectRule('甲方'),
  },
  {
    label: '乙方',
    prop: 'partyB',
    type: 'select',
    order: 150,
    dicUrl:
      '/blade-transport/customer-archive/list?current=1&size=9999&customerType=承运商&approvalStatus=approved&status=1',
    dicFormatter: listDicFormatter,
    props: {
      label: 'fullName',
      value: 'fullName',
    },
    filterable: true,
    minWidth: 160,
    rules: selectRule('乙方'),
  },
  {
    label: '合同期限',
    prop: 'contractPeriod',
    formslot: true,
    order: 130,
    hide: true,
    rules: [{ required: true, message: '请选择合同期限', trigger: 'change' }],
  },
  {
    label: '开始日期',
    prop: 'startDate',
    sortable: true,
    type: 'date',
    format: 'YYYY-MM-DD',
    valueFormat: 'YYYY-MM-DD',
    minWidth: 130,
    addDisplay: false,
    editDisplay: false,
  },
  {
    label: '结束日期',
    prop: 'endDate',
    sortable: true,
    type: 'date',
    format: 'YYYY-MM-DD',
    valueFormat: 'YYYY-MM-DD',
    minWidth: 130,
    addDisplay: false,
    editDisplay: false,
  },
  {
    label: '临时效力起',
    prop: 'temporaryStartDate',
    sortable: true,
    type: 'date',
    format: 'YYYY-MM-DD',
    valueFormat: 'YYYY-MM-DD',
    minWidth: 130,
    addDisplay: false,
    editDisplay: false,
  },
  {
    label: '临时效力止',
    prop: 'temporaryEndDate',
    sortable: true,
    type: 'date',
    format: 'YYYY-MM-DD',
    valueFormat: 'YYYY-MM-DD',
    minWidth: 130,
    addDisplay: false,
    editDisplay: false,
  },
  {
    label: '经办人',
    prop: 'handlerUserName',
    order: 110,
    disabled: true,
    minWidth: 120,
    rules: textRule('经办人', 50, true),
  },
  {
    label: '签订日期',
    prop: 'signDate',
    type: 'date',
    format: 'YYYY-MM-DD',
    valueFormat: 'YYYY-MM-DD',
    hide: true,
    order: 100,
    minWidth: 130,
    rules: [{ required: true, message: '请选择签订日期', trigger: 'change' }],
  },
  {
    label: '结算类型',
    prop: 'settlementMode',
    type: 'select',
    dicData: contractSettlementModeOptions,
    hide: true,
    order: 90,
    minWidth: 120,
    rules: selectRule('结算类型'),
  },
  {
    label: '合同格式',
    prop: 'contractFormat',
    type: 'select',
    dicData: [
      { label: '电子合同', value: '电子合同' },
      { label: '纸质合同', value: '纸质合同' },
    ],
    hide: true,
    order: 80,
    minWidth: 120,
    rules: selectRule('合同格式'),
  },
  {
    label: '是否需要加盖法人章',
    prop: 'legalSealFlag',
    type: 'select',
    dicData: [
      { label: '否', value: 0 },
      { label: '是', value: 1 },
    ],
    value: 0,
    hide: true,
    order: 70,
    minWidth: 100,
    rules: selectRule('是否需要加盖法人章'),
  },
  {
    label: '一式',
    prop: 'copyCount',
    formslot: true,
    hide: true,
    order: 60,
    minWidth: 110,
  },
  {
    label: '回款账期',
    prop: 'paymentDays',
    formslot: true,
    hide: true,
    order: 50,
    minWidth: 130,
  },
  {
    label: '创建时间',
    prop: 'createTime',
    sortable: true,
    type: 'datetime',
    format: 'YYYY-MM-DD HH:mm:ss',
    valueFormat: 'YYYY-MM-DD HH:mm:ss',
    minWidth: 190,
    addDisplay: false,
    editDisplay: false,
  },
  {
    label: '合同阶段',
    prop: 'contractStage',
    type: 'select',
    search: true,
    searchOrder: 2,
    searchPlaceholder: '请选择',
    slot: true,
    dicData: contractStageOptions,
    minWidth: 120,
    addDisplay: false,
    editDisplay: false,
  },
  {
    label: '审核状态',
    prop: 'approvalStatus',
    type: 'select',
    search: true,
    searchOrder: 4,
    searchPlaceholder: '请选择',
    slot: true,
    dicData: contractApprovalStatusOptions,
    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: 'billingEnabled',
    type: 'switch',
    dicData: [
      { label: '关闭', value: 0 },
      { label: '开启', value: 1 },
    ],
    value: 1,
    hide: true,
    display: false,
    minWidth: 110,
  },
  {
    label: '备注',
    prop: 'remark',
    type: 'textarea',
    span: 24,
    hide: true,
    order: 40,
    maxlength: 2000,
    showWordLimit: true,
    rules: textRule('备注', 2000),
  },
  {
    label: '',
    prop: 'contractFileTitle',
    formslot: true,
    span: 24,
    order: 30,
    hide: true,
    labelWidth: 0,
  },
  {
    label: '附件',
    prop: 'contractFileJson',
    formslot: true,
    span: 24,
    order: 20,
    hide: true,
  },
  {
    label: '',
    prop: 'billingInfoTitle',
    formslot: true,
    span: 24,
    order: 10,
    hide: true,
    labelWidth: 0,
  },
  {
    label: '',
    prop: 'billingPlanJson',
    formslot: true,
    span: 24,
    order: 0,
    hide: true,
    labelWidth: 0,
  },
  {
    label: '',
    prop: 'settlementRuleTitle',
    formslot: true,
    span: 24,
    order: -10,
    hide: true,
    labelWidth: 0,
  },
  {
    label: '',
    prop: 'settlementRuleJson',
    formslot: true,
    span: 24,
    order: -20,
    hide: true,
    labelWidth: 0,
  },
  {
    label: '',
    prop: 'reconciliationTitle',
    formslot: true,
    span: 24,
    order: -30,
    hide: true,
    labelWidth: 0,
  },
  {
    label: '',
    prop: 'reconciliationJson',
    formslot: true,
    span: 24,
    order: -40,
    hide: true,
    labelWidth: 0,
  },
  {
    label: '',
    prop: 'attachmentTitle',
    formslot: true,
    span: 24,
    order: -50,
    hide: true,
    labelWidth: 0,
  },
  {
    label: '',
    prop: 'attachmentsJson',
    formslot: true,
    span: 24,
    order: -60,
    hide: true,
    labelWidth: 0,
  },
  {
    label: '',
    prop: 'changeRecordTitle',
    formslot: true,
    span: 24,
    order: -70,
    hide: true,
    labelWidth: 0,
  },
  {
    label: '',
    prop: 'changeRecordJson',
    formslot: true,
    span: 24,
    order: -80,
    hide: true,
    labelWidth: 0,
  },
  ...auditColumns
    .filter(column => column.prop !== 'createTime')
    .map(column => ({ ...column, hide: true })),
]);

option.span = 8;
option.dialogWidth = 1480;
option.dialogTop = '10px';
option.dialogCloseBtn = true;
option.menuWidth = 320;
