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

import { z } from '#/adapter/form';
import { getPurchaseOrderPage } from '#/api/erp/purchase/order';
import { getInquirySimpleList } from '#/api/srm/inquiry';
import { getSupplierSimpleList } from '#/api/srm/supplier';

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 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';
}

/** 采购订单下拉数据 */
async function getPurchaseOrderOptions() {
  const result = await getPurchaseOrderPage({ pageNo: 1, pageSize: 200 });
  return result.list ?? [];
}

/** 新增/修改投标保证金的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'supplierId',
      label: '供应商',
      component: 'ApiSelect',
      rules: 'required',
      componentProps: {
        placeholder: '请选择供应商',
        allowClear: true,
        showSearch: true,
        api: getSupplierSimpleList,
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'projectName',
      label: '项目名称',
      component: 'Input',
      componentProps: { placeholder: '选择询价单后自动带出，也可手工填写' },
    },
    {
      fieldName: 'inquiryId',
      label: '关联询价单',
      component: 'ApiSelect',
      componentProps: {
        placeholder: '请选择询价单',
        allowClear: true,
        showSearch: true,
        api: getInquirySimpleList,
        labelField: 'title',
        valueField: 'id',
      },
    },
    {
      fieldName: 'purchaseOrderId',
      label: '关联采购订单',
      component: 'ApiSelect',
      componentProps: {
        placeholder: '请选择采购订单',
        allowClear: true,
        showSearch: true,
        api: getPurchaseOrderOptions,
        labelField: 'no',
        valueField: 'id',
      },
    },
    {
      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: 'won',
      label: '是否中标',
      component: 'Switch',
      componentProps: {
        checkedChildren: '中标',
        unCheckedChildren: '未中标',
      },
      rules: z.boolean().default(false),
    },
    {
      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: 'supplierId',
      label: '供应商',
      component: 'ApiSelect',
      componentProps: {
        placeholder: '请选择供应商',
        allowClear: true,
        showSearch: true,
        api: getSupplierSimpleList,
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'projectName',
      label: '项目名称',
      component: 'Input',
      componentProps: { placeholder: '请输入项目名称', allowClear: true },
    },
    {
      fieldName: 'won',
      label: '是否中标',
      component: 'Select',
      componentProps: {
        placeholder: '请选择',
        allowClear: true,
        options: [
          { label: '中标', value: true },
          { label: '未中标', value: false },
        ],
      },
    },
    {
      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: 'supplierName', title: '供应商', minWidth: 150 },
    { field: 'projectName', title: '项目名称', minWidth: 180 },
    { field: 'inquiryNo', title: '关联询价单', minWidth: 150 },
    { field: 'purchaseOrderNo', title: '关联采购订单', minWidth: 160 },
    {
      field: 'bondAmount',
      title: '保证金金额',
      minWidth: 120,
      formatter: 'formatAmount2',
    },
    { field: 'payDate', title: '缴纳日期', minWidth: 110 },
    {
      field: 'won',
      title: '是否中标',
      minWidth: 90,
      slots: { default: 'won' },
    },
    {
      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' },
    },
  ];
}
