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

const { compute } = useCompute();

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;
		form.device_id=Number(form.device);
		form.title=form.article_title;
		form.content=form.article_content;
		return await api.UpdateObj(form);
	};
	const delRequest = async ({ row }: DelReq) => {
		// console.log("delform:::",row)
		return await api.DelObj(row.id);
	};
	const addRequest = async ({ form }: AddReq) => {
		form.device_id=Number(form.device);
		form.title=form.article_title;
		form.content=form.article_content;
		// console.log("sdfsdfsdfform::::",form);
		
		return await api.AddObj(form);
	};

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

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

				}

			},
			pagination: {
				show: false,
			},		
			columns: {
				_index: {
					title: '序号',
					form: { show: false },
					column: {
						show:false,
						type: 'index',
						align: 'center',
						width: '70px',
						columnSetDisabled: true, //禁止在列设置中选择
					},
				},
				device:{
					title: '设备id',
					search: {
						show: false,
					},
					treeNode: true,
					type: 'dict-select',
					column: {
						show:false,
						minWidth: 120,
					},
					dict: dict({
						url: '/api/system/device/',
						value: 'id',
						label: 'name',
					}),
					form: {
						show:true,
						component: { placeholder: '请选择设备id' },
						rules: [{ required: true, message: '请选择设备id' }],
					},
				},
				article:{
					title: '文章id',
					search: {
						show: false,
					},
					type: 'input',
					column: {
						show:false,
						minWidth: 120,
					},
					form: {
						show:false,
					}

				},
				device_code: {
					title: '设备编码',
					search: {
						show: false,
					},
					treeNode: true,
					type: 'input',
					column: {
						minWidth: 120,
					},
					form: {
						show:false,
					},
				},
				article_title: {
					title: '设备标题',
					search: {
						show: true,
					},
					treeNode: true,
					type: 'input',
					column: {
						minWidth: 120,
					},
					form: {
						show:true,
						rules: [
							// 表单校验规则
							{ required: true, message: '标题必填项' },
						],
						component: {
							placeholder: '请输入标题',
						},
					},
					viewForm:{
						component: { placeholder: '' },
						rules: [{ required: true, message: '' }],
					}
				},
				article_content:{
					title: '内容',
					// type: 'textarea',
					type: 'editor-wang5',
					column: {
						show:true,
						minWidth: 120,
					},
					form: {
						show:true,
						col: {
							span: 24
						},
						// component: { showWordLimit: true, maxlength: 200 ,placeholder: '请填写内容' },
						rules: [{ 
							required: true,
								message: '内容为必填项',
							}],
						component: {
							disabled: false,
							id: '1', // 当同一个页面有多个editor时，需要配置不同的id
							editorConfig: {
								// 是否只读
								readOnly: compute((context) => {
									const { mode } = context;
									if (mode === 'add') {
										return false;
									}
									return true;
								}),
							},
							uploader: {
								type: 'form',
								buildUrl(res: any) {
									console.log("res::::",res)
									return res.url;
								},
							},
							},

					},
					viewForm:{
						component: { placeholder: '' },
						rules: [{ required: true, message: '' }],
					}
				},
				device_name:{
					title: '发布人',
					type: 'input',
					column: {
						minWidth: 120,
					},
					form: {
						show:false,
						component: { placeholder: '请填发布人' },
						rules: [{ required: false, message: '请填写发布人' }],
					},
					viewForm:{
						component: { placeholder: '' },
						rules: [{ required: true, message: '' }],
					}
				},
				create_datetime:{
					title: '发布时间',
					type: 'input',
					column: {
						minWidth: 120,
					},
					form: {
						show:false,
						component: { placeholder: '请填写发布时间' },
						rules: [{ required: false, message: '请填写发布时间' }],
					},
					viewForm:{
						component: { placeholder: '' },
						rules: [{ required: true, message: '' }],
					}
				},
				is_default:{
					title: '状态',
					type: 'input',
					column: {
						show:false,
						minWidth: 120,
					},
					form: {
						value:true,
						show:false,
						component: { placeholder: '请填写状态' },
						rules: [{ required: false, message: '请填写状态' }],
					},
					viewForm:{
						component: { placeholder: '' },
						rules: [{ required: true, message: '' }],
					}
				},
				// device_id:{
				// 	title: '设备id',
				// 	type: 'input',
				// 	column: {
				// 		show:false,
				// 		minWidth: 120,
				// 	},
				// 	form: {
				// 		show:true,
				// 		component: { placeholder: '请填写状态' },
				// 		rules: [{ required: false, message: '请填写状态' }],
				// 	},
				// },
				// title:{
				// 	title: '设备标题',
				// 	type: 'input',
				// 	column: {
				// 		show:false,
				// 		minWidth: 120,
				// 	},
				// 	form: {
				// 		show:true,
				// 		component: { placeholder: '请填写设备标题' },
				// 		rules: [{ required: false, message: '请填写设备标题' }],
				// 	},
				// },
				// content:{
				// 	title: '设备内容',
				// 	type: 'input',
				// 	column: {
				// 		show:false,
				// 		minWidth: 120,
				// 	},
				// 	form: {
				// 		show:true,
				// 		component: { placeholder: '请填写设备内容' },
				// 		rules: [{ required: false, message: '请填写设备内容' }],
				// 	},
				// },



			},
		},
	};
};
