<?php session_start();
require_once("./../../config/config.php");
require_once("./validated_admin.php");

$myformId=SESS_MASTER;
$myIp= ipCheck() ;
$dateceated=date("Y-m-d G:i:s");
$createdby='0';
$api_type=$_REQUEST['api_type'];


// flipkart category
if($api_type=='flipkart'){

$url='https://affiliate-api.flipkart.net/affiliate/api/mukeshgehl.json';

$headers = array( 
"Content-Type: application/json",
"Accept: application/json",
"Access-Control-Request-Method: GET",
"Fk-Affiliate-Token:8fadc54d9b60408c9fa0d408b223706d",
"Fk-Affiliate-Id:mukeshgehl"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

$result = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$data = json_decode($result,true); 
$datas=$data['apiGroups']['affiliate']['apiListings']; 
echo ' <div class="row">
	       
<div class="col-md-12 offset-md-3"><select onChange="products(this.value);" name="Fetch Data" id="fetch" class="w-100 p-1 my-5">
<option value="">Select Ctegory</option>' ;
  	

foreach($datas as $product_cat){

   $options= $product_cat['availableVariants']['v1.1.0']['resourceName'] ;
   $url= $product_cat['availableVariants']['v1.1.0']['get'] ;

   echo '<option value="'.$url.'">'.$options.'</option>' ; 

}
   echo '</select></div>
  
   
   </div>
   ' ;
} 
// flipkart category

// flipkart products
if($api_type=='products'){
  $urls=$_REQUEST['url'];
  $sig=$_REQUEST['sig'];

  
$url= $urls.'&sig='.$sig;

$headers = array( 
"Content-Type: application/json",
"Accept: application/json",
"Access-Control-Request-Method: GET",
"Fk-Affiliate-Token:8fadc54d9b60408c9fa0d408b223706d",
"Fk-Affiliate-Id:mukeshgehl"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

$result = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$data = json_decode($result,true); 
$valid=$data['validTill'];

$product=$data['products'];
$dt = gmdate('d-m-Y',$valid);  // convert UNIX timestamp to PHP DateTime

foreach($product as $pro){
$title=addslashes($pro['productBaseInfoV1']['title']);
$description=addslashes($pro['productBaseInfoV1']['productDescription']);
$company_name='FLIPKART';
$product_image=$pro['productBaseInfoV1']['imageUrls']['400x400'];
$amount=$pro['productBaseInfoV1']['maximumRetailPrice']['amount'];
$product_url=$pro['productBaseInfoV1']['productUrl'];
$brand=addslashes($pro['productBaseInfoV1']['productBrand']);
$category=$pro['productBaseInfoV1']['categoryPath'];
$coupon_country_name='INDIA';                                             
try{

   $olgaquery="INSERT INTO product_master SET company_name='$company_name', title ='$title',
   discription='$description',product_url='$product_url',amount='$amount',category='$category',product_image='$product_image',
   exp_date='$dt',country_name='$coupon_country_name',
   brand_store='$brand',created_by='0',created_type='Admin'"; 
         $stmt = $conn->prepare($olgaquery);
         if($stmt->execute()){
         
   
     
            }
     }  catch(PDOException $exception){
               die('ERROR: ' . $exception->getMessage());
           } 

}
} 
// flipkart products
// omgpm
if($api_type=='optimisemedia'){
   
   $t = microtime(true);
   $micro = sprintf("%03d",($t - floor($t)) * 1000);
   $utc = gmdate('Y-m-d H:i:s.', $t).$micro;
   
   $sig_data= $utc;
   //########################Please Add your API Key & Private Key here to test##################################################
   $api_key='dad5c1d4-8f54-4324-af0e-0fa225bc8180';
   $private_key='12a2ec9ddf8d47ada4d7943aeb14c017';
   //############################################################################################################################
   
   $concateData = $private_key.$sig_data;
   $sig = md5($concateData);
   
   //############# This is a test url. You have to change the parameters according to your need #######################
   $url="http://api.omgpm.com/network/OMGNetworkApi.svc/v1.2/ProductFeeds/GetProducts?". http_build_query(array(
    'AgencyID' => 95, 
    'AID'=>447553,
    'MID'=> '',
    'Keyword'=>'ELECTRONICS',
    'MinPrice'=>'2000',
    'MaxPrice'=>'',
    'Currency'=>'INR',
    'Brand'=>'',
    'DiscountedOnly'=>'False',
    'MPN'=>'',
    'ProductSKU'=>'',
    'FeedID'=>'',
    'Key' => $api_key,
    'Sig' => $sig,
    'SigData' => $sig_data
   ));
   

   $headers = array( 
    "Content-Type: application/json",
    "Accept: application/json",
    "Access-Control-Request-Method: GET" 
    );
   
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $url);
   curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
   curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
   curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    
   $result = curl_exec($ch);
   $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    
   
   $data = json_decode($result,true); 
   $product=$data['GetProductsFeedsResult'];

   print_r($product);
   foreach($product as $pro){
      $title=addslashes($pro['ProductName']);
      $description=addslashes($pro['ProductDescription']);
      $company_name='OPTIMISE MEDIA';
      $product_image=$pro['ProductMediumImageURL'];
      $amount=$pro['ProductPrice'];
      $product_url=$pro['ProductURL'];
      $brand=addslashes($pro['Brand']);
      $category=$pro['CategoryName'];
      $coupon_country_name='INDIA';                                             
      try{
      
         $olgaquery="INSERT INTO product_master SET company_name='$company_name',title='$title',
         discription='$description',product_url='$product_url',amount='$amount',category='$category',product_image='$product_image',
         exp_date='$dt',country_name='$coupon_country_name',
         brand_store='$brand',created_by='0',created_type='Admin'"; 
               $stmt = $conn->prepare($olgaquery);
               if($stmt->execute()){
               
         
           
                  }
           }  catch(PDOException $exception){
                     die('ERROR: ' . $exception->getMessage());
                 } 
      
      }

}



//
// omgpm

   //affiliatefuture
   if($api_type=='affiliatefuture'){

    
         echo '<div class="col-md-6 offset-md-3">
         <form name="upload_data_csv" class="p-5" enctype="multipart/form-data" action="forms/upload_csv_affiliatefuture.php" method="POST">
            <div class="mb-5 padding-style">
            <h5 class="py-3">You can upload Affiliatefuture CSV file <br>( CSV file format )</h5><a>
               <div class="justify-content-between d-flex pt-2">
              <input type="file" name="upload_data" id="upload_data" required="">
                 <input type="submit" name="upload_data" id="upload_data" class="btn btn-search" value="Upload CSV File">
               </div>
             </a></div><a>
           
           </a></form></div>';
      
      
     
   }

   //
   //tradedoubler
   if($api_type=='tradedoubler'){

      $url="http://api.tradedoubler.com/1.0/products.json;page=1;pageSize=100;fid=41833?token=7D07260193F54B8774804347D9827E9DEBA8C6E9";
   
   
      $headers = array( 
       "Content-Type: application/json",
       "Accept: application/json",
       "Access-Control-Request-Method: GET" 
       );
      
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
       
      $result = curl_exec($ch);
      $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
      $data = json_decode($result,true);
      print_r($data);

      $product= $data['products'];
      print_r($product[0]['offers'][0]['priceHistory'][0]['price']['value'] );
    
     if($data){
     foreach($product as $pro){
      $title=addslashes($pro['name']);
      $description=addslashes($pro['description']);
      $company_name='Tradedoubler';
      $product_image=$pro['productImage']['url'];
      $amount=$pro['offers'][0]['priceHistory'][0]['price']['value'] ;
      $product_url=$pro['offers'][0]['productUrl'];
      $brand=addslashes($pro['fields'][0]['value']);
      $category=$pro['categories'][0]['tdCategoryName'];
      $coupon_country_name='INDIA';    
                                             
      try{
      
         $olgaquery="INSERT INTO product_master SET company_name='$company_name',title='$title',
         discription='$description',product_url='$product_url',amount='$amount',category='$category',product_image='$product_image',
         exp_date='$dt',country_name='$coupon_country_name',
         brand_store='$brand',created_by='0',created_type='Admin'"; 
               $stmt = $conn->prepare($olgaquery);
               if($stmt->execute()){
               
         
           
                  }
           }  catch(PDOException $exception){
                     die('ERROR: ' . $exception->getMessage());
                 } 
      
      } }


   }

   //tradedoubler
   
   // awin
  if($api_type=='awin'){
  $api = array("api"=>"awin");
  echo json_encode($api);
   
 } 
   
   //awin

?>