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

import { computed } from 'vue';

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

import dayjs from 'dayjs';

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

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

export const tableSchema: any = computed((): VxeGridProps['columns'] => [
  { title: $t('common.seq'), type: 'seq', width: 50 },
  {
    field: 'ioTPlatformName',
    title: $t('common.BelongingIoTPlatform'),
    minWidth: '150',
    slots: { default: 'ioTPlatformName' },
  },
  {
    field: 'accountPhoneNumber',
    title: $t('common.BelongingAccountName'),
    minWidth: '150',
  },
  {
    field: 'ioTPlatformProductName',
    title: $t('common.BelongingProductName'),
    minWidth: '150',
  },
  {
    field: 'deviceName',
    title: $t('abp.deviceInfos.deviceName'),
    minWidth: '150',
  },
  {
    field: 'deviceAddress',
    title: $t('abp.deviceInfos.deviceAddress'),
    minWidth: '150',
  },
  {
    field: 'ioTPlatformDeviceOpenInfo',
    title: $t('abp.deviceInfos.ioTPlatformDeviceOpenInfo'),
    minWidth: '180',
  },
  {
    field: 'deviceOnlineStatusName',
    title: $t('abp.deviceInfos.DeviceOnlineStatus'),
    minWidth: '150',
  },
  {
    field: 'lastOnlineTime',
    title: $t('abp.deviceInfos.LastOnlineTime'),
    minWidth: '150',
    formatter: ({ cellValue }) => {
      return cellValue ? dayjs(cellValue).format('YYYY-MM-DD HH:mm:ss') : '';
    },
  },
  {
    field: 'lastOfflineTime',
    title: $t('abp.deviceInfos.LastOfflineTime'),
    minWidth: '150',
    formatter: ({ cellValue }) => {
      return cellValue ? dayjs(cellValue).format('YYYY-MM-DD HH:mm:ss') : '';
    },
  },
  {
    field: 'platformPassword',
    title: $t('abp.deviceInfos.platformPassword'),
    minWidth: '150',
  },
  {
    field: 'ioTPlatformResponse',
    title: $t('abp.deviceInfos.ioTPlatformResponse'),
    minWidth: '150',
  },
  {
    title: $t('common.action'),
    field: 'action',
    fixed: 'right',
    width: '180',
    slots: { default: 'action' },
  },
]);

