<?php if ( !defined( 'ABSPATH' ) ) exit;//PHPの宣言と直接アクセスの拒否
/* セクションID haumenu_define_section_id_06_addcontent() */
/* タブのスラグ haumenu_define_tab_slug_06_addcontent() */
/* $option_name 設定名 haumenu_define_option_name_06_addcontent() */
/* get_option GetOP06_addcontent() */

/***** 親設定の有効／無効に合わせて子設定の表示方法を変更する *****/
/* 関数名・設定名は画面ごとに変えること（関数重複を防ぐ） */
/* add_action('admin_notices', 'current_pagehook');を追加してページ名を取得し、$page_fooksuffixの値を変える */
/* 連動マップ（ID => セレクター）の項目を編集する */

function haup_child_setting_view_control_06_addcontent( $hook_suffix ) {
	//ページフックにより出力を判断
	$page_fooksuffix = 'ha-utilitiy_page_haumenu_page_addcontent';

	if ( $page_fooksuffix !== $hook_suffix ) {
		return;
	}

	//メインスクリプトのハンドル
	$handle = 'haup-child-setting-control-js';

	// メインスクリプトが登録されていなければ終了(スクリプトの読み込みはmain-menu.phpで記述)
	if ( ! wp_script_is( $handle, 'registered' ) ) {
		return;
	}

	//設定名
	$set_name = haumenu_define_option_name_06_addcontent();

	// 連動マップ（ID => セレクター）
	// 1つのIDに対して複数のCSSクラスを指定する（1つなら1行）
	$dependency_map = array(
		$set_name . 'use-top-home-scroll' => array(
			'.home-top-button-align-wrapper',
			'.top-home-scroll-content-color', // ボタンのアイコンと枠線色
			'.top-home-scroll-int-bg-color-wrapper', // ボタンの内部色
			'.enable-top-home-s-scroll-wrapper', // スムーススクロールを有効にする
		),

	);

	$inner_js = "";
	foreach ( $dependency_map as $id => $selectors ) {
		foreach ( (array) $selectors as $selector ) {
			// translators: %s stands for the CSS Class & ID.
			$inner_js .= sprintf(
				"haup_apply_dependency('%s', '%s');\n",
				esc_js( $id ),
				esc_js( $selector )
			);
		}
	}

	if ( $inner_js ) {
		$js_code = "if (window.haup_apply_dependency) {\n" . $inner_js . "}";
		// 第3引数を 'after' にすることで、本体JSの直後に出力
		wp_add_inline_script( $handle, $js_code, 'after' );
	}
}
add_action( 'admin_enqueue_scripts', 'haup_child_setting_view_control_06_addcontent', 20 );

/*** ボタンを表示させる ***/
/* 項目の追加とデータの更新 */
function haumenu_init_field_is_use_top_home_scroll(){
//項目の追加
	add_settings_field(
		'use-top-home-scroll', // 設定名
		__('Show Scroll Top & Return home Button','hima-art-utility'), // 設定タイトル
		'haumenu_is_use_top_home_scroll', // 設定項目のHTMLを出力する関数名
		haumenu_define_tab_slug_06_addcontent(), //表示させるタブのスラグ
		haumenu_define_section_id_06_addcontent(),// $section 表示させるセクションのID
		array( 'class' => 'ha-settings-separate' )
	);

}
add_action( 'admin_init', 'haumenu_init_field_is_use_top_home_scroll' );

