<?php
require_once plugin_dir_path( __FILE__ ) .'header/header-main.php';
require_once plugin_dir_path( __FILE__ ) .'class/channels_api.php';

global $wpdb; 

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

?>
<style>
    .error
    {
        padding: 18px 0px 0px 10px;
    }
</style>
<div class="content_hero_container">
<div class="container">
  <div class="row">
    <div class="col-md-12">
        <div class="tabbable-panel">
            <div class="tabbable-line">
                <h2 style="margin: 20px 0 20px;">Add Channels</h2><br/>
                <div class="tab-content-channel">
                          <!-- Multiple Item Picker -->
                     <form id="form-article">   
						<div class="form-group row">
							<label for="staticEmail" class="col-sm-2 col-form-label" style="font-size: 16px;">Select Type:</label>
							<div class="col-sm-3">
								<select id="channel_select_result" name="channel_select_result">
								  <option value="ytube_mych">Youtube Channels</option>
								</select>
							</div>
						</div><br/>
						<div class="form-group row">
							<label for="staticEmail" class="col-sm-2 col-form-label" style="font-size: 16px;">Query:</label>
							<div class="col-sm-3">
							<input type="text" id="channel_query_id" placeholder="Enter the username , channel name" value="" style="width:100%;"/>
							</div>
						</div><br/>	
						<?php 	
						$result_article_ezine_enable = get_option('wpch_articleezine_enable');
						$result_ezine_category_id_setting = get_option('wpch_articleezine_category_setting');
						$result_ezine_subcategory_id_setting = get_option('wpch_articleezine_subcategory_setting');
						
						if(!empty($result_article_ezine_enable) && $result_article_ezine_enable == 'on')
						{
							$channel_detail_api_class = new channelapi();									
							$channel_ezine_category = $channel_detail_api_class->channel_ezine_category();							
							if(!empty($channel_ezine_category))
							{
							?>						
								<div class="form-group row">
									<label for="staticEmail" class="col-sm-2 col-form-label" style="font-size: 16px;">Ezine Articles:</label>
								</div><br/>						
								<div class="form-group row">
									<label for="staticEmail" class="col-sm-2 col-form-label" style="font-size: 16px;">Select Category:</label>
									<div class="col-sm-3">
										<select id="select_ezine_category_id" name="select_ezine_category_name" disabled="disabled" style="color: #000;background-color: #ffffff;">								
										  <?php if(empty($result_ezine_category_id_setting)) { ?>
										  <option value="">Select Option</option>										  
										  <?php } else { ?>
										  <option value="<?php echo $result_ezine_category_id_setting; ?>"><?php echo str_replace("-", " ", $result_ezine_category_id_setting); ?></option>
										  <?php } ?>											
										</select>
									</div>
								</div><br/>
							    <?php if(!empty($result_ezine_subcategory_id_setting)) { ?>
								<div class="form-group row">
									<label for="staticEmail" class="col-sm-2 col-form-label" style="font-size: 16px;">Sub Category:</label>
									<div class="col-sm-3">
										<select id="select_ezine_subcategory_id_main" name="select_ezine_subcategory_name_main" disabled="disabled" style="color: #000;background-color: #ffffff;">								
										  <option value="<?php echo $result_ezine_subcategory_id_setting; ?>"><?php echo str_replace("-", " ", $result_ezine_subcategory_id_setting); ?></option>
										</select>
									</div>
								</div><br/>
							<?php 
								}
							} 
						}
						?>
						<div class="form-group row content_hero_tab">
							<div class="col-sm-3">
								<div class="wrapper ytube_channel_submit_keyword">
									<input type="button" class="button ytube_channel_playlist" value="Search"/>
								</div>		
							</div>
						</div>
						<div class="form-group row content_hero_tab">
							<div id="afe_img_subcat_loader" style="display: flex;justify-content: center;"><img src="<?php echo plugins_url('MyContentCreatorPro-Free/includes/assets/images/loader_afe.gif'); ?>" id="afe_img_search_loaders" style="display:none;width:5%;"> </div>
						</div>
                    </form>
					<div class="wrap">
						<div class="updated" id="updated_text" style="display:none;"><p>Channel is created</p></div>
						<div class="error" id="error_text" style="display:none;"><p>Note:- Previous article forge request has not completed. It usually takes around 8 to 15 to complete the article. Article forge only allows one article at a time. Please wait and try again.</p></div>
					</div>
					<div id="response_channels_detail"></div>						
					<form>
						<div class="channel_result_api" style="display:none;">
							<h2 style="margin-bottom:50px;">Results <input type="button" name="channel_result_name" id="channel_result_id" class="button-primary alignright" value="Save Youtube Channel" /></h2>
						</div>					
						<div id="response_channels"></div>
					</form>						
                </div>
            </div>
        </div>
    </div>
  </div>
