<?php
require_once plugin_dir_path( __FILE__ ) .'header/header-main.php';
require_once plugin_dir_path( __FILE__ ) .'ch-auto-content-tabs.php';
?>
<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;" >Import Results</h2>
				<ul class="nav nav-tabs ">
					<li class="active">
						<a href="#tab_default_1" data-toggle="tab"> Article Search </a>
					</li>
					<li>
						<a href="#tab_default_2" data-toggle="tab"> Video Search </a>
					</li>
					<li>
						<a href="#tab_default_3" data-toggle="tab"> Image Search </a>
					</li>
				</ul>
				<div class="tab-content">
					<div class="tab-pane active" id="tab_default_1">
				    <?php
                    global $user_ID, $wpdb;
                    
                    if(isset($_POST['id_send_post']))
                    {
                        $afe_convert_post = $wpdb->get_results("SELECT * FROM ".$wpdb->base_prefix."ch_saved_result WHERE ID = '".$_POST['id_send_post']."' AND type = 'article'");
                        
                        $postTitle = $afe_convert_post[0]->title;
                        $query = $wpdb->prepare(
                            'SELECT ID FROM ' . $wpdb->posts . ' WHERE post_title = %s',
                            $postTitle
                        );
                        $wpdb->query( $query );
                        if ( $wpdb->num_rows ) 
                        {
                        ?>
                            <div class="wrap">
                                <div class="updated"><p>Post is alreday created</p></div>
                            </div>
                        <?php
                        } 
                        else 
                        {
                            $afe_my_post_insert = array(
                                              'post_title'    =>$afe_convert_post[0]->title,
                                              'post_content'  =>$afe_convert_post[0]->long_desc,
                                              'post_status'   => 'publish',
                                              'post_author'   => 1,
                                              'post_category' => array( 8,39 )
                                            );
                                             
                                            // Insert the post into the database
                            $afe_post_insert = wp_insert_post( $afe_my_post_insert );
                        
                            if($afe_post_insert)
                            {
                                ?><script>window.open("<?php echo admin_url('post.php?post='.$afe_post_insert.'&action=edit'); ?>");</script><?php
                            }
                        }
                    }

                    if(isset($_POST['id_delete']))
                    {
                    	$tablename_del = $wpdb->base_prefix.'ch_saved_result';
                    	$delete = $wpdb->delete($tablename_del, array( 'id' => $_POST['id_delete'] ) );
                        if($delete)
                        {?>
                        <div class="wrap">
                            <div class="updated"><p>Successfully Deleted!</p></div>
                        </div>
                        <?php }
                    }
                    
			        $afe_show_saved_result_detail = $wpdb->get_results("SELECT * FROM ".$wpdb->base_prefix."ch_saved_result WHERE type = 'article' 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;">Title</th>
                          <th style="text-align: inherit;font-weight: 700;">Desc</th>
                          <th style="text-align: inherit;font-weight: 700;">Action<h>
                        </tr>
                      </thead>
                      <tbody>
                        <?php foreach($afe_show_saved_result_detail as $afe_show_saved_result_detail1) 
                        {?> 
                        <tr>
                            <td></td>
                            <td><p style="color: firebrick;"><?php echo $afe_show_saved_result_detail1->title; ?></p></td>
                            <td><?php echo wp_trim_words($afe_show_saved_result_detail1->short_des,15); ?></td>
                            <td>
                                 <!--<a href="post-new.php?article_id=<?php //echo $afe_show_saved_result_detail1->id; ?>&post_title=<?php //echo $afe_show_saved_result_detail1->title?>" class="button button-success" id="convert_post">Convert To Post</a>-->
                                <?php
                                $query1 = $wpdb->prepare(
                                    'SELECT ID FROM ' . $wpdb->posts . ' WHERE post_title = %s',
                                    trim($afe_show_saved_result_detail1->title)
                                );
                                $wpdb->query( $query1 );
                                if ( $wpdb->num_rows ) 
                                { ?>
                                <input type="submit" class="btn btn-success" style="margin-bottom: 5px;cursor:default;" value="Already posted">                                    
                                <?php } else {
                                ?>
                                <form method="post" action="" style="margin-bottom: 5px;display: inline-block;"><input type="hidden" name="id_send_post" value="<?php echo $afe_show_saved_result_detail1->id; ?>" /><input type="submit" class="btn btn-success" value="Add to post"></form>
                                <?php } ?>
                                <form method="post" action="" style="margin-bottom: 5px;display: inline-block;"><input type="hidden" name="id_delete" value="<?php echo $afe_show_saved_result_detail1->id; ?>" /><input type="submit" class="btn class_edit btn-danger" value="Delete"></form>
                            </td>
                        </tr>
                        <?php } ?>
                      </tbody>
                    </table>
					</div>
					<div class="tab-pane" id="tab_default_2">
					<?php    
					global $wpdb;
					
                    if(isset($_POST['id_video_send_post']))
                    {
                        $afe_video_convert_post = $wpdb->get_results("SELECT * FROM ".$wpdb->base_prefix."ch_saved_result WHERE ID = '".$_POST['id_video_send_post']."' AND type = 'video'");
                        

                        $afe_post_saved_fetch_link_data = unserialize($afe_video_convert_post[0]->link);
            		    $afe_post_saved_video_url = $afe_post_saved_fetch_link_data['link'];
            		    $video_post_content = "<iframe width=420 height=315 src=https://www.youtube.com/embed/$afe_post_saved_video_url></iframe>";
            		    $video_post_content_desc = $afe_video_convert_post[0]->short_des;
            		    
            		    $combined_post_content = $video_post_content."<br/><br/>".$video_post_content_desc;


                        $videopostTitle = $afe_video_convert_post[0]->title;
                        $video_query = $wpdb->prepare(
                            'SELECT ID FROM ' . $wpdb->posts . ' WHERE post_title = %s',
                            trim($videopostTitle)
                        );
                        $wpdb->query( $video_query );
                        if ( $wpdb->num_rows ) 
                        {
                        ?>
                            <div class="wrap">
                                <div class="updated"><p>Post is alreday created</p></div>
                            </div>
                        <?php
                        } 
                        else 
                        {
                            $afe_video_my_post_insert = array(
                                              'post_title'    =>$afe_video_convert_post[0]->title,
                                              'post_content'  =>$combined_post_content,
                                              'post_status'   => 'publish',
                                              'post_author'   => 1
                                            );
                                             
                                            // Insert the post into the database
                            $afe_video_post_insert = wp_insert_post( $afe_video_my_post_insert );
                        
                            if($afe_video_post_insert)
                            {
                                ?><script>window.open("<?php echo admin_url('post.php?post='.$afe_video_post_insert.'&action=edit'); ?>");</script><?php
                            }
                        }
                    }

                    if(isset($_POST['id_video_delete']))
                    {
                    	$video_tablename_del = $wpdb->base_prefix.'ch_saved_result';
                    	$video_delete = $wpdb->delete($video_tablename_del, array( 'id' => $_POST['id_video_delete'] ) );
                        if($video_delete)
                        {?>
                        <div class="wrap">
                            <div class="updated"><p>Successfully Deleted!</p></div>
                        </div>
                        <?php }
                    }
					
			        $afe_show_video_saved_result_detail = $wpdb->get_results("SELECT * FROM ".$wpdb->base_prefix."ch_saved_result WHERE type = 'video' ORDER BY id DESC");
                    ?>
                    <table id="example_video" 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;">Image</th>
                          <th style="text-align: inherit;font-weight: 700;">Title</th>
                          <th style="text-align: inherit;font-weight: 700;">Description</th>
                          <th style="text-align: inherit;font-weight: 700;">Action<h>
                        </tr>
                      </thead>
                      <tbody>
                        <?php foreach($afe_show_video_saved_result_detail as $afe_show_video_saved_result_detail1) 
                        {
                            $afe_saved_fetch_link_data = unserialize($afe_show_video_saved_result_detail1->link);
                		    $afe_saved_video_url = $afe_saved_fetch_link_data['link'];
                		    $afe_saved_video_image = $afe_saved_fetch_link_data['thum_img_url'];
                        ?> 
                        <tr>
                            <td></td>
                            <td><a href="https://www.youtube.com/watch?v=<?php echo $afe_saved_video_url;?>" target="_blank"><img src="<?php echo $afe_saved_video_image; ?>"></a></td>
                            <td><p style="color: firebrick;"><?php echo $afe_show_video_saved_result_detail1->title; ?></p></td>
                            <td><?php echo wp_trim_words($afe_show_video_saved_result_detail1->short_des,10); ?></td>
                            <td>
                                 <!--<a href="post-new.php?article_id=<?php //echo $afe_show_saved_result_detail1->id; ?>&post_title=<?php //echo $afe_show_saved_result_detail1->title?>" class="button button-success" id="convert_post">Convert To Post</a>-->
                                <?php
                                $query2 = $wpdb->prepare(
                                    'SELECT ID FROM ' . $wpdb->posts . ' WHERE post_title = %s',
                                    trim($afe_show_video_saved_result_detail1->title)
                                );
                                $wpdb->query( $query2 );
                                if ( $wpdb->num_rows ) 
                                { ?>
                                <input type="submit" class="btn btn-success" style="cursor:default;" value="Already posted">                                    
                                <?php } else {
                                ?>
                                <form method="post" action="" style="margin-bottom: 5px;display: inline-block;"><input type="hidden" name="id_video_send_post" value="<?php echo $afe_show_video_saved_result_detail1->id; ?>" /><input type="submit" class="btn btn-success" value="Add to post"></form>
                                <?php } ?>
                                <form method="post" action="" style="margin-bottom: 5px;display: inline-block;"><input type="hidden" name="id_video_delete" value="<?php echo $afe_show_video_saved_result_detail1->id; ?>" /><input type="submit" class="btn class_edit btn-danger" value="Delete"></form>
                            </td>
                        </tr>
                        <?php } ?>
                      </tbody>
                    </table>
					    
					</div>
					<div class="tab-pane" id="tab_default_3">
                        <?php global $wpdb;
                        $args = array(
                                    'posts_per_page' => -1,
                                    'post_type'=> 'attachment',
                        			'post_status' => 'inherit',
                        			'order' => 'DESC', // 'ASC'
                           			'orderby' => 'date', // modified | title | name | ID | rand
                                    'meta_query'  => array(
                        				array(
                        					'key'  => 'wpch_img',
                        					'value'=> 'wpch_img_saved',
                        					
                        				),
                        			)
                                );
                        $afe_get_posts = new Wp_Query( $args );
                		
                		if($afe_get_posts->posts)
                		{
                		?>
                        <table id="example_img" 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;">Image</th>
                              <th style="text-align: inherit;font-weight: 700;">Title</th>
                              <th style="text-align: inherit;font-weight: 700;">Action<h>
                            </tr>
                          </thead>
                          <tbody>
                        <?php
                			$a=0;
                			foreach($afe_get_posts->posts as $attachment)
                			{
                    			$afe_thum = wp_get_attachment_thumb_url($attachment->ID);
                    			?>
                    			<tr>
                    			    <td></td>
                                    <td width="80"><img src="<?php echo $afe_thum; ?>" style="width:60px;height:45px;" /></a></td>
        							<td>
                                       <p><a href="<?php echo get_edit_post_link( $attachment->ID ); ?>"><strong><?php echo (!empty($attachment->post_title)?$attachment->post_title:'no title');?></strong></a></p>	
                                       <p><?php echo $attachment->post_content; ?></p>
                                    </td>
                                    
                                    <td width="80"><a href="<?php echo get_delete_post_link($attachment->ID,'', true)?>" class="btn class_edit btn-danger" id="delete_saved">Delete</a></td>
        						</tr>
                    			<?php 
                    			$a++;
                			}
                		?>
                           </tbody>
                        </table>
                		<?php
                		}
                		else
                		{
                			echo '<table class=widefat cellspacing=0><tr><td align="center">No Image.</td></tr></table>';
                		}
                        ?>
					</div>
				</div>
			</div>
		</div>

    </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

  
});

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

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

</script>
