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

export const createCrudOptions = function ({ crudExpose }: CreateCrudOptionsProps): CreateCrudOptionsRet {
	const pageRequest = async (query: UserPageQuery) => {
		try {
			const res = await api.GetList(query);
			return {
				...res,
				records: res.data,
				total: res.total,
			};
		} catch (error) {
			return {
				code: 2000,
				msg: 'success',
				records: [],
				total: 0,
			};
		}
	};
	const editRequest = async ({ form, row }: EditReq) => {
		form.id = row.id;
		return await api.UpdateObj(row.id,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,
			},
			actionbar: {
				buttons: {
					add: { 
						show: true,
						text: '新建审批流程',
						click: () => {
							window.dispatchEvent(new CustomEvent('workflow-add'))
						}
					},
				},
			},
			search:{show:false},
			toolbar:{show:false},
			rowHandle: {
				fixed: 'right',
				width: 220,
				buttons: {
					view: {
						show: true,
						click: ({ row }) => {
							window.dispatchEvent(new CustomEvent('workflow-view', { detail: row }));
						},
					},
					edit: {
						show: true,
						click: ({ row }) => {
							window.dispatchEvent(new CustomEvent('workflow-edit', { detail: row }));
						},
					},
					remove: {
						show: true,
					},
				},
			},
			columns: {
				_index: {
					title: '序号',
					form: { show: false },
					column: {
						align: 'center',
						width: '70px',
						columnSetDisabled: true,
						formatter: (context: any) => {
							let index = context.index ?? 1;
							let pagination = crudExpose!.crudBinding.value.pagination;
							return ((pagination!.currentPage ?? 1) - 1) * pagination!.pageSize + index + 1;
						},
					},
				},
				search:{
					title: '关键字搜索',
					search: { show: true,type: 'input', },
					type: 'input',
					form: {
						component: { placeholder: '请输入' },
						show:false},
					column: {show:false}
				},
				name: { title: '流程名称',type:'input', column: { minWidth: 140 }, search: { show: true} , form:{
					component: { placeholder: '请输入设备编号' },
				}},
				workflow_type:{
					title: '流程类型id',type:'input', column: { show:false,minWidth: 140 },
						search: { show: false },
						form:{
						component: { placeholder: '请输入设备编号' },
					}
				},
				"trigger_conditions.team_type":{
					title: '流程类型id', column: { show:false,minWidth: 140 }, search: { show: false }
				},
				"trigger_conditions.user_types":{
					title: '流程类型id', column: { show:false,minWidth: 140 }, search: { show: false }
				},
				"trigger_conditions.borrow_types":{
					title: '流程类型id', column: { show:false,minWidth: 140 }, search: { show: false }
				},
				"trigger_conditions.equipment_categories":{
					title: '流程类型id', column: { show:false,minWidth: 140 }, search: { show: false }
				},
				workflow_type_label: { title: '流程类型', column: { minWidth: 120 } },
				description: { title: '流程描述', column: { minWidth: 200 } },
				step_count: { title: '步骤数', column: { minWidth: 80, align: 'center' } },
				is_active: {
					title: '状态',
					column: {
						minWidth: 80,
						formatter: ({ value }: any) => (value ? '启用' : '停用'),
					},
				},
				create_datetime: {
					title: '创建时间',
					column: { minWidth: 160, formatter: ({ value }: any) => (value ? dayjs(value).format('YYYY-MM-DD HH:mm:ss') : '') },
				},
				update_datetime: {
					title: '更新时间',
					column: { minWidth: 160, formatter: ({ value }: any) => (value ? dayjs(value).format('YYYY-MM-DD HH:mm:ss') : '') },
				},



			},
		},
	};
};
