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 {getBaseURL} from '/@/utils/baseUrl';
import Cookies from 'js-cookie';
import {request} from '/@/utils/service';
import {Ref} from 'vue'

export const createCrudOptions = function ({ crudExpose,  dialogId,
	dialogVisible }: CreateCrudOptionsProps & { dialogId: Ref<number | null>; dialogVisible: Ref<boolean> }): 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,
					},
					updateStatus: {
						text: '修改状态',
						show: true,
						order: 99,
						type: 'primary',
						click({ row }) {
							dialogId.value = row.id
							dialogVisible.value = 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: '' }],
					}
				},
				remark:{
					title: '备注',
					type: 'input',
					column: {
						minWidth: 120,
					},
					form: {
						show:true,
						component: { placeholder: '请填写备注' },
						rules: [{ required: false, message: '请填写备注' }],
					},
					viewForm:{
						component: { placeholder: '' },
						rules: [{ required: true, message: '' }],
					}
				},
				damage_images:{
					title: '附件',
					type: 'image-uploader',
					column: {
						minWidth: 120,
					},
					form: {
						show: auth('image:upload'),
						component: {
							uploader: {
								type: 'form',
								limit: 1,
								action: '/api/system/device/upload-image/',
								accept: ".jpg,.png",
								uploadRequest: async ({ action, file, onProgress }) => {
									const token = Cookies.get('token'); 
									const data = new FormData();
									data.append("image", file);
									return await request({
										url: action,
										method: "post",
										timeout: 60000,
										headers: {
										"Content-Type": "multipart/form-data",
										"Authorization": token ? `JWT ${token}` : ''
										},
										data,
										onUploadProgress: (p) => {
										onProgress({ percent: Math.round((p.loaded / p.total) * 100) });
										}
									});
								},
								successHandle(ret) {
									// eslint-disable-next-line no-console
									// console.log("ret.data.image_url:::",ret.data.image_url);

									return {
										url: getBaseURL(ret.data.image_url), 
										key: ret.data.id,
										...ret.data
									};
								},
							
								

								
							}
						},
						
					},
					valueBuilder({ row, key }) {
						return row[key] ? [row[key]] : [];
					},
					valueResolve({ form, key }) {
						// eslint-disable-next-line no-console
						console.log("form[key]:::",form[key]);
						form[key] = Array.isArray(form[key]) ? form[key][0] : form[key];
					},
					viewForm:{
						component: { placeholder: '' },
						rules: [{ required: true, message: '' }],
					}
				}








			},
		},
	};
};
