<?php

session_start();
error_reporting(1);
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 updateCurrentWeight($productID, $weight)
    {
		return $this->update('tb_product', ['cl_weight' => $weight], ['cl_product_id' => $productID]);
	}
}

$obj = new Upload();


$productArray = array();
$obj = 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('ab_product.csv'));
           $count = 1;
           ?>
           <table style="width:100%" border="1">
               <?php
               foreach($csvAsArray AS $each)
               {

                if($count==1)
                {
                    ?>
                    <tr><td colspan="16"><?php echo $each[0] ?></td></tr>
                    <?php
                }
                else if($count == 2)
                {
                    ?>
                    <tr>
                        <td><?php echo $count ?></td>
                        <?php
                        $columntCount = 1;
                        $columns = explode(";",$each[0]);
                        foreach($columns AS $column)
                        {
                            
                            ?>
                            <td>
                                <?php echo $column; ?>
                            </td>
                            <?php
                            $columntCount++;
                        }
                     ?>
                    
                    </tr>
                    <?php
                }
                else if($count >= $start && $count < ($start + 200))
                {
                    ?>
                    <tr>
                        <td><?php echo $count ?></td>
                        <?php
                        $columns = explode(";",$each[0]);
                        $columntCount = 1;
                        $productID = '';
                        $weight = '';
                        foreach($columns AS $column)
                        {
                            if($columntCount == 13)
                            {
                                $productID = str_replace('"', '', $column);
                            }
                            else if($columntCount == 2)
                            {
                                $dataBaseproductID = str_replace('"', '', $column);
                            }
                            else if($columntCount == 15)
                            {
                                $weight = str_replace('"', '', $column);
                            }
                            ?>
                            <td>
                                <?php echo $column; ?>
                            </td>
                            <?php
                            $columntCount++;
                        }

                        if(trim($weight) != '' && trim($dataBaseproductID) != '')
                        {
                            $obj->updateCurrentWeight($dataBaseproductID, $weight);
                        }

                      
                     ?>
                    </tr>
                    <?php
                }

                 
                ?>
                <?php
                $count++;
               }

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


<script>
    window.location.href = 'test_csv_weight_loader?start=<?php echo ($start + 200) ?>&updated=<?php echo $updatedCount ?>';
</script>