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

export const createCrudOptions = function ({ crudExpose , inventoryDialog}: CreateCrudOptionsProps& { inventoryDialog: Ref<any> }): 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: false,
					},
					startTaking: {
						text: '开始盘点',
						// icon: 'el-icon-document',
						click: async () => {
						const res =await api.StartScan({ remark: '例行盘点' });
						if (res?.code === 2000) {
							inventoryDialog.value.setCheckNo(res.data.check_no); 
							inventoryDialog.value.visible = true; 
						}
					},
					},

				},
			},
			toolbar:{
				show:true,
			},
			rowHandle:{
				fixed: 'right',
				width: 220,
				buttons:{
					view:{
						show:false
					},
					remove:{
						show:false,
					},
					edit:{
						show:false,
					}

				}

			},
			pagination: {
				show: false,
			},		
			columns: {
				_index: {
					title: '序号',
					form: { show: false },
					column: {
						type: 'index',
						align: 'center',
						width: '70px',
						columnSetDisabled: true, //禁止在列设置中选择
					},
				},
				check_no: {
					title: '编号',
					search: {
						show: false,
					},
					treeNode: true,
					type: 'input',
					column: {
						minWidth: 120,
					},
					form: {
						show:true,
						rules: [
							// 表单校验规则
							{ required: true, message: '名称必填项' },
						],
						component: {
							placeholder: '请输入',
						},
					},
					viewForm:{
						component: { placeholder: '' },
						rules: [{ required: true, message: '' }],
					}
				},
				start_time:{
					title: '维护日期',
					type: 'datetime',
					column: {
						minWidth: 120,
						// formatter: ({ value }) => (value ? dayjs(value).format('YYYY-MM-DD') : ''),
					},
					form: {
						show:false,
						component: { placeholder: '请填写维护日期' },
						rules: [{ required: true, message: '请填写维护日期' }],
					},
					viewForm:{
						component: { placeholder: '' },
						rules: [{ required: true, message: '' }],
					},
					valueResolve({ form, value }) {
						form.start_time = value ? dayjs(value).format('YYYY-MM-DD HH:mm:ss') : null;
					},
					// valueBuilder({ form, value }) {
					// 	console.log("valuessss::",value)
					// 	form.start_time =  value ;
					// },
				},
				checker_name:{
					title: '盘点人',
					type: 'input',
					column: {
						minWidth: 120,
					},
					form: {
						show:false,
						component: { placeholder: '请填写盘点人' },
						rules: [{ required: false, message: '请填写盘点人' }],
					},
					viewForm:{
						component: { placeholder: '' },
						rules: [{ required: false, message: '' }],
					}
				},
				status_display: {
					title: '状态',
					search: {
						show: false,
					},
					treeNode: true,
					type: 'input',
					column: {
						minWidth: 120,
					},
					form: {
						show:false,
						rules: [
							// 表单校验规则
							{ required: true, message: '维护标题必填项' },
						],
						component: {
							placeholder: '请输入维护标题',
						},
					},
					viewForm:{
						component: { placeholder: '' },
						rules: [{ required: true, message: '' }],
					}
				},	
				

			
			},
		},
	};
};
