import type { VxeGridProps } from '#/adapter/vxe-table';

import { computed } from 'vue';

import { z } from '@vben/common-ui';

import {
  getCommonGetSelectList,
  postOneNetProductListAsync,
} from '#/api-client';
import { $t } from '#/locales';

export const querySchema = computed(() => [
  {
    component: 'Input',
    fieldName: 'deviceAddress',
    label: $t('abp.deviceInfos.deviceAddress'),
  },
]);

export const ioTPlatformOptions = [
  {
    label: 'OneNET',
    value: 1,
  },
  {
    label: 'CTWing',
    value: 2,
  },
  {
    label: 'Other',
    value: 0,
  },
];
export const tableSchema: any = computed((): VxeGridProps['columns'] => [
  { title: $t('common.seq'), type: 'seq', width: 50 },
  {
    field: 'deviceName',
    title: $t('abp.deviceInfos.deviceName'),
    minWidth: '150',
  },
  {
    field: 'deviceAddress',
    title: $t('abp.deviceInfos.deviceAddress'),
    minWidth: '150',
  },
  {
    field: 'ioTPlatform',
    title: $t('abp.deviceInfos.ioTPlatform'),
    minWidth: '150',
  },
  {
    field: 'ioTPlatformName',
    title: $t('abp.deviceInfos.ioTPlatformName'),
    minWidth: '150',
  },
  {
    field: 'ioTPlatformDeviceOpenInfo',
    title: $t('abp.deviceInfos.ioTPlatformDeviceOpenInfo'),
    minWidth: '100',
  },
  {
    field: 'platformPassword',
    title: $t('abp.deviceInfos.platformPassword'),
    minWidth: '150',
  },
  {
    field: 'ioTPlatformProductName',
    title: $t('abp.deviceInfos.ioTPlatformProductName'),
    minWidth: '150',
  },
  {
    field: 'ioTPlatformAccountName',
    title: $t('abp.deviceInfos.ioTPlatformAccountName'),
    minWidth: '150',
  },
  {
    field: 'ioTPlatformResponse',
    title: $t('abp.deviceInfos.ioTPlatformResponse'),
    minWidth: '150',
  },
  {
    title: $t('common.action'),
    field: 'action',
    fixed: 'right',
    width: '150',
    slots: { default: 'action' },
  },
]);

export const addDeviceFormSchema: any = computed(() => [
  {
    component: 'ApiSelect',
    fieldName: 'IoTPlatform',
    label: $t('common.IoTPlatform'),
    componentProps: {
      api: getCommonGetSelectList,
      params: {
        query: {
          typeName: 'IoTPlatformTypeEnum',
        },
      },
      labelField: 'value',
      valueField: 'key',
      optionsPropName: 'options',
      immediate: true,
      afterFetch: (res: any) => {
        // 确保返回的是数组格式
        if (Array.isArray(res)) {
          return res;
        }
        // 如果是包装在 items 中的，提取出来
        if (res && Array.isArray(res.items)) {
          return res.items;
        }
        // 如果是包装在 data 中的，提取出来
        if (res && Array.isArray(res.data)) {
          return res.data;
        }
        // 如果都不是，返回空数组
        return [];
      },
    },
    rules: z.string().min(1, {
      message: `${$t('common.pleaseSelect')}${$t('common.IoTPlatform')}`,
    }),
  },
  {
    component: 'ApiSelect',
    fieldName: 'ioTPlatformProductId',
    label: $t('abp.OneNETManagement.BelongingProductName'),
    componentProps: {
      api: postOneNetProductListAsync,
      params: {
        body: {
          pageIndex: 1,
          pageSize: 1000,
        },
      },
      labelField: 'label',
      valueField: 'ioTPlatformProductId',
      immediate: true,
      afterFetch: (res: any) => {
        // 如果是 Axios 响应对象，提取 data
        if (res && res.data) {
          const data = res.data;

          // 确保返回的是数组格式
          let items = [];
          if (Array.isArray(data)) {
            items = data;
          } else if (data && Array.isArray(data.items)) {
            items = data.items;
          } else if (data && Array.isArray(data.data)) {
            items = data.data;
          }

          // 为每个产品项添加组合标签
          return items.map((item: any) => ({
            ...item,
            label: `${item.productName || item.ProductName || ''} (${item.ioTPlatformProductId || ''})`,
          }));
        }

        // 如果都不是，返回空数组
        return [];
      },
      placeholder: `${$t('common.pleaseSelect')}${$t('abp.OneNETManagement.BelongingProductName')}`,
    },
    rules: z.string().min(1, {
      message: `${$t('common.pleaseSelect')}${$t('abp.OneNETManagement.BelongingProductName')}`,
    }),
  },
  {
    component: 'Input',
    fieldName: 'deviceAddress',
    label: $t('abp.deviceInfos.deviceAddress'),
    rules: z.string().min(1, {
      message: `${$t('common.pleaseInput')}${$t('abp.deviceInfos.deviceAddress')}`,
    }),
  },
]);

