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


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) => {
		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'),
					},
				},
			},
			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' }],
					},
				},
				device_code: {
					title: '设备编码',
					search: {
						show: false,
					},
					treeNode: true,
					type: 'input',
					column: {
						minWidth: 120,
					},
					form: {
						show:false,
					},
				},
				article_title: {
					title: '设备标题',
					search: {
						show: false,
					},
					treeNode: true,
					type: 'input',
					column: {
						minWidth: 120,
					},
					form: {
						show:true,
						rules: [
							// 表单校验规则
							{ required: true, message: '标题必填项' },
						],
						component: {
							placeholder: '请输入标题',
						},
					},
				},
				article_content:{
					title: '内容',
					type: 'textarea',
					column: {
						show:true,
						minWidth: 120,
					},
					form: {
						show:true,
						col: {
							span: 24
						},
						component: { showWordLimit: true, maxlength: 200 ,placeholder: '请填写内容' },
						rules: [{ 
							required: true,
								message: '内容为必填项',
							}],
					},
				},
				device_name:{
					title: '发布人',
					type: 'input',
					column: {
						minWidth: 120,
					},
					form: {
						show:false,
						component: { placeholder: '请填发布人' },
						rules: [{ required: false, message: '请填写发布人' }],
					},
				},
				create_datetime:{
					title: '发布时间',
					type: 'input',
					column: {
						minWidth: 120,
					},
					form: {
						show:false,
						component: { placeholder: '请填写发布时间' },
						rules: [{ required: false, message: '请填写发布时间' }],
					},
				},
				is_default:{
					title: '状态',
					type: 'input',
					column: {
						show:false,
						minWidth: 120,
					},
					form: {
						value:true,
						show:false,
						component: { placeholder: '请填写状态' },
						rules: [{ required: false, 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: '请填写设备内容' }],
				// 	},
				// },



			},
		},
	};
};
