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(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: {
			request: {
				pageRequest,
				addRequest,
				editRequest,
				delRequest,
			},
			actionbar: {
				buttons: {
					add: {
						show: false,//auth('area:Create'),
					},
				},
			},
			rowHandle: {
				//固定右侧
				fixed: 'right',
				width: 200,
				buttons: {
					view: {
						iconRight: 'view',
						show: true,
						type: 'text',
						click: ({ row }) => {
							// 在新窗口中打开报告详情页面，确保使用正确的哈希路由格式
							const baseUrl = window.location.origin;
							const url = `${baseUrl}/#/report?id=${row.id}`;
							window.open(url, '_blank');
						}
					},
					edit: {
						iconRight: 'Edit',
						type: 'text',
						show: false,//auth('area:Update'),
					},
					remove: {
						iconRight: 'Delete',
						type: 'text',
						show: false,//auth('area:Delete'),
					},
				},
			},
			pagination: {
				show: true,
			},
			table: {
				rowKey: 'id',
			},
			search: {
				show: true,
				layout: 'auto',
			},
			columns: {
				_index: {
					title: '序号',
					form: { show: false },
					column: {
						type: 'index',
						align: 'center',
						width: '70px',
						columnSetDisabled: true, //禁止在列设置中选择
					},
				},
				report_id: {
					title: '报告编号',
					search: {
						show: true,
						component: {
							placeholder: '请输入报告编号',
						},
					},
					type: 'input',
					column: {
						minWidth: 150,
					},
					form: {
						rules: [
							{ required: true, message: '报告编号必填项' },
						],
					},
				},
				applicant_name: {
					title: '申请人姓名',
					search: {
						show: true,
						component: {
							placeholder: '请输入申请人姓名',
						},
					},
					type: 'input',
					column: {
						minWidth: 120,
					},
					form: {
						rules: [
							{ required: true, message: '申请人姓名必填项' },
						],
					},
				},
				applicant_phone: {
					title: '申请人电话',
					search: {
						show: true,
						component: {
							placeholder: '请输入申请人电话',
						},
					},
					type: 'input',
					column: {
						minWidth: 130,
					},
					form: {
						rules: [
							{ required: true, message: '申请人电话必填项' },
						],
					},
				},
				position_title: {
					title: '申请职位',
					search: {
						show: true,
						component: {
							placeholder: '请输入申请职位',
						},
					},
					type: 'input',
					column: {
						minWidth: 120,
					},
					form: {
						rules: [
							{ required: true, message: '申请职位必填项' },
						],
					},
				},
				status: {
					title: '申请状态',
					search: {
						show: true,
						component: {
							name: 'el-select',
							placeholder: '请选择申请状态',
						},
					},
					type: 'dict-select',
					column: {
						minWidth: 100,
					},
					dict: dict({
						data: [
							{ value: 1, label: '待面试' },
							{ value: 2, label: '已面试' },
							{ value: 3, label: '已录用' },
							{ value: 4, label: '已拒绝' },
						],
					}),
				},
				comprehensive_score: {
					title: '综合评分',
					type: 'number',
					column: {
						minWidth: 100,
					},
				},
				hire_recommendation: {
					title: '录用建议',
					type: 'textarea',
					column: {
						minWidth: 150,
						showOverflowTooltip: true,
					},
				},
				/* resume_url: {
					title: '简历链接',
					type: 'link',
					column: {
						minWidth: 100,
						component: {
							name: 'fs-button',
							type: 'primary',
							link: true,
							text: '查看简历',
							click: compute(({ row }) => {
								return () => {
									if (row.resume_url) {
										window.open(row.resume_url);
									}
								};
							}),
						},
						show: compute(({ row }) => {
							return !!row.resume_url;
						}),
					},
				}, */
				create_datetime: {
					title: '创建时间',
					type: 'datetime',
					column: {
						minWidth: 160,
					},
					form: {
						show: false,
					},
				},
				update_datetime: {
					title: '更新时间',
					type: 'datetime',
					column: {
						minWidth: 160,
					},
					form: {
						show: false,
					},
				},
			},
		},
	};
};
