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

$edate=date("Y-m-d");
$userid=$_SESSION['hrm_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 = 'cus.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%">Cus Code</th>               
                <th width="10%">cat</th>
                <th width="10%">name</th>              
                
            </tr>
        </thead>
 <tbody>


 <?php 




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

        <?php

        for ($col = 'A'; $col <= $highestColumn; $col++) 
            { 
               // $empno=0;
               // $empname='';
                //$mb=0;
               // $ua=0;
               // $tot=0;
                ?>
                   
        <?php    $cellValue = $worksheet->getCell($col . $row)->getValue();

            if($col == 'A'){ $cuscode= $cellValue;  }
            if($col == 'B'){ $cat= $cellValue;  }
            if($col == 'C'){ $cusname= $cellValue;  }
          //  if($col == 'D'){ $ref= $cellValue;  }
    
 
         ?>

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



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

<?php

$rr="INSERT INTO customer_master(cus_code,cus_name,cus_cat) 
                                    VALUES('$cuscode','$cat',$cusname) ";
                                             echo $rr;

if ( mysqli_query($con,"INSERT INTO customer_master(cus_code,cus_name,cus_cat) 
                                             VALUES('$cuscode','$cusname','$cat')  ")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());
}
