<?php if( ! defined( 'ABSPATH' ) ) exit;
/**
 * Enqueue scripts and styles.
 */
 
add_action('add_categories_home','live_news_categories_post');

function live_news_categories_post () { ?>
<?php if ( get_theme_mod('live_news_categories_activate') ) { ?>

	<div id="cat-home-act">
		<?php if ( get_theme_mod( 'live_news_categories_title_') ) { ?>
		<h2><?php echo esc_html( get_theme_mod('live_news_categories_title_') ); ?></h2>
		<?php } ?>

		<?php for($i=1;$i<=50;$i++) { ?>
			<?php if ( get_theme_mod( 'categories_image'.$i) ) { ?> 
			<a href="<?php echo esc_url( get_theme_mod('live_news_categories_url_'.$i) ); ?>">
				<div class="cont-cat">
					<img src="<?php echo esc_url( get_theme_mod('categories_image'.$i) ); ?>" />
					<div class="cat-title"><?php echo esc_html( get_theme_mod('live_news_categories_name_'.$i) ); ?></div>
				</div>
			</a>	
				<?php } ?>
		<?php } ?>
	</div>
	<?php
	}
}

function live_news_categories_customize_register( $wp_customize ) {

		$wp_customize->add_panel( 'live_news_panel_cats' , array(
			'title'       => __( 'Categories', 'live-news' ),
			'priority'   => 2,
		) );
		
		$wp_customize->add_section( 'live_news_categories_options' , array(
			'title'       => __( 'General', 'live-news' ),
			'panel' => 'live_news_panel_cats',
			'priority'   => 1,
		) );
		
		$wp_customize->add_setting( 'live_news_categories_activate', array (
		    'default' => false,
			'sanitize_callback' => 'live_news_sanitize_checkbox',
		) );
		
		
		$wp_customize->add_control( new WP_Customize_Control( $wp_customize, 'live_news_categories_activate', array(
			'label'    => __( 'Activate on Homepage', 'live-news' ),
			'section'  => 'live_news_categories_options',
			'settings' => 'live_news_categories_activate',
			'type' => 'checkbox',
		) ) );
		
		$wp_customize->add_setting( 'live_news_categories_title_', array (
			'sanitize_callback' => 'sanitize_text_field',
		) );
			
		$wp_customize->add_control( new WP_Customize_Control( $wp_customize, 'live_news_categories_title_', array(
			'label'    => __( 'Section Title', 'live-news' ),
			'section'  => 'live_news_categories_options',
			'settings' => 'live_news_categories_title_',
			'type' => 'text',
		) ) );		
		
	for($a=1;$a<=50;$a++) {
		
			$wp_customize->add_section( 'live_news_categories_section_'.$a , array(
				'title'       => __( 'Category '.$a, 'live-news' ),
				'panel' => 'live_news_panel_cats',
				'priority'   => 1,
			) );
		
			$wp_customize->add_setting( 'categories_image'.$a, array (
				'sanitize_callback' => 'esc_url_raw',
			) );
			
			$wp_customize->add_control( 
				new WP_Customize_Image_Control( $wp_customize, 'categories_image'.$a, 
				array(
					'label'      => __( 'Category IMG ', 'live-news' ),
					'section'    => 'live_news_categories_section_'.$a,
					'settings'   => 'categories_image'.$a,
				) ) );

			$wp_customize->add_setting( 'live_news_categories_name_'.$a, array (
				'sanitize_callback' => 'sanitize_text_field',
			) );
			
			$wp_customize->add_control( new WP_Customize_Control( $wp_customize, 'live_news_categories_name_'.$a, array(
				'label'    => __( 'Category Name ', 'live-news' ),
				'section'  => 'live_news_categories_section_'.$a,
				'settings' => 'live_news_categories_name_'.$a,
				'type' => 'text',
			) ) );
			
			$wp_customize->add_setting( 'live_news_categories_url_'.$a, array (
				'sanitize_callback' => 'esc_url_raw',
			) );
			
			$wp_customize->add_control( new WP_Customize_Control( $wp_customize, 'live_news_categories_url_'.$a, array(
				'label'    => __( 'Category URL ', 'live-news' ),
				'section'  => 'live_news_categories_section_'.$a,
				'settings' => 'live_news_categories_url_'.$a,
				'type' => 'url',
			) ) );

	}		
}
add_action( 'customize_register', 'live_news_categories_customize_register' );

function live_news_cat_scripts() {
	if ( get_theme_mod('live_news_categories_activate') ) {
		wp_enqueue_style( 'seos-categories-css', get_template_directory_uri() . '/include/categories/cat.css' );
	}	
}

add_action( 'wp_enqueue_scripts', 'live_news_cat_scripts' );