<?php
require_once plugin_dir_path( __FILE__ ) .'header/header-main.php';
?>
<div class="container">
  <div class="row">
    <div class="col-md-12">

        <div class="afe_campaign_list_main_div">
            <h2 style="margin: 20px 0 20px;">Campaign Details <a href="<?php echo admin_url('admin.php?page=auto-campaign'); ?>"><input type="button" name="afe_campaign_list" id="afe_campaign_list" class="button button-primary alignright" style="background-color: #511fb4d1;box-shadow: none;border: none;text-shadow: none;" value=" Add New Campaign" /></a></h2>
        </div>
		<div>
		    <?php
		    global $wpdb;
            if(isset($_POST['id_campaign_list_del']))
            {
            	$campaign_list_post_del = $wpdb->prefix.'afe_camapign';
            	$campaign_list_post_del1 = $wpdb->prefix.'afe_campaign_post';

            	$afe_list_campaign_saved_del = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."afe_campaign_post WHERE campaign_id='".$_POST['id_campaign_list_del']."'");
            	if(!empty($afe_list_campaign_saved_del))
            	{
            	    foreach($afe_list_campaign_saved_del as $afe_list_campaign_saved_del1)
            	    {
                	    $post_camp_id = $afe_list_campaign_saved_del1->post_id;
                	    wp_delete_post($post_camp_id,true);
            	    }
            	}

            	$campaign_list_del1 = $wpdb->delete($campaign_list_post_del1, array( 'campaign_id' => $_POST['id_campaign_list_del'] ) );
            	$campaign_list_del = $wpdb->delete($campaign_list_post_del, array( 'id' => $_POST['id_campaign_list_del'] ) );
                if($campaign_list_del || $campaign_list_del1)
                {?>
                <div class="wrap">
                    <div class="updated"><p>Successfully Deleted!</p></div>
                </div>
                <?php }
            }

	        $afe_detail_campaign_saved_result1 = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."afe_camapign ORDER BY id DESC");
            ?>
            <table id="example" class="table table-striped widefat" style="width:100%;">
              <thead>
                <tr>
                  <th style="text-align: inherit;font-weight: 700;"></th>
                  <th style="text-align: inherit;font-weight: 700;">ID</th>
                  <th style="text-align: inherit;font-weight: 700;">Campaign Name</th>
                  <th style="text-align: inherit;font-weight: 700;">Action<h>
                </tr>
              </thead>
              <tbody>
                <?php
                foreach($afe_detail_campaign_saved_result1 as $afe_detail_campaign_saved_result)
                {
                	$afe_list_campaign_count = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."afe_campaign_post WHERE campaign_id='".$afe_detail_campaign_saved_result->id."'");
                ?>
                <tr>
                    <td></td>
                    <td><?php echo $afe_detail_campaign_saved_result->id; ?></td>
                    <td><p style="color: firebrick;"><?php echo $afe_detail_campaign_saved_result->c_title; ?></p></td>
                    <td>
                        <?php if(count($afe_list_campaign_count) == 0) {?>
                            <input type="submit" class="btn class_edit btn-success" value="Completed">
                        <?php } else {?>
                            <input type="submit" class="btn class_edit btn-warning" value="Processing">
                        <?php } $s = $afe_detail_campaign_saved_result->id;?>
                        <form method="post" action="admin.php?page=auto-campaign-edit&id=<?php echo $s; ?>" style="margin-bottom: 5px;display: inline-block;"><input type="hidden" name="id_campaign_list_edit" value="<?php echo $afe_detail_campaign_saved_result->id; ?>" /><input type="submit" style="background-color:#511fb4d1;border:none;color:white;" class="btn" value="Edit"></form>
                        <form method="post" action="" style="margin-bottom: 5px;display: inline-block;"><input type="hidden" name="id_campaign_list_del" value="<?php echo $afe_detail_campaign_saved_result->id; ?>" /><input type="submit" style="background-color:red;color:white;" class="btn class_edit btn-error" value="Delete"></form>
                    </td>
                </tr>
                <?php
                }
                ?>
              </tbody>
            </table>
		</div>

    </div>
  </div>
</div>

<script>
jQuery(function() {
    setTimeout(function() {
          jQuery('.updated').fadeOut('slow');
        }, 5000); // <-- time in milliseconds
});

jQuery(document).ready(function() 
{
  var table = jQuery('#example').DataTable({ 
        select: false,
        "columnDefs": [{
            className: "Name", 
            "targets":[0],
            "visible": false,
            "searchable":false
        }]
    });//End of create main table
});
</script>