<?php
if (!defined('ABSPATH')) {
	exit;
}

$probonoseo_manage_notice = '';
$probonoseo_manage_notice_type = '';

if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['probonoseo_reset_free_settings'])) {
	if (!isset($_POST['probonoseo_manage_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['probonoseo_manage_nonce'])), 'probonoseo_manage_action')) {
		$probonoseo_manage_notice = '認証に失敗しました。';
		$probonoseo_manage_notice_type = 'error';
	} else {
		$probonoseo_free_keys = array(
			'probonoseo_basic_title',
			'probonoseo_title_separator',
			'probonoseo_title_sitename',
			'probonoseo_title_h1_check',
			'probonoseo_title_category',
			'probonoseo_title_duplicate',
			'probonoseo_title_symbols',
			'probonoseo_basic_metadesc',
			'probonoseo_meta_extraction',
			'probonoseo_meta_keywords',
			'probonoseo_meta_summary',
			'probonoseo_meta_forbidden',
			'probonoseo_meta_length',
			'probonoseo_meta_duplicate',
			'probonoseo_basic_canonical',
			'probonoseo_canonical_auto',
			'probonoseo_canonical_slash',
			'probonoseo_canonical_merge',
			'probonoseo_canonical_params',
			'probonoseo_basic_ogp',
			'probonoseo_ogp_title',
			'probonoseo_ogp_desc',
			'probonoseo_ogp_image_auto',
			'probonoseo_ogp_image_fixed',
			'probonoseo_ogp_facebook',
			'probonoseo_ogp_line',
			'probonoseo_ogp_thumbnail',
			'probonoseo_ogp_size_detect',
			'probonoseo_ogp_alt',
			'probonoseo_ogp_japanese_url',
			'probonoseo_basic_twitter',
			'probonoseo_basic_schema',
			'probonoseo_basic_breadcrumb',
			'probonoseo_internal_prev_next',
			'probonoseo_internal_category',
			'probonoseo_internal_child_pages',
			'probonoseo_internal_related',
			'probonoseo_internal_tag_logic',
			'probonoseo_internal_nofollow',
			'probonoseo_internal_category_format',
			'probonoseo_speed_lazy_images',
			'probonoseo_speed_lazy_iframes',
			'probonoseo_speed_minify_css',
			'probonoseo_speed_minify_js',
			'probonoseo_speed_optimize_wp_scripts',
			'probonoseo_article_heading_check',
			'probonoseo_article_alt_check',
			'probonoseo_article_image_count',
			'probonoseo_article_word_count',
			'probonoseo_article_category_match',
			'probonoseo_article_tag_duplicate',
			'probonoseo_diagnosis_title_duplicate',
			'probonoseo_diagnosis_meta_duplicate',
			'probonoseo_diagnosis_speed',
			'probonoseo_meta_cleanup',
			'probonoseo_gsc_verify',
			'probonoseo_aio_faq_schema',
			'probonoseo_aio_article_schema',
			'probonoseo_aio_organization_schema',
			'probonoseo_aio_robots',
		);
		foreach ($probonoseo_free_keys as $probonoseo_key) {
			delete_option($probonoseo_key);
		}
		$probonoseo_manage_notice = '設定をリセットしました。';
		$probonoseo_manage_notice_type = 'success';
	}
}
?>

<?php if (!empty($probonoseo_manage_notice)) : ?>
<div class="notice notice-<?php echo esc_attr($probonoseo_manage_notice_type); ?> is-dismissible probonoseo-notice">
	<p><?php echo esc_html($probonoseo_manage_notice); ?></p>
</div>
<?php endif; ?>

<div class="probonoseo-section">
	<h2 class="probonoseo-section-title">プラグイン情報</h2>
	<p class="probonoseo-section-description">ProbonoSEO Basicの状態と設定管理を行います。</p>
</div>

<div class="probonoseo-info-grid">
	<div class="probonoseo-info-left">

		<div class="probonoseo-card">
			<h3 class="probonoseo-card-title">プラグイン情報</h3>
			<div class="probonoseo-info-list">
				<div class="probonoseo-info-row">
					<span class="probonoseo-info-label">プラグインバージョン</span>
					<span class="probonoseo-info-value"><?php echo esc_html(PROBONOSEO_VERSION); ?></span>
				</div>
				<div class="probonoseo-info-row">
					<span class="probonoseo-info-label">エディション</span>
					<span class="probonoseo-info-value"><span class="probonoseo-status-free">Basic版</span></span>
				</div>
				<div class="probonoseo-info-row">
					<span class="probonoseo-info-label">利用可能機能</span>
					<span class="probonoseo-info-value">75項目</span>
				</div>
				<div class="probonoseo-info-row">
					<span class="probonoseo-info-label">PHP バージョン</span>
					<span class="probonoseo-info-value"><?php echo esc_html(PHP_VERSION); ?></span>
				</div>
				<div class="probonoseo-info-row">
					<span class="probonoseo-info-label">WordPress バージョン</span>
					<span class="probonoseo-info-value"><?php echo esc_html(get_bloginfo('version')); ?></span>
				</div>
			</div>
		</div>

	</div>

	<div class="probonoseo-info-right">

		<div class="probonoseo-card">
			<h3 class="probonoseo-card-title">設定のエクスポート</h3>
			<p class="probonoseo-card-description">現在の設定をJSONファイルとしてダウンロードします。</p>
			<div class="probonoseo-card-action">
				<button type="button" id="probonoseo-export-settings" class="button button-primary" data-export-nonce="<?php echo esc_attr(wp_create_nonce('probonoseo_export_settings')); ?>">
					<span class="dashicons dashicons-download probonoseo-btn-icon"></span>
					設定をエクスポート
				</button>
			</div>
		</div>

		<div class="probonoseo-card probonoseo-card-danger">
			<h3 class="probonoseo-card-title probonoseo-card-title-danger">設定のリセット</h3>
			<p class="probonoseo-card-description">設定を初期状態に戻します。この操作は取り消せません。</p>
			<form method="post" action="">
				<?php wp_nonce_field('probonoseo_manage_action', 'probonoseo_manage_nonce'); ?>
				<div class="probonoseo-card-action">
					<button type="submit" name="probonoseo_reset_free_settings" value="1" class="button" onclick="return confirm('設定をすべてリセットします。この操作は取り消せません。続行しますか？');">設定をリセット</button>
				</div>
			</form>
		</div>

	</div>
</div>

<script>
(function() {
	document.addEventListener('DOMContentLoaded', function() {
		var exportBtn = document.getElementById('probonoseo-export-settings');
		if (exportBtn) {
			exportBtn.addEventListener('click', function() {
				var nonce = exportBtn.getAttribute('data-export-nonce');
				if (!nonce || typeof ajaxurl === 'undefined') {
					alert('エラーが発生しました。');
					return;
				}
				var originalHtml = exportBtn.innerHTML;
				exportBtn.disabled = true;
				exportBtn.textContent = 'エクスポート中...';
				var formData = new FormData();
				formData.append('action', 'probonoseo_export_settings');
				formData.append('nonce', nonce);
				fetch(ajaxurl, {
					method: 'POST',
					credentials: 'same-origin',
					body: formData
				}).then(function(response) {
					return response.json();
				}).then(function(response) {
					if (!response || !response.success || !response.data || !response.data.settings) {
						alert('エクスポートに失敗しました。');
						return;
					}
					var jsonString = JSON.stringify(response.data.settings, null, 2);
					var blob = new Blob([jsonString], { type: 'application/json' });
					var url = URL.createObjectURL(blob);
					var a = document.createElement('a');
					a.href = url;
					a.download = response.data.filename || 'probonoseo-settings.json';
					document.body.appendChild(a);
					a.click();
					document.body.removeChild(a);
					URL.revokeObjectURL(url);
				}).catch(function() {
					alert('エラーが発生しました。');
				}).finally(function() {
					exportBtn.disabled = false;
					exportBtn.innerHTML = originalHtml;
				});
			});
		}
	});
})();
</script>