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

import { useUserStore } from '@vben/stores';

import { z } from '#/adapter/form';
import { getProductionList } from '#/api';
import { $t } from '#/locales';

const userStore = useUserStore();
/**
 * 获取表格列配置
 * @description 使用函数的形式返回列数据而不是直接export一个Array常量，是为了响应语言切换时重新翻译表头
 * @param onActionClick 表格操作按钮点击事件
 */
export const useColumns = (
  onActionClick: OnActionClickFn<DeviceApi.Device>,
): VxeTableGridOptions<DeviceApi.Device>['columns'] => {
  return [
    {
      field: 'name',
      title: $t('device.name'),
      width: 150,
    },
    {
      field: 'device_key',
      title: $t('device.list.device_key'),
    },
    {
      field: 'org_id',
      title: $t('device.list.org_id'),
      width: 150,
    },
    { field: 'location', title: $t('device.list.location') },
    { field: 'description', title: $t('device.list.description') },
    { field: 'last_online_time', title: $t('device.list.last_online_time') },
    { field: 'last_offline_time', title: $t('device.list.last_offline_time') },
    { field: 'enabled', title: $t('device.list.enabled') },
    { field: 'online_status', title: $t('device.list.online_status') },
    {
      align: 'right',
      cellRender: {
        attrs: {
          nameField: 'name',
          nameTitle: $t('system.dept.name'),
          onClick: onActionClick,
        },
        name: 'CellOperation',
        options: [
          // {
          //   code: 'append',
          //   text: '新增下级',
          // },
          'edit', // 默认的编辑按钮
          {
            code: 'delete', // 默认的删除按钮
            disabled: (_row: DeviceApi.Device) => {
              return false;
              // return !!(row.children && row.children.length > 0);
            },
          },
        ],
      },
      field: 'operation',
      fixed: 'right',
      headerAlign: 'center',
      showOverflow: false,
      title: $t('system.dept.operation'),
      width: 200,
    },
  ];
};

export function useEditSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'id',
      disabled: true,
      label: $t('device.list.id'),
      rules: z
        .number()
        .min(1, $t('ui.formRules.minLength', [$t('device.data.id'), 1]))
        .max(
          20,
          $t('ui.formRules.maxLength', [$t('system.user.userName'), 20]),
        ),
    },
    {
      component: 'Input',
      fieldName: 'device_key',
      disabled: true,
      label: $t('device.list.device_key'),
      rules: z
        .string()
        .min(1, $t('ui.formRules.minLength', [$t('device.data.id'), 1]))
        .max(
          20,
          $t('ui.formRules.maxLength', [$t('system.user.userName'), 20]),
        ),
    },
    {
      component: 'Input',
      fieldName: 'online_status',
      label: $t('device.list.online_status'),
      disabled: true,
    },
    {
      component: 'Input',
      fieldName: 'name',
      label: $t('device.list.name'),
      rules: z
        .string()
        .min(2, $t('ui.formRules.minLength', [$t('system.user.userName'), 2]))
        .max(
          20,
          $t('ui.formRules.maxLength', [$t('system.user.userName'), 20]),
        ),
    },
    {
      component: 'Input',
      fieldName: 'location',
      label: $t('device.list.location'),
    },
    {
      component: 'Input',
      fieldName: 'description',
      label: $t('device.list.description'),
    },
    {
      component: 'Input',
      fieldName: 'org_id',
      label: $t('device.list.org_id'),
      disabled: true,
    },
    {
      component: 'ApiTreeSelect',
      fieldName: 'product_id',
      componentProps: {
        api: async () => {
          const res = () => getProductionList(userStore!.userInfo!.org_id);
          console.warn(res);
          return [];
        },
      },
    },
    // {
    //   component: 'Switch',
    //   fieldName: 'enabled',
    //   label: $t('device.list.enabled'),
    // },
  ];
}
export function useCreateSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'device_key',
      label: $t('device.list.device_key'),
      rules: z
        .string()
        .min(2, $t('ui.formRules.minLength', [$t('system.user.nickName'), 2]))
        .max(
          20,
          $t('ui.formRules.maxLength', [$t('device.data.device_key'), 20]),
        ),
    },
    // {
    //   component: 'Input',
    //   fieldName: 'org_id',
    //   label: $t('device.data.org_id'),
    //   rules: z
    //     .string()
    //     .min(1, $t('ui.formRules.minLength', [$t('device.data.org_id'), 1]))
    //     .max(20, $t('ui.formRules.maxLength', [$t('device.data.org_id'), 20])),
    // },
    {
      component: 'Input',
      fieldName: 'name',
      label: $t('device.list.name'),
      rules: z
        .string()
        .min(2, $t('ui.formRules.minLength', [$t('system.user.userName'), 2]))
        .max(
          20,
          $t('ui.formRules.maxLength', [$t('system.user.userName'), 20]),
        ),
    },
    {
      component: 'ApiTreeSelect',
      fieldName: 'product_id',
      label: $t('device.list.product_id'),
      rules: z.number(),
      componentProps: {
        required: true,
        class: 'w-full',
        api: async () => {
          const { list } = await getProductionList(userStore!.userInfo!.org_id);
          // console.warn(res);
          return list;
        },
        childrenField: 'products',
        labelField: 'name',
        // treeDefaultExpandAll: true,
        valueField: 'id',
      },
    },
    {
      component: 'Input',
      fieldName: 'location',
      label: $t('device.list.location'),
    },
    {
      component: 'Input',
      fieldName: 'description',
      label: $t('device.list.description'),
    },
  ];
}