export const addDeviceFormSchema: any = computed(() => [
  {
    component: 'Input',
    fieldName: 'deviceAddress',
    label: $t('abp.deviceInfos.deviceAddress'),
    rules: z.string().min(1, {
      message: `${$t('common.pleaseInput')}${$t('abp.deviceInfos.deviceAddress')}`,
    }),
  },
  {
    component: 'ApiSelect',
    fieldName: 'ioTPlatform',
    label: $t('abp.deviceInfos.ioTPlatform'),
    componentProps: {
      api: getCommonGetSelectList,
      params: {
        query: {
          typeName: 'IoTPlatformTypeEnum',
        },
      },
      labelField: 'value',
      valueField: 'key',
      optionsPropName: 'options',
      immediate: true,
      allowClear: true,
      placeholder:
        $t('common.pleaseSelect') + $t('abp.deviceInfos.ioTPlatform'),
      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('abp.deviceInfos.ioTPlatform')}`,
    }),
  },
  {
    component: 'ApiSelect',
    fieldName: 'oneNETAccountId',
    label: $t('abp.deviceInfos.ioTPlatformAccountName'),
    dependencies: {
      show(values: any) {
        return values.ioTPlatform === 2 || values.ioTPlatform === '2'; // OneNET平台
      },
      rules(values: any) {
        if (values.ioTPlatform === 2 || values.ioTPlatform === '2') {
          return 'required';
        }
        return null;
      },
      triggerFields: ['ioTPlatform'],
    },
    componentProps: {
      api: postOneNetAccountListAsync,
      params: {
        body: {
          pageIndex: 1,
          pageSize: 1000,
        },
      },
      labelField: 'accountName',
      valueField: 'oneNETAccountId',
      immediate: true,
      afterFetch: (res: any) => {
        // 如果是 Axios 响应对象，提取 data
        if (res && res.data) {
          const data = res.data;

          // 确保返回的是数组格式
          if (Array.isArray(data)) {
            return data;
          }
          // 如果是包装在 items 中的，提取出来
          if (data && Array.isArray(data.items)) {
            return data.items;
          }
          // 如果是包装在 data 中的，提取出来
          if (data && Array.isArray(data.data)) {
            return data.data;
          }
        }

        // 如果都不是，返回空数组
        return [];
      },
      placeholder: `${$t('common.pleaseSelect')}${$t('abp.deviceInfos.ioTPlatformAccountName')}`,
    },
    rules: z.string().optional(),
  },
  {
    component: 'ApiSelect',
    fieldName: 'ctWingAccountId',
    label: $t('abp.deviceInfos.ioTPlatformAccountName'),
    dependencies: {
      show(values: any) {
        return values.ioTPlatform === 1 || values.ioTPlatform === '1'; // CTWing平台
      },
      rules(values: any) {
        if (values.ioTPlatform === 1 || values.ioTPlatform === '1') {
          return 'required';
        }
        return null;
      },
      triggerFields: ['ioTPlatform'], // 添加这一行，使其能够响应平台切换
    },
    componentProps: {
      api: postCtWingAccountListAsync,
      params: {
        body: {
          pageIndex: 1,
          pageSize: 1000,
        },
      },
      labelField: 'accountName',
      valueField: 'accountId',
      immediate: true,
      afterFetch: (res: any) => {
        // 如果是 Axios 响应对象，提取 data
        if (res && res.data) {
          const data = res.data;

          // 确保返回的是数组格式
          if (Array.isArray(data)) {
            return data;
          }
          // 如果是包装在 items 中的，提取出来
          if (data && Array.isArray(data.items)) {
            return data.items;
          }
          // 如果是包装在 data 中的，提取出来
          if (data && Array.isArray(data.data)) {
            return data.data;
          }
        }

        // 如果都不是，返回空数组
        return [];
      },
      placeholder: `${$t('common.pleaseSelect')}${$t('abp.deviceInfos.ioTPlatformAccountName')}`,
    },
    rules: z.string().optional(),
  },
  {
    component: 'ApiSelect',
    fieldName: 'oneNETProductId',
    label: $t('abp.deviceInfos.ioTPlatformProductName'),
    dependencies: {
      show(values: any) {
        return (
          (values.ioTPlatform === 2 || values.ioTPlatform === '2') &&
          values.oneNETAccountId
        ); // OneNET平台且已选择账号
      },
      rules(values: any) {
        if (
          (values.ioTPlatform === 2 || values.ioTPlatform === '2') &&
          values.oneNETAccountId
        ) {
          return 'required';
        }
        return null;
      },
      triggerFields: ['ioTPlatform', 'oneNETAccountId'],
    },
    componentProps: {
      api: postOneNetProductListAsync,
      params: (formValues: any) => ({
        body: {
          pageIndex: 1,
          pageSize: 1000,
          oneNETAccountId: formValues.oneNETAccountId,
        },
      }),
      labelField: 'productName',
      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.ioTPlatformProductId || ''})`,
          }));
        }

        // 如果都不是，返回空数组
        return [];
      },
      placeholder: `${$t('common.pleaseSelect')}${$t('abp.deviceInfos.ioTPlatformProductName')}`,
    },
    rules: z.string().optional(),
  },
  {
    component: 'ApiSelect',
    fieldName: 'ctWingProductId',
    label: $t('abp.deviceInfos.ioTPlatformProductName'),
    dependencies: {
      show(values: any) {
        return (
          (values.ioTPlatform === 1 || values.ioTPlatform === '1') &&
          values.ctWingAccountId
        ); // CTWing平台且已选择账号
      },
      rules(values: any) {
        if (
          (values.ioTPlatform === 1 || values.ioTPlatform === '1') &&
          values.ctWingAccountId
        ) {
          return 'required';
        }
        return null;
      },
      triggerFields: ['ioTPlatform', 'ctWingAccountId'],
    },
    componentProps: {
      api: postCtWingProductListAsync,
      params: (formValues: any) => ({
        body: {
          pageIndex: 1,
          pageSize: 1000,
          ctWingAccountId: formValues.ctWingAccountId,
        },
      }),
      labelField: 'productName',
      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;
          }

          // 为每个产品项添加组合标签，并确保产品ID是字符串类型
          return items.map((item: any) => ({
            ...item,
            ioTPlatformProductId: String(item.ioTPlatformProductId || ''),
            label: `${item.productName || ''} (${item.ioTPlatformProductId || ''})`,
          }));
        }

        // 如果都不是，返回空数组
        return [];
      },
      placeholder: `${$t('common.pleaseSelect')}${$t('abp.deviceInfos.ioTPlatformProductName')}`,
    },
    rules: z.string().optional(),
  },
]);

