import { AddReq, CreateCrudOptionsProps, CreateCrudOptionsRet, DelReq, dict, EditReq, UserPageQuery } from '@fast-crud/fast-crud';
import * as api from './api';
import { switchspost } from './api';

export const createCrudOptions = function ({ crudExpose }: CreateCrudOptionsProps): CreateCrudOptionsRet {
	const pageRequest = async (query: UserPageQuery) => {
		try {
			const res = await api.GetList(query);
			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,
			},
			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;
						},
					},
				},
				name: {
					title: '审批单名称',
					search: { show: true },
					type: 'input',
					column: { minWidth: 120 },
					form: {
						component: { placeholder: '请输入审批单名称' },
						rules: [{ required: true, message: '请输入审批单名称' }],
					},
				},
				workflow_type: {
					title: '流程类型',
					search: { show: true },
					type: 'dict-select',
					column: { minWidth: 120 },
					dict: dict({
						data: [
							{ label: '设备借用申请', value: 0 },
							{ label: '设备维修申请', value: 1 },
							{ label: '设备采购申请', value: 2 },
							{ label: '其他申请', value: 3 },
						],
					}),
					form: {
						component: { placeholder: '请选择流程类型' },
						rules: [{ required: true, message: '请选择流程类型' }],
					},
				},
				description: {
					title: '流程描述',
					search: { show: false },
					type: 'input',
					column: { minWidth: 120 },
					form: {
						component: { placeholder: '请输入流程描述' },
						rules: [{ required: false, message: '请输入流程描述' }],
					},
				},
				is_active: {
					title: '状态',
					type: 'dict-select',
					search: { show: false },
					dict: dict({
						data: [
							{ label: '启用', value: true },
							{ label: '禁用', value: false },
						],
					}),
					column: { minWidth: 100 },
					form: {
						component: { placeholder: '请选择状态' },
						rules: [{ required: true, message: '请选择状态' }],
					},
				},
				'trigger_conditions.equipment_categories': {
					title: '分类列表',
					type: 'dict-select',
					search: { show: false },
					column: {
						minWidth: 160,
					},
					dict: dict({
						url: '/api/system/device/category/',
						value: 'id', 
						label: 'name'
					}),
					form: { 
						component: { placeholder: '请选择分类列表' },
						valueResolve({ form, value }) {
							form.trigger_conditions.equipment_categories =[value];
						},
						// valueBuilder({ form, value }) {
						// 	form.trigger_conditions.equipment_categories =Array.from(value);
						// },
				},
				},
				'trigger_conditions.borrow_types': {
					title: '借用类型',
					type: 'dict-select',
					search: { show: false },
					
					column: {
						minWidth: 160,
						// formatter: ({ value }) => (value ? dayjs(value).format('YYYY-MM-DD HH:mm:ss') : '')
					},
					dict: dict({
						data: [
							{ label: '常规借用', value: "0" },
							{ label: '课堂借用', value: '1' },
							{ label: '特殊借用', value: '2' },
						],
					}),
					form: { 
						component: { placeholder: '请选择借用类型' },
						valueResolve({ form, value }) {
							form.trigger_conditions.borrow_types = [value];
						},
						// valueBuilder({ form, value }) {
						// 	form.trigger_conditions.borrow_types=Array.from(value);
						// },
					},
				},
				'trigger_conditions.team_type': {
					title: '借用方式',
					type: 'dict-select',
					search: { show: false },
					column: { minWidth: 120 },
					dict: dict({
						data: [
							{ label: '个人借用', value: '0' },
							{ label: '团队借用', value: '1' },
						],
					}),
					form: {
						component: { placeholder: '请选择借用方式' },
						valueResolve({ form, value }) {
							form.trigger_conditions.team_type =[value];
						},
					},
				},
				'trigger_conditions.user_types': {
					title: '用户类型',
					type: 'dict-select',
					search: { show: false },
					column: { minWidth: 120 },
					dict: dict({
						data: [
							{ label: '学生', value: 'student' },
							{ label: '老师', value: 'teacher' },
						],
					}),
					form: {
						component: { placeholder: '请选择用户类型' },
						valueResolve({ form, value }) {
							form.trigger_conditions.user_types =[value];
						},
					},
				},
				tenant_id:{
					title: '租户id',
					type: 'dict-select',
					column: {
						show: false,
						minWidth: 120,
					},
					dict: dict({
						url: '/api/system/tenant/list/',
						value: 'id', 
						label: 'name'
					}),
					form: {
						component: { placeholder: '请填租户id' },
						rules: [{ required: false, message: '请填租户id' }],
					},
				},

		
			
		
		
		
		},
		},
	};
};
