import { h, ref } from 'vue';
import { Tag } from 'ant-design-vue';
import type { VxeGridProps } from '#/adapter/vxe-table';
import { getPopupContainer } from '@vben/utils';
import type { DescItem } from '#/components/description';
import { cloneDeep } from 'lodash-es';
import type { VbenFormSchema } from '@vben/common-ui';
import dayjs from 'dayjs';
import { z } from '@vben/common-ui';
import { renderDict, renderPopoverMemberSumma, type MemberSumma } from '#/utils';
import { DictEnum } from '@vben/constants';
import { getDictOptions } from '#/utils/dict';
import { List as TemplateList } from '#/api/iotlcm/tChannel/tSimChannelTemplate';

export class State {
  public id = 0; // ID
  public tempKey = ''; // 模板标识
  public chanKey = ''; // 通道编号
  public chanName = ''; // 通道名称
  public chanConfig = ''; // 通道配置
  public chanStatus = null; // 通道状态(0未启用,1启用)
  public chanAlias = ''; // 通道别名
  public regionCode = ''; // 通道行政区域代码(省市)
  public appKey = ''; // 应用标识
  public appSecret = ''; // 应用密钥
  public accessToken = ''; // 接口调用凭证
  public accessTokenCreated = ''; // 凭证申请时间
  public accessTokenExpire = ''; // 凭证过期时间
  public refreshToken = ''; // 刷新凭证
  public refreshTokenCreated = ''; // 刷新凭证申请时间
  public refreshTokenExpire = ''; // 刷新凭证过期时间
  public remark = ''; // 备注
  public loginUser = ''; // 登录账号
  public loginPassword = ''; // 登录密码
  public loginUrl = ''; // 登录地址
  public loginState = 0; // 登录状态(0未启用,1启用)
  public tenantId = ''; // 租户ID
  public createdDept = 0; // 创建部门
  public createdBy = 0; // 创建者
  public createdBySumma?: null | MemberSumma = null; // 创建者摘要信息
  public createdAt = ''; // 创建时间
  public updatedBy = 0; // 更新者
  public updatedBySumma?: null | MemberSumma = null; // 更新者摘要信息
  public updatedAt = ''; // 更新时间

  constructor(state?: Partial<State>) {
    if (state) {
      Object.assign(this, state);
    }
  }
}

export function newState(state: State | Record<string, any> | null): State {
  if (state !== null) {
    if (state instanceof State) {
      return cloneDeep(state);
    }
    return new State(state);
  }
  return new State();
}

// 表单验证规则

// 表格搜索表单
export const querySchema: VbenFormSchema[] = [
			{
				fieldName: 'chanKey',
				component: 'Input',
				label: '通道编号',
				componentProps: {
					placeholder: '请输入通道编号',
					onUpdateValue: (e: any) => {
						console.log(e);
					},   
				},  rules:null,
				formItemClass: 'col-span-1',
			},
			{
				fieldName: 'chanName',
				component: 'Input',
				label: '通道名称',
				componentProps: {
					placeholder: '请输入通道名称',
					onUpdateValue: (e: any) => {
						console.log(e);
					},   
				},  rules:null,
				formItemClass: 'col-span-1',
			}
			
			,  {    
			fieldName: 'chanStatus',    
			component: 'Select',    
			label: '通道状态',    
			defaultValue: null,    
			componentProps: {    
				placeholder: '请选择通道状态',    
				options: getDictOptions('sys_normal_disable', true),    
				onUpdateValue: (e: any) => {    
					console.log(e);    
				},  
			},
			rules:null,
			formItemClass: 'col-span-1',
		},];

