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

import { DICT_TYPE } from '@vben/constants';

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

/** 表单类型 */
export type FormType = 'create' | 'detail' | 'edit';

/** 需求单状态 */
export const DEMAND_STATUS = {
  UNPURCHASED: 1, // 未采购
  PURCHASED: 2, // 已采购
};

/** 表单的配置项 */
export function useFormSchema(formType: FormType): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'no',
      label: '需求单号',
      component: 'Input',
      componentProps: {
        placeholder: '系统自动生成',
        disabled: true,
      },
    },
    {
      fieldName: 'supplierId',
      label: '供货商',
      component: 'ApiSelect',
      componentProps: {
        placeholder: '请选择供货商',
        allowClear: true,
        showSearch: true,
        api: getSupplierSimpleList,
        labelField: 'name',
        valueField: 'id',
        disabled: formType === 'detail',
      },
      rules: 'required',
    },
    {
      fieldName: 'purchaseCategory',
      label: '采购品类',
      component: 'RadioGroup',
      componentProps: {
        buttonStyle: 'solid',
        optionType: 'button',
        options: [
          { label: '代理商品', value: 1 },
          { label: '原辅料', value: 2 },
        ],
        disabled: formType === 'detail',
      },
      rules: 'required',
    },
    {
      fieldName: 'workOrderNo',
      label: '关联生产任务',
      component: 'Input',
      componentProps: {
        placeholder: '关联工单号，填写后来源记为「生产计划」',
        allowClear: true,
        disabled: formType === 'detail',
      },
    },
    {
      fieldName: 'demandTime',
      label: '需求日期',
      component: 'DatePicker',
      componentProps: {
        placeholder: '选择需求日期',
        format: 'YYYY-MM-DD',
        valueFormat: 'YYYY-MM-DD',
        disabled: formType === 'detail',
      },
      rules: 'required',
    },
    {
      fieldName: 'purpose',
      label: '用途说明',
      component: 'Input',
      componentProps: {
        placeholder: '请输入用途，如：车间维修备件',
        allowClear: true,
        disabled: formType === 'detail',
      },
    },
    {
      fieldName: 'remark',
      label: '备注',
      component: 'Textarea',
      componentProps: {
        placeholder: '请输入备注',
        autoSize: { minRows: 1, maxRows: 2 },
        disabled: formType === 'detail',
      },
      formItemClass: 'col-span-2',
    },
    {
      fieldName: 'items',
      label: '需求明细',
      component: 'Input',
      formItemClass: 'col-span-3',
    },
  ];
}

/** 明细表格列 */
export function useFormItemColumns(
  disabled: boolean,
): VxeTableGridOptions['columns'] {
  return [
    { type: 'seq', title: '序号', minWidth: 50, fixed: 'left' },
    {
      field: 'productId',
      title: '产品名称',
      minWidth: 220,
      slots: { default: 'productId' },
    },
    {
      field: 'productUnitName',
      title: '单位',
      minWidth: 80,
    },
    {
      field: 'productPrice',
      title: '参考单价',
      minWidth: 120,
      fixed: 'right',
      slots: { default: 'productPrice' },
    },
    {
      field: 'count',
      title: '数量',
      minWidth: 120,
      fixed: 'right',
      slots: { default: 'count' },
    },
    {
      field: 'remark',
      title: '备注',
      minWidth: 160,
      slots: { default: 'remark' },
    },
    {
      title: '操作',
      width: 60,
      fixed: 'right',
      slots: { default: 'actions' },
      visible: !disabled,
    },
  ];
}

/** 列表的搜索表单 */
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: 'purchaseCategory',
      label: '采购品类',
      component: 'Select',
      componentProps: {
        placeholder: '请选择采购品类',
        allowClear: true,
        options: [
          { label: '代理商品', value: 1 },
          { label: '原辅料', value: 2 },
        ],
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'Select',
      componentProps: {
        placeholder: '请选择状态',
        allowClear: true,
        options: [
          { label: '未采购', value: 1 },
          { label: '已采购', value: 2 },
        ],
      },
    },
    {
      fieldName: 'workOrderNo',
      label: '关联生产任务',
      component: 'Input',
      componentProps: {
        placeholder: '请输入工单号',
        allowClear: true,
      },
    },
    {
      fieldName: 'demandTime',
      label: '需求日期',
      component: 'RangePicker',
      componentProps: {
        ...getDateRangePickerDefaultProps(),
        allowClear: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions['columns'] {
  return [
    { type: 'checkbox', width: 50, fixed: 'left' },
    { field: 'no', title: '需求单号', width: 190, fixed: 'left' },
    { field: 'supplierName', title: '供货商', minWidth: 160 },
    {
      field: 'purchaseCategory',
      title: '采购品类',
      minWidth: 110,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.ERP_PURCHASE_CATEGORY },
      },
    },
    {
      field: 'sourceType',
      title: '来源',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.ERP_PURCHASE_DEMAND_SOURCE },
      },
    },
    { field: 'workOrderNo', title: '关联生产任务', minWidth: 160 },
    { field: 'demandTime', title: '需求日期', width: 120 },
    {
      field: 'totalCount',
      title: '合计数量',
      minWidth: 110,
      align: 'right',
      formatter: 'formatAmount3',
    },
    { field: 'productNames', title: '需求产品', minWidth: 180, showOverflow: 'tooltip' },
    { field: 'purpose', title: '用途说明', minWidth: 140, showOverflow: 'tooltip' },
    {
      field: 'status',
      title: '状态',
      width: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.ERP_PURCHASE_DEMAND_STATUS },
      },
    },
    { field: 'orderNo', title: '采购订单号', width: 180 },
    { field: 'createTime', title: '创建时间', width: 160, formatter: 'formatDate' },
    {
      title: '操作',
      width: 220,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
