<?php 
header('X-Frame-Options: GOFORIT');
@session_start();
error_reporting(E_ALL);
include_once 'inc/functions.php';
include 'inc/database.php';

require_once __DIR__ . '/vendor/autoload.php';
use Bigcommerce\Api\Client as Bigcommerce;

$config = array();


if( isset($_GET['signed_payload']) || (isset($_GET['store_hash']) && !empty($_GET['store_hash'])) ){
	$data = decodeSignedRequest($_GET['signed_payload']);
	$hash = isset($data['store_hash']) ? $data['store_hash'] : $_GET[
		'store_hash'];
	$_SESSION['store_hash'] = $hash;
	$access_token = $db->getSingleColumn( "stores", "access_token", array( "store_id" => $hash ));
		$_SESSION['access_token']	= $access_token;
        if( $access_token == "")
            die("store access token not found.");
        $config = array(
            'client_id'  => CLIENT_ID,
            'auth_token' => $access_token,
            'store_hash' => $hash
        );
    Bigcommerce::configure($config);
    Bigcommerce::verifyPeer(false);
	
	
   	$limit			= 250;
	$page 			= 1;
	$pro_id			= $_REQUEST['product_id'];
	$pro_name		= $_REQUEST['pro_name'];
	$customer_products=array();
	
	for($i=1; $i<=$page; $i++){
		$curl = curl_init();
		curl_setopt_array($curl, array(
		  CURLOPT_URL => "https://api.bigcommerce.com/stores/".$hash."/v3/wishlists?page=".$page."&limit=".$limit,
		  CURLOPT_RETURNTRANSFER => true,
		  CURLOPT_ENCODING => "",
		  CURLOPT_MAXREDIRS => 10,
		  CURLOPT_TIMEOUT => 30,
		  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
		  CURLOPT_CUSTOMREQUEST => "GET",
		  CURLOPT_HTTPHEADER => array(
			"Accept: application/json",
			"Cache-Control: no-cache",
			"Connection: keep-alive",
			"Content-Type: application/json",
			"X-Auth-Client: ".CLIENT_ID,
			"X-Auth-Token: ".$access_token,
			"cache-control: no-cache"
		  ),
		));
		$response 	= curl_exec($curl);
		$err 		= curl_error($curl);
		curl_close($curl);
		if($response){	
			$response=json_decode($response,true);
			$wishlists=$response['data'];
			$count=$response['meta']['pagination']['count'];
			if($count){
				foreach($wishlists as $key=>$singleWishlist){
				$id				= $singleWishlist['id'];
				$customer_id	= $singleWishlist['customer_id'];	
				$items			= $singleWishlist['items'];
					if(!empty($items)) {
						foreach($items as $key=>$item){	
							$customer_products[$customer_id][]	= $item['product_id'];

						}	
					}
				}
				$page++;
			}
		}
	}
	
    $store_url = $db->getSingleColumn("stores", "store_url", array( "access_token" => $access_token) );
}else{
//	echo "Not in if condition";
//	exit();
}



?>
<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Bigcommerce Wishlist App</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<script  src="https://code.jquery.com/jquery-3.3.1.js"></script>
        <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
        <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.19/css/dataTables.bootstrap.min.css">
        <link rel="stylesheet" href="<?php  echo BASE_PATH ?>assets/css/custom.css?<?= time(); ?>">
        <!-- Add icon library -->
		<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

