<?php
/*
call list function here
*/
if (!function_exists('cmd_admin_call_list') ) {
	function cmd_admin_call_list() {
		$pagenum = isset( $_GET['pagenum'] ) ? absint( $_GET['pagenum'] ) : 1;
		$limit = 10;
		$offset = ( $pagenum - 1 ) * $limit;
		cmb_call_list($offset,$limit,$pagenum);
	}
	function delete_request_data(){
		global $wpdb;
		if($wpdb->delete( $wpdb->prefix.'ss_call_request', array( 'call_id' =>$_REQUEST['rid']  ), array( '%d' ) )){
			echo "done";
		} else {
			echo "error";
		}
		exit();
	}
	function call_request_success(){
		global $wpdb;
		if($wpdb->query("UPDATE ".$wpdb->prefix ."ss_call_request set status = 1 where call_id = '".(int)$_REQUEST['rid']."'")) {
			echo "done";
		} else {
			echo "error";
		}
		exit();
	}
?>
<?php  function cmb_call_list($offset,$limit,$pagenum) { ?>
<div><h2><img style="width:60px;height:60px;" src="<?php echo plugins_url();?>/call-me-back/icon/call-back-request.png" />Call Requested List</h2></div>
<table class="widefat" style="width:99%;">
  <thead>
    <tr>
      <th scope="col" class="manage-column column-name">Name</th>
	  <th scope="col" class="manage-column column-phone">Phone</th>
	  <th scope="col" class="manage-column column-email">Email</th>
	  <th scope="col" class="manage-column column-message">Message</th>
	  <th scope="col" class="manage-column column-from">From Time</th>
	  <th scope="col" class="manage-column column-to">To Time</th>
	  <th scope="col" class="manage-column column-status">Call Status</th>
	  <th scope="col" class="manage-column column-time">Requested Date</th>
      <th scope="col" class="manage-column column-action">Action</th>
    </tr>
  </thead>
  <tbody>
    <?php
	$result=cmb_all_call_list($offset,$limit);
	if(!empty($result))
	{
		foreach($result as $rows){
			$class = '';
			$class = ( $count % 2 == 0 ) ? ' class="alternate"' : '';
			$cmb_light = "<img title='Call waiting' style='width:15px;' src='". plugins_url(). "/call-me-back/icon/red.png' />";
			if($rows->status == '1') {
				$cmb_light = "<img title='Call Done' style='width:15px;' src='". plugins_url(). "/call-me-back/icon/green.png' />";
				echo "<tr class=".$class."><td>" . $rows->name . "</td><td>" . $rows->phone_no . "</td><td>" . $rows->email . "</td><td>" . $rows->message . "</td><td>" . $rows->from_time . "</td><td>" . $rows->to_time . "</td><td>" . $cmb_light . "</td><td>" . date("d/m/Y", strtotime($rows->requested_date)) . "</td><td><i style='color:#1fa67a;' title='Call Done' class='dashicons dashicons-phone'></i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a title='Delete Requested' href=javascript:void(0); onclick=cmb_delete_requested_date(".$rows->call_id.");><i class='wp-menu-image dashicons-before dashicons-post-trash'></i></a></td></tr>";
			} else {
				echo "<tr class=".$class."><td>" . $rows->name . "</td><td>" . $rows->phone_no . "</td><td>" . $rows->email . "</td><td>" . $rows->message . "</td><td>" . $rows->from_time . "</td><td>" . $rows->to_time . "</td><td>" . $cmb_light . "</td><td>" . date("d/m/Y", strtotime($rows->requested_date)) . "</td><td><a style='color:red;' title='Change Call Status' href=javascript:void(0); onclick=cmb_call_success(".$rows->call_id.");><i class='dashicons dashicons-phone'></i></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a title='Delete Requested' href=javascript:void(0); onclick=cmb_delete_requested_date(".$rows->call_id.");><i class='wp-menu-image dashicons-before dashicons-post-trash'></i></a></td></tr>";
			}
		}
	}
	?>
  </tbody>
  <tfoot>
  	<tr>
      <th scope="col" class="manage-column column-name">Name</th>
	  <th scope="col" class="manage-column column-phone">Phone</th>
	  <th scope="col" class="manage-column column-email">Email</th>
	  <th scope="col" class="manage-column column-message">Message</th>
	  <th scope="col" class="manage-column column-from">From Time</th>
	  <th scope="col" class="manage-column column-to">To Time</th>
	  <th scope="col" class="manage-column column-status">Call Status</th>
	  <th scope="col" class="manage-column column-time">Requested Date</th>
      <th scope="col" class="manage-column column-action">Action</th>
    </tr>
  </tfoot>
</table>
		<?php
		$total = countAllCallData();
		$num_of_pages = ceil( $total / $limit );
		$num_of_pages = ceil( $total / $limit );
		$page_links = paginate_links( array(
			'base' 		=> add_query_arg( 'pagenum', '%#%' ),
			'format' 	=> '',
			'prev_text' => __( '&laquo;', 'aag' ),
			'next_text' => __( '&raquo;', 'aag' ),
			'total' 	=> $num_of_pages,
			'current' 	=> $pagenum
		) );
		if ( $page_links ) {
			echo '<div class="tablenav"><div class="tablenav-pages wp_table_pag">' . $page_links . '</div></div>';
		}
		?>
		
<script type="text/javascript">
function cmb_delete_requested_date(rid){
	if(confirm('Are you sure you want to delete this request ?')) {
		var ajax_url = '<?php echo admin_url( 'admin-ajax.php' ); ?>';
		jQuery.ajax({
			type: "POST",
			url:ajax_url,
			data : {'action':'delete_request_data','rid': rid},
			success: function(response){
				if(response=='done') {
					alert('Requested information deleted successfully');
					window.location.reload();
				} else {
					alert('oops error occured try again or reload your page');
				}
			}
		});
	}
}
function cmb_call_success(rid){
	if(confirm('Are you sure call request done ?')) {
		var ajax_url = '<?php echo admin_url( 'admin-ajax.php' ); ?>';
		jQuery.ajax({
			type: "POST",
			url:ajax_url,
			data : {'action':'call_request_success','rid': rid},
			success: function(response){
				if(response=='done') {
					alert('Updated requested information successfully');
					window.location.reload();
				} else {
					alert('oops error occured try again or reload your page');
				}
			}
		});
	}
}
</script>
<?php } } ?>