/* 入力フォーム */
function haumenu_is_use_top_home_scroll() {
	$set_name = haumenu_define_option_name_06_addcontent(); //  $option_name 設定名
	$options = get_option($set_name);
	$option_name = 'use-top-home-scroll'; //個別配列名

	//テーブルに値がなかった場合の措置（PHP8.x以降への対応）
	if(isset($options[$option_name])){
		$checked = $options[$option_name];
	}else{
		$checked = '';
	}
?>
<label>
	<input type="checkbox" id="<?php echo esc_html($set_name . $option_name ); ?>" name="<?php echo esc_html($set_name . '[' .$option_name. ']'); ?>" class="wppd-ui-toggle" <?php checked( $checked , 1 ); ?> value="1">
	<p><?php esc_html_e('When enabled, the "Return to top of page" and "Return to home page" buttons will be displayed at the bottom right of the screen when scrolling the page.','hima-art-utility'); ?></p>
</label>
<details class="accordion"  style="margin: 5px 0px 0px 0px;overflow: hidden;">
<summary class="accordion__title" style="cursor: pointer;"><?php esc_html_e('When it does not transition even if it is enabled','hima-art-utility'); ?></summary>
<div class="accordion__content">
<p><?php esc_html_e('This function uses the WordPress built-in function "wp_body_open" to output the target "&lt;div id=&quot;hau-page-head&quot;&gt;&lt;/div&gt;" tag below the body tag. Masu.','hima-art-utility'); ?></p>
<p><?php esc_html_e('If you click on the link output to the page with this function and it does not move to the top of the screen, your theme may not be compatible and this tag may not have been output.','hima-art-utility'); ?></p>
<p><?php esc_html_e('Please display the source of the page and check if the "&lt;div id=&quot;hau-page-head&quot;&gt;&lt;/div&gt;" tag is output near the bottom of the body tag, and if it is not output Please try the following','hima-art-utility'); ?></p>
<h3><?php esc_html_e('For classic theme','hima-art-utility'); ?></h3>
<ol>
	<li><?php esc_html_e('Find the template (usually "header.php") that outputs the body tag in your theme (parent theme)','hima-art-utility'); ?></li>
	<li><?php esc_html_e('As a test, add "&lt;div id=&quot;hau-page-head&quot;&gt;&lt;/div&gt;" under the body tag of the parent theme template and save.','hima-art-utility'); ?></li>
	<li><?php esc_html_e('Display the browsing page and check if it works properly','hima-art-utility'); ?></li>
	<li><?php esc_html_e('Since what was added to the parent theme will be overwritten when the theme is updated, copy this template file into the child page folder and add it to the parent theme "&lt;div id=&quot;hau-page-head&quot;&gt; Remove &lt;/div&gt;','hima-art-utility'); ?></li>
	<li><?php esc_html_e('Check the operation again, and if there is no problem, the response is complete','hima-art-utility'); ?></li>
</ol>
<h3><?php esc_html_e('For block theme','hima-art-utility'); ?></h3>
<ol>
	<li><?php esc_html_e('From the admin panel, go to Appearance -> Editor','hima-art-utility'); ?></li>
	<li><?php esc_html_e('Open the header template you are using on your site','hima-art-utility'); ?></li>
	<li><?php esc_html_e('Add a custom HTML block at the top, type &lt;div id=&quot;hau-page-head&quot;&lt;/div&gt; and save','hima-art-utility'); ?></li>
	<li><?php esc_html_e('Display the browsing page and check if it works properly','hima-art-utility'); ?></li>
</ol>
</div>
</details>
<?php
}

/* ボタン位置のプリセット */
/* 項目の追加とデータの更新 */
function haumenu_init_field_home_top_button_align(){
//項目の追加
	add_settings_field(
		'home-top-button-align', // 設定名
		__('How to position the button','hima-art-utility'), // 設定タイトル
		'haumenu_home_top_button_align', // 設定項目のHTMLを出力する関数名
		haumenu_define_tab_slug_06_addcontent(), //表示させるタブのスラグ
		haumenu_define_section_id_06_addcontent(),// $section 表示させるセクションのID
		array( 'class' => 'ha-settings-separate' )
	);

}
add_action( 'admin_init', 'haumenu_init_field_home_top_button_align' );

