<?php

session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once "../config.php";
/*include_once "../ichat/admin/route.php";
if ($session->is_logged_in() != true ) {
        redirect_to(WEB_LINK.'/login');
} else {
    $current_user = User::get_specific_id($session->admin_id);
    $current_user->set_online();
    $group = $current_user->prvlg_group;
} */


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

$start = $_GET['start'];

$updatedCount = 0;
if(isset($_GET['updated']))
{
    $updatedCount = $_GET['updated'];
}


require_once("../ichat/admin/assets/includes/Library/SessManager.php");
require_once("../ichat/admin/assets/includes/Library/User.php");
$session = new  SessManager();

//require_once "check_admin.php";
require_once 'database.php';
class Upload extends database
{
   

    public function getCurrentProduct($productID)
    {
        return $this->fetchRow('tb_product', ['cl_product_id' => $productID, 'cl_supplier' => 'abpl']);
    }

    public function updateProduct($brandName, $productID)
    {
        $sql = ' SELECT * FROM tb_brands WHERE cl_deleted_at IS NULL AND (cl_name=? OR cl_name=? OR cl_name=? OR cl_name=? ) ';
        $whereBrand[] = $brandName;
        $whereBrand[] = ucfirst($brandName);
        $whereBrand[] = strtoupper($brandName);
        $whereBrand[] = strtolower($brandName);

        $brand = $this->callQuery($sql, $whereBrand);
        if(empty($brand))
        {
            $insert['cl_name'] = $brandName;
            $insert['cl_description'] = $brandName;

            echo $brandName . ' Added <br/>';
            $res = $this->insert('tb_brands', $insert);
            if(!$res)
            {
                return 0;
            }

            $brandID = $res;
        }
        else
        {
            $brandID = $brand['cl_id'];
        }

        $data['cl_brand_id'] = $brandID;

        echo $productID . ' Updated <br/>';

       
        return $this->update('tb_product', $data, ['cl_product_id' => $productID, 'cl_supplier' => 'abpl']);
    }
}

$obj = new Upload();


$productArray = array();
$dev = new Upload();	
$msg="";
if(isset($_GET['message']))
{
	$msg=$_GET['message'];
}
	/*if(isset($_FILES['csv']['tmp_name']) && is_uploaded_file($_FILES['csv']['tmp_name']))
    {
        
        $target_dir = "uploads/cenas-category/";
        $randName = date('dmYhis') . rand(10000,99999) . basename($_FILES['csv']["name"]);
        $target_file = $target_dir . $randName;
        $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
        // Check if image file is a actual image or fake image

      

        // Check if file already exists
        if (file_exists($target_file)) {
            $msg.=  "Sorry, file already exists.";
            $uploadOk = 0;
        }
        // Check file size
        if ($_FILES['csv']["size"] > 500000000) {
            $msg.=  "Sorry, your file is too large.";
            $uploadOk = 0;
        }
        // Allow certain file formats
        if($imageFileType != "csv" ) {
            $msg.=  "Sorry, only CSV files are allowed.";
            $uploadOk = 0;
        }
        // Check if $uploadOk is set to 0 by an error
        if(trim($msg) == '')
        { */
           $csvAsArray = array_map('str_getcsv', file('582415_cennik_AB.csv'));
           $count = 1;

               foreach($csvAsArray AS $each)
               {
                
                if($count==1)
                {

                }
                else if($count == 2)
                {
                    
                }
                else if($count >= $start)
                {
                   
                        
                        $columntCount = 1;
                        $columns = explode(";",$each[0]);
                        foreach($columns AS $column)
                        {
                            if($columntCount == 1)
                            {
                                $brandName = str_replace('"', '', $column);
                            }
                            else if($columntCount == 2)
                            {
                                $dataBaseproductID = str_replace('"', '', $column);
                            }
                            else if($columntCount == 13)
                            {
                                $productID = str_replace('"', '', $column);
                            }
                            
                            $columntCount++;
                        }


                        if(trim($dataBaseproductID) != '')
                        {
                            
                            $product = $obj->getCurrentProduct($dataBaseproductID);
                            if($product)
                            {      
                                $obj->updateProduct($brandName, $dataBaseproductID);
                            }
                                
                            
                            
                        }
                       
                }

                $count++;

               /* if($count == 100)
                {
                    exit;
                } */
               }

              
          
       /* }
    }*/
  /* if($count < $start)
   {
    echo 'finished';
    echo '<br/>';
    echo $count;
    exit;
   }*/
?>
