<?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["center"])) {
    $center = $_REQUEST["center"];
    $medium = $_REQUEST["medium"];
    $searchTerm = $_REQUEST["search"];

    $cond = "";
    $centcond = "";
    $medmcond = "";
    if (!empty($searchTerm)) {
        $cond = "AND `custcode` like '%$searchTerm%'";
    }

    if (!empty($center)) {
        $centcond = "AND `centcode` = '$center'";
    }

    if (!empty($medium)) {
        $medmcond = "AND `medmcode` = '$center'";
    }

    $mctr = 0;
    $srchresult = mysqli_query($conn, "SELECT * FROM `accmast` WHERE  `grpcode`='010' and email != '' $cond $centcond $medmcond")or die(mysqli_error($conn));
    if (mysqli_num_rows($srchresult) > 0) {
        echo "<table class='table table-bordered table-hover mytable'>";
        echo "<tr class='clickable-row' style='background-color: #6082BF;color: white;'>";
        echo "<td width='5%'><input type='checkbox' class='form-control'style='width:50%'></td>";
        echo "<td width='15%'><b>Code</b></td>";
        echo "<td width='60%'><b>Name</b></td>";
        echo "<td width='20%'><b>Email</b></td>";
        echo "</tr>";
        $ctr = 0;
        while ($row2 = mysqli_fetch_array($srchresult)) {
            $custcode = $row2['custcode'];
            $email = $row2['email'];
            $scname = $row2['name'];

            echo "<tr>";
            echo "<td><input type='checkbox' class='form-control mcheck' name='chk_$ctr' value='1' style='width:50%'></td>";
            echo "<td><b>$custcode</b></td>";
            echo "<td><b>$scname</td>";
            echo "<td><b>$email<input type='hidden' value='$email' name='email_$ctr'></td>";
            echo "</tr>";
            $ctr++;
        }
        echo"<input type='hidden' value='$ctr' name='count' class='form-control' style='width:50%'>";
        echo "</table>";
    } else {
        echo "<h3>No Records Found.</h3>";
    }
}