<?php
require_once plugin_dir_path( __FILE__ ) .'header/header-main.php';
require_once plugin_dir_path( __FILE__ ) .'ch-auto-content-tabs.php';
require_once plugin_dir_path( __FILE__ ) .'class/afe_blog.php';
global $wpdb;

$afe_blog_main=new afeblogs();
$afe_catgories=$afe_blog_main->afe_article_category('main');

$afe_campaign_result = $wpdb->get_results("SELECT * FROM ".$wpdb->base_prefix."ch_camapign WHERE id = '".$_GET['id']."' ORDER BY id ASC");
$afe_campaign_post_result = $wpdb->get_results("SELECT * FROM ".$wpdb->base_prefix."ch_campaign_post WHERE campaign_id = '".$_GET['id']."'");

$c_title = $afe_campaign_result[0]->c_title;
$c_keyword = $afe_campaign_result[0]->c_keyword;
$c_img_key = $afe_campaign_result[0]->c_img_key;
$c_ytube_key = $afe_campaign_result[0]->c_ytube_key;
$c_prod_shrt = $afe_campaign_result[0]->c_prod_short;
$c_hr = $afe_campaign_result[0]->c_hours;
$c_day = $afe_campaign_result[0]->c_days;

?>
<style>
#subcat_prev {
  font-family: arial, sans-serif;
  border-collapse: collapse;
  width: 100%;
}

#subcat_prev td{
  border: 1px solid #dddddd;
  text-align: left;
  padding: 8px;
}

#subcat_prev th{
  border: 1px solid #dddddd;
  text-align: left;
  padding: 8px;
}

