import { AddReq, CreateCrudOptionsProps,dict, CreateCrudOptionsRet, UserPageQuery,compute } from '@fast-crud/fast-crud';
import * as api from './api';
import { auth } from '/@/utils/authFunction';
import { ja } from 'element-plus/es/locale';


export const createCrudOptions = function ({ crudExpose }: CreateCrudOptionsProps): CreateCrudOptionsRet {
	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);
	};

	return {
		crudOptions: {
			request: {
				pageRequest,
				addRequest,
				editRequest,
				delRequest,

			},
			form:{
				wrapper: {
					buttons: {
						ok:{
							text:'提交'
						}
					}
				}
			},
			actionbar: {
				buttons: {
					add: {
						show: auth('area:Create'),
					},
				},
			},
			toolbar:{
				show:false,
			},
			rowHandle:{
				buttons:{
					remove:{
						show:true,
					},
					edit:{
						show:true,
					}

				}

			},
			pagination: {
				show: false,
			},		
			columns: {
				_index: {
					title: '序号',
					form: { show: false },
					column: {
						type: 'index',
						align: 'center',
						width: '70px',
						columnSetDisabled: true, //禁止在列设置中选择
					},
				},
				damage_no: {
					title: '编号',
					search: {
						show: true,
					},
					treeNode: true,
					type: 'input',
					column: {
						minWidth: 120,
					},
					form: {
						show:false,
						// rules: [
						// 	// 表单校验规则
						// 	// { required: true, message: '名称必填项' },
						// ],
						component: {
							placeholder: '请输入编号',
						},
					},
					viewForm:{
						component: { placeholder: '' },
						rules: [{ required: true, message: '' }],
					}
				},
				device:{
					title: '设备id',
					type: 'dict-select',
					column: {
						show:false,
						minWidth: 120,
					},
					dict: dict({
						url: '/api/system/device/',
						value: 'id',
						label: 'name',
					}),
					form: {
						component: { placeholder: '请填写设备id' },
						rules: [{ required: true, message: '请填写设备id' }],
					},
					editForm:{
						component: { disabled: true },
					},
					viewForm:{
						component: { placeholder: '' },
						rules: [{ required: true, message: '' }],
					}
				},
				device_code:{
					title: '设备编码',
					type: 'input',
					column: {
						show:false,
						minWidth: 120,
					},
					form: {
						show:true,
						component: { placeholder: '请填写设备编码' },
						rules: [{ required: true, message: '请填写设备编码' }],
					},
					viewForm:{
						component: { placeholder: '' },
						rules: [{ required: true, message: '' }],
					}
				},
				device_name:{
					title: '设备名称',
					type: 'input',
					column: {
						show:true,
						minWidth: 120,
					},
					form: {
						show:false,
						component: { placeholder: '请填写设备名称' },
						rules: [{ required: true, message: '请填写设备名称' }],
					},
					viewForm:{
						component: { placeholder: '' },
						rules: [{ required: true, message: '' }],
					}
				},
				quantity:{
					title: '库存数量',
					type: 'input',
					column: {
						minWidth: 120,
					},
					form: {
						component: { placeholder: '请填写库存数量'},
						rules: [{ required: false, message: '请填写库存数量' }],
					},
					editForm:{
						component: { disabled: true },
					},
					valueResolve({ form, value }) {
						form.quantity = Number(value);
					},
					viewForm:{
						component: { placeholder: '' },
						rules: [{ required: true, message: '' }],
					}
				},
				damage_type:{
					title: '维修类型',
					type: 'dict-select',
					dict: dict({
						data: [
							// { label: '报废', value: 0 },
							{ label: '维修', value: 1 },
						],
					}),
					column: {
						minWidth: 120,
					},
					form: {
						value:1,
						show:true,
						component: { placeholder: '请填写排序'},
						rules: [{ required: false, message: '请填写排序' }],
					},
					editForm:{
						component: { disabled: true },
					},
					viewForm:{
						component: { placeholder: '' },
						rules: [{ required: true, message: '' }],
					}
				},
				damage_reason:{
					title: '原因',
					type: 'input',
					column: {
						minWidth: 120,
					},
					form: {
						component: { placeholder: '请填写原因' },
						rules: [{ required: false, message: '请填写原因' }],
					},
					viewForm:{
						component: { placeholder: '' },
						rules: [{ required: true, message: '' }],
					}
				},
				estimated_loss:{
					title: '预估维修费用',
					type: 'input',
					column: {
						minWidth: 120,
						show:compute(({ form }) => {
							// 只有当选择了胜任力标签时才显示配置
							return form && form.damage_type === 1;
						})
					},
					form: {
						component: { placeholder: '请填写预估维修费用' },
						rules: [{ required: false, message: '请填写预估维修费用' }],
						show:compute(({ form }) => {
							// 只有当选择了胜任力标签时才显示配置
							return form && form.damage_type === 1;
						})
					},
					viewForm:{
						component: { placeholder: '' },
						rules: [{ required: true, message: '' }],
						show:compute(({ form }) => {
							// 只有当选择了胜任力标签时才显示配置
							return form && form.damage_type === 1;
						})
					}
				},
				responsible_person:{
					title: '负责人',
					type: 'input',
					column: {
						minWidth: 120,
					},
					form: {
						component: { placeholder: '请填写负责人' },
						rules: [{ required: false, message: '请填写负责人' }],
					},
					viewForm:{
						component: { placeholder: '' },
						rules: [{ required: true, message: '' }],
					}
				},
				status_display:{
					title: '状态',
					type: 'input',
					column: {
						minWidth: 120,
					},
					form: {
						show:false,
						component: { placeholder: '请填写负责人' },
						rules: [{ required: false, message: '请填写负责人' }],
					},
					viewForm:{
						component: { placeholder: '' },
						rules: [{ required: true, message: '' }],
					}
				},








			},
		},
	};
};