/* 入力フォーム */
function haumenu_home_top_button_align() {
	$set_name = haumenu_define_option_name_06_addcontent(); //  $option_name 設定名
	$options = get_option($set_name);
	$option_name = 'home-top-button-align'; //個別配列名

	if(isset($options[$option_name])){
		$selected = $options[$option_name];
	}else{
		$selected = '';
	}
?>
<div class="home-top-button-align-wrapper">
<select name= '<?php echo esc_html($set_name . '[' .$option_name. ']'); ?>' >
<option value='1' <?php selected($selected , 1 ); ?> selected ><?php esc_html_e('Calculate and display the distance from the right edge of the screen','hima-art-utility'); ?></option>
<option value='2' <?php selected($selected , 2 ); ?>><?php esc_html_e('Calculate and display the distance from the right edge of the content width','hima-art-utility'); ?></option>
</select>
<details class="accordion"  style="margin: 5px 0px 0px 0px;overflow: hidden;">
<summary class="accordion__title" style="cursor: pointer;"><?php esc_html_e('Learn more about the differences depending on the method you choose','hima-art-utility'); ?></summary>
<div class="accordion__content">
<h4><?php esc_html_e('If you select "Calculate and display distance from right edge of screen"','hima-art-utility'); ?></h4>
<p><?php esc_html_e('Displays the button at a position 15px from the right based on the right edge of the screen','hima-art-utility'); ?></p>
<p style="color:green"><?php esc_html_e('This is a method often used when arranging general buttons of the same type, and is a method that is less prone to display distortion.','hima-art-utility'); ?></p>
<p style="color:red"><?php esc_html_e('With this method, the content and buttons will be spaced apart when viewed on a monitor with a large screen resolution.','hima-art-utility'); ?></p>
<h4><?php esc_html_e('If you select "Calculate and display distance from right edge of content width"','hima-art-utility'); ?></h4>
<p><?php esc_html_e('Display the button based on the width of the content','hima-art-utility'); ?></p>
<p style="color:green"><?php esc_html_e('This style makes it easy to see the buttons because there is little gap between the buttons and the content depending on the resolution.','hima-art-utility'); ?></p>
<p style="color:red"><?php esc_html_e('The screen width is automatically calculated from the width specified in the wide global style or the wideSize value in theme.json, so it only works properly with block themes.','hima-art-utility'); ?></p>
</div>
</details>
</div>
<?php
}

/*** ボタンのアイコンと枠線色 ***/
/* 項目の追加とデータの更新 */
function haumenu_init_field_top_home_scroll_content_color(){
//項目の追加
	add_settings_field(
		'top-home-scroll-content-color', // 設定名
		__('Icon and border color','hima-art-utility'), // 設定タイトル
		'haumenu_top_home_scroll_content_color', // 設定項目のHTMLを出力する関数名
		haumenu_define_tab_slug_06_addcontent(), //表示させるタブのスラグ
		haumenu_define_section_id_06_addcontent(),// $section 表示させるセクションのID
		array( 'class' => 'ha-settings-separate' )
	);

}
add_action( 'admin_init', 'haumenu_init_field_top_home_scroll_content_color' );

/* 入力フォーム */
function haumenu_top_home_scroll_content_color() {
	$set_name = haumenu_define_option_name_06_addcontent(); //  $option_name 設定名
	$options = get_option($set_name);
	$option_name = 'top-home-scroll-content-color'; //個別配列名
?>
<div class="top-home-scroll-content-color">
<label>
	<input name="<?php echo esc_html($set_name . '[' .$option_name. ']'); ?>" class="color-picker-hex" type="text" id="<?php echo esc_html($set_name . $option_name); ?>" maxlength="7" value="<?php echo esc_attr($options[ $option_name ] ?? ''); ?>" placeholder="#000000" data-default-color="#000000">
</label>
<p><?php esc_html_e('Specifies the button icon and border color','hima-art-utility'); ?></p>
<p><?php esc_html_e('default is black (#000000)','hima-art-utility'); ?></p>
</div>
<?php
}

