<?php
session_start();
include('../includes/dbcon.php');

$edate=date("Y-m-d");
$userid=$_SESSION['rm_id'];

require 'vendor/autoload.php'; // Load the PhpSpreadsheet library

use PhpOffice\PhpSpreadsheet\IOFactory;

// Replace 'your_excel_file.xlsx' with the path to your Excel file
$excelFile = 'itemrm.xlsx';

try {
    $spreadsheet = IOFactory::load($excelFile);
    $worksheet = $spreadsheet->getActiveSheet();
    
    // Get the highest row and column numbers to iterate through
    $highestRow = $worksheet->getHighestRow();
    $highestColumn = $worksheet->getHighestColumn();
    
    // Initialize an array to store the imported data
    $importedData = [];?>
 <table id="order-listing" class="table">
        <thead>
            <tr class="bg-primary text-white">        
                <th width="5%">Emp No</th>               
                <th width="10%">Emp Name</th>
                <th width="10%">mb</th>
                <th width="5%">ua</th>                 
            </tr>
        </thead>
 <tbody>

 <?php 

    // Loop through the rows and columns to fetch the data
    for ($row = 2; $row <= $highestRow; $row++) {
         $empno=0;
               $gc='';
                $itd=0;
                $up=0;
                //$tot=0;
        $rowData = [];
        ?>   
            <tr> 

        <?php

        for ($col = 'A'; $col <= $highestColumn; $col++) 
            { 
               
                ?>
                   
        <?php    $cellValue = $worksheet->getCell($col . $row)->getValue();
        
                if($col == 'A'){ $barcode= $cellValue;  }
                if($col == 'B'){ $itemname= $cellValue;  }

            ///if($col == 'A'){ $itemid= $cellValue;  }
            //if($col == 'B'){ $globalcode= $cellValue;  }
            //if($col == 'C'){ $itemdescription= $cellValue;  }
            //if($col == 'D'){ $brand= $cellValue;  }
            //if($col == 'E'){ $Country= $cellValue;  }
            //if($col == 'F'){ $cate= $cellValue;  }
            //if($col == 'G'){ $qoh= $cellValue;  }
            //if($col == 'H'){ $sp= $cellValue;  }
 
         ?>

        
        <td><?php echo $cellValue;?></td> 
        <?php
            $rowData[] = $cellValue;
        }



         ?>  <!------End of column------------->
</tr>

<?php

$rr="INSERT INTO item_master(item_name,description,sales_price) 
                                             VALUES('$gc','$itd',$up) ";
                                             echo $rr;

if ( mysqli_query($con,"INSERT INTO item_master(item_name,description,sales_price) 
                                             VALUES('$itemname','$barcode',1200)  ")or die(mysqli_error($con)))
            {       
               
                 //$last_id = mysqli_insert_id($con);
                      
            }
        
///////////////////item Stock ////////////////
//if ( mysqli_query($con,"INSERT INTO item_stocks(item_id,loc_id,loc_qoh) 
                       //  VALUES(last_id,1,$qoh)  ")or die(mysqli_error($con)))
            //{       
               // echo "<script>document.location='pay_frequncy.php'</script>";        
           // }



//////////////////////////////////////////////////




        $importedData[] = $rowData;
    }

    // Display the imported data
    foreach ($importedData as $row) {
       // echo implode("\t", $row);
       // echo "aaxxxxx";
      //  echo implode("\t", $row) . "\n";

/////////~~~~~~~~~~~table entry part ------------

////if ( mysqli_query($con,"INSERT INTO imp(impno,emp,empname,mb,ua) 
                                             //VALUES('$m_month','$m_year','$mdes',$fa,2,$userid,'$edate')  ")or die(mysqli_error($con)))
           // {       
               // echo "<script>document.location='pay_frequncy.php'</script>";        
           // }


///////////////////////////////////////////////


    }
} catch (Exception $e) {
    die('Error loading Excel file: ' . $e->getMessage());
}
