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

import { getSimpleUserList } from '#/api/system/user';

/** 询价状态 */
export const INQUIRY_STATUS_OPTIONS = [
  { label: '草稿', value: 0, color: 'default' },
  { label: '询价中', value: 10, color: 'processing' },
  { label: '已转订单', value: 20, color: 'success' },
  { label: '已取消', value: 30, color: 'error' },
];

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

/** 新增/修改询价单的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'title',
      label: '询价标题',
      component: 'Input',
      rules: 'required',
      componentProps: { placeholder: '请输入询价标题' },
      formItemClass: 'col-span-2',
    },
    {
      fieldName: 'inquiryDate',
      label: '询价日期',
      component: 'DatePicker',
      componentProps: {
        class: 'w-full',
        placeholder: '请选择询价日期',
        format: 'YYYY-MM-DD',
        valueFormat: 'YYYY-MM-DD',
      },
      rules: 'required',
    },
    {
      fieldName: 'deadline',
      label: '报价截止日期',
      component: 'DatePicker',
      componentProps: {
        class: 'w-full',
        placeholder: '请选择报价截止日期',
        format: 'YYYY-MM-DD',
        valueFormat: 'YYYY-MM-DD',
      },
    },
    {
      fieldName: 'purchaserUserId',
      label: '采购员',
      component: 'ApiSelect',
      componentProps: {
        class: 'w-full',
        placeholder: '请选择采购员',
        allowClear: true,
        showSearch: true,
        api: getSimpleUserList,
        labelField: 'nickname',
        valueField: 'id',
      },
    },
    {
      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: 'title',
      label: '询价标题',
      component: 'Input',
      componentProps: { placeholder: '请输入询价标题', allowClear: true },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'Select',
      componentProps: {
        placeholder: '请选择状态',
        allowClear: true,
        options: INQUIRY_STATUS_OPTIONS.map(({ label, value }) => ({
          label,
          value,
        })),
      },
    },
  ];
}

/** 表格列 */
export function useGridColumns(): VxeTableGridOptions['columns'] {
  return [
    { field: 'no', title: '询价单号', minWidth: 160 },
    { field: 'title', title: '询价标题', minWidth: 200 },
    {
      field: 'status',
      title: '状态',
      minWidth: 110,
      slots: { default: 'status' },
    },
    { field: 'inquiryDate', title: '询价日期', minWidth: 120 },
    { field: 'deadline', title: '报价截止', minWidth: 120 },
    { field: 'createTime', title: '创建时间', minWidth: 160 },
    {
      title: '操作',
      width: 260,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
