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: 200,
				buttons: {
					view: { iconRight: 'View', type: 'text',show: true },
					edit: { iconRight: 'Edit', type: 'text', show: false },//auth('account:Update')
					remove: { iconRight: 'Delete', type: 'text', show: false },//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 },
					form: {
						component: { placeholder: '请输入密码' },
						rules: [{ required: true, message: '请输入密码' }],
						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/tenants/',
						value: 'tenant_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:false, component: { placeholder: '请选择账号状态' } },
					column: { minWidth: 120 },
					dict: dict({
						data: [
							{ value: true, label: '启用' },
							{ value: false, label: '禁用' }
						]
					}),
				},
				date_joined: {
					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 },
				},
				permission_level: {
					title: '权限等级',
					search: { show: false },
					type: 'dict-select',
					form: { component: { placeholder: '请选择权限等级' },show:false  },
					column: { minWidth: 120 },
					dict: dict({
						data: [
							{ value: 'admin', label: '管理员' },
							{ value: 'user', label: '普通用户' },
							{ value: 'guest', label: '访客' }
						]
					}),
				},
				/* job_count: {
					title: '岗位数量',
					type: 'number',
					column: { minWidth: 100 },
				},
				report_count: {
					title: '报告数量',
					type: 'number',
					column: { minWidth: 100 },
				}, */
			},
		},
	};
}; 