/*** ボタンの内部色 ***/
/* 項目の追加とデータの更新 */
function haumenu_init_field_top_home_scroll_internal_bg_color(){
//項目の追加
	add_settings_field(
		'top-home-scroll-int-bg-color', // 設定名
		__('Internal background color','hima-art-utility'), // 設定タイトル
		'haumenu_top_home_scroll_internal_bg_color', // 設定項目のHTMLを出力する関数名
		haumenu_define_tab_slug_06_addcontent(), //表示させるタブのスラグ
		haumenu_define_section_id_06_addcontent(),// $section 表示させるセクションのID
		array( 'class' => 'ha-settings-separate' )
	);

}
add_action( 'admin_init', 'haumenu_init_field_top_home_scroll_internal_bg_color' );

/* 入力フォーム */
function haumenu_top_home_scroll_internal_bg_color() {
	$set_name = haumenu_define_option_name_06_addcontent(); //  $option_name 設定名
	$options = get_option($set_name);
	$option_name = 'top-home-scroll-int-bg-color'; //個別配列名
?>
<div class="top-home-scroll-int-bg-color-wrapper">
<label>
	<input name="<?php echo esc_html($set_name . '[' .$option_name. ']'); ?>" class="color-picker-hex" type="text" id="<?php echo esc_html($set_name . $option_name); ?>" maxlength="7" value="<?php echo esc_attr($options[ $option_name ] ?? ''); ?>" placeholder="#ffffff" data-default-color="#ffffff">
</label>
<p><?php esc_html_e('Specifies the background color inside the button','hima-art-utility'); ?></p>
<p><?php esc_html_e('The default is white (#ffffff)','hima-art-utility'); ?></p>
</div>
<?php
}

/*** スムーススクロールを有効にする ***/
/* 項目の追加とデータの更新 */
function haumenu_init_field_top_home_smooth_scroll(){
//項目の追加
	add_settings_field(
		'enable-top-home-s-scroll', // 設定名
		__('Enable Smooth Scroll','hima-art-utility'), // 設定タイトル
		'haumenu_top_home_smooth_scroll', // 設定項目のHTMLを出力する関数名
		haumenu_define_tab_slug_06_addcontent(), //表示させるタブのスラグ
		haumenu_define_section_id_06_addcontent(),// $section 表示させるセクションのID
		array( 'class' => 'ha-settings-separate' )
	);

}
add_action( 'admin_init', 'haumenu_init_field_top_home_smooth_scroll' );

/* 入力フォーム */
function haumenu_top_home_smooth_scroll() {
	$set_name = haumenu_define_option_name_06_addcontent(); //  $option_name 設定名
	$options = get_option($set_name);
	$option_name = 'enable-top-home-s-scroll'; //個別配列名

	//テーブルに値がなかった場合の措置（PHP8.x以降への対応）
	if(isset($options[$option_name])){
		$checked = $options[$option_name];
	}else{
		$checked = '';
	}
?>
<div class="enable-top-home-s-scroll-wrapper">
<label>
<input type="checkbox" id="<?php echo esc_html($set_name . $option_name ); ?>" name="<?php echo esc_html($set_name . '[' .$option_name. ']'); ?>" class="wppd-ui-toggle" <?php checked( $checked , 1 ); ?> value="1">
	<p><?php esc_html_e('This feature adds CSS "scroll-behavior: smooth;" to the style','hima-art-utility'); ?></p>
</label>
<p><?php esc_html_e('And this style is applied not only to this function, but also to most cases when moving on the screen, such as transitioning links within the page.','hima-art-utility'); ?></p>
<p class="haup-admin-bk haup-admin-font-red"><?php esc_html_e('Please leave it off if problems occur when used in combination with the JS smooth scroll function etc.','hima-art-utility'); ?></p>
</div>
<?php
}