<?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";



$url = 'https://api.action.pl/api/ade/v2/Product/GetAll?Language=English&Currency=EUR';
$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);



class Insert extends database
{
    public function checkProduct($productID)
    {
        return $this->fetchRow('tb_product', ['cl_product_id' => $productID, 'cl_supplier' => 'action']);
    }
    
    public function updateBulk($products)
    {
        $count  = 0;
        $currentDateTime = date('Y-m-d H:i:s');
        
        $insertHTML = '';
        $where = [];
        
        $bulkCount = 1;
          

        foreach($products AS $product)
        {

            if($count == 500)
            {

                  $sql = 'INSERT INTO tb_product (cl_cat_id, cl_sub_cat_id, cl_brand_id, cl_name, cl_slug, cl_regular_price, cl_sale_price,cl_supplier,cl_product_id,cl_stock,cl_ean, cl_x, cl_y, cl_z, cl_weight,cl_last_price_updated, cl_manufacturer, cl_created_at, cl_updated_at)
VALUES ' . $insertHTML;
            
  
             //   $sql = ' UPDATE tb_product SET ' . $stockHtml . ' ELSE cl_stock END, ' . $supplierPriceHTML . ' ELSE cl_supplier_price END, ' . $profitPercentageHTML . ' ELSE cl_profit_percentage END, ' . $monthlyPriceHTML . ' ELSE cl_monthly_price END ';



                //$whereMultiple = array_merge($whereMultipleStock,$whereMultipleSupplier, $whereMultipleProfitPercentage, $whereMultipleMonthlyPrice);

                $this->callQueryInsert($sql, $where);
                
                 $fp = fopen('action_insert_product_text.txt', 'a');
                //opens file in append mode  
                fwrite($fp, 'Inserted ' . $bulkCount * 500 . ' Products<br/>');  
                fclose($fp); 
                
                
                $bulkCount++;


                $count = 0;
                $insertHTML = '';
                $where = [];

                
            }
            
            $productArray = $product;
            
            if($count > 0)
            {
                $insertHTML.= ',';
            }
            $insertHTML.= '(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)';
            
            foreach($product AS $each)
            {
                $where[] = $each;
            }
            

            $count++;
        }

           $sql = 'INSERT INTO tb_product (cl_cat_id, cl_sub_cat_id, cl_brand_id, cl_name, cl_slug, cl_regular_price, cl_sale_price,cl_supplier,cl_product_id,cl_stock,cl_ean, cl_x, cl_y, cl_z, cl_weight,cl_last_price_updated, cl_manufacturer, cl_created_at, cl_updated_at)
VALUES ' . $insertHTML;
        //  $whereMultiple = array_merge($whereMultipleStock,$whereMultipleSupplier, $whereMultipleProfitPercentage, $whereMultipleMonthlyPrice);
     
         $this->callQueryInsert($sql, $where);

         $fp = fopen('action_insert_product_text.txt', 'a');
                //opens file in append mode  
                fwrite($fp, 'Inserted All<br/>');  
                fclose($fp); 
            
        return 1;
    }
}
//print_r($response->data[0]);
$obj = new Insert();
$insertData = array();
$count = 0;
foreach($response->data AS $data)
{
    if(!$obj->checkProduct($data->productId))
    {
        $productNameSlug = preg_replace('/\s+/', '-',  $data->productName);
        $productNameSlug = str_replace('+', '-', $productNameSlug);
        $productNameSlug = str_replace('?', '-', $productNameSlug);
        $productNameSlug = str_replace('&', '-', $productNameSlug);
        $productNameSlug = str_replace('#', '-', $productNameSlug);
        $productNameSlug = str_replace('/', '-', $productNameSlug);
        $productNameSlug = str_replace('\\', '-', $productNameSlug);
        $productNameSlug = str_replace('.', '-', $productNameSlug);
        $productNameSlug = str_replace(',', '-', $productNameSlug);
        $productNameSlug = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE',  $productNameSlug);



        $insertData[$count]['cl_cat_id'] = 1587;
        $insertData[$count]['cl_sub_cat_id'] = 344;
        $insertData[$count]['cl_brand_id'] = 1291;
        $insertData[$count]['cl_name'] = $data->productName;
        $insertData[$count]['cl_slug'] = $productNameSlug;
        $insertData[$count]['cl_regular_price'] = $data->price;
        $insertData[$count]['cl_sale_price'] = $data->price;
        $insertData[$count]['cl_supplier'] = 'action';
        $insertData[$count]['cl_product_id'] = $data->productId;
        if($data->quantity > 0)
        {
            $insertData[$count]['cl_stock'] = 1;
        }
        else {
            $insertData[$count]['cl_stock'] = 0;
        }

        $insertData[$count]['cl_ean'] = $data->ean;
        $insertData[$count]['cl_x'] = $data->sizeX;
        $insertData[$count]['cl_y'] = $data->sizeY;
        $insertData[$count]['cl_z'] = $data->sizeZ;
        $insertData[$count]['cl_weight'] = $data->weight;
        $insertData[$count]['cl_last_price_updated'] = date('Y-m-d H:i:s');
        $insertData[$count]['cl_manufacturer'] = $data->producerId;
        $insertData[$count]['cl_created_at'] = date('Y-m-d H:i:s');
        $insertData[$count]['cl_updated_at'] = date('Y-m-d H:i:s');
    
    
        $count++;
        
    }
    
}


$obj->updateBulk($insertData);

curl_close($ch);