// 表格列
export const columns: VxeGridProps['columns'] = [
  {
    title: 'ID',
    field: 'id',
    align: 'center',
    width: -1,
    type: 'checkbox',
  },
  {
    title: '通道编号',
    field: 'chanKey',
    align: 'left',
    width: -1,
 },
  {
    title: '通道名称',
    field: 'chanName',
    align: 'left',
    width: -1,
 },


  {
    title: '模板标识',
    field: 'tempName',
    align: 'left',
    width: -1,
 },
  {
    title: '通道配置',
    field: 'chanConfig',	
    align: 'left',
    width: -1,
 },
 
 {    
	title: '通道状态',    field: 'chanStatus',    align: 'left',    width: -1, 
	slots: {
		default: ({ row }) => {
			return renderDict(row.chanStatus, 'sys_normal_disable');
		}
	},
},
 {
    title: '通道行政区域代码(省市)',
    field: 'regionCode',
    align: 'left',
    width: -1,
 },

{
    title: '通道别名',
    field: 'chanAlias',
    align: 'left',
    width: -1,
 },
 
  {
    title: '应用标识',
    field: 'appKey',
    align: 'left',
    width: -1,
 },
  {
    title: '应用密钥',
    field: 'appSecret',
    align: 'left',
    width: -1,
 },
  {
    title: '接口调用凭证',
    field: 'accessToken',
    align: 'left',
    width: -1,
 },
  {
    title: '凭证申请时间',
    field: 'accessTokenCreated',
    align: 'left',
    width: -1,
 },
  {
    title: '凭证过期时间',
    field: 'accessTokenExpire',
    align: 'left',
    width: -1,
 },
  {
    title: '刷新凭证申请时间',
    field: 'refreshTokenCreated',
    align: 'left',
    width: -1,
 },
  {
    title: '刷新凭证过期时间',
    field: 'refreshTokenExpire',
    align: 'left',
    width: -1,
 },
  {
    title: '登录账号',
    field: 'loginUser',
    align: 'left',
    width: -1,
 },
  {
    title: '登录密码',
    field: 'loginPassword',
    align: 'left',
    width: -1,
 },
  {
    title: '登录状态',
    field: 'loginState',
    align: 'left',
    width: -1,
    slots: {
		default: ({ row }) => {
			return renderDict(row.loginState, 'sys_normal_disable');
		}
	},
 },
  { title: '操作', width: -1, slots: { default: 'action' } },
];

// 表格列接口
export interface RowType {
  id: number;
  tempKey: string;
  chanKey: string;
  chanName: string;
  chanConfig: string;
  chanStatus: number;
  chanAlias: string;
  regionCode: string;
  appKey: string;
  appSecret: string;
  accessToken: string;
  accessTokenCreated: string;
  accessTokenExpire: string;
  refreshToken: string;
  refreshTokenCreated: string;
  refreshTokenExpire: string;
  remark: string;
  loginUser: string;
  loginPassword: string;
  loginUrl: string;
  loginState: number;
  tenantId: string;
  createdDept: number;
  createdBy: number;
  createdAt: string;
  updatedBy: number;
  updatedAt: string;
};

// 查看字段列表
export const viewSchema: DescItem[] = [
  {  field: 'id',  label: 'ID'},
  {  field: 'tempKey',  label: '模板标识'},
  {  field: 'tempName',  label: '模板名称'},
  {  field: 'chanKey',  label: '通道编号'},
  {  field: 'chanName',  label: '通道名称'},
  {  field: 'chanConfig',  label: '通道配置'},
  {
		field: 'chanStatus',                                                                                                                                                                                                          
		label: '通道状态(0未启用,1启用)',
		render(chanStatus) {
			return renderDict(chanStatus, 'sys_normal_disable');
		},
	},
	{  field: 'chanAlias',  label: '通道别名'},
  {  field: 'regionCode',  label: '通道行政区域代码(省市)'},
  {  field: 'appKey',  label: '应用标识'},
  {  field: 'appSecret',  label: '应用密钥'},
  {  field: 'accessToken',  label: '接口调用凭证'},
  {  field: 'accessTokenCreated',  label: '凭证申请时间'},
  {  field: 'accessTokenExpire',  label: '凭证过期时间'},
  {  field: 'refreshToken',  label: '刷新凭证'},
  {  field: 'refreshTokenCreated',  label: '刷新凭证申请时间'},
  {  field: 'refreshTokenExpire',  label: '刷新凭证过期时间'},
  {  field: 'remark',  label: '备注'},
  {  field: 'loginUser',  label: '登录账号'},
  {  field: 'loginPassword',  label: '登录密码'},
  {  field: 'loginUrl',  label: '登录地址'},
  {
		field: 'loginState',
		label: '登录状态(0未启用,1启用)',
		render(loginState) {
			return renderDict(loginState, 'sys_normal_disable');
		},
	},
];