<!--        <script  src="https://code.jquery.com/jquery-3.3.1.js"></script>-->
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

		<script src="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.19/js/jquery.dataTables.min.js" type="text/javascript" charset="utf-8" ></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.19/js/dataTables.bootstrap.min.js" type="text/javascript" charset="utf-8" ></script>
   <!--        adding for test start-->
      
       <link rel="stylesheet" href="//cdn.datatables.net/responsive/2.0.2/css/responsive.dataTables.min.css">
        <script src="//cdn.datatables.net/responsive/2.2.3/js/dataTables.responsive.min.js" type="text/javascript" charset="utf-8" ></script>
        
        <link rel="stylesheet" href="//cdn.datatables.net/buttons/1.1.2/css/buttons.dataTables.min.css">
        <script src="//cdn.datatables.net/buttons/1.1.2/js/dataTables.buttons.min.js" type="text/javascript" charset="utf-8" ></script>
        
         <link rel="stylesheet" href="//cdn.datatables.net/select/1.1.2/css/select.dataTables.min.css">
        <script src="//cdn.datatables.net/select/1.1.2/js/dataTables.select.min.js" type="text/javascript" charset="utf-8" ></script>
        
<!--        test ends-->
        <script src="<?php  echo BASE_PATH ?>assets/js/cstm.js?v=1.19"></script>
        
    </head>
    <body>
        <div class="app_conatiner">
				<div style="overflow: hidden;">
					<h1 class="total" >
						<span class="total-text">
							Product Name: &nbsp;<?php echo $pro_name;?>
						</span>
						<span class="req_feature"> 
							<?php if($pro_id){	?>		
							<span>
								<button id="down_btn" class="btn down_btn"><i class="fa fa-download"></i> Download</button>
							</span>
							<?php } ?>
							<button id="request_btn" class="btn request_btn">Request a Feature</button>    
						</span>
					</h1>  
				</div>	
                <div id="main_content">
                	<div class="sub_conatiner">
                        <table id="show_record1" class="table table-striped ">
                    	    <thead>
                    	      <tr>
                    	      	<th>Product Id</th>
                                <th>Customer Id</th>
                                <th>Customer Name</th> 
                                <th>Customer Email</th>
                               
                    	       </tr>
                    	    </thead>
                        <tbody>
                        	<?php 											
								if($customer_products){
									foreach($customer_products as $keys=>$customer_product){
										if(in_array($pro_id, $customer_product)){
											$customers		= Bigcommerce::getCustomer($keys);
											$customer_id	= $customers->id;
											$first_name		= $customers->first_name;
											$last_name		= $customers->last_name;
											$email			= $customers->email;
											?>
											<tr class="data">
												<td><?php echo $pro_id ; ?></td>
												<td><?php echo $customer_id ; ?></td>
												<td><?php echo $first_name." ".$last_name;?></td>  
												<td><?php echo $email ; ?></td> 
                       					
                        					</tr>
								<?php	}		
									}
									?>
									
								<?php											
								}else{ ?>
								<tr>
                        			<td colspan="4"  align="center">Sorry, Customers not found..!</td>
                        		</tr>
                            	
                            <?php }?>
                        </tbody>                
                    </table>
                	</div>	              	
                </div>  			
				<div style="text-align: center; margin: 20px;">
					<a href="//www.webecommercepros.com/" target="_blank">
						<img src="assets/images/logo.png" width="170px" height="40px"> 
					</a>
<!--
					<a href="//www.webecommercepros.com/" style="text-decoration: none;" target="_blank">
						<span style="color: rgb(141,141,141);">	App designed and Developed by Web Ecommerce Pros.</span>
					</a>
-->
				</div>
       				     
        </div>    
    	    	
    </body>
</html>   
<script>
    $(document).ready(function(){
        $('select[name="show_record1_length"]').css('padding-top','0px');  
		
		var custom_btn	= setInterval(function(){ 
			if($("#show_record1_paginate").length){
				console.log("pagination check");
//				$('<button id="back_btn" class="btn back_btn"><i class="fa fa-arrow-left" aria-hidden="true"></i>back</button>').prepend('ul.pagination');
				$("#show_record1_paginate").prepend('<button id="back_btn" class="btn back_btn" style="margin-right: 10px; margin-bottom: 30px;"><i class="fa fa-arrow-left" aria-hidden="true"></i> back</button>'); 
				clearInterval(custom_btn);
			}
							  						  
		}, 1000);
    });
    
</script> 	 
