import { AddReq, CreateCrudOptionsProps, CreateCrudOptionsRet,dict, UserPageQuery,compute, InfoReq, EditReq, DelReq } from '@fast-crud/fast-crud';
import * as api from './api';
import { auth } from '/@/utils/authFunction';
import { computed } from 'vue';

export const createCrudOptions = function ({ crudExpose }: CreateCrudOptionsProps): CreateCrudOptionsRet {
	const pageRequest = async (query: UserPageQuery) => {
		return await api.GetList(query);
	};

		const getDetail = async ({ row }: InfoReq) => {
		return await api.GetObj(row.id);
	};

	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);
	};

	return {
		crudOptions: {
			request: {
				pageRequest,
				addRequest,
				editRequest,
				delRequest,
				getDetail,
			},
			form:{
				wrapper: {
					buttons: {
						ok:{
							text:'提交',
							show:compute((row) => {
								return row.mode !=='view'
							})
						}
					}
				}
			},
			toolbar:{
				show:false,
			},
			rowHandle:{
				buttons:{
					remove:{
						show:true,
					},
					edit:{
						show:true,
					}

				}

			},
			actionbar: {
				buttons: {
					add: {
						show: true,//auth('area:Create'),
					},
				},
			},
			pagination: {
				show: true,
			},		
			columns: {
				_index: {
					title: '序号',
					form: { show: false },
					column: {
						type: 'index',
						align: 'center',
						width: '70px',
						columnSetDisabled: true, //禁止在列设置中选择
					},
				},
				keyword:{
					title: '关键字搜索',
					search: { show: true,type: 'input', },
					type: 'input',
					form: {
						component: { placeholder: '请输入' },
						show:false},
					column: {show:false}
				},
				code:{
					title:'分类编码',
					type:'input',
					column: {
						show:false,
						minWidth: 120,
					},
					form: {
						show: false,
						
					}
				},
				name: {
					title: '分类名称',
					search: {
						show: false,
					},
					treeNode: true,
					type: 'input',
					column: {
						
					},
					form: {
						rules: [
							// 表单校验规则
							{ required: true, message: '名称必填项' },
						],
						component: {
							placeholder: '请输入名称',
						},
					},
					viewForm:{
						component: { placeholder: '' },
						rules: [{ required: true, message: '' }],
					}
				},
				description:{
					title: '分类描述',
					type: 'input',
					column: {
						width: 800,
					},
					form: {
						component: { placeholder: '请填写分类描述' },
						rules: [{ required: false, message: '请填写分类描述' }],
					},
					viewForm:{
						component: { placeholder: '' },
						rules: [{ required: true, message: '' }],
					}
				},
				parent:{
					title: '设备父级',
					type: 'input',
					column: {
						show:false,
						minWidth: 120,
					},
					form: {
						show:false,
						component: { placeholder: '请填写设备父级' },
						rules: [{ required: false, message: '请填写设备父级' }],
					},
					viewForm:{
						component: { placeholder: '' },
						rules: [{ required: true, message: '' }],
					}
				},
				sort:{
					title: '排序',
					type: 'input',
					column: {
						show:false,
						minWidth: 120,
					},
					form: {
						value:1,
						show:false,
						component: { placeholder: '请填写排序' },
						rules: [{ required: true, message: '请填写排序' }],
					},
					viewForm:{
						component: { placeholder: '' },
						rules: [{ required: true, message: '' }],
					}
				},
				status:{
					title: '状态',
					type: 'dict-select',
					search:{
						show:true
					},
					column: {
						width: 120,
					},
					dict: dict({
						data: [
							{ label: '启用', value: true },
							{ label: '禁用', value: false,color: "danger" },
						],
					}),
					form: {
						component: { placeholder: '请选择状态' },
						rules: [{ required: true, message: '请选择状态' }],
					},
					viewForm:{
						component: { placeholder: '' },
						rules: [{ required: true, message: '' }],
					}
				},
				tenant:{
					title: '租户',
					type: 'input',
					value: 1,
					column: {
						show:false,
						minWidth: 120,
					},
					form: {
						show:false,
						component: { placeholder: '请填写租户' },
						rules: [{ required: false, message: '请填写租户' }],
					},
				},
			},
		},
	};
};
