<?php

add_action( 'admin_menu', 'template_color_main_plugin_menu' );
add_action( 'admin_init', 'template_color_setting' );
add_action( 'wp_enqueue_scripts', 'template_color_enqueue_scripts' );


function template_color_setting() {
	 add_settings_section( 'template-color-section', 'Template Color Status', null, 'template-color-setting' );
	add_settings_field( 'template_color_active_intive', 'Template Color', 'template_color_display_oil_title', 'template-color-setting', 'template-color-section' );
	register_setting( 'template-color-section', 'template_color_active_intive' );
}
function template_color_main_plugin_menu() {
	add_menu_page( 'Template Color', 'Template Color', 'manage_options', 'templatecolor', 'template_color_settings_page', '', 10 );
}
function template_color_display_oil_title() {   ?>
	 <input name="template_color_active_intive" type="checkbox" value="1" <?php checked( '1', get_option( 'template_color_active_intive' ) ); ?> /> 
	<?php
}

function template_color_settings_page() {
	?>
	<div class="wrap">
		<h1>Theme Panel</h1>
		<form method="post" action="options.php">
	<?php
				settings_fields( 'template-color-section' );
				do_settings_sections( 'template-color-setting' );
				submit_button();
	?>
					  
		</form>
		</div>
	<?php
}

function template_color_enqueue_scripts() {
	if ( get_option( 'template_color_active_intive' ) == 1 ) {
		wp_enqueue_style( 'colorswatch-style', plugin_dir_url( __FILE__ ) . 'css/style.css' );
		wp_enqueue_script( 'colorswatch-js', plugin_dir_url( __FILE__ ) . 'js/change_color_template.js', array( 'jquery' ) );
		wp_localize_script(
			'colorswatch-js',
			'colorswatch_object',
			array(
				'colorswatchimg' => plugins_url( 'img/colorswatch-icon.png', __FILE__ ),
				'ajax_url'       => admin_url( 'admin-ajax.php' ),
				'color_url'      => get_template_directory_uri() . '/css/colors/',
			)
		);
		wp_localize_script(
			'colorswatch-js',
			'changetemplateimg_object',
			array(
				'ajax_url'     => admin_url( 'admin-ajax.php' ),
				'setting_icon' => plugins_url( 'img/layout-setting.png', __FILE__ ),
				'layout1_img'  => plugins_url( 'img/layout-1.png', __FILE__ ),
				'layout2_img'  => plugins_url( 'img/layout-2.png', __FILE__ ),
				'layout3_img'  => plugins_url( 'img/layout-3.png', __FILE__ ),
			)
		);

	}
}

add_action( 'wp_ajax_template_color_select_callback', 'template_color_select' );
add_action( 'wp_ajax_nopriv_template_color_select_callback', 'template_color_select' );

function template_color_select() {
	wp_dequeue_style( 'bestel-color' );
	$color_css_file = $_POST['variable'];
	if ( $color_css_file != 'default' ) {
		wp_enqueue_style( 'bestel-color', get_template_directory_uri() . '/css/colors/' . $color_css_file . '.css', '', null );
	}
	exit();
}




