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


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'
						})
						}
					}
				}
			},
			toolbar:{
				show:false,
			},
			actionbar: {
                buttons: {
                    add: {
                        show: auth('user:Create')
                    }					
                }
            },
			rowHandle: {
				fixed:'right',
				width: 230,
				buttons: {
					view: {
					show: true,
					},
					edit: {
						show: true,
					},
					remove: {
						show: true,
					},
				},
			},
			columns: {
				_index: {
					title: '序号',
					form: { show: false },
					column: {
						align: 'center',
						width: '70px',
						columnSetDisabled: true,
						formatter: (context) => {
							let index = context.index ?? 1;
							let pagination = crudExpose!.crudBinding.value.pagination;
							return ((pagination!.currentPage ?? 1) - 1) * pagination!.pageSize + index + 1;
						},
					},
				},
				code: {
					title: '供应商编号',
					search: { show: true },
					type: 'text',
					column: { minWidth: 120},
					form: {
						show: false,
						
					}
				},
				name: {
					title: '供应商名称',
					search: { show: false },
					type: 'input',
					column: { minWidth: 120 },
					form: {
						component: { placeholder: '请输入供应商名称' },
						rules: [{ required: true, message: '请输入供应商名称' }]
					},
					viewForm:{
						component: { placeholder: '' },
						rules: [{ required: true, message: '' }],
					}
				},
				contact_person: {
					title: '联系人',
					search: { show: true },
					type: 'input',
					column: { minWidth: 100 },
					form: {
						component: { placeholder: '请选择联系人' },
						rules: [{ required: true, message: '请选择联系人' }],
					},
					viewForm:{
						component: { placeholder: '' },
						rules: [{ required: true, message: '' }],
					}
				},
				contact_phone: {
					title: '联系手机号',
					search: { show: true },
					type: 'input',
					column: { minWidth: 100 },
					form: {
						component: { placeholder: '请选择联系手机号' },
						rules: [{ required: true, message: '请选择联系手机号' }],
					},
					viewForm:{
						component: { placeholder: '' },
						rules: [{ required: true, message: '' }],
					}
				},
				contact_email: {
					title: '联系邮箱',
					search: { show: true },
					type: 'input',
					column: { minWidth: 100 },
					form: {
						component: { placeholder: '请输入联系邮箱' },
						rules: [{ required: true, message: '请输入联系邮箱' }],
					},
					viewForm:{
						component: { placeholder: '' },
						rules: [{ required: true, message: '' }],
					}
				},
				address: {
					title: '地址',
					search: { show: false },
					type: 'input',
					column: { minWidth: 100 },
					form: {
						component: { placeholder: '请输入地址' },
						rules: [{ required: false, message: '请输入地址' }],
					},
					viewForm:{
						component: { placeholder: '' },
						rules: [{ required: true, message: '' }],
					}
				},
				website: {
					title: '网址',
					search: { show: false },
					type: 'input',
					column: { minWidth: 100 },
					form: {
						component: { placeholder: '请输入网址' },
						rules: [{ required: false, message: '请输入网址' }],
					},
					viewForm:{
						component: { placeholder: '' },
						rules: [{ required: true, message: '' }],
					}
				},
				business_license: {
					title: '营业执照',
					search: { show: false },
					type: 'input',
					column: { minWidth: 100 },
					form: {
						component: { placeholder: '请输入营业执照' },
						rules: [{ required: false, message: '请输入营业执照' }],
					},
					viewForm:{
						component: { placeholder: '' },
						rules: [{ required: true, message: '' }],
					}
				},
				tax_number: {
					title: '税号',
					search: { show: false },
					type: 'input',
					column: { minWidth: 100 },
					form: {
						component: { placeholder: '请输入税号' },
						rules: [{ required: false, message: '请输入税号' }],
					},
					viewForm:{
						component: { placeholder: '' },
						rules: [{ required: true, message: '' }],
					}
				},
				bank_account: {
					title: '银行卡号',
					search: { show: false },
					type: 'input',
					column: { minWidth: 100 },
					form: {
						component: { placeholder: '请输入银行卡号' },
						rules: [{ required: false, message: '请输入银行卡号' }],
					},
					viewForm:{
						component: { placeholder: '' },
						rules: [{ required: true, message: '' }],
					}
				},
				credit_rating: {
					title: '信用评级',
					search: { show: true },
					type: 'dict-select',
					column: { minWidth: 100 },
					dict: dict({
						data: [
							{ label: 'A', value: 'A' },
							{ label: 'B', value: 'B' },
							{ label: 'C', value: 'C' },
							{ label: 'D', value: 'D' },
							{ label: 'E', value: 'E' },
							{ label: 'F', value: 'F' },
						],
					}),
					form: {
						component: { placeholder: '请输入信用评级' },
						rules: [{ required: false, message: '请输入信用评级' }],
					},
					viewForm:{
						component: { placeholder: '' },
						rules: [{ required: true, message: '' }],
					}
				},
				cooperation_years: {
					title: '合作年份',
					search: { show: false },
					type: 'input',
					column: { minWidth: 100 },
					form: {
						component: { placeholder: '请输入合作年份' },
						rules: [{ required: false, message: '请输入合作年份' }],
					},
					viewForm:{
						component: { placeholder: '' },
						rules: [{ required: true, message: '' }],
					}
				},
				remark: {
					title: '备注',
					search: { show: false },
					type: 'input',
					column: { minWidth: 100 },
					form: {
						component: { placeholder: '请输入备注' },
						rules: [{ required: false, message: '请输入备注' }],
					},
					viewForm:{
						component: { placeholder: '' },
						rules: [{ required: true, message: '' }],
					}
				},
				tenant_id:{
					title: '租户id',
					type: 'dict-select',
					tenant_id:1,
					column: {
						show:false,
						minWidth: 120,
					},
					dict: dict({
						url: '/api/system/tenant/list/',
						value: 'id', 
						label: 'name',
					}),
					form: {	
						show:false,
						value: 1,
						component: { placeholder: '请填租户id' },
						rules: [{ required: false, message: '请填租户id' }],
					},
					viewForm:{
						component: { placeholder: '' },
						rules: [{ required: true, message: '' }],
					}
				},		
				status: {
					title: '状态',
					type: 'dict-select',
					status:true,
					search: { show: false },
					dict: dict({
						data: [
							{ label: '启用', value: true },
							{ label: '禁用', value: false },
						],
					}),
					column: { minWidth: 100 },
					form: {
						show:true,
						value:true,
						component: { placeholder: '请选择状态' },
						rules: [{ required: true, message: '请选择状态' }],
					},
					viewForm:{
						component: { placeholder: '' },
						rules: [{ required: true, message: '' }],
					}
				},
				...commonCrudConfig({
					create_datetime: { search: false },
					update_datetime: { search: false },
				}),
			},
		},
	};
};