#subcat_prev tr:nth-child(even) {
  background-color: #dddddd;
}
</style>
<div class="content_hero_container">
<div class="container">
    <div class="row">
        <div class="col-md-12">
            <h2>Edit Campaign</h2>
            <br/>
            <form action="" method="post" id="form-update">
                <input type="hidden" name="id_campaign_edit" id="id_campaign_edit" value="<?php echo $_GET['id']; ?>" />
                <div>
                    <label for="campaign_title" style="font-size: 17px;">Title</label>
                    <input type="text" name="afe_edit_campaign_title" class="afe_class_edit_ctitle" id="afe_id_edit_ctitle" placeholder="enter title name" value="<?php if(!empty($c_title)){ echo $c_title; } ?>"> 
                </div>
                <br/><br/>
                <div>
                    <div style="display:flex;">
                        <label for="campaign_schedule" style="font-size: 17px;margin-right:10px;">Schedule</label> 
                        <!--<p>(You are limited to 20 max posts per campaign) </p> -->
                    </div>    
                    <p></p>
                    <?php if(!empty($c_hr))
                    { ?>
                    <div>
                        <p>Every <input type="text" name="afe_campaign_edit_timer" id="afe_campaign_edit_hours" value="<?php if(!empty($c_hr)){ echo $c_hr; } ?>" style="height:40px;border-radius:10px;">
                         <select name="afe_campaign_edit_increment" id="afe_campaign_edit_increments" style="width:25%;height:40px;border-radius:10px;margin-top:-5px;">
    						<option value="hours" selected="selected">Hour(s)</option>
                            <option value="days">Day(s)</option>		
                         </select>
                        </p>
                    </div>
                    <?php } elseif(!empty($c_day)) { ?>
                    <div>
                        <p>Every <input type="text" name="afe_campaign_edit_timer" id="afe_campaign_edit_hours" value="<?php if(!empty($c_day)){ echo $c_day; } ?>" style="height:40px;border-radius:10px;">
                         <select name="afe_campaign_edit_increment" id="afe_campaign_edit_increments" style="width:25%;height:40px;border-radius:10px;margin-top:-5px;">
    						<option value="hours">Hour(s)</option>
                            <option value="days" selected="selected">Day(s)</option>		
                         </select>
                        </p>
                    </div>
                    <?php } ?>
                </div>
                <br/><br/>
                <div>
                    <label for="campaign_image_keyword" style="font-size: 17px;">Choose Image Keyword</label> 
                    <input type="text" name="afe_edit_img_keyword" class="afe_cls_edit_img_keyword" id="afe_id_edit_img_keyword" placeholder="enter your keyword" value="<?php if(!empty($c_img_key)){ echo $c_img_key; } ?>"> 
                </div>
                <br/><br/>
                <div style="display:none;">
                    <label for="campaign_video_keyword" style="font-size: 17px;">Choose Video Keyword</label> 
                    <input type="text" name="afe_edit_video_keyword" class="afe_cls_edit_video_keyword" id="afe_id_edit_video_keyword" placeholder="enter your keyword" value="<?php if(!empty($c_ytube_key)){ echo $c_ytube_key; } ?>"> 
                </div>    
                <br/><br/>
                <?php if(!empty(get_option( 'wpch_spin_api_setting' ))) {?>
                <div class="onoffswitch" style="display: flex;margin-top:10px;">
                    <label for="campaign_spinner" style="font-size: 17px;padding-top: 7px;">Spin Content</label> 
                    <input type="checkbox" class="onoffswitch-checkbox" id="myonoffswitch" name="onoffswitch"/>
                    <label class="onoffswitch-label" for="myonoffswitch">
                        <span class="onoffswitch-inner"></span>
                   </label>
                </div>
                <br/><br/>
                <?php } ?>
                <div>
                    <div style="display:flex;">
                        <label for="campaign_schedule" style="font-size: 17px;">Select Category</label> 
                    </div>   
                    <p></p>
                    <div class="jumbotron" style="padding:0;background-color:white;">
                        <select class="form-control " name="selectpicker" id="selectpicker" style="background-color:#fff;padding: 10px;height:auto;color: black;max-width:100%;">
                            <option value="0">Choose one of the following category</option>
                            <?php
                            foreach($afe_catgories as $catgory)
                            {
                                if($catgory->category_name == 'ViralMarketing' || $catgory->category_name == 'ListBuilding')
                                            { } else {                                            
                                echo '<option value="'.$catgory->id.'">'.$catgory->category_name.'</option>';
                                }
                            }
                            ?>
                        </select>
                        <div id="afe_subcat_edit_loader" style="display: flex;justify-content: center;"><img src="<?php echo plugins_url('MyContentCreatorPro-Free/includes/assets//images/loader_afe.gif'); ?>" id="afe_edit_loader" style="display:none;"> </div>
                        <div id="afe_subcat_result"></div>
                    </div>  
                    <div style="display: flex;margin: 1em;"></div>
                    <div style="display:flex;">
                        <input type="text" name="afe_c_edit_keyword" class="afe_c_edit_keyword" id="afe_c_edit_keyword" placeholder="enter your keyword" value="" style="width:25%;"> 
                        <div class="wrapper article_submit_keyword">
                            <input type="button" class="button btnPush btnnPush btnBlueGreen" value="Search Category" />
                        </div>
                    </div>
                </div>    
            </form>
            <form id="form-update-save">
                <div id="afe_subcat_edit_loader" style="display: flex;justify-content: center;"><img src="<?php echo plugins_url('MyContentCreatorPro-Free/includes/assets/images/loader_afe.gif'); ?>" id="afe_search_edit_loaders" style="display:none;"> </div>
                <div id="afe_campaign_check_result"></div>                        
                <div class="afe_search_main_div" style="display:none;">
                    <h2>Results <input type="button" name="afe_edit_campaign_save" id="afe_edit_campaign_save" style="background-color: #0073aa;box-shadow: none;border: none;text-shadow: none;" class="button button-primary alignright" value="Update Campaign" /></h2>
                </div>
                <div id="afe_search_result"> </div>
                <br/><br/>
                <?php if(count($afe_campaign_post_result) > 0 ) {?>
                <table id="subcat_prev">
                  <tr>
                    <th>Title</th>
                    <th>Description</th>
                  </tr>
                <?php
                    foreach($afe_campaign_post_result as $afe_campaign_post_result1)
                    {
        			    $afe_cron_post_datas = get_post($afe_campaign_post_result1->post_id);
    					$afe_cron_post_titles = $afe_cron_post_datas->post_title;
    					$afe_cron_post_descs = $afe_cron_post_datas->post_content;
                    ?>
                    <tr>
                        <td><?php echo $afe_cron_post_titles; ?></td>
                        <td><?php echo wp_trim_words($afe_cron_post_descs,50) ?></td>
                    </tr>
                  <?php } ?>
                </table>
                <?php } ?>
            </form>    
        </div>
    </div>        
</div> 
</div>   
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.11.2/js/bootstrap-select.min.js"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script>
     /* Multiple Item Picker */
  jQuery('.selectpicker').selectpicker({
    style: 'btn-default'
  });
/*  
jQuery('#selectpicker').change(function () 
{
	var afe_category_id = jQuery(this).val();

	if(afe_category_id>0)
	{
		jQuery("#afe_edit_loader").css("display","block");
		jQuery("#afe_edit_loader").css("height","100px");
		jQuery("#afe_subcat_result").css("display","none");
		var data = {
			action: 'ch_get_subcat',
			afe_subcat_id : afe_category_id
		};
		jQuery.post(ajaxurl, data, function(response) {
			jQuery("#afe_subcat_result").html(response);
			jQuery("#afe_edit_loader").css("display","none"); 
			jQuery("#afe_subcat_result").css("display","block");
		});
		
	}
	else
	{
		jQuery("#afe_subcat_result").html(' ');	
		jQuery("#afe_subcat_result").hide();
	}
});
*/