export const editDeviceFormSchemaEdit: any = computed(() => [
  {
    component: 'ApiSelect',
    fieldName: 'IoTPlatform',
    label: $t('common.IoTPlatform'),
    componentProps: {
      api: getCommonGetSelectList,
      params: {
        query: {
          typeName: 'IoTPlatformTypeEnum',
        },
      },
      labelField: 'value',
      valueField: 'key',
      optionsPropName: 'options',
      immediate: true,
      afterFetch: (res: any) => {
        // 确保返回的是数组格式
        if (Array.isArray(res)) {
          return res;
        }
        // 如果是包装在 items 中的，提取出来
        if (res && Array.isArray(res.items)) {
          return res.items;
        }
        // 如果是包装在 data 中的，提取出来
        if (res && Array.isArray(res.data)) {
          return res.data;
        }
        // 如果都不是，返回空数组
        return [];
      },
    },
  },
  {
    component: 'ApiSelect',
    fieldName: 'ioTPlatformProductId',
    label: $t('abp.OneNETManagement.BelongingProductName'),
    componentProps: {
      api: postOneNetProductListAsync,
      params: {
        body: {
          pageIndex: 1,
          pageSize: 1000,
        },
      },
      labelField: 'label',
      valueField: 'ioTPlatformProductId',
      immediate: true,
      afterFetch: (res: any) => {
        // 如果是 Axios 响应对象，提取 data
        if (res && res.data) {
          const data = res.data;

          // 确保返回的是数组格式
          let items = [];
          if (Array.isArray(data)) {
            items = data;
          } else if (data && Array.isArray(data.items)) {
            items = data.items;
          } else if (data && Array.isArray(data.data)) {
            items = data.data;
          }

          // 为每个产品项添加组合标签
          return items.map((item: any) => ({
            ...item,
            label: `${item.productName || item.ProductName || ''} (${item.ioTPlatformProductId || ''})`,
          }));
        }

        // 如果都不是，返回空数组
        return [];
      },
      placeholder: `${$t('common.pleaseSelect')}${$t('abp.OneNETManagement.BelongingProductName')}`,
    },
    rules: z.string().min(1, {
      message: `${$t('common.pleaseSelect')}${$t('abp.OneNETManagement.BelongingProductName')}`,
    }),
  },
  {
    component: 'Input',
    fieldName: 'deviceName',
    label: $t('abp.deviceInfos.deviceName'),
    rules: z.string().min(1, {
      message: `${$t('common.pleaseInput')}${$t('abp.deviceInfos.deviceName')}`,
    }),
  },
  {
    component: 'Input',
    fieldName: 'deviceAddress',
    label: $t('abp.deviceInfos.deviceAddress'),
    disabled: true,
  },
  {
    component: 'Input',
    fieldName: 'platformPassword',
    label: $t('abp.deviceInfos.platformPassword'),
    rules: z.string().min(1, {
      message: `${$t('common.pleaseInput')}${$t('common.info')}${$t('abp.deviceInfos.platformPassword')}`,
    }),
  },
]);