// 编辑字段列表
export const editSchema: VbenFormSchema[] = [
 {
					fieldName: 'id',
					component: 'Input',
					label: 'ID',
					dependencies: {   show: () => false,    triggerFields: [''],   },
					componentProps: {
						placeholder: '',
						onUpdateValue: (e: any) => {
							console.log(e);
						},   
					},
					formItemClass: 'col-span-1',
				},  {
			fieldName: 'tempKey',
			component: 'Select',
			label: '模板标识',
			componentProps: {
				placeholder: '请选择模板标识',
				options: [],
				onUpdateValue: (e: any) => {
					console.log(e);
				},   
			},  
			rules:'required',
			formItemClass: 'col-span-1',
			},  {
				fieldName: 'chanKey',
				component: 'Input',
				label: '通道编号',
				componentProps: {
					placeholder: '请输入通道编号',
					onUpdateValue: (e: any) => {
						console.log(e);
					},   
				},  
				rules:'required',
				formItemClass: 'col-span-1',
			},  {
				fieldName: 'chanName',
				component: 'Input',
				label: '通道名称',
				componentProps: {
					placeholder: '请输入通道名称',
					onUpdateValue: (e: any) => {
						console.log(e);
					},   
				},  
				rules:'required',
				formItemClass: 'col-span-1',
			},  {
				fieldName: 'chanConfig',
				component: 'Input',
				label: '通道配置',
				componentProps: {
					placeholder: '请输入通道配置',
					onUpdateValue: (e: any) => {
						console.log(e);
					},   
				},  
				rules:null,
				formItemClass: 'col-span-1',
			},  {    
			fieldName: 'chanStatus',    
			component: 'Select',    
			label: '通道状态(0未启用,1启用)',    
			defaultValue: null,    
			componentProps: {    
				placeholder: '请选择通道状态(0未启用,1启用)',    
				options: getDictOptions('sys_normal_disable', true),    
				onUpdateValue: (e: any) => {    
					console.log(e);    
				},  
			},
			rules:null,
			formItemClass: 'col-span-1',
		},
		  {
				fieldName: 'chanAlias',
				component: 'Input',
				label: '通道别名',
				componentProps: {
					placeholder: '请输入通道别名',
					onUpdateValue: (e: any) => {
						console.log(e);
					},   
				},  
				rules:'required',
				formItemClass: 'col-span-1',
			},  {
				fieldName: 'regionCode',
				component: 'Input',
				label: '通道行政区域代码(省市)',
				componentProps: {
					placeholder: '请输入通道行政区域代码(省市)',
					onUpdateValue: (e: any) => {
						console.log(e);
					},   
				},  
				rules:null,
				formItemClass: 'col-span-1',
			},  {
				fieldName: 'appKey',
				component: 'Input',
				label: '应用标识',
				componentProps: {
					placeholder: '请输入应用标识',
					onUpdateValue: (e: any) => {
						console.log(e);
					},   
				},  
				rules:'required',
				formItemClass: 'col-span-1',
			},  {
				fieldName: 'appSecret',
				component: 'Input',
				label: '应用密钥',
				componentProps: {
					placeholder: '请输入应用密钥',
					onUpdateValue: (e: any) => {
						console.log(e);
					},   
				},  
				rules:'required',
				formItemClass: 'col-span-1',
			},  {
				fieldName: 'remark',
				component: 'Input',
				label: '备注',
				componentProps: {
					placeholder: '请输入备注',
					onUpdateValue: (e: any) => {
						console.log(e);
					},   
				},  
				rules:null,
				formItemClass: 'col-span-1',
			},  {
				fieldName: 'loginUser',
				component: 'Input',
				label: '登录账号',
				componentProps: {
					placeholder: '请输入登录账号',
					onUpdateValue: (e: any) => {
						console.log(e);
					},   
				},  
				rules:null,
				formItemClass: 'col-span-1',
			},  {
				fieldName: 'loginPassword',
				component: 'Input',
				label: '登录密码',
				componentProps: {
					placeholder: '请输入登录密码',
					onUpdateValue: (e: any) => {
						console.log(e);
					},   
				},  
				rules:null,
				formItemClass: 'col-span-1',
			},  {
				fieldName: 'loginUrl',
				component: 'Input',
				label: '登录地址',
				componentProps: {
					placeholder: '请输入登录地址',
					onUpdateValue: (e: any) => {
						console.log(e);
					},   
				},  
				rules:'required',
				formItemClass: 'col-span-1',
			},  {
				fieldName: 'loginState',
				component: 'Select',
				label: '登录状态(0未启用,1启用)',
				componentProps: {
					placeholder: '请输入登录状态(0未启用,1启用)',
					options: getDictOptions('sys_normal_disable', true),
					onUpdateValue: (e: any) => {
						console.log(e);
					},   
				},  rules:null,
				formItemClass: 'col-span-1',
			},];