jQuery(document).ready(function() {
    jQuery("#form-update").validate({
        rules: {
            afe_edit_campaign_title: "required",
            afe_campaign_edit_hours: "required",
            afe_campaign_edit_increment: {
                    selectchecks: true
                }            
        },
        messages: {
            afe_edit_campaign_title: "Please specify title name",
            afe_campaign_edit_hours: "Please enter the numeric value"
        }
    });
    jQuery('.btnnPush').click(function () 
    {
        if(jQuery("#form-update").valid())
        {
            var afe_selectcat_array = [];
            var afe_c_array_val = jQuery("#selectpicker").val();                
            var afe_c_keyword = jQuery("#afe_c_edit_keyword").val();
            
            jQuery("#afe_search_edit_loaders").css("display","block");
        	jQuery("#afe_search_edit_loaders").css("height","100px");
        	jQuery("#afe_search_result").css("display","none");
        	jQuery("#afe_check_result").css("display","none");
        
        
        	var c_data_search = {
        		action: 'ch_campaign_search_result',
        		afe_ajax_cmp_array_val : afe_c_array_val,
        		afe_ajax_cmp_keyword : afe_c_keyword
        	};
        	jQuery.post(ajaxurl, c_data_search, function(cmp_response) 
        	{
        		jQuery("#afe_search_result").html(cmp_response);
        		jQuery("#afe_search_edit_loaders").css("display","none"); 
        		jQuery("#afe_search_result").css("display","block");
        		jQuery(".afe_search_main_div").css("display","block");
        	});
        }    
    });
        jQuery.validator.addMethod('selectcheck', function (value) {
            return (value != '0');
        }, "Please Choose atleast one option");    
        jQuery.validator.addMethod('selectchecks', function (values) {
            return (values != '0');
        }, "Please Choose atleast one option");    
});

jQuery(document).ready(function() {
    jQuery("#form-update-save").validate({
        rules: {
            'afe_cmp_check_vals[]': {required: true}
        },
        messages: {
            'afe_cmp_check_vals[]': "You must check at least 1 box"
        }
    });
    jQuery('#afe_edit_campaign_save').click(function()
    {
        if(jQuery("#form-update-save").valid())
        {
            var afe_array_edit_check_result1 = [];
            var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";
        
            var afe_c_id = jQuery("#id_campaign_edit").val();
            jQuery.each(jQuery(".afe_cmp_check_val:checked"), function(){            
                afe_array_edit_check_result1.push(jQuery(this).val());
            });
            var afe_array_edit_check_val1 = afe_array_edit_check_result1.join(",");
            var afe_campaign_edit_keyword = jQuery('.afe_c_edit_keyword').val(); 
            var afe_campaign_edit_title = jQuery('.afe_class_edit_ctitle').val(); 
            var afe_img_edit_keyword = jQuery("#afe_id_edit_img_keyword").val();
            //var afe_vid_edit_keyword = jQuery("#afe_id_edit_video_keyword").val();
            var afe_vid_edit_keyword = '';
            
            //var afe_edit_spin_content_check = document.getElementById("myonoffswitch");
            //if (afe_edit_spin_content_check.checked) 
            if(jQuery("#myonoffswitch").is(":checked"))
            {
                var afe_edit_spin_content = 'on';
            } 
            else 
            {
                var afe_edit_spin_content = 'off';
            } 
            
            var c_edit_hour = [];
            var c_edit_days = [];
            var edit_campaign_time = jQuery("#afe_campaign_edit_increments").val();
            if(edit_campaign_time == 'hours')
            {
              c_edit_hour.push(jQuery('#afe_campaign_edit_hours').val());
            }
            else if(edit_campaign_time == 'days')
            {
              c_edit_days.push(jQuery('#afe_campaign_edit_hours').val());
            }
        
            
            jQuery("#afe_campaign_check_result").css("display","none");
        	var edit_campaign_search = {
                    			action: 'ch_edit_check_campaign_result',
                    			afe_ajax_campaign_id : afe_c_id,
                    			afe_ajax_edit_campaign_check_array_val : afe_array_edit_check_val1,
                    			afe_ajax_edit_campaign_title : afe_campaign_edit_title,
                    			afe_ajax_edit_campaign_key : afe_campaign_edit_keyword,
                    			afe_ajax_edit_campaign_hour : c_edit_hour,
                    			afe_ajax_edit_campaign_days : c_edit_days, 
                    			afe_ajax_edit_campaign_img_key : afe_img_edit_keyword, 
                    			afe_ajax_edit_campaign_vid_key : afe_vid_edit_keyword, 
                    			afe_ajax_afe_edit_spin_content : afe_edit_spin_content
                    		};
            jQuery.post(ajaxurl, edit_campaign_search, function(edit_campaign_response) 
            {
                jQuery("#afe_campaign_check_result").html(edit_campaign_response);
        		jQuery("#afe_campaign_check_result").css("display","block");
            });
        }        
    });
});
</script>