<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('memory_limit', '2048M');

include_once "../config.php";
include_once "../database1.php";
include_once "../../validation.php";
include_once "../../Helper.php";

if(!isset($_GET['start']))
{
    die("Page Not Found");
}

$start = $_GET['start'];

class insertImages extends database{
    public function insertData($images)
    {
       
        $count = 1;
        foreach($images['images'] AS $image)
        {
            if($count == 1)
            {
                $this->update('tb_product', ['cl_image' => $image], ['cl_id' => $images['cl_id']]);
            }
            else
            {
                $dataInsert['cl_type'] = 'Product';
                $dataInsert['cl_product_id'] = $images['cl_id'];
                $dataInsert['cl_name'] = $image;
                $dataInsert['cl_user_id'] = 0;
                $this->insert('tb_files', $dataInsert);
            }
            $count++;
        }
    }
    
    public function checkImage($productID)
    {
        return $this->fetchRow('tb_product', ['cl_product_id' => $productID, 'cl_supplier' => 'action']);
    }
}


$url = 'https://api.action.pl/api/ade/v2/ProductPictures/GetForAllProducts';
$headers = array(
  'CustomerId: 83473',
  'UserName: axo9622',
  'ActionApiKey: UtCNXXjIRHRR0w0gL2P6FbMc2FBknu5MzhMZckzAKS0='  
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);

$response = json_decode($response);

$obj = new insertImages;
$productCount = 1;
foreach($response->data AS $data)
{
    
    if($productCount >= $start)
    {
       // $productID = 'VETHLSKMP0001';
        $productID = $data->productId;
        $product = $obj->checkImage($productID);


        if($product)
        {

            $images = $data->images;
            $count = 1;
            $imagesInsert = [];

            $imagesInsert['product_id'] = $data->productId;
            $imagesInsert['cl_id'] = $product['cl_id'];
            foreach($images AS $image)
            {
                $imageArray = explode('.', $image->path);
                $extension = end($imageArray);
                $path = 'https://cdn.action.pl/File.aspx?CID=83473&UID=axo9622&PID=1c8dac81aaf0fce96def27109fb6aa9d&P=' . $image->path;
                
                $file = file_get_contents($path);
                $imageName = 'action/' . $image->productId . '_' . $count . '.' . $extension;
                file_put_contents(__DIR__ . "/../../uploads/products/" . $imageName, $file);
                $imagesInsert['images'][] = $imageName;
                $count++;



            }

            $obj->insertData($imagesInsert);

            $fp = fopen('action_insert_image_text.txt', 'a');
            //opens file in append mode  
            fwrite($fp, 'Inserted ' . $productCount . ' - (' . $productID . ')<br/>');  
            fclose($fp); 
        }
        
       
    }
    $productCount++;
    
}