<?php
/** WordPress Customizer controls for the store homepage. @package Fragrance_Trade */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

function fragrance_trade_sanitize_checkbox( $checked ) {
	return isset( $checked ) && true === (bool) $checked;
}

function fragrance_trade_customize_add_field( $wp_customize, $id, $section, $label, $type = 'text', $priority = 10 ) {
	$sanitizers = array(
		'text'     => 'sanitize_text_field',
		'textarea' => 'sanitize_textarea_field',
		'url'      => 'esc_url_raw',
		'email'    => 'sanitize_email',
		'checkbox' => 'fragrance_trade_sanitize_checkbox',
		'image'    => 'esc_url_raw',
	);
	$wp_customize->add_setting(
		$id,
		array(
			'default'           => fragrance_trade_get_default( $id ),
			'sanitize_callback' => $sanitizers[ $type ],
			'transport'         => 'refresh',
		)
	);
	if ( 'image' === $type ) {
		$wp_customize->add_control(
			new WP_Customize_Image_Control(
				$wp_customize,
				$id,
				array( 'label' => $label, 'section' => $section, 'priority' => $priority )
			)
		);
		return;
	}
	$wp_customize->add_control( $id, array( 'label' => $label, 'section' => $section, 'type' => $type, 'priority' => $priority ) );
}

