<?php

require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include("includes/function.php");

$zdb = $_SESSION["zdbyear_$dbname"];
$ztit = $_SESSION["tit"];
$usertype = $_SESSION["usertype"];
$user = $_SESSION['username_' . $dbname];
$keyfld = "vouchno";

mysqli_select_db($conn, $zdb);

if (isset($_REQUEST["search"])) {
    $searchTerm = $_REQUEST["search"];
    $section = $_REQUEST["section"];
    $session = $_REQUEST["session"];



    $cond = "AND a.`custcode` like '%$searchTerm%'";
    $sectcond = "AND a.`section` = '$section'";
    $sesncond = "AND a.`session` = '$session'";


    $mctr = 0;
    $srchresult = mysqli_query($conn, "SELECT a.custcode,a.section, a.session, b.name,b.accode FROM `demdmain` a, `accmast` b WHERE a.`custcode` = b.`custcode` 
                               $cond $sectcond $sesncond group by a.custcode order by a.custcode")or die(mysqli_error($conn));



    if (mysqli_num_rows($srchresult) > 0) {
        echo "<table class='table table-bordered table-hover myTable'>";
        echo "<tr style='background-color: #6082BF;color: white;'>";
        echo "<td style='width: 8%;'><input type='checkbox' class='allchk' value='' style='width:20px;height:20px;' > &nbsp; Select All</td>";
        echo "<td style='width: 13%;'><b>Code</b></td>";
        echo "<td style='width: 30%;'><b>Name</b></td>";
        echo "<td style='width: 10%;'><b>Section</b></td>";
        echo "<td style='width: 10%;'><b>Session</b></td>";
        echo "</tr>";
        $ctr = 0;
        while ($row2 = mysqli_fetch_array($srchresult)) {
            $modcode = $row2['custcode'];
            $modname = $row2['name'];

            if ($row2['section'] == 'P') {
                $section = 'Primary';
            } elseif ($row2['section'] == 'S') {
                $section = 'Secondry';
            } else {
                $section = 'Prelim';
            }
            if($row2['session'] == 'M'){
                $session = 'Morning';
            }elseif($row2['session'] == 'A'){
                $session = 'Afternoon';
            }



            echo "<tr>";
            echo "<td>";
            echo "<input type='checkbox' style='width:20px;height:20px;' class='mcheck' name='schlchk_$ctr' value='1' id='checkBoxList'>";
            echo "</td>";
            echo "<td><b>$modcode</b>
                                    <input type='hidden' style='border: none' name='scust_$ctr' class='scust' value='$modcode' readonly>
                                     <input type='hidden' name='accode_$ctr' value='$row2[accode]' class='accode' >
                                    </td>";
            echo "<td><b>$modname<input type='hidden' style='border: none' class='sname' name='sname_$ctr' value='$modname' readonly></b></td>";
            echo "<td><b>$section</b></td>";
            echo "<td><b>$session</b></td>";
            echo "<input type='hidden' name='saccode_$ctr' class='saccode' value='' >";
        echo "<input type='hidden' name='ncust_$ctr' class='ncust' value=''>";
            echo "</tr>";
            $ctr++;
        }
        echo "</table>";
        
    }
    echo "<input type='hidden' name='items' class='' value='$ctr'>";
}