import * as api from './api';
import { dict, UserPageQuery, AddReq, DelReq, EditReq, compute, CreateCrudOptionsProps, CreateCrudOptionsRet } from '@fast-crud/fast-crud';
import { dictionary } from '/@/utils/dictionary';
import { successMessage } from '/@/utils/message';
import { auth } from '/@/utils/authFunction';
import tableSelector from '/@/components/tableSelector/index.vue';
import { shallowRef } from 'vue';
import { useRouter } from 'vue-router';
import { Session } from '/@/utils/storage';

export const createCrudOptions = function ({ crudExpose }: CreateCrudOptionsProps): CreateCrudOptionsRet {
	const router = useRouter();
	
	const pageRequest = async (query: UserPageQuery) => {
		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({id:row.id});
	};
	const addRequest = async ({ form }: AddReq) => {
		return await api.AddObj(form);
	};

	/**
	 * 懒加载
	 * @param row
	 * @returns {Promise<unknown>}
	 */
	const loadContentMethod = (tree: any, treeNode: any, resolve: Function) => {
		pageRequest({ pcode: tree.code }).then((res: APIResponseData) => {
			resolve(res.data);
		});
	};

	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
					},
				}
			},
			request: {
				pageRequest,
				addRequest,
				editRequest,
				delRequest,
			},
			actionbar: {
				buttons: {
					add: {
						show: true, // 允许添加标签类别
					},
				},
			},
			rowHandle: {
				//固定右侧
				fixed: 'right',
				width: 200,
				buttons: {
					view: {
						show: false, // 移除查看按钮，因为标签类别不需要详情页
					},
					edit: {
						iconRight: 'Edit',
						type: 'text',
						show: true, // 允许编辑标签类别
					},
					remove: {
						iconRight: 'Delete',
						type: 'text',
						show: true, // 允许删除标签类别
					},
				},
			},
			pagination: {
				show: true,
			},
			table: {
				rowKey: 'id',
			},
			search: {
				show: true,
				autoSearch:false,
				buttons: {
					search: {
						size: 'small', // 设置查询按钮大小为small
					},
					reset: {
						size: 'small', // 设置重置按钮大小为small
					}
				}
			},
			columns: {
				/* _index: {
					title: '序号',
					form: { show: false },
					column: {
						type: 'index',
						align: 'center',
						width: '70px',
						columnSetDisabled: true, //禁止在列设置中选择
					},
				}, */
				id: {
					title: 'ID',
					form: { show: false },
					column: {
						show: false,
					},
				},
				name: {
					title: '标签名称',
					search: {
						show: true,
						component: {
							placeholder: '请输入标签名称',
						},
						size: 'small',
						col:{ span:3},
					},
					type: 'input',
					column: {
						minWidth: 120,
					},
					form: {
						rules: [
							{ required: true, message: '标签名称为必填项' },
						],
					},
				},
				category_id: {
					title: '分类',
					search: {
						show: true,
						size: 'small',
						col:{ span:3},
					},
					type: 'dict-select',
					column: {
						minWidth: 120,
						show:false,
						// component: {
						// 	name: 'fs-dict-label',
						// },
					},
					dict: dict({
						getData: async () => {
							const res = await api.GetcategoryList({page:1,limit:100,tenant_id:Session.get('tenant_id')});
							console.log(res.data.items);
							return res.data.items || [];
						},
						label: 'name',
						value: 'id'
					}),
					form: {
						rules: [
							{ required: true, message: '分类为必填项' },
						],
					},
				},
				category_name: {
					title: '分类名称',
					column: {
						minWidth: 120,
						
					},
					form: {
						show: false, // 表单中不显示，由category_id自动关联
					},
				},
				/* color: {
					title: '标签颜色',
					type: 'colorpicker',
					column: {
						minWidth: 100,
						component: {
							name: 'fs-color-block',
						},
					},
					form: {
						value: '#52c41a', // 默认颜色
					},
				}, */
				sort: {
					title: '排序',
					type: 'number',
					column: {
						minWidth: 80,
					},
					form: {
						value: 1, // 默认排序值
					},
				},
				/* status: {
					title: '状态',
					type: 'dict-switch',
					column: {
						minWidth: 90,
						component: {
							name: 'fs-dict-switch',
						},
					},
					dict: dict({
						data: [
							{ value: true, label: '启用' },
							{ value: false, label: '禁用' },
						],
					}),
					form: {
						value: true, // 默认启用
					},
				}, */
				questions_count: {
					title: '问题数量',
					type: 'number',
					column: {
						minWidth: 100,
					},
					form: {
						show: false, // 表单中不显示，由系统自动计算
					},
				},
				create_datetime: {
					title: '创建时间',
					type: 'datetime',
					column: {
						minWidth: 160,
					},
					form: {
						show: false, // 表单中不显示，由系统自动生成
					},
				},
				update_datetime: {
					title: '更新时间',
					type: 'datetime',
					column: {
						minWidth: 160,
					},
					form: {
						show: false, // 表单中不显示，由系统自动更新
					},
				},
				description: {
					title: '描述',
					search: {
						show: false,
						component: {
							placeholder: '请输入描述',
						},
					},
					type: 'textarea',
					column: {
						minWidth: 150,
						showOverflowTooltip: true,
					},
					form: {
						component: {
							span: 24,
							placeholder: '请输入标签描述',
						},
					},
				},
			},
		},
	};
};