function fragrance_trade_customize_register( $wp_customize ) {
	$wp_customize->add_panel(
		'fragrance_b2b_homepage',
		array(
			'title'       => __( '门店首页', 'fragrance-trade' ),
			'description' => __( '编辑固定布局的汽车贴膜门店首页。上线前请将所有演示文案替换为真实门店信息。', 'fragrance-trade' ),
			'priority'    => 30,
		)
	);

	$sections = array(
		'fragrance_b2b_hero'           => __( '首屏定位', 'fragrance-trade' ),
		'fragrance_b2b_trust'          => __( '信任栏', 'fragrance-trade' ),
		'fragrance_b2b_factory'        => __( '门店环境与参观', 'fragrance-trade' ),
		'fragrance_b2b_documents'      => __( '文档支持', 'fragrance-trade' ),
		'fragrance_b2b_faq'            => __( '常见问题', 'fragrance-trade' ),
		'fragrance_contact_social'     => __( '联系方式与社交链接', 'fragrance-trade' ),
	);
	$priority = 10;
	foreach ( $sections as $id => $title ) {
		$wp_customize->add_section( $id, array( 'title' => $title, 'panel' => 'fragrance_b2b_homepage', 'priority' => $priority ) );
		$priority += 10;
	}

	$fields = array(
		array( 'b2b_hero_eyebrow', 'fragrance_b2b_hero', __( '眉标', 'fragrance-trade' ), 'text' ),
		array( 'b2b_hero_title', 'fragrance_b2b_hero', __( '主标题', 'fragrance-trade' ), 'text' ),
		array( 'b2b_hero_text', 'fragrance_b2b_hero', __( '简介文案', 'fragrance-trade' ), 'textarea' ),
		array( 'b2b_hero_primary_label', 'fragrance_b2b_hero', __( '主按钮文字', 'fragrance-trade' ), 'text' ),
		array( 'b2b_hero_primary_url', 'fragrance_b2b_hero', __( '主按钮链接', 'fragrance-trade' ), 'url' ),
		array( 'b2b_hero_secondary_label', 'fragrance_b2b_hero', __( '次按钮文字', 'fragrance-trade' ), 'text' ),
		array( 'b2b_hero_secondary_url', 'fragrance_b2b_hero', __( '次按钮链接', 'fragrance-trade' ), 'url' ),
		array( 'b2b_trust_title', 'fragrance_b2b_trust', __( '信任栏标题', 'fragrance-trade' ), 'text' ),
		array( 'b2b_factory_eyebrow', 'fragrance_b2b_factory', __( '眉标', 'fragrance-trade' ), 'text' ),
		array( 'b2b_factory_title', 'fragrance_b2b_factory', __( '板块标题', 'fragrance-trade' ), 'text' ),
		array( 'b2b_factory_text', 'fragrance_b2b_factory', __( '门店简介', 'fragrance-trade' ), 'textarea' ),
		array( 'b2b_show_video', 'fragrance_b2b_factory', __( '显示门店视频', 'fragrance-trade' ), 'checkbox' ),
		array( 'b2b_video_title', 'fragrance_b2b_factory', __( '视频标题', 'fragrance-trade' ), 'text' ),
		array( 'b2b_video_text', 'fragrance_b2b_factory', __( '视频描述', 'fragrance-trade' ), 'textarea' ),
		array( 'b2b_video_url', 'fragrance_b2b_factory', __( 'YouTube、Vimeo 或视频链接', 'fragrance-trade' ), 'url' ),
		array( 'b2b_video_image', 'fragrance_b2b_factory', __( '视频封面图', 'fragrance-trade' ), 'image' ),
		array( 'b2b_show_documents', 'fragrance_b2b_documents', __( '显示文档板块', 'fragrance-trade' ), 'checkbox' ),
		array( 'b2b_documents_title', 'fragrance_b2b_documents', __( '板块标题', 'fragrance-trade' ), 'text' ),
		array( 'b2b_documents_text', 'fragrance_b2b_documents', __( '板块简介', 'fragrance-trade' ), 'textarea' ),
		array( 'b2b_show_faq', 'fragrance_b2b_faq', __( '显示常见问题', 'fragrance-trade' ), 'checkbox' ),
		array( 'b2b_faq_title', 'fragrance_b2b_faq', __( '常见问题标题', 'fragrance-trade' ), 'text' ),
		array( 'inquiry_email', 'fragrance_contact_social', __( '联系邮箱', 'fragrance-trade' ), 'email' ),
		array( 'whatsapp', 'fragrance_contact_social', __( 'WhatsApp 号码（含国家代码）', 'fragrance-trade' ), 'text' ),
		array( 'facebook_url', 'fragrance_contact_social', __( 'Facebook 链接', 'fragrance-trade' ), 'url' ),
		array( 'twitter_url', 'fragrance_contact_social', __( 'X / Twitter 链接', 'fragrance-trade' ), 'url' ),
		array( 'youtube_url', 'fragrance_contact_social', __( 'YouTube 链接', 'fragrance-trade' ), 'url' ),
		array( 'linkedin_url', 'fragrance_contact_social', __( 'LinkedIn 链接', 'fragrance-trade' ), 'url' ),
		array( 'instagram_social_url', 'fragrance_contact_social', __( 'Instagram 链接', 'fragrance-trade' ), 'url' ),
		array( 'store_brand', 'fragrance_contact_social', __( '主营品牌', 'fragrance-trade' ), 'text' ),
		array( 'store_authorization', 'fragrance_contact_social', __( '授权级别', 'fragrance-trade' ), 'text' ),
		array( 'store_city', 'fragrance_contact_social', __( '所在城市', 'fragrance-trade' ), 'text' ),
		array( 'store_contact_person', 'fragrance_contact_social', __( '门店联系人', 'fragrance-trade' ), 'text' ),
		array( 'store_address', 'fragrance_contact_social', __( '门店地址', 'fragrance-trade' ), 'textarea' ),
		array( 'store_phone', 'fragrance_contact_social', __( '联系电话', 'fragrance-trade' ), 'text' ),
		array( 'store_hours', 'fragrance_contact_social', __( '营业时间', 'fragrance-trade' ), 'text' ),
		array( 'store_map_url', 'fragrance_contact_social', __( '地图导航链接', 'fragrance-trade' ), 'url' ),
		array( 'store_map_lat', 'fragrance_contact_social', __( '百度地图纬度', 'fragrance-trade' ), 'text' ),
		array( 'store_map_lng', 'fragrance_contact_social', __( '百度地图经度', 'fragrance-trade' ), 'text' ),
		array( 'icp_number', 'fragrance_contact_social', __( 'ICP备案号', 'fragrance-trade' ), 'text' ),
		array( 'icp_url', 'fragrance_contact_social', __( 'ICP备案查询链接', 'fragrance-trade' ), 'url' ),
		array( 'footer_text', 'fragrance_contact_social', __( '页脚定位文案', 'fragrance-trade' ), 'text' ),
	);

	for ( $index = 1; $index <= 4; $index++ ) {
		$fields[] = array( 'b2b_metric_' . $index . '_value', 'fragrance_b2b_trust', sprintf( __( '信任点 %d 数值', 'fragrance-trade' ), $index ), 'text' );
		$fields[] = array( 'b2b_metric_' . $index . '_label', 'fragrance_b2b_trust', sprintf( __( '信任点 %d 标签', 'fragrance-trade' ), $index ), 'text' );
		$fields[] = array( 'b2b_factory_point_' . $index, 'fragrance_b2b_factory', sprintf( __( '门店亮点 %d', 'fragrance-trade' ), $index ), 'text' );
		$fields[] = array( 'b2b_factory_image_' . $index, 'fragrance_b2b_factory', sprintf( __( '门店图片 %d', 'fragrance-trade' ), $index ), 'image' );
		$fields[] = array( 'b2b_document_' . $index . '_title', 'fragrance_b2b_documents', sprintf( __( '文档卡片 %d 标题', 'fragrance-trade' ), $index ), 'text' );
		$fields[] = array( 'b2b_document_' . $index . '_text', 'fragrance_b2b_documents', sprintf( __( '文档卡片 %d 描述', 'fragrance-trade' ), $index ), 'textarea' );
	}
	for ( $index = 1; $index <= 8; $index++ ) {
		$fields[] = array( 'b2b_faq_' . $index . '_question', 'fragrance_b2b_faq', sprintf( __( '问题 %d', 'fragrance-trade' ), $index ), 'text' );
		$fields[] = array( 'b2b_faq_' . $index . '_answer', 'fragrance_b2b_faq', sprintf( __( '回答 %d', 'fragrance-trade' ), $index ), 'textarea' );
	}

	foreach ( $fields as $field ) {
		fragrance_trade_customize_add_field( $wp_customize, $field[0], $field[1], $field[2], $field[3] );
	}
}
add_action( 'customize_register', 'fragrance_trade_customize_register' );
