import { defineAsyncComponent, h } from 'vue';
import LucideServer from '~icons/lucide/server';
import { getTeam } from '../data/team';
import router from '../router';
import { icon } from '../utils/components';
import { duration, planTitle, userCurrency } from '../utils/format';
import { trialDays } from '../utils/site';
import { getJobsTab } from './common/jobs';
import { tagTab } from './common/tags';

export default {
	pagetype: 'Jsite Server',
	whitelistedMethods: {
		reboot: 'reboot',
		rename: 'rename',
		dropServer: 'drop_server',
		addTag: 'add_resource_tag',
		removeTag: 'remove_resource_tag'
	},
	list: {
		route: '/jsite-servers',
		title: '服务器',
		fields: [
			'name',
			'title',
			'status',
			'region',
			'cpu',
			'memory',
			'disk_size',
			'public_ip',
			'private_ip',
			'end_date',
			'bandwidth',
			'team',
			'instance_id',
			'order_id',
			'planid',
			'image_id',
			'system'
		],
		filterControls() {
			return [
				{
					type: 'select',
					label: '状态',
					fieldname: 'status',
					options: [
						{ label: '', value: '' },
						{ label: '准备中', value: 'Pending' },
						{ label: '启动中', value: 'Starting' },
						{ label: '运行中', value: 'Running' },
						{ label: '停止中', value: 'Stopping' },
						{ label: '已停止', value: 'Stopped' },
						{ label: '重置中', value: 'Resetting' },
						{ label: '升级中', value: 'Upgrading' },
						{ label: '已禁用', value: 'Disabled' }
					]
				},
				{
					type: 'select',
					label: '区域',
					fieldname: 'region',
					options: [
						{ label: '', value: '' },
						{ label: '华北1（青岛）', value: 'cn-qingdao' },
						{ label: '华北2（北京）', value: 'cn-beijing' },
						{ label: '华北3（张家口）', value: 'cn-zhangjiakou' },
						{ label: '华北5（呼和浩特）', value: 'cn-huhehaote' },
						{ label: '华东1（杭州）', value: 'cn-hangzhou' },
						{ label: '华东2（上海）', value: 'cn-shanghai' },
						{ label: '华南1（深圳）', value: 'cn-shenzhen' },
						{ label: '华南2（河源）', value: 'cn-heyuan' },
						{ label: '西南1（成都）', value: 'cn-chengdu' },
						{ label: '华南3（广州）', value: 'cn-guangzhou' },
						{ label: '华北6（乌兰察布）', value: 'cn-wulanchabu' },
						{ label: '华东5（南京）', value: 'cn-nanjing' },
						{ label: '华东6（福州）', value: 'cn-fuzhou' },
						{ label: '华中1（武汉）', value: 'cn-wuhan-lr' },
						{ label: '中国香港', value: 'cn-hongkong' },
						{ label: '新加坡', value: 'ap-southeast-1' },
						{ label: '马来西亚（吉隆坡）', value: 'ap-southeast-3' },
						{ label: '印度尼西亚（雅加达）', value: 'ap-southeast-5' },
						{ label: '日本（东京）', value: 'ap-northeast-1' },
						{ label: '美国（硅谷）', value: 'us-west-1' },
						{ label: '美国（弗吉尼亚）', value: 'us-east-1' },
						{ label: '德国（法兰克福）', value: 'eu-central-1' },
						{ label: '英国（伦敦）', value: 'eu-west-1' },
						{ label: '菲律宾（马尼拉）', value: 'ap-southeast-6' },
						{ label: '泰国（曼谷）', value: 'ap-southeast-7' },
						{ label: '韩国（首尔）', value: 'ap-northeast-2' }
					]
				}
			];
		},
		orderBy: 'creation desc',
		searchField: 'title',
		columns: [
			{
				label: '服务器',
				fieldname: 'name',
				width: 1.5,
				class: 'font-medium',
				format(value, row) {
					return row.title || value;
				}
			},
			{ 
				label: '状态', 
				fieldname: 'status', 
				type: 'Badge', 
				width: 0.8,
				format(value) {
					const statusMap = {
						'Pending': '准备中',
						'Starting': '启动中',
						'Running': '运行中',
						'Stopping': '停止中',
						'Stopped': '已停止',
						'Resetting': '重置中',
						'Upgrading': '升级中',
						'Disabled': '已禁用'
					};
					return statusMap[value] || value;
				}
			},
			{
				label: '配置',
				fieldname: 'cpu',
				format(value, row) {
					const cpu = row.cpu || '未知';
					const memory = row.memory || '未知';
					const disk = row.disk_size || '未知';
					return `${cpu}核/${memory}GB/${disk}GB`;
				}
			},
			{
				label: '公网IP',
				fieldname: 'public_ip',
				format(value) {
					return value || '-';
				}
			},
			{
				label: '区域',
				fieldname: 'region',
				format(value) {
					if (!value) return '-';
					
					// 区域ID到中文名称的映射表
					const regionMap = {
						'cn-qingdao': '华北1（青岛）',
						'cn-beijing': '华北2（北京）',
						'cn-zhangjiakou': '华北3（张家口）',
						'cn-huhehaote': '华北5（呼和浩特）',
						'cn-hangzhou': '华东1（杭州）',
						'cn-shanghai': '华东2（上海）',
						'cn-shenzhen': '华南1（深圳）',
						'cn-heyuan': '华南2（河源）',
						'cn-chengdu': '西南1（成都）',
						'cn-guangzhou': '华南3（广州）',
						'cn-wulanchabu': '华北6（乌兰察布）',
						'cn-nanjing': '华东5（南京）',
						'cn-fuzhou': '华东6（福州）',
						'cn-wuhan-lr': '华中1（武汉）',
						'cn-hongkong': '中国香港',
						'ap-southeast-1': '新加坡',
						'ap-southeast-3': '马来西亚（吉隆坡）',
						'ap-southeast-5': '印度尼西亚（雅加达）',
						'ap-northeast-1': '日本（东京）',
						'us-west-1': '美国（硅谷）',
						'us-east-1': '美国（弗吉尼亚）',
						'eu-central-1': '德国（法兰克福）',
						'eu-west-1': '英国（伦敦）',
						'ap-southeast-6': '菲律宾（马尼拉）',
						'ap-southeast-7': '泰国（曼谷）',
						'ap-northeast-2': '韩国（首尔）'
					};
					
					// 如果是RegionId，返回对应的中文名称；如果已经是中文名称，直接返回
					return regionMap[value] || value;
				}
			},
			{
				label: '到期时间',
				fieldname: 'end_date',
				format(value) {
					if (!value) return '-';
					return value;
				}
			}
		],
		primaryAction({ listResource: jsiteServers }) {
			return {
				label: '新建服务器',
				variant: 'solid',
				slots: {
					prefix: icon('plus')
				},
				onClick() {
					router.push('/jsite-servers/new');
				}
			};
		},
		statusBadge({ documentResource: jsiteServer }) {
			const status = jsiteServer.pg?.status;
			const statusMap = {
				'Pending': '准备中',
				'Starting': '启动中',
				'Running': '运行中',
				'Stopping': '停止中',
				'Stopped': '已停止',
				'Resetting': '重置中',
				'Upgrading': '升级中',
				'Disabled': '已禁用'
			};
			return {
				label: statusMap[status] || status
			};
		},
		breadcrumbs({ documentResource: jsiteServer }) {
			return [
				{
					label: '服务器',
					route: '/jsite-servers'
				},
				{
					label: jsiteServer.pg?.title || jsiteServer.pg?.name,
					route: `/jsite-servers/${jsiteServer.pg?.name}`
				}
			];
		},
		actions({ documentResource: jsiteServer }) {
			if (!jsiteServer) return [];
			
			const actions = [
				{
					label: '重启',
					icon: 'refresh-cw',
					onClick() {
						jsiteServer.reboot.submit();
					},
					condition: () => jsiteServer.pg?.status === 'Running'
				},
				{
					label: '重命名',
					icon: 'edit-3',
					onClick() {
						jsiteServer.rename.submit();
					}
				},
				{
					label: '删除',
					icon: 'trash-2',
					onClick() {
						jsiteServer.dropServer.submit();
					},
					condition: () => jsiteServer.pg?.status !== 'Running'
				}
			];
			return actions.filter(action => !action.condition || action.condition());
		}
	},
	detail: {
		route: '/jsite-servers/:name',
		title: '服务器详细信息',
		tabs: [
			{
				label: '概览',
				route: '',
				type: 'Component',
				component: defineAsyncComponent(() => import('../components/JsiteServerOverview.vue')),
				props: jsiteServer => {
					return { server: jsiteServer.pg?.name };
				}
			},
			{
				label: '防火墙',
				route: 'firewall',
				type: 'Component',
				component: defineAsyncComponent(() => import('../components/JsiteServerFirewallRules.vue')),
				props: jsiteServer => {
					return { server: jsiteServer.pg?.name };
				}
			}
		],
		fields: [
			{
				label: '基本信息',
				fields: [
					'title',
					'status',
					'region',
					'instance_id',
					'order_id'
				]
			},
			{
				label: '服务器配置',
				fields: [
					'cpu',
					'memory',
					'disk_size',
					'bandwidth',
					'public_ip',
					'image_id',
					'planid',
					'system'
				]
			},
			{
				label: 'SSH连接',
				fields: [
					'ssh_user',
					'ssh_port',
					'password',
					'key_pair_name',
					'private_key'
				]
			},
			{
				label: '其他信息',
				fields: [
					'end_date',
					'period'
				]
			}
		],
		actions({ documentResource: jsiteServer }) {
			return [];
		}
	}
}; 