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

        <h2 style="margin: 20px 0 20px;" >Campaign Posts</h2>
		<div>
		    <?php
		    global $wpdb;
            if(isset($_POST['id_campaign_delete']))
            {
            	$campaign_post_del = $wpdb->prefix.'afe_campaign_post';
            	$camapaign_delete = $wpdb->delete($campaign_post_del, array( 'post_id' => $_POST['id_campaign_delete'] ) );
            	$campaign_post_delete = wp_delete_post( $_POST['id_campaign_delete'], true);
                if($camapaign_delete && $campaign_post_delete)
                {?>
                <div class="wrap">
                    <div class="updated"><p>Successfully Deleted!</p></div>
                </div>
                <?php }
            }

	        $afe_show_campaign_saved_result_detail1 = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."afe_campaign_post ORDER BY id ASC");
            ?>
            <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;">Schedule Post</th>
                  <th style="text-align: inherit;font-weight: 700;">Schedule Time</th>
                  <th style="text-align: inherit;font-weight: 700;">Compaign Name</th>
                  <th style="text-align: inherit;font-weight: 700;">Action<h>
                </tr>
              </thead>
              <tbody>
                <?php
                foreach($afe_show_campaign_saved_result_detail1 as $afe_show_campaign_saved_result_detail)
                {
			        $afe_show_campaign_name = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."afe_camapign where id='".$afe_show_campaign_saved_result_detail->campaign_id."'");
                ?>
                <tr>
                    <td></td>
                    <td><p style="color: firebrick;"><?php echo get_the_title( $afe_show_campaign_saved_result_detail->post_id ); ?></p></td>
                    <td><?php echo $afe_show_campaign_saved_result_detail->schedule_date; ?></td>
                    <td><?php echo $afe_show_campaign_name[0]->c_title; ?></td>
                    <td>
                        <form method="post" action="" style="margin-bottom: 5px;display: inline-block;"><input type="hidden" name="id_campaign_delete" value="<?php echo $afe_show_campaign_saved_result_detail->post_id; ?>" /><input type="submit" class="btn class_edit btn-warning" 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>