import * as api from './api';
import * as companyApi from '../company/api';
import { dict, UserPageQuery, AddReq, DelReq, EditReq, compute, CreateCrudOptionsProps, CreateCrudOptionsRet } from '@fast-crud/fast-crud';
import { dictionary } from '/@/utils/dictionary';
import { successMessage } from '/@/utils/message';
import { auth } from '/@/utils/authFunction';
import { useRouter } from 'vue-router';

export const createCrudOptions = function ({ crudExpose }: CreateCrudOptionsProps): CreateCrudOptionsRet {
	const router = useRouter();
	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) => {
		form.document_link = form.document_link[0];
		return await api.AddObj(form);
	};

	const handleViewContract = (row: any) => {
		console.log(row.document_link);
		router.push({
			path: '/system/contract/preview',
			query: {
				file: row.document_link,
				contractName: row.contract_name
			}
		});
	};

	return {
		crudOptions: {
			toolbar:{
				buttons:{
					search:{show:false},
					refresh:{show:false},
					compact:{show:false},
					export:{
						text: '导出',
						type: 'primary',
						size: 'small',
						icon: 'upload',
						circle: false,
						display: true,
						show:false
					},
					columns:{
						show:false
					},
				}
			},
			search: {
				col: { span: 3 },
				show: true,
				autoSearch:false,
				buttons: {
					search: {
						size: 'small',
					},
					reset: {
						size: 'small',
					}
				}
			},
			request: {
				pageRequest,
				addRequest,
				editRequest,
				delRequest,
			},
			actionbar: {
				buttons: {
					add: {
						show: true,
					},
				},
			},
			rowHandle: {
				fixed: 'right',
				width: 200,
				buttons: {
					view: { text: '详细信息', iconRight: 'View', type: 'text', show: true },
					edit: { iconRight: 'Edit', type: 'text', show: false },
					remove: { iconRight: 'Delete', type: 'text', show: false },
					Viewcontract: {
						text:'查看合约',
						iconRight:'',
						type:'text',
						show:true,
						click: (opts: any) => {
							handleViewContract(opts.row);
						}
					},
				},
			},
			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}
				},
				contract_no: {
					title: '合约编号',
					search: { show: true },
					type: 'input',
					form: { 
						rules: [{ required: true, message: '合约编号必填' }],
						component: { placeholder: '请输入合约编号' }
					},
					column: { minWidth: 150 },
				},
				document_link: {
					title: '合约文档',
					type: 'file-uploader',
					form: {
						/* rules: [{ required: true, message: '请上传合约文档' }], */
						component: {
							uploader: {
								type: 'form',
								action: '/api/uploadfile/',
								accept: '.pdf,.doc,.docx',
								maxSize: 10 * 1024 * 1024, // 10MB
								limit: 1,
							},
							placeholder: '请上传PDF或Word格式的合约文档',
							helpMessage: '支持PDF、Word格式，文件大小不超过10MB'
						}
					},
					column: {
						show:false,
						component: {
							// 自定义显示
							render: ({ value }) => {
								if (!value) return '未上传';
								const fileName = value.split('/').pop();
								return <el-link type="primary" href={value} target="_blank">{fileName}</el-link>;
							}
						},
						minWidth: 180
					}
				},
				title: {
					title: '合约名称',
					search: { show: true },
					type: 'input',
					form: { 
						rules: [{ required: true, message: '合约名称必填' }],
						component: { placeholder: '请输入合约名称' }
					},
					column: { minWidth: 180 },
				},
				tenant: {
					title: '企业名称',
					search: { show: true },
					type: 'dict-select',
					dict: dict({
						url: '/api/platform/tenants/',
						value: 'tenant_id',
						label: 'name',
						getData: (url) => {
							return companyApi.GetList({ page: 1, limit: 20 }).then((res:any) => {
								return res.data.results;
							});
						}
					}),
					form: { 
						rules: [{ required: true, message: '企业名称必填' }],
						component: { 
							placeholder: '请选择企业名称',
							filterable: true,
							clearable: true
						}
					},
					column: { minWidth: 120 },
				},
				total_amount: {
					title: '合约金额',
					type: 'number',
					form: {
						rules: [{ required: true, message: '合约金额必填' }],
						component: { 
							placeholder: '请输入合约金额',
							precision: 2,
							step: 1000
						}
					},
					column: { minWidth: 120 },
				},
				effective_date: {
					title: '开始时间',
					type: 'datetime',
					search: { show: true },
					form: {
						rules: [{ required: true, message: '开始时间必填' }],
						component: { 
							type: 'datetime',
							valueFormat: 'YYYY-MM-DD',placeholder: '请选择开始时间' }
					},
					column: { minWidth: 150 },
				},
				expire_date: {
					title: '结束时间',
					type: 'datetime',
					search: { show: true },
					form: {
						rules: [{ required: true, message: '结束时间必填' }],
						component: { type: 'datetime',
							valueFormat: 'YYYY-MM-DD',placeholder: '请选择结束时间' }
					},
					column: { minWidth: 150 },
				},
				status: {
					title: '合约状态',
					search: { show: true },
					type: 'dict-select',
					form: {
						rules: [{ required: true, message: '合约状态必填' }],
						component: { placeholder: '请选择合约状态' }
					},
					column: { minWidth: 120 },
					dict: dict({
						data: [
							{ value: 1, label: '服务中' },
							{ value: 2, label: '试用中' },
							{ value: 0, label: '已停用' }
						]
					}),
				},
				party_b_contact: {
					title: '联系人',
					type: 'input',
					form: {
						rules: [{ required: true, message: '联系人必填' }],
						component: { placeholder: '请输入联系人姓名' }
					},
					column: { minWidth: 120 },
				},
				party_b_phone: {
					title: '联系电话',
					type: 'input',
					form: {
						rules: [
							{ required: true, message: '联系电话必填' },
							{ pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码' }
						],
						component: { placeholder: '请输入联系电话' }
					},
					column: { minWidth: 130 },
				},
				/* contact_email: {
					title: '联系邮箱',
					type: 'input',
					form: {
						rules: [
							{ required: true, message: '联系邮箱必填' },
							{ type: 'email', message: '请输入正确的邮箱格式' }
						],
						component: { placeholder: '请输入联系邮箱' }
					},
					column: { minWidth: 180 },
				}, */
				contract_type_display: {
					title: '服务描述',
					type: 'textarea',
					form: {
						rules: [{ required: true, message: '服务描述必填' }],
						component: { 
							placeholder: '请输入服务描述',
							showWordLimit: true,
							maxlength: 500,
							rows: 3
						}
					},
					column: { minWidth: 200 },
				},
				/* payment_method: {
					title: '支付方式',
					type: 'dict-select',
					form: {
						rules: [{ required: true, message: '支付方式必填' }],
						component: { placeholder: '请选择支付方式' }
					},
					column: { minWidth: 120 },
					dict: dict({
						data: [
							{ value: 1, label: '一次性付款' },
							{ value: 2, label: '分期付款' },
							{ value: 3, label: '其他' }
						]
					}),
				}, */
				remark: {
					title: '备注',
					type: 'textarea',
					form: { 
						component: { 
							placeholder: '请输入备注',
							showWordLimit: true,
							maxlength: 200,
							rows: 2
						}
					},
					column: { minWidth: 200 },
				},
			},
		},
	};
}; 