import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { WmsApprovalApi } from '#/api/mes/wm/approval';

import { h } from 'vue';

import { Switch, Tag } from 'ant-design-vue';

import { getWarehouseSimpleList } from '#/api/mes/wm/warehouse';

/** 业务类型选项 */
export const BIZ_TYPE_OPTIONS = [
  { label: '入库审批', value: 'INBOUND' },
  { label: '出库审批', value: 'OUTBOUND' },
  { label: '调拨审批', value: 'TRANSFER' },
  { label: '盘点审批', value: 'CHECK' },
];

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'warehouseId',
      label: '仓库',
      component: 'ApiSelect',
      componentProps: {
        allowClear: true,
        api: getWarehouseSimpleList,
        labelField: 'name',
        valueField: 'id',
        placeholder: '请选择仓库',
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(
  handleChange: (row: any, bizType: string, enabled: boolean) => void,
): VxeTableGridOptions['columns'] {
  return [
    {
      field: 'warehouseName',
      title: '仓库名称',
      minWidth: 160,
    },
    {
      field: 'warehouseCode',
      title: '仓库编码',
      minWidth: 120,
    },
    {
      title: '入库审批',
      width: 100,
      align: 'center',
      slots: {
        default: ({ row }) => {
          const config = row.configs?.find(
            (c: WmsApprovalApi.ApprovalConfig) => c.bizType === 'INBOUND',
          );
          return h(Switch, {
            checked: config?.approvalEnabled ?? false,
            checkedChildren: '开',
            unCheckedChildren: '关',
            onChange: (checked: boolean) => {
              handleChange(row, 'INBOUND', checked);
            },
          });
        },
      },
    },
    {
      title: '出库审批',
      width: 100,
      align: 'center',
      slots: {
        default: ({ row }) => {
          const config = row.configs?.find(
            (c: WmsApprovalApi.ApprovalConfig) => c.bizType === 'OUTBOUND',
          );
          return h(Switch, {
            checked: config?.approvalEnabled ?? false,
            checkedChildren: '开',
            unCheckedChildren: '关',
            onChange: (checked: boolean) => {
              handleChange(row, 'OUTBOUND', checked);
            },
          });
        },
      },
    },
    {
      title: '调拨审批',
      width: 100,
      align: 'center',
      slots: {
        default: ({ row }) => {
          const config = row.configs?.find(
            (c: WmsApprovalApi.ApprovalConfig) => c.bizType === 'TRANSFER',
          );
          return h(Switch, {
            checked: config?.approvalEnabled ?? false,
            checkedChildren: '开',
            unCheckedChildren: '关',
            onChange: (checked: boolean) => {
              handleChange(row, 'TRANSFER', checked);
            },
          });
        },
      },
    },
    {
      title: '盘点审批',
      width: 100,
      align: 'center',
      slots: {
        default: ({ row }) => {
          const config = row.configs?.find(
            (c: WmsApprovalApi.ApprovalConfig) => c.bizType === 'CHECK',
          );
          return h(Switch, {
            checked: config?.approvalEnabled ?? false,
            checkedChildren: '开',
            unCheckedChildren: '关',
            onChange: (checked: boolean) => {
              handleChange(row, 'CHECK', checked);
            },
          });
        },
      },
    },
    {
      title: '流程配置',
      minWidth: 200,
      slots: {
        default: ({ row }) => {
          const configs = row.configs || [];
          if (configs.length === 0) {
            return h(Tag, { color: 'default' }, () => '未配置');
          }
          const enabledConfigs = configs.filter(
            (c: WmsApprovalApi.ApprovalConfig) => c.approvalEnabled,
          );
          if (enabledConfigs.length === 0) {
            return h(Tag, { color: 'default' }, () => '未配置');
          }
          return h(
            'div',
            { class: 'flex flex-wrap gap-1' },
            enabledConfigs.map((config: WmsApprovalApi.ApprovalConfig) => {
              const option = BIZ_TYPE_OPTIONS.find(
                (o) => o.value === config.bizType,
              );
              return h(
                Tag,
                {
                  color: config.processDefinitionKey ? 'green' : 'orange',
                },
                () => option?.label || config.bizType,
              );
            }),
          );
        },
      },
    },
    {
      title: '操作',
      width: 100,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}

/** 流程配置表单 */
export function useProcessFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'warehouseId',
      label: '仓库',
      component: 'Input',
      componentProps: {
        disabled: true,
      },
    },
    {
      fieldName: 'bizType',
      label: '业务类型',
      component: 'Select',
      componentProps: {
        options: BIZ_TYPE_OPTIONS,
        placeholder: '请选择业务类型',
      },
      rules: 'required',
    },
    {
      fieldName: 'processDefinitionKey',
      label: '审批流程',
      component: 'ApiSelect',
      componentProps: {
        allowClear: true,
        labelField: 'name',
        valueField: 'key',
        placeholder: '请选择审批流程',
      },
      rules: 'required',
    },
  ];
}