import * as api from './api';
import { dict, UserPageQuery, AddReq, DelReq, EditReq, compute, CreateCrudOptionsProps, CreateCrudOptionsRet } from '@fast-crud/fast-crud';
import * as companyApi from '../company/api';
import { dictionary } from '/@/utils/dictionary';
import { successMessage } from '/@/utils/message';
import { auth } from '/@/utils/authFunction';
import { Md5 } from 'ts-md5';

export const createCrudOptions = function ({ crudExpose }: CreateCrudOptionsProps): CreateCrudOptionsRet {
	const pageRequest = async (query: any) => {
		console.log(query)
		return await api.GetList(query);
	};
	const editRequest = async ({ form, row }: EditReq) => {
		form.id = row.id;
		return await api.UpdateObj(form);
	};
	const delRequest = async ({ row }: DelReq) => {
		return await api.DelObj(row.id);
	};
	const addRequest = async ({ form }: AddReq) => {
		return await api.AddObj(form);
	};

	return {
		crudOptions: {
			toolbar:{
				buttons:{
					search:{show:false},
					// 刷新按钮
					refresh:{show:false},
					// 紧凑模式
					compact:{show:false},
					// 导出按钮
					export:{
						text: '导出',
						type: 'primary',
						size: 'small',
						icon: 'upload',
						circle: false,
						display: true,
						show:false
					},
					// 列设置按钮
					columns:{
						show:false
					},
				}
			},
			search: {
				col: { span: 3 },
				show: true,
				autoSearch:false,
				buttons: {
					search: {
						size: 'small', // 设置查询按钮大小为small
					},
					reset: {
						size: 'small', // 设置重置按钮大小为small
					}
				}
			},
			request: {
				pageRequest,
				addRequest,
				editRequest,
				delRequest,
			},
			actionbar: {
				buttons: {
					add: {
						show: true,//auth('account:Create'),
					},
				},
			},
			rowHandle: {
				fixed: 'right',
				width: 230,
				buttons: {
					view: { show: true,size:'small' },
					edit: { type: 'primary', show: true,size:'small' },//auth('account:Update')
					remove: {type: 'danger', show: true,size:'small' },//auth('account:Delete')
				},
			},
			columns: {
				_index: {
					title: '序号',
					form: { show: false },
					column: { type: 'index', align: 'center', width: '70px', columnSetDisabled: true },
				},
				username: {
					title: '用户名',
					type: 'text',
					search: { show: true },
					form: {
						component: { placeholder: '请输入用户名' },
						rules: [{ required: true, message: '请输入用户名' }]
					},
					column: { minWidth: 120 },
				},
				password: {
					title: '密码',
					type: 'password',
					search: { show: false },
					column: { show: false },
					addForm: {
						component: { placeholder: '请输入密码' },
						rules: [{ required: true, message: '请输入密码' },
							{
								validator: (rule: any, value: string, callback: Function) => {
									if (!value) {
										callback();
										return;
									}
									// 检查密码长度不少于8位
									if (value.length < 8) {
										callback(new Error('密码长度不能少于8位'));
										return;
									}
									// 检查是否包含字母
									const hasLetter = /[a-zA-Z]/.test(value);
									// 检查是否包含数字
									const hasNumber = /[0-9]/.test(value);
									
									if (!hasLetter || !hasNumber) {
										callback(new Error('密码必须包含字母和数字'));
										return;
									}
									callback();
								},
								trigger: 'blur'
							}
						],
						show: true,
						column: { show: false }
					},
					valueResolve({ form }) {
                        if (form.password) {
                            form.password = Md5.hashStr(form.password)
                        }
                    }
				},
				name: {
					title: '姓名',
					type: 'text',
					search: { show: false },
					form: {
						component: { placeholder: '请输入姓名' },
						rules: [{ required: true, message: '请输入姓名' }]
					},
					column: { minWidth: 120 },
				},
				mobile: {
					title: '手机号',
					type: 'text',
					search: { show: true },
					form: {
						component: { placeholder: '请输入手机号' },
						rules: [{ required: true, message: '请输入手机号' }]
					},
					column: { minWidth: 120 },
				},
				email: {
					title: '邮箱',
					type: 'text',
					search: { show: true },
					form: {
						component: { placeholder: '请输入邮箱' },
						rules: [{ required: true, message: '请输入邮箱' }]
					},
					column: { minWidth: 180 },
				},
				tenant_id: {
					title: '商户ID',
					search: { show: true },
					type: 'dict-select',
					dict: dict({
						url: '/api/platform-admin/tenants/',
						value: 'id',
						label: 'name',
						/* getData: (url) => {
							return companyApi.GetList({ page: 1, limit: 20 }).then((res:any) => {
								return res.data.results;
							});
						} */
					}),
				},
				is_active: {
					title: '账号状态',
					search: { show: true },
					type: 'dict-select',
					form: { show:true, component: { placeholder: '请选择账号状态' } },
					column: { minWidth: 120 },
					dict: dict({
						data: [
							{ value: true, label: '启用' },
							{ value: false, label: '禁用' }
						]
					}),
				},
				create_datetime: {
					title: '开通时间',
					type: 'datetime',
					search: { show: false },
					form: { component: { placeholder: '请选择开通时间' },show:false },
					column: { minWidth: 150 },
				},
				account_expiry_days: {
					title: '截至天数',
					type: 'number',
					search: { show: false },
					form: { 
						component: { 
							placeholder: '请输入截至天数',
							min: 1,
							max: 365
						}, 
						show: true 
					},
					column: { minWidth: 150,show:false },
				},
				account_expiry:{
					title: '账号到期时间',
					type: 'datetime',
					search: { show: false },
					form: { component: { placeholder: '请选择账号到期时间' },show:false  },
					column: { minWidth: 150 },
				},
				last_login: {
					title: '最后登录时间',
					type: 'datetime',
					search: { show: false },
					form: { component: { placeholder: '请选择最后登录时间' },show:false  },
					column: { minWidth: 150 },
				},
				
			},
		},
	};
}; 