import { 
	AddReq, 
	CreateCrudOptionsProps, 
	CreateCrudOptionsRet, 
	DelReq,
	dict, 
	EditReq,
	FormWrapperContext,
	ScopeContext,
	UserPageQuery,
	UserPageRes,
	utils
 } from '@fast-crud/fast-crud';
import * as api from './api';
import { BorrowingReview, Switchspost } from './api';
import { ElMessage , ElInput, ElDialog, ElButton } from 'element-plus';
import dayjs from 'dayjs';
import { h, ref, computed ,inject,defineAsyncComponent} from "vue";
// import ItemsList from './ItemsList/index.vue';

export const createCrudOptions = function ({ crudExpose ,context, showRejectDialog, rejectReason, currentFormId,expose }: CreateCrudOptionsProps& {
	showRejectDialog: Ref<boolean>,
	rejectReason: Ref<string>,
	currentFormId: Ref<number | null>
  }): CreateCrudOptionsRet {






	const pageRequest = async (query: UserPageQuery) => {
		try {
			const res = await api.GetList(query);
			console.log("res::::",res)
			return {
				...res,
				records: res.data,
				total: res.total,
			};
		} catch (error) {
			return {
				code: 2000,
				msg: 'success',
				records: [],
				total: 0,
			};
		}
	};
	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);
	};
	//切换合作商
	const res = Switchspost(1);		


	return {
		crudOptions: {
			request: {
				// switchspost,
				pageRequest,
				addRequest,
				editRequest,
				delRequest,
			},
			form: {
				labelWidth: computed(() => {
					return context.labelWidthRef?.value ? context.labelWidthRef.value + "px" : "100px";
				}),
				wrapper: {
					buttons: {
					ok: {
						text: "通过", 
						click: async (ctx: FormWrapperContext) => {
						await api.BorrowingReview(ctx.form.id, "approve", "同意借用");
						ElMessage.success("审批通过");
						ctx.close();
						crudExpose.doRefresh();
						}
					},
					reject: {
						text: "拒绝",
						type: "danger",
						click: async (context: FormWrapperContext) => {
							currentFormId.value = context.form.id;
							showRejectDialog.value = true;
							context.close();
							crudExpose.doRefresh();
						} 
					},			  
				
				}
			}
			},
			search:{
				show:true,
			},
			toolbar:{show: false},
			actionbar:{show:false},		  
			rowHandle:{
				fixed:'right',
				width: 100,
				show: true,
				buttons: {
					view: { 
						show: true,
						order:1,
						click: ({ row }) => {
							// eslint-disable-next-line no-console
							// console.log("row:::",row);
							window.dispatchEvent(new CustomEvent('borrow-view', { detail: row }));
						},
					},
					edit: { show: false },
					remove: { show: false },
				// custom: {
				// 	text: "审批",
				// 	order: 5,
				// 	size: "small",
				// 	show: true,
				// 	click({row}) {
				// 			// eslint-disable-next-line no-console
				// 		// console.log("click", context);
				// 		// alert(context);
				// 		try{
				// 			// approvalDialogRef.value.openApprovalDialog(row);
				// 			// BorrowingReview(context.row.id,'approve','同意借用');
				// 			// ElMessage.success('审批通过');
				// 		}catch(err){
				// 			// eslint-disable-next-line no-console
				// 			console.log("error:::::", err);
				// 			ElMessage.error("系统错误!");
				// 		}
				// 	}
				// }

				// customView: {
				// 	text: '审批',
				// 	type: 'primary',
				// 	size: 'small',
				// 	order: 1,
				// 	show: true,
				// }



				}

			},
			columns: {
				_index: {
					title: '序号',
					form: { show: false },
					column: {
						align: 'center',
						width: '70px',
						columnSetDisabled: true,
						formatter: (context) => {
							let index = context.index ?? 1;
							let pagination = crudExpose!.crudBinding.value.pagination;
							return ((pagination!.currentPage ?? 1) - 1) * pagination!.pageSize + index + 1;


						},
					},
				},
				application_no: {
					title: '借用单编号',
					search: { show: false },
					type: 'input',
					column: { minWidth: 120 },
					form: {
						show: true,
						component: { placeholder: '请输入借用编号' },
						rules: [{ required: false, message: '请输入借用编号' }],
					},
				},
				borrower_type_label: { title: '借用人角色', column: { minWidth: 100 } },
				borrow_type_label: {
					title: '借用性质',
					search: { show: false },
					type: 'input',
					column: { minWidth: 120 },
					form: {
						show: true,
						component: { placeholder: '请输入借用性质' },
						rules: [{ required: false, message: '请输入借用性质' }],
					},
				},
				"borrower_info.user_code": {
					title: '学号/工号',
					search: { show: true },
					type: 'input',
					column: { minWidth: 120 },
					form: {
						show: true,
						component: { placeholder: '请输入学号/工号' },
						rules: [{ required: false, message: '请输入学号/工号' }],
					},
				},
				"borrower_info.name": {
					title: '借用人',
					search: { show: true },
					type: 'input',
					column: { show:true, minWidth: 120 },
					form: {
						show: true,
						component: { placeholder: '请输入借用人' },
						rules: [{ required: false, message: '请输入借用人' }],
					},
				},
				emergency_contact: {
					title: '借用人',
					search: { show: true },
					type: 'input',
					column: { show:false, minWidth: 120 },
					form: {
						show: false,
						component: { placeholder: '请输入借用人' },
						rules: [{ required: false, message: '请输入借用人' }],
					},
				},
				emergency_phone: {
					title: '联系方式',
					search: { show: false },
					type: 'input',
					column: {
						minWidth: 120
					},
					form: {
						show: true,
						component: { placeholder: '请输入联系方式' },
						rules: [{ required: false, message: '请输入联系方式' }],
					}

				},
				team_type_label: {
					title: '个人/团队',
					search: { show: false },
					type: 'input',
					column: { minWidth: 120 },
					form: {
						show: true,
						component: { placeholder: '请输入个人/团队' },
						rules: [{ required: false, message: '请输入个人/团队' }],
					},
				},
				team_members: { title: '团队成员', column: { show:false,minWidth: 120 } },
				team_members_count: { title: '团队人数', column: { show:false,minWidth: 80 }},
				create_datetime: {
					title: '创建时间',
					search: { show: false },
					type: 'input',
					column: { minWidth: 120 },
					form: {
						show: true,
						component: { placeholder: '请输入创建时间' },
						rules: [{ required: false, message: '请输入创建时间' }],
					},
				},
				status_label: { title: '当前状态', column: { minWidth: 80 } },
				actual_start_time: {
					title: '借出时间',
					column: { minWidth: 140, formatter: ({ value }: any) => (value ? dayjs(value).format('YYYY-MM-DD HH:mm:ss') : '') },
				},
				expected_end_time: {
					title: '预计归还',
					search: { show: false },
					type: 'input',
					column: { minWidth: 120 },
					form: {
						show: true,
						component: { placeholder: '请输入预计归还时间' },
						rules: [{ required: false, message: '请输入预计归还时间' }],
					},
				},
				actual_end_time: {
					title: '完结时间',
					column: { minWidth: 140, formatter: ({ value }: any) => (value ? dayjs(value).format('YYYY-MM-DD HH:mm:ss') : '') },
				},
				return_location: { title: '存放仓库', column: { minWidth: 120 } },
				purpose: { title: '借用目的', column: { minWidth: 150 ,show:false} },
				expected_start_time: {
					title: '预计开始',
					
					column: { minWidth: 140,show:true, formatter: ({ value }: any) => (value ? dayjs(value).format('YYYY-MM-DD HH:mm:ss') : '') },
				},
			
				
				approve_time: {
					title: '审批时间',
					column: { show:false,minWidth: 140, formatter: ({ value }: any) => (value ? dayjs(value).format('YYYY-MM-DD HH:mm') : '') },
					from:{show:false}
				},
				approve_remark: { title: '审批备注', column: {show:false, minWidth: 120 } ,from:{show:false}},
				total_items: { title: '设备数量', column: { show:false,minWidth: 80 } },
				
				
				applicant_remark: { title: '申请备注', column: { show:false, minWidth: 120 } },
				
				update_datetime: {
					title: '更新时间',
					column: { show:false,minWidth: 160, formatter: ({ value }: any) => (value ? dayjs(value).format('YYYY-MM-DD HH:mm:ss') : '') },
					
				},
				

				approver_name: {
					title: '当前审批人',
					search: { show: false },
					type: 'input',
					column: { minWidth: 120 },
					form: {
						show: false,
						component: { placeholder: '请输入当前审批人' },
						rules: [{ required: false, message: '请输入当前审批人' }],
					},
				},
				"items[0].device_category_name": {
					title: '设备分类',
					search: { show: false },
					type: 'input',
					column: { show:false,minWidth: 120 },
					form: {
						show: true,
						component: { placeholder: '' },
						// rules: [{ required: false, message: '请输入当前审批人' }],
					},
				},
				// "items[0].quantity": {
				// 	title: '借用数量',
				// 	search: { show: false },
				// 	type: 'input',
				// 	column: {show:false, minWidth: 120 },
				// 	form: {
				// 		show: true,
				// 		component: { placeholder: '' },
				// 		// rules: [{ required: false, message: '请输入当前审批人' }],
				// 	},
				// },
				// items: {
				// 	title: '设备列表',
				// 	type: 'input', // 必须是 component 类型
				// 	form: {
				// 	  show: true, // 确保表单中显示
				// 	  slot: 'ItemsList',
				// 	}
				//   },
				
				
				  
			
		
		
		
		},
		},
	};
};
