<?php
/**
 * Today's Deal popup Customizer Controls
 *
 * @package shopbar
 */

defined( 'ABSPATH' ) || exit;

if ( ! function_exists( 'shopbar_customizer_deal_settings_register' ) ) :
function shopbar_customizer_deal_settings_register( $wp_customize ) {

	$shopbar_deal_shop_url = ( shopbar_is_active_woocommerce() && function_exists( 'wc_get_page_permalink' ) )
		? wc_get_page_permalink( 'shop' )
		: home_url( '/' );

	/**
	 * Today's Deal Section
	 */
	$wp_customize->add_section(
		'shopbar_deal_settings',
		array(
			'priority'   => 25,
			'capability' => 'edit_theme_options',
			'title'      => esc_html__( "Today's Deal Popup", 'shopbar' ),
		)
	);

	// Title label.
	$wp_customize->add_setting(
		'shopbar_deal_title_label',
		array(
			'sanitize_callback' => 'shopbar_sanitize_title',
		)
	);

	$wp_customize->add_control(
		new Shopbar_Title_Info_Control( $wp_customize, 'shopbar_deal_title_label',
		array(
			'label'    => esc_html__( "Today's Deal Popup", 'shopbar' ),
			'section'  => 'shopbar_deal_settings',
			'type'     => 'shopbar-title',
			'settings' => 'shopbar_deal_title_label',
		)
	));

	// Show / hide the Today's Deal button in the header.
	$wp_customize->add_setting(
		'shopbar_deal_show_header',
		array(
			'default'           => true,
			'type'              => 'theme_mod',
			'sanitize_callback' => 'shopbar_sanitize_checkbox',
			'transport'         => 'refresh',
		)
	);

	$wp_customize->add_control(
		new Shopbar_Toggle_Control( $wp_customize, 'shopbar_deal_show_header',
		array(
			'label'       => esc_html__( "Show Today Deal Button", 'shopbar' ),
			'description' => esc_html__( 'Display the Today Deal button in the site header. When disabled, the button is removed completely.', 'shopbar' ),
			'section'     => 'shopbar_deal_settings',
			'type'        => 'shopbar-toggle',
			'settings'    => 'shopbar_deal_show_header',
		)
	));

	// Today's Deal header button label text.
	$wp_customize->add_setting(
		'shopbar_deal_button_label',
		array(
			'default'           => __( "Today's Deal", 'shopbar' ),
			'type'              => 'theme_mod',
			'sanitize_callback' => 'shopbar_sanitize_text_field',
			'transport'         => 'refresh',
		)
	);

	$wp_customize->add_control(
		'shopbar_deal_button_label',
		array(
			'label'           => esc_html__( "Today's Deal Button Text", 'shopbar' ),
			'description'     => esc_html__( 'Change the text shown on the Today Deal button in the header.', 'shopbar' ),
			'section'         => 'shopbar_deal_settings',
			'type'            => 'text',
			'active_callback' => 'shopbar_deal_header_button_enable',
		)
	);

	// Enable / Disable popup.
	$wp_customize->add_setting(
		'shopbar_deal_enable',
		array(
			'default'           => true,
			'type'              => 'theme_mod',
			'sanitize_callback' => 'shopbar_sanitize_checkbox',
			'transport'         => 'refresh',
		)
	);

	$wp_customize->add_control(
		new Shopbar_Toggle_Control( $wp_customize, 'shopbar_deal_enable',
		array(
			'label'            => esc_html__( 'Enable Deal Popup', 'shopbar' ),
			'description'      => esc_html__( 'When enabled, the Today Deal button opens a popup instead of linking to the shop.', 'shopbar' ),
			'section'          => 'shopbar_deal_settings',
			'type'             => 'shopbar-toggle',
			'settings'         => 'shopbar_deal_enable',
			'active_callback'  => 'shopbar_deal_header_button_enable',
		)
	));

	// Badge text (eyebrow).
	$wp_customize->add_setting(
		'shopbar_deal_badge',
		array(
			'default'           => __( 'Limited Time Offer', 'shopbar' ),
			'type'              => 'theme_mod',
			'sanitize_callback' => 'shopbar_sanitize_text_field',
			'transport'         => 'refresh',
		)
	);

	$wp_customize->add_control(
		'shopbar_deal_badge',
		array(
			'label'           => esc_html__( 'Badge Label', 'shopbar' ),
			'section'         => 'shopbar_deal_settings',
			'type'            => 'text',
			'active_callback' => 'shopbar_deal_header_button_enable',
		)
	);

	// Discount number (media panel).
	$wp_customize->add_setting(
		'shopbar_deal_discount',
		array(
			'default'           => __( '50%', 'shopbar' ),
			'type'              => 'theme_mod',
			'sanitize_callback' => 'shopbar_sanitize_text_field',
			'transport'         => 'refresh',
		)
	);

	$wp_customize->add_control(
		'shopbar_deal_discount',
		array(
			'label'           => esc_html__( 'Discount Amount', 'shopbar' ),
			'description'     => esc_html__( 'Shown large in the popup panel (e.g. 50%).', 'shopbar' ),
			'section'         => 'shopbar_deal_settings',
			'type'            => 'text',
			'active_callback' => 'shopbar_deal_header_button_enable',
		)
	);

	// Title.
	$wp_customize->add_setting(
		'shopbar_deal_title',
		array(
			'default'           => __( 'Flash Sale — Up to 50% Off', 'shopbar' ),
			'type'              => 'theme_mod',
			'sanitize_callback' => 'shopbar_sanitize_text_field',
			'transport'         => 'refresh',
		)
	);

	$wp_customize->add_control(
		'shopbar_deal_title',
		array(
			'label'           => esc_html__( 'Title', 'shopbar' ),
			'section'         => 'shopbar_deal_settings',
			'type'            => 'text',
			'active_callback' => 'shopbar_deal_header_button_enable',
		)
	);

	// Description.
	$wp_customize->add_setting(
		'shopbar_deal_text',
		array(
			'default'           => __( 'Enjoy exclusive savings across our entire collection. Use the code below at checkout to claim your discount before it is gone.', 'shopbar' ),
			'type'              => 'theme_mod',
			'sanitize_callback' => 'shopbar_sanitize_textarea_field',
			'transport'         => 'refresh',
		)
	);

	$wp_customize->add_control(
		'shopbar_deal_text',
		array(
			'label'           => esc_html__( 'Description', 'shopbar' ),
			'section'         => 'shopbar_deal_settings',
			'type'            => 'textarea',
			'active_callback' => 'shopbar_deal_header_button_enable',
		)
	);

	// Coupon code.
	$wp_customize->add_setting(
		'shopbar_deal_coupon',
		array(
			'default'           => 'SHOP50',
			'type'              => 'theme_mod',
			'sanitize_callback' => 'shopbar_sanitize_text_field',
			'transport'         => 'refresh',
		)
	);

	$wp_customize->add_control(
		'shopbar_deal_coupon',
		array(
			'label'           => esc_html__( 'Coupon Code', 'shopbar' ),
			'section'         => 'shopbar_deal_settings',
			'type'            => 'text',
			'active_callback' => 'shopbar_deal_header_button_enable',
		)
	);

	// Button text.
	$wp_customize->add_setting(
		'shopbar_deal_button_text',
		array(
			'default'           => __( 'Shop the Sale', 'shopbar' ),
			'type'              => 'theme_mod',
			'sanitize_callback' => 'shopbar_sanitize_text_field',
			'transport'         => 'refresh',
		)
	);

	$wp_customize->add_control(
		'shopbar_deal_button_text',
		array(
			'label'           => esc_html__( 'Button Text', 'shopbar' ),
			'section'         => 'shopbar_deal_settings',
			'type'            => 'text',
			'active_callback' => 'shopbar_deal_header_button_enable',
		)
	);

	// Button URL.
	$wp_customize->add_setting(
		'shopbar_deal_button_url',
		array(
			'default'           => $shopbar_deal_shop_url,
			'type'              => 'theme_mod',
			'sanitize_callback' => 'shopbar_sanitize_url',
			'transport'         => 'refresh',
		)
	);

	$wp_customize->add_control(
		'shopbar_deal_button_url',
		array(
			'label'           => esc_html__( 'Button URL', 'shopbar' ),
			'section'         => 'shopbar_deal_settings',
			'type'            => 'url',
			'active_callback' => 'shopbar_deal_header_button_enable',
		)
	);
}
endif;

add_action( 'customize_register', 'shopbar_customizer_deal_settings_register' );
