<?php

ini_set('display_errors', 1);
error_reporting(E_ERROR);
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include ('includes/function.php');

$zdb = $_SESSION["zdbyear_" . $dbname];

mysqli_select_db($conn, $zdb);
$admin = $_SESSION["admin_" . $dbname];

$ctr = 1;
$handle = fopen($_FILES['file']['tmp_name'], "r");

$utype = $_REQUEST['utype'];

$maindb = 'onlmain';
$trandb = '';
$checkMax = mysqli_query($conn, "SELECT MAX(`vouchno`) AS totcursor FROM `$maindb`  LIMIT 0,1") or die(mysqli_error($conn));
$mcurs = mysqli_fetch_object($checkMax) or die(mysqli_error($conn));
if ($mcurs->totcursor > 1) {
    $increcode = ($mcurs->totcursor + 1);
} else {
    $increcode = "1";
}
$ctr = 1;
$mctr = 0;
$pctr = 0;
//print_r(fgetcsv($handle));
//echo fgetc($handle);

while (($data = fgetcsv($handle, 10000, ",")) !== FALSE) {

//    print_r($data);
    $numRows = count($data);
//           $numRows = length($data);
//    echo $numRows . "<br>";

    if ($ctr == 1) {
        Echo "<form method='post'>";
        Echo "<table style='background: white;width: 50%' class='table table-bordered table-condensed'>";
        Echo "<thead class='mySearchTr'>";
        Echo "<tr>";
        Echo "<th style='background-color:#393284;color:white'>X</th>";
        Echo "<th style='background-color:#393284;color:white'>Sr.</th>";

        for ($i = 0; $i < $numRows; $i++) {

            $fld = "tnfld_" . $i . "_" . $ctr;

            $sz = 'fld' . $i;
            $$sz = $data[$i];
            $mn = $$sz;
            Echo "<th style='background-color:#393284;color:white'>$mn</th>";
        }
        Echo "</tr>";
        Echo "</thead>";
    } else {
        $rd = "";
        $tb = "";

        if ($data[1] != "") {
            $mctr++;

            Echo "<tr>";
            echo "<td><i class='fa fa-close remove'>Remove</i></td>";
            echo "<td>$mctr</td>";

//            Echo "<td><input type='text' name='srno_$ctr' class='srno form-control  form'  value='$mctr' readonly></td>";
            Echo "<input type='hidden' name='rwcnt_$ctr' class='rwcnt form-control  form'  value='$numRows'>";
            for ($i = 0; $i < $numRows; $i++) {

                // $fld = "tnfld_" . $i . "_" . $ctr;
                // $sz = 'fld' . $i;
                // $$sz = $data[$i];
                // $mn = $$sz;
                // Echo "<td><input type='text' name='$fld' class='newctn form-control  form'  value='$mn' readonly></td>";

                $mn = $data[$i];
                if ($i == 2 && is_numeric($mn) && stripos($mn, 'E') !== false) {
                    $mn = sprintf('%.0f', $mn); // Convert scientific notation to full number
                }
                $mn = htmlspecialchars($mn);
                echo "<td><input type='text' name='tnfld_{$i}_{$ctr}' class='newctn form-control form' value='$mn' readonly></td>";
            }
            Echo "</tr>";
        }
    }

    $fld = 0;
    $increcode++;
    $ctr++;
}
$count = $mctr - 1;

Echo "</table>";
Echo "<input type='hidden' class='form-control count' name='items' value='$ctr'>";
//Echo "<input type = 'submit' class = 'btn btn-primary' name = 'save' value = 'SAVE'>";
Echo "</form>";
?>