import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';

import { getCustomerSimpleList } from '#/api/crm/customer';
import { getSaleOrderOptions } from '#/api/crm/bidBond';

export const BID_RESULT_OPTIONS = [
  { label: '待开标', value: 0, color: 'processing' },
  { label: '已中标', value: 1, color: 'success' },
  { label: '未中标', value: 2, color: 'default' },
];

export const REFUND_STATUS_OPTIONS = [
  { label: '未退回', value: 0, color: 'warning' },
  { label: '部分退回', value: 1, color: 'processing' },
  { label: '已退回', value: 2, color: 'success' },
  { label: '无需退回', value: 3, color: 'default' },
];

export function getBidResultLabel(result?: number) {
  return BID_RESULT_OPTIONS.find((item) => item.value === result)?.label ?? '-';
}

export function getBidResultColor(result?: number) {
  return BID_RESULT_OPTIONS.find((item) => item.value === result)?.color ?? 'default';
}

export function getRefundStatusLabel(status?: number) {
  return REFUND_STATUS_OPTIONS.find((item) => item.value === status)?.label ?? '-';
}

export function getRefundStatusColor(status?: number) {
  return REFUND_STATUS_OPTIONS.find((item) => item.value === status)?.color ?? 'default';
}

/** 新增/修改投标保证金的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'customerId',
      label: '客户（招标方）',
      component: 'ApiSelect',
      rules: 'required',
      componentProps: {
        placeholder: '请选择客户',
        allowClear: true,
        showSearch: true,
        api: getCustomerSimpleList,
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'projectName',
      label: '项目名称',
      component: 'Input',
      componentProps: { placeholder: '请输入本次投标项目名称' },
    },
    {
      fieldName: 'tenderNo',
      label: '招标编号',
      component: 'Input',
      componentProps: { placeholder: '请输入招标方发布的招标编号' },
    },
    {
      fieldName: 'saleOrderId',
      label: '关联销售订单',
      component: 'ApiSelect',
      componentProps: {
        placeholder: '请先选择客户',
        allowClear: true,
        showSearch: true,
        labelField: 'no',
        valueField: 'id',
        optionFilterProp: 'label',
      },
      dependencies: {
        triggerFields: ['customerId'],
        componentProps: (values) => {
          const customerId = values.customerId as number | undefined;
          return {
            placeholder: customerId
              ? '请选择该客户已审核的销售订单（销售台账）'
              : '请先选择客户',
            disabled: !customerId,
            api: customerId
              ? () => getSaleOrderOptions(customerId)
              : () => Promise.resolve([]),
          };
        },
      },
    },
    {
      fieldName: 'bondAmount',
      label: '保证金金额',
      component: 'InputNumber',
      rules: 'required',
      componentProps: {
        class: '!w-full',
        placeholder: '请输入缴纳的保证金金额',
        min: 0,
        precision: 2,
      },
    },
    {
      fieldName: 'payDate',
      label: '缴纳日期',
      component: 'DatePicker',
      componentProps: {
        class: 'w-full',
        placeholder: '请选择向客户缴纳保证金的日期',
        format: 'YYYY-MM-DD',
        valueFormat: 'YYYY-MM-DD',
      },
    },
    {
      fieldName: 'bidResult',
      label: '中标结果',
      component: 'Select',
      rules: 'required',
      componentProps: {
        placeholder: '请选择中标结果',
        options: BID_RESULT_OPTIONS.map(({ label, value }) => ({
          label,
          value,
        })),
      },
      defaultValue: 0,
    },
    {
      fieldName: 'refundStatus',
      label: '退回状态',
      component: 'Select',
      componentProps: {
        placeholder: '未中标时跟踪保证金是否退回',
        options: REFUND_STATUS_OPTIONS.map(({ label, value }) => ({
          label,
          value,
        })),
      },
    },
    {
      fieldName: 'refundAmount',
      label: '退回金额',
      component: 'InputNumber',
      componentProps: {
        class: '!w-full',
        placeholder: '已退回时须等于保证金金额；部分退回时须小于保证金金额',
        min: 0,
        precision: 2,
      },
    },
    {
      fieldName: 'refundDate',
      label: '退回日期',
      component: 'DatePicker',
      componentProps: {
        class: 'w-full',
        placeholder: '请选择客户实际退回保证金的日期',
        format: 'YYYY-MM-DD',
        valueFormat: 'YYYY-MM-DD',
      },
    },
    {
      fieldName: 'remark',
      label: '备注',
      component: 'Textarea',
      componentProps: { placeholder: '请输入备注', rows: 2 },
      formItemClass: 'col-span-2',
    },
  ];
}

/** 搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'no',
      label: '保证金单号',
      component: 'Input',
      componentProps: { placeholder: '请输入保证金单号', allowClear: true },
    },
    {
      fieldName: 'customerId',
      label: '客户',
      component: 'ApiSelect',
      componentProps: {
        placeholder: '请选择客户',
        allowClear: true,
        showSearch: true,
        api: getCustomerSimpleList,
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'projectName',
      label: '项目名称',
      component: 'Input',
      componentProps: { placeholder: '请输入项目名称', allowClear: true },
    },
    {
      fieldName: 'saleOrderNo',
      label: '销售订单号',
      component: 'Input',
      componentProps: { placeholder: '请输入关联销售订单号', allowClear: true },
    },
    {
      fieldName: 'bidResult',
      label: '中标结果',
      component: 'Select',
      componentProps: {
        placeholder: '请选择',
        allowClear: true,
        options: BID_RESULT_OPTIONS.map(({ label, value }) => ({
          label,
          value,
        })),
      },
    },
    {
      fieldName: 'refundStatus',
      label: '退回状态',
      component: 'Select',
      componentProps: {
        placeholder: '请选择',
        allowClear: true,
        options: REFUND_STATUS_OPTIONS.map(({ label, value }) => ({
          label,
          value,
        })),
      },
    },
  ];
}

/** 表格列 */
export function useGridColumns(): VxeTableGridOptions['columns'] {
  return [
    { field: 'no', title: '保证金单号', minWidth: 150 },
    { field: 'customerName', title: '客户（招标方）', minWidth: 160 },
    { field: 'projectName', title: '项目名称', minWidth: 180 },
    { field: 'tenderNo', title: '招标编号', minWidth: 140 },
    {
      field: 'saleOrderNo',
      title: '关联销售订单',
      minWidth: 160,
      slots: { default: 'saleOrderNo' },
    },
    {
      field: 'saleOrderAmount',
      title: '订单金额',
      minWidth: 120,
      formatter: 'formatAmount2',
    },
    {
      field: 'bondAmount',
      title: '保证金金额',
      minWidth: 120,
      formatter: 'formatAmount2',
    },
    { field: 'payDate', title: '缴纳日期', minWidth: 110 },
    {
      field: 'bidResult',
      title: '中标结果',
      minWidth: 100,
      slots: { default: 'bidResult' },
    },
    {
      field: 'refundStatus',
      title: '退回状态',
      minWidth: 110,
      slots: { default: 'refundStatus' },
    },
    {
      field: 'refundAmount',
      title: '退回金额',
      minWidth: 120,
      formatter: 'formatAmount2',
    },
    { field: 'refundDate', title: '退回日期', minWidth: 110 },
    { field: 'remark', title: '备注', minWidth: 150 },
    {
      title: '操作',
      width: 130,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
