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';
import { ElMessage, ElMessageBox } from 'element-plus'
import { request } from '/@/utils/service'


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:{
				fixed: 'right',
				width: 220,
				buttons:{
					remove:{
						show:true,
					},
					edit:{
						show:true,
					},
					cancelDamage: {
						text: '撤销报损',
						type: 'danger',
						order: 98,
						show: true,
						click({ row }) {
							ElMessageBox.confirm('确认撤销该报损记录？', '提示', {
							confirmButtonText: '确定',
							cancelButtonText: '取消',
							type: 'warning'
							}).then(async () => {
							try {
								const res = await api.UpdateDamageCancel(row);
								console.log("res::::",res)
								if (res.code === 2000) {
								ElMessage.success('撤销成功')
								crudExpose.doRefresh()
								} else {
								ElMessage.error(res.msg || '撤销失败')
								}
							} catch (err) {
								ElMessage.error('请求失败')
							}
							}).catch(() => {
							// 用户取消操作
							})
						}
					}
						

				}

			},
			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:0,
						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:false,
						// show:compute(({ form }) => {
						// 	// 只有当选择了胜任力标签时才显示配置
						// 	return form && form.damage_type === 1;
						// })
					},
					form: {
						show:false,
						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: '' }],
					}
				},








			},
		},
	};
};