</div>
</div>
<script>
jQuery(document).ready(function($){
	$('#channel_query_id').on('keyup keypress', function(e) {
	  var keyCode = e.keyCode || e.which;
	  if (keyCode === 13) { 
		e.preventDefault();
		return false;
	  }
	});

	$('.ytube_channel_playlist').click(function(){		
	
		var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";
		var channel_select_result = $("#channel_select_result").val();
		var channel_query_id = $("#channel_query_id").val();
		var select_ezine_category_id = $("#select_ezine_category_id").val();
		var select_ezine_subcategory_id = $("#select_ezine_subcategory_id_main").val();
		
        $("#afe_img_search_loaders").css("display","block"); 
			
		var affili_channel_data_search = {
			action: 'wpmy_channels_result',
			ajax_channel_select_result : channel_select_result,
			ajax_channel_query_id : channel_query_id,
			ajax_select_ezine_category_id : select_ezine_category_id,
			ajax_select_ezine_subcategory_id : select_ezine_subcategory_id,
		};
		$.post(ajaxurl, affili_channel_data_search, function(affili_channel_response) 
		{
			$("#response_channels").html(affili_channel_response);
            $("#afe_img_search_loaders").css("display","none"); 
			$("#updated_text").css("display", 'none');
			$("#error_text").css("display", 'none');	
            //$(".channel_result_api").css("display","block");			
		});
	});
});

/*
jQuery(document).ready(function($){
	$('#channel_result_id').click(function(){		
		var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";
		
            var mych_array_check_result = [];
            var table = jQuery("#example_channel").dataTable();
            jQuery(".channel_id_submit:checked", table.fnGetNodes()).each(function () 
            {
                var tuisre = jQuery(this).is(":checked");
                if (tuisre) 
                {
                    var no = jQuery(this).val();
                    mych_array_check_result.push(no);
                }
            }); 
            var mych_array_check_val = mych_array_check_result.join(",");
			
            var mych_array_auto_post = [];
            var table1 = jQuery("#example_channel").dataTable();
            jQuery(".channel_auto_post_class:checked", table1.fnGetNodes()).each(function () 
            {
                var tuisre1 = jQuery(this).is(":checked");
                if (tuisre1) 
                {
                    var no1 = jQuery(this).val();
                    mych_array_auto_post.push(no1);
                }
            }); 
            var mych_array_auto_post = mych_array_auto_post.join(",");
				
			$("#afe_img_search_loaders").css("display","block"); 
				
			var mych_channel_data_search = {
				action: 'wpmy_channels_detail',
				ajax_mych_array_check_val : mych_array_check_val,
				ajax_mych_array_auto_post : mych_array_auto_post,
			};
			$.post(ajaxurl, mych_channel_data_search, function(mych_channel_response) 			
			{
				$("#response_channels_detail").html(mych_channel_response);
				$("#afe_img_search_loaders").css("display","none"); 
				//$(".channel_result_api").css("display","block");			
			});
			
	});
});
*/

jQuery(document).ready(function($){
	$(document).on('click' , '#channel_result_id1', function()
	{		
		var channel_query_id = $(this).data('yid');				
		var channel_query_result = $(this).data('aftitle');	
		var channel_query_result_categ = $(this).data('categ');	
		var channel_query_result_subcateg = $(this).data('subcateg');	
		var channel_query_result_enableaf = $(this).data('enableaf');	
		
		var afytubechannel_select_result = $("#mych_box_ytube_"+channel_query_id+" #afytubechannel_select_cat_id").val();
		
		if($("#mych_box_ytube_"+channel_query_id+" .channel_auto_post_class").is(':checked')) 
		{
			var channel_query_id1 = $("#mych_box_ytube_"+channel_query_id+" .channel_auto_post_class").val();
		}
		else
		{
			var channel_query_id1 = $("#mych_box_ytube_"+channel_query_id+" #channel_id_hidden").val();
		}
		
		var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";
		jQuery.ajax({
			url:  ajaxurl,
			type: "POST",
			data: {
				action:'wpmy_channels_detail',
				ajax_mych_array_check_val : channel_query_id1,
				ajax_channel_query_result : channel_query_result,
				ajax_afytubechannel_select_result : afytubechannel_select_result,
				ajax_channel_query_result_categ : channel_query_result_categ,
				ajax_channel_query_result_subcateg : channel_query_result_subcateg,
			},
			beforeSend: function(){
				$("#mych_box_ytube_"+channel_query_id+" #channel_result_id1").prop("value", 'Saving...');
			},
			success: function (response) {
				$("#mych_box_ytube_"+channel_query_id+" #auto_post_div").css("display", 'none');
				$("#mych_box_ytube_"+channel_query_id+" #view_channel_btn").css("display", 'block');
				$("#updated_text").css("display", 'block');
				$("#error_text").css("display", 'none');
			}
		});	
		return false;		
	});
});

</script>