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';

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
					},
					// 列设置按钮
					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,
				buttons: {
					search: {
						size: 'small', // 设置查询按钮大小为small
					},
					reset: {
						size: 'small', // 设置重置按钮大小为small
					}
				}
			},
			columns: {
				_index: {
					title: '序号',
					form: { show: false },
					column: {
						type: 'index',
						align: 'center',
						width: '70px',
						columnSetDisabled: true, //禁止在列设置中选择
					},
				},
				name: {
					title: '分类名称',
					search: {
						show: true,
						component: {
							placeholder: '请输入分类名称',
						},
						size: 'small',
						col:{ span:3},
					},
					type: 'input',
					column: {
						minWidth: 120,
					},
					form: {
						rules: [
							{ required: true, message: '分类名称为必填项' },
						],
					},
				},
				description: {
					title: '描述',
					search: {
						show: true,
						component: {
							placeholder: '请输入描述',
						},
						size: 'small',
						col:{ span:3},
					},
					type: 'textarea',
					column: {
						minWidth: 150,
						showOverflowTooltip: true,
					},
					form: {
						component: {
							span: 24,
							placeholder: '请输入标签描述',
						},
					},
				},
				/* icon: {
					title: '图标',
					type: 'icon-selector',
					column: {
						minWidth: 80,
						component: {
							name: 'fs-icon',
							props: {
								icon: (scope: any) => {
									return typeof scope.row.icon === 'string' ? scope.row.icon : '';
								}
							}
						}
					},
					form: {
						component: {
							placeholder: '请选择图标'
						}
					}
				}, */
				/* color: {
					title: '颜色',
					type: 'colorpicker',
					column: {
						minWidth: 100,
						component: {
							name: 'fs-color-block',
						},
					},
					form: {
						value: '#1890ff', // 默认颜色
					},
				}, */
				sort: {
					title: '排序',
					type: 'number',
					column: {
						minWidth: 80,
					},
					form: {
						value: 1, // 默认排序值
						component: {
							placeholder: '请输入排序值，数字越小越靠前'
						}
					},
				},
				/* 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, // 默认启用
					},
				},
				is_default: {
					title: '默认分类',
					type: 'dict-switch',
					column: {
						minWidth: 100,
						component: {
							name: 'fs-dict-switch',
						},
					},
					dict: dict({
						data: [
							{ value: true, label: '是' },
							{ value: false, label: '否' },
						],
					}),
					form: {
						value: false, // 默认非默认分类
					},
				}, */
				tags_count: {
					title: '标签数量',
					type: 'number',
					column: {
						minWidth: 100,
					},
					form: {
						show: false, // 表单中不显示，由系统自动计算
					},
				},
				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, // 表单中不显示，由系统自动更新
					},
				},
			},
		},
	};
};
