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

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:'提交',
							show:compute((row) => {
								return row.mode !=='view'
							})
						}
					}
				}
			},
			actionbar: {
				buttons: {
					add: {
						show: true,//auth('area:Create'),
					},
				},
			},
			toolbar:{
				show:false,
			},
			rowHandle:{
				buttons:{
					remove:{
						show:true,
					},
					edit:{
						show:true,
					}

				}

			},
			pagination: {
				show: true,
			},		
			columns: {
				_index: {
					title: '序号',
					form: { show: false },
					column: {
						type: 'index',
						align: 'center',
						width: '70px',
						columnSetDisabled: true, //禁止在列设置中选择
					},
				},
				search:{
					title: '关键字搜索',
					search: { show: true,type: 'input', },
					type: 'input',
					form: {
						component: { placeholder: '请输入' },
						show:false},
					column: {show:false}
				},
				title: {
					title: '标题',
					search: {
						show: true,
					},
					treeNode: true,
					type: 'input',
					column: {
						minWidth: 120,
					},
					form: {
						rules: [
							// 表单校验规则
							{ required: true, message: '标题必填项' },
						],
						component: {
							placeholder: '请输入标题',
						},
					},
				},
				content:{
					title: '内容',
					type: 'textarea',
					column: {
						show:false,
						minWidth: 120,
					},
					form: {
						col: {
							span: 24
						},
						component: { showWordLimit: true, maxlength: 200 ,placeholder: '请填写内容' },
						rules: [{ 
							required: true,
								message: '内容为必填项',
							}],
					},
				},
				is_top:{
					title: '是否置顶',
					type: 'dict-switch',
					column: {
						show:true,
						minWidth: 120,
						component: {
							name: 'fs-dict-switch',
							activeText: '',
							inactiveText: '',
							style: '--el-switch-on-color: var(--el-color-primary); --el-switch-off-color: #dcdfe6',
							onChange: compute((context) => {
								return () => {
									api.UpdateObj(context.row).then((res:APIResponseData) => {
										successMessage(res.msg as string);
									});
								};
							}),
						},
					},
					dict: dict({
						data: [
							{ value: true, label: "置顶" },
							{ value: false, label: "取消" }
						]
					}),
					form: {
						value:true,
						show:true,
						component: { placeholder: '请选择是否置顶' },
						rules: [{ required: false, message: '请选择是否置顶' }],
					},
				},

				publisher_name:{
					title: '发布人',
					type: 'input',
					column: {
						minWidth: 120,
					},
					form: {
						show:false,
						component: { placeholder: '请填发布人' },
						rules: [{ required: false, message: '请填写发布人' }],
					},
				},
				publish_time:{
					title: '发布时间',
					type: 'input',
					column: {
						minWidth: 120,
					},
					form: {
						show:false,
						component: { placeholder: '请填写发布时间' },
						rules: [{ required: false, message: '请填写发布时间' }],
					},
				},
				article_type:{
					title: '文章类型',
					type: 'input',
					column: {
						show:false,
						minWidth: 120,
					},
					form: {
						value:"borrow_rules",
						show:false,
						component: { placeholder: '文章类型' },
						rules: [{ required: false, message: '文章类型' }],
					},
				},
				status:{
					title: '状态',
					type: 'input',
					column: {
						show:false,
						minWidth: 120,
					},
					form: {
						value:1,
						show:false,
						component: { placeholder: '请填写状态' },
						rules: [{ required: false, message: '请填写状态' }],
					},
				},



			},
		},
	};
};
