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

import { getSupplierSimpleList } from '#/api/srm/supplier';

import { COMPOSITE_SCORE_CELLTAG, EVALUATION_GRADE_CELLTAG } from '../enums';

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'id',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'supplierId',
      label: '供应商',
      component: 'ApiSelect',
      rules: 'required',
      componentProps: {
        placeholder: '请选择供应商',
        allowClear: true,
        showSearch: true,
        api: getSupplierSimpleList,
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'evaluationPeriod',
      label: '评价周期',
      component: 'Input',
      rules: 'required',
      componentProps: { placeholder: '如 2024-Q1' },
    },
    {
      fieldName: 'qualityScore',
      label: '质量评分',
      component: 'InputNumber',
      rules: 'required',
      componentProps: { min: 0, max: 100, placeholder: '请输入质量评分' },
    },
    {
      fieldName: 'deliveryScore',
      label: '交付评分',
      component: 'InputNumber',
      rules: 'required',
      componentProps: { min: 0, max: 100, placeholder: '请输入交付评分' },
    },
    {
      fieldName: 'priceScore',
      label: '价格评分',
      component: 'InputNumber',
      rules: 'required',
      componentProps: { min: 0, max: 100, placeholder: '请输入价格评分' },
    },
    {
      fieldName: 'serviceScore',
      label: '服务评分',
      component: 'InputNumber',
      rules: 'required',
      componentProps: { min: 0, max: 100, placeholder: '请输入服务评分' },
    },
    {
      fieldName: 'remark',
      label: '评价说明',
      component: 'Textarea',
      componentProps: { placeholder: '请输入评价说明', rows: 3 },
      formItemClass: 'col-span-2',
    },
  ];
}

/** 搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'supplierId',
      label: '供应商',
      component: 'ApiSelect',
      componentProps: {
        placeholder: '请选择供应商',
        allowClear: true,
        showSearch: true,
        api: getSupplierSimpleList,
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'evaluationPeriod',
      label: '评价周期',
      component: 'Input',
      componentProps: { placeholder: '请输入评价周期', allowClear: true },
    },
  ];
}

/** 表格列 */
export function useGridColumns(): VxeTableGridOptions['columns'] {
  return [
    { field: 'supplierName', title: '供应商', minWidth: 150 },
    { field: 'evaluationPeriod', title: '评价周期', minWidth: 120 },
    { field: 'qualityScore', title: '质量评分', minWidth: 100 },
    { field: 'deliveryScore', title: '交付评分', minWidth: 100 },
    { field: 'priceScore', title: '价格评分', minWidth: 100 },
    { field: 'serviceScore', title: '服务评分', minWidth: 100 },
    {
      field: 'compositeScore',
      title: '综合评分',
      minWidth: 120,
      cellRender: { name: 'CellTag', props: COMPOSITE_SCORE_CELLTAG },
    },
    {
      field: 'grade',
      title: '等级',
      minWidth: 80,
      cellRender: { name: 'CellTag', props: EVALUATION_GRADE_CELLTAG },
    },
    { field: 'createTime', title: '创建时间', minWidth: 160 },
    {
      title: '操作',
      width: 130,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