export const editDeviceFormSchemaEdit: any = computed(() => [
  {
    component: 'ApiSelect',
    fieldName: 'ioTPlatform',
    label: $t('abp.deviceInfos.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('abp.deviceInfos.ioTPlatform')}`,
    }),
  },
  {
    component: 'ApiSelect',
    fieldName: 'oneNETAccountId',
    label: $t('abp.deviceInfos.ioTPlatformAccountName'),
    dependencies: {
      show(values: any) {
        return values.ioTPlatform === 2 || values.ioTPlatform === '2'; // OneNET平台
      },
      rules(values: any) {
        if (values.ioTPlatform === 2 || values.ioTPlatform === '2') {
          return 'required';
        }
        return null;
      },
      triggerFields: ['ioTPlatform'],
    },
    componentProps: {
      api: postOneNetAccountListAsync,
      params: {
        body: {
          pageIndex: 1,
          pageSize: 1000,
        },
      },
      labelField: 'accountName',
      valueField: 'oneNETAccountId',
      immediate: true,
      afterFetch: (res: any) => {
        // 如果是 Axios 响应对象，提取 data
        if (res && res.data) {
          const data = res.data;

          // 确保返回的是数组格式
          if (Array.isArray(data)) {
            return data;
          }
          // 如果是包装在 items 中的，提取出来
          if (data && Array.isArray(data.items)) {
            return data.items;
          }
          // 如果是包装在 data 中的，提取出来
          if (data && Array.isArray(data.data)) {
            return data.data;
          }
        }

        // 如果都不是，返回空数组
        return [];
      },
      placeholder: `${$t('common.pleaseSelect')}${$t('abp.deviceInfos.ioTPlatformAccountName')}`,
    },
    rules: z.string().optional(),
  },
  {
    component: 'ApiSelect',
    fieldName: 'ctWingAccountId',
    label: $t('abp.deviceInfos.ioTPlatformAccountName'),
    dependencies: {
      show(values: any) {
        return values.ioTPlatform === 1 || values.ioTPlatform === '1'; // CTWing平台
      },
      rules(values: any) {
        if (values.ioTPlatform === 1 || values.ioTPlatform === '1') {
          return 'required';
        }
        return null;
      },
      triggerFields: ['ioTPlatform'],
    },
    componentProps: {
      api: postCtWingAccountListAsync,
      params: {
        body: {
          pageIndex: 1,
          pageSize: 1000,
        },
      },
      labelField: 'accountName',
      valueField: 'ctWingAccountId',
      immediate: true,
      afterFetch: (res: any) => {
        // 如果是 Axios 响应对象，提取 data
        if (res && res.data) {
          const data = res.data;

          // 确保返回的是数组格式
          if (Array.isArray(data)) {
            return data;
          }
          // 如果是包装在 items 中的，提取出来
          if (data && Array.isArray(data.items)) {
            return data.items;
          }
          // 如果是包装在 data 中的，提取出来
          if (data && Array.isArray(data.data)) {
            return data.data;
          }
        }

        // 如果都不是，返回空数组
        return [];
      },
      placeholder: `${$t('common.pleaseSelect')}${$t('abp.deviceInfos.ioTPlatformAccountName')}`,
    },
    rules: z.string().optional(),
  },
  {
    component: 'ApiSelect',
    fieldName: 'oneNETProductId',
    label: $t('abp.deviceInfos.ioTPlatformProductName'),
    dependencies: {
      show(values: any) {
        return (
          (values.ioTPlatform === 2 || values.ioTPlatform === '2') &&
          values.oneNETAccountId
        ); // OneNET平台且已选择账号
      },
      rules(values: any) {
        if (
          (values.ioTPlatform === 2 || values.ioTPlatform === '2') &&
          values.oneNETAccountId
        ) {
          return 'required';
        }
        return null;
      },
      triggerFields: ['ioTPlatform', 'oneNETAccountId'],
    },
    componentProps: {
      api: postOneNetProductListAsync,
      params: (formValues: any) => ({
        body: {
          pageIndex: 1,
          pageSize: 1000,
          oneNETAccountId: formValues.oneNETAccountId,
        },
      }),
      labelField: 'productName',
      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.ioTPlatformProductId || ''})`,
          }));
        }

        // 如果都不是，返回空数组
        return [];
      },
      placeholder: `${$t('common.pleaseSelect')}${$t('abp.deviceInfos.ioTPlatformProductName')}`,
    },
    rules: z.string().optional(),
  },
  {
    component: 'ApiSelect',
    fieldName: 'ctWingProductId',
    label: $t('abp.deviceInfos.ioTPlatformProductName'),
    dependencies: {
      show(values: any) {
        return (
          (values.ioTPlatform === 1 || values.ioTPlatform === '1') &&
          values.ctWingAccountId
        ); // CTWing平台且已选择账号
      },
      rules(values: any) {
        if (
          (values.ioTPlatform === 1 || values.ioTPlatform === '1') &&
          values.ctWingAccountId
        ) {
          return 'required';
        }
        return null;
      },
      triggerFields: ['ioTPlatform', 'ctWingAccountId'],
    },
    componentProps: {
      api: postCtWingProductListAsync,
      params: (formValues: any) => ({
        body: {
          pageIndex: 1,
          pageSize: 1000,
          ctWingAccountId: formValues.ctWingAccountId,
        },
      }),
      labelField: 'productName',
      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;
          }

          // 为每个产品项添加组合标签，并确保产品ID是字符串类型
          return items.map((item: any) => ({
            ...item,
            ioTPlatformProductId: String(item.ioTPlatformProductId || ''),
            label: `${item.productName || ''} (${item.ioTPlatformProductId || ''})`,
          }));
        }

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

export const commandFormSchema: any = computed(() => [
  {
    component: 'Textarea',
    fieldName: 'CommandContent',
    label: $t('abp.IoTDBBase.Command'),
    componentProps: {
      rows: 8,
      placeholder: $t('common.pleaseInput') + $t('abp.IoTDBBase.Command'),
      showCount: true,
      maxLength: 1000,
    },
    rules: z.string().min(1, {
      message: `${$t('common.pleaseInput')}${$t('abp.IoTDBBase.Command')}`,
    }),
  },
]);

export const batchAddDeviceFormSchema: any = computed(() => [
  {
    component: 'Textarea',
    fieldName: 'addressList',
    label: $t('abp.deviceInfos.deviceAddress'),
    componentProps: {
      rows: 4,
      placeholder: `${
        $t('common.pleaseInput') + $t('abp.deviceInfos.deviceAddress')
      }，每行一个设备地址`,
      showCount: false,
      maxLength: 10_000,
      style: {
        resize: 'vertical',
        minHeight: '32px',
        maxHeight: '200px',
      },
    },
    rules: z.string().min(1, {
      message: `${$t('common.pleaseInput')}${$t('abp.deviceInfos.deviceAddress')}`,
    }),
  },
  {
    component: 'ApiSelect',
    fieldName: 'ioTPlatform',
    label: $t('abp.deviceInfos.ioTPlatform'),
    componentProps: {
      api: getCommonGetSelectList,
      params: {
        query: {
          typeName: 'IoTPlatformTypeEnum',
        },
      },
      labelField: 'value',
      valueField: 'key',
      optionsPropName: 'options',
      immediate: true,
      allowClear: true,
      placeholder:
        $t('common.pleaseSelect') + $t('abp.deviceInfos.ioTPlatform'),
      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('abp.deviceInfos.ioTPlatform')}`,
    }),
  },
  {
    component: 'ApiSelect',
    fieldName: 'oneNETAccountId',
    label: $t('abp.deviceInfos.ioTPlatformAccountName'),
    dependencies: {
      show(values: any) {
        return values.ioTPlatform === 2 || values.ioTPlatform === '2'; // OneNET平台
      },
      rules(values: any) {
        if (values.ioTPlatform === 2 || values.ioTPlatform === '2') {
          return 'required';
        }
        return null;
      },
      triggerFields: ['ioTPlatform'],
    },
    componentProps: {
      api: postOneNetAccountListAsync,
      params: {
        body: {
          pageIndex: 1,
          pageSize: 1000,
        },
      },
      labelField: 'accountName',
      valueField: 'oneNETAccountId',
      immediate: true,
      afterFetch: (res: any) => {
        // 如果是 Axios 响应对象，提取 data
        if (res && res.data) {
          const data = res.data;

          // 确保返回的是数组格式
          if (Array.isArray(data)) {
            return data;
          }
          // 如果是包装在 items 中的，提取出来
          if (data && Array.isArray(data.items)) {
            return data.items;
          }
          // 如果是包装在 data 中的，提取出来
          if (data && Array.isArray(data.data)) {
            return data.data;
          }
        }

        // 如果都不是，返回空数组
        return [];
      },
      placeholder: `${$t('common.pleaseSelect')}${$t('abp.deviceInfos.ioTPlatformAccountName')}`,
    },
    rules: z.string().optional(),
  },
  {
    component: 'ApiSelect',
    fieldName: 'ctWingAccountId',
    label: $t('abp.deviceInfos.ioTPlatformAccountName'),
    dependencies: {
      show(values: any) {
        return values.ioTPlatform === 1 || values.ioTPlatform === '1'; // CTWing平台
      },
      rules(values: any) {
        if (values.ioTPlatform === 1 || values.ioTPlatform === '1') {
          return 'required';
        }
        return null;
      },
      triggerFields: ['ioTPlatform'], // 添加这一行，使其能够响应平台切换
    },
    componentProps: {
      api: postCtWingAccountListAsync,
      params: {
        body: {
          pageIndex: 1,
          pageSize: 1000,
        },
      },
      labelField: 'accountName',
      valueField: 'accountId',
      immediate: true,
      afterFetch: (res: any) => {
        // 如果是 Axios 响应对象，提取 data
        if (res && res.data) {
          const data = res.data;

          // 确保返回的是数组格式
          if (Array.isArray(data)) {
            return data;
          }
          // 如果是包装在 items 中的，提取出来
          if (data && Array.isArray(data.items)) {
            return data.items;
          }
          // 如果是包装在 data 中的，提取出来
          if (data && Array.isArray(data.data)) {
            return data.data;
          }
        }

        // 如果都不是，返回空数组
        return [];
      },
      placeholder: `${$t('common.pleaseSelect')}${$t('abp.deviceInfos.ioTPlatformAccountName')}`,
    },
    rules: z.string().optional(),
  },
  {
    component: 'ApiSelect',
    fieldName: 'oneNETProductId',
    label: $t('abp.deviceInfos.ioTPlatformProductName'),
    dependencies: {
      show(values: any) {
        return (
          (values.ioTPlatform === 2 || values.ioTPlatform === '2') &&
          values.oneNETAccountId
        ); // OneNET平台且已选择账号
      },
      rules(values: any) {
        if (
          (values.ioTPlatform === 2 || values.ioTPlatform === '2') &&
          values.oneNETAccountId
        ) {
          return 'required';
        }
        return null;
      },
      triggerFields: ['ioTPlatform', 'oneNETAccountId'],
    },
    componentProps: {
      api: postOneNetProductListAsync,
      params: (formValues: any) => ({
        body: {
          pageIndex: 1,
          pageSize: 1000,
          oneNETAccountId: formValues.oneNETAccountId,
        },
      }),
      labelField: 'productName',
      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.ioTPlatformProductId || ''})`,
          }));
        }

        // 如果都不是，返回空数组
        return [];
      },
      placeholder: `${$t('common.pleaseSelect')}${$t('abp.deviceInfos.ioTPlatformProductName')}`,
    },
    rules: z.string().optional(),
  },
  {
    component: 'ApiSelect',
    fieldName: 'ctWingProductId',
    label: $t('abp.deviceInfos.ioTPlatformProductName'),
    dependencies: {
      show(values: any) {
        return (
          (values.ioTPlatform === 1 || values.ioTPlatform === '1') &&
          values.ctWingAccountId
        ); // CTWing平台且已选择账号
      },
      rules(values: any) {
        if (
          (values.ioTPlatform === 1 || values.ioTPlatform === '1') &&
          values.ctWingAccountId
        ) {
          return 'required';
        }
        return null;
      },
      triggerFields: ['ioTPlatform', 'ctWingAccountId'],
    },
    componentProps: {
      api: postCtWingProductListAsync,
      params: (formValues: any) => ({
        body: {
          pageIndex: 1,
          pageSize: 1000,
          ctWingAccountId: formValues.ctWingAccountId,
        },
      }),
      labelField: 'productName',
      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;
          }

          // 为每个产品项添加组合标签，并确保产品ID是字符串类型
          return items.map((item: any) => ({
            ...item,
            ioTPlatformProductId: String(item.ioTPlatformProductId || ''),
            label: `${item.productName || ''} (${item.ioTPlatformProductId || ''})`,
          }));
        }

        // 如果都不是，返回空数组
        return [];
      },
      placeholder: `${$t('common.pleaseSelect')}${$t('abp.deviceInfos.ioTPlatformProductName')}`,
    },
    rules: z.string().optional(),
  },
]);
