import { defineAsyncComponent, h } from 'vue';
import LucideGlobe from '~icons/lucide/globe';
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 Domain',
	whitelistedMethods: {
		renew: 'renew',
		rename: 'rename',
		dropDomain: 'drop_domain',
		addTag: 'add_resource_tag',
		removeTag: 'remove_resource_tag'
	},
	list: {
		route: '/domains',
		title: '域名',
		fields: [
			'name',
			'domain',
			'status',
			'domain_owner',
			'domain_registrar',
			'registration_date',
			'end_date',
			'price',
			'period',
			'auto_renew',
			'team',
			'order_id',
			'description'
		],
		filterControls() {
			return [
				{
					type: 'select',
					label: '状态',
					fieldname: 'status',
					options: [
						{ label: '', value: '' },
						{ label: '正常', value: 'ok' },
						{ label: '锁定', value: 'clienthold' }
					]
				}
			];
		},
		orderBy: 'creation desc',
		searchField: 'domain',
		columns: [
			{
				label: '域名',
				fieldname: 'domain',
				width: 2,
				class: 'font-medium',
				format(value) {
					return value;
				}
			},
			{ 
				label: '状态', 
				fieldname: 'status', 
				type: 'Badge', 
				width: 0.8,
				format(value) {
					const statusMap = {
						'ok': '正常',
						'clienthold': '锁定'
					};
					return statusMap[value] || value;
				}
			},
			{
				label: '注册时间',
				fieldname: 'registration_date',
				format(value) {
					if (!value) return '-';
					return value;
				}
			},
			{
				label: '到期时间',
				fieldname: 'end_date',
				format(value) {
					if (!value) return '-';
					return value;
				}
			}
		],
		primaryAction({ listResource: domains }) {
			return {
				label: '新建域名',
				variant: 'solid',
				slots: {
					prefix: icon('plus')
				},
				onClick() {
					router.push('/domains/new');
				}
			};
		},
		statusBadge({ documentResource: domain }) {
			const status = domain.pg?.status;
			const statusMap = {
				'Pending': '待处理',
				'Active': '正常',
				'Expired': '已过期',
				'Suspended': '已暂停',
				'Cancelled': '已取消'
			};
			return {
				label: statusMap[status] || status
			};
		},
		breadcrumbs({ documentResource: domain }) {
			return [
				{
					label: '域名',
					route: '/domains'
				},
				{
					label: domain.pg?.domain || domain.pg?.name,
					route: `/domains/${domain.pg?.name}`
				}
			];
		},
		actions({ documentResource: domain }) {
			if (!domain) return [];
			
			const actions = [
				{
					label: '续费',
					icon: 'refresh-cw',
					onClick() {
						domain.renew.submit();
					},
					condition: () => domain.pg?.status === 'Active'
				},
				{
					label: '重命名',
					icon: 'edit-3',
					onClick() {
						domain.rename.submit();
					}
				},
				{
					label: '删除',
					icon: 'trash-2',
					onClick() {
						domain.dropDomain.submit();
					},
					condition: () => domain.pg?.status !== 'Active'
				}
			];
			return actions.filter(action => !action.condition || action.condition());
		}
	},
	detail: {
		route: '/domains/:name',
		title: '域名详细信息',
		tabs: [
			{
				label: '概览',
				route: '',
				type: 'Component',
				component: defineAsyncComponent(() => import('../components/JsiteDomainOverview.vue')),
				props: domain => {
					return { domain: domain.pg?.name };
				}
			},
			{
				label: '域名解析',
				route: 'dns',
				type: 'Component',
				component: defineAsyncComponent(() => import('../components/JsiteDomainDNSRecords.vue')),
				props: domain => {
					return { domain: domain.pg?.name };
				}
			},
			{
				label: '所有者模板',
				route: 'owners',
				type: 'Component',
				component: defineAsyncComponent(() => import('../components/DomainOwner.vue')),
				props: domain => {
					return { domain: domain.pg?.name };
				}
			}
		],
		fields: [
			{
				label: '基本信息',
				fields: [
					'domain',
					'status',
					'domain_owner',
					'domain_registrar',
					'registration_date',
					'end_date'
				]
			},
			{
				label: '价格信息',
				fields: [
					'price',
					'period',
					'auto_renew',
					'order_id'
				]
			},
			{
				label: 'DNS设置',
				fields: [
					'dns_host1',
					'dns_host2',
					'dns_host3',
					'dns_host4',
					'dns_host5',
					'dns_host6'
				]
			},
			{
				label: '其他信息',
				fields: [
					'description',
					'whois_protection'
				]
			}
		],
		actions({ documentResource: domain }) {
			return [];
		}
	}
}; 