import * as api from './api';
import { dict, UserPageQuery, AddReq, DelReq, EditReq, compute, CreateCrudOptionsProps, CreateCrudOptionsRet } from '@fast-crud/fast-crud';
import {GetList} from '../contract/api';
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) => {
		return await api.AddObj(form);
	};

	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
					},
				}
			},
			request: {
				pageRequest,
				addRequest,
				editRequest,
				delRequest,
			},
			search: {
				col: { span: 3 },
				show: true,
				autoSearch:false,
				buttons: {
					search: {
						size: 'small', // 设置查询按钮大小为small
					},
					reset: {
						size: 'small', // 设置重置按钮大小为small
					}
				}
			},
			actionbar: {
				buttons: {
					add: {
						size: 'small',
						show: true//auth('company:Create'),
					},
				},
			},
			rowHandle: {
				//固定右侧
				fixed: 'right',
				width: 260,
				buttons: {
					view: {
						size: 'small',
						text: '详细信息',
						iconRight: 'View',
						type: 'text',
						show: true,//auth('company:Detail'),
					},
					viewContract: {
						size: 'small',
						text: '查看合约',
						iconRight: '',
						type: 'text',
						show: true,//auth('company:ViewContract'),
						click: async ({ row }) => {
							const res = await GetList({
								tenant_id: row.id,
								page: 1,
								page_size: 10
							});
							// TODO: 实现查看合约逻辑，例如导航到合约页面
							res.data.forEach((item: any) => {
								console.log(item.tenant,row.tenant_id);
								if(item.tenant==row.tenant_id){
									console.log(item);
									router.push({
										path: '/system/contract/preview',
										query: {
											file: item.document_link,
											contractName: item.contract_name
										}
									});
								}
							});
						}
					},
					edit: {
						show: false,
					},
					remove: {
						show: false,
					},
				},
			},
			columns: {
				_index: {
					title: '序号',
					form: { show: false },
					column: {
						type: 'index',
						align: 'center',
						width: '70px',
						columnSetDisabled: true, //禁止在列设置中选择
					},
				},
				code:{
					title: '企业编码',
					type: 'input',
					search: { show: true },
					form: { 
						rules: [{ required: true, message: '企业编码必填' }],
						component: { placeholder: '请输入企业编码' },
					},
					column: { minWidth: 120 },
				},
				name: {
					title: '企业名称',
					search: {
						show: true,
					},
					type: 'input',
					form: {
						rules: [{ required: true, message: '企业名称必填' }],
						component: { placeholder: '请输入企业名称' },
					},
					column: { minWidth: 120 },
				},
				company_size: {
					title: '企业规模',
					type: 'dict-select',
					search: { show: true },
					form: { 
						component: { 
							placeholder: '请选择企业规模',
						} 
					},
					column: { minWidth: 100 },
					dict: dict({
						data: [
							{ value: '1', label: '1-10人' },
							{ value: '2', label: '11-50人' },
							{ value: '3', label: '51-200人' },
							{ value: '4', label: '201-500人' },
							{ value: '5', label: '501-1000人' },
							{ value: '6', label: '1000人以上' }
						]
					}),
				},
				company_address: {
					title: '企业地点',
					type: 'input',
					search: { show: true },
					form: { component: { placeholder: '请输入企业地点' } },
					column: { minWidth: 120 },
				},
				industry: {
					title: '行业',
					type: 'dict-select',
					search: { show: true },
					form: { 
						component: { 
							placeholder: '请选择行业',
						} 
					},
					column: { minWidth: 100 },
					dict: dict({
						data: [
							{ value: '1', label: '互联网/软件' },
							{ value: '2', label: '金融/保险' },
							{ value: '3', label: '制造业' },
							{ value: '4', label: '教育培训' },
							{ value: '5', label: '医疗健康' },
							{ value: '6', label: '房地产/建筑' },
							{ value: '7', label: '零售/电商' },
							{ value: '8', label: '餐饮/酒店' },
							{ value: '9', label: '物流/运输' },
							{ value: '10', label: '咨询服务' },
							{ value: '11', label: '媒体/广告' },
							{ value: '12', label: '能源/化工' },
							{ value: '13', label: '政府机构' },
							{ value: '14', label: '非营利组织' },
							{ value: '15', label: '其他' }
						]
					}),
				},
				status: {
					title: '状态',
					search: { show: true },
					type: 'dict-select',
					form: { component: { placeholder: '请选择状态' } },
					column: { minWidth: 120 },
					dict: dict({
						data: [
							{ value: '1', label: '服务中' },
							{ value: '2', label: '试用中' },
							{ value: '0', label: '已停用' }
						]
					}),
				},
				published_jobs_count: {
					title: '岗位数',
					type: 'number',
					column: { minWidth: 90 },
					
				},
				/* usageCount: {
					title: '使用次数',
					type: 'number',
					column: { minWidth: 100 },
				}, */
				update_datetime: {
					title: '数据更新时间',
					type: 'datetime',
					column: { minWidth: 150 },
					form:{
						show:false
					}
				},
			},
		},
	};
}; 