<?php

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];
$success = false;
$maindb = 'accmast';
$society = $_SESSION["society_" . $dbname];
$file = "multenvp.php";

//$keyfld = "vouchno";
$city = $_REQUEST["city"];
$exam = $_REQUEST["exam"];
$medium = $_REQUEST["medium"];
$center = $_REQUEST["center"];
$orderby = $_REQUEST["orderby"];



$citycond = '';
$examcond = '';
$centcond = '';
$medmcond = '';
$ordercond = '';

if (!empty($city)) {
    $citycond = "AND a.city='$city'";
    $citycond1 = "AND city='$city'";
}
if (!empty($exam)) {
    $examcond = "AND b.`examcode` = '$exam' ";
}
if (!empty($center)) {
    $centcond = "AND a.`centcode` = '$center' ";
    $centcond1 = "AND `centcode` = '$center' ";
}
if (!empty($medium)) {
    $medmcond = "AND a.medmcode = '$medium' ";
    $medmcond1 = "AND medmcode = '$medium' ";
}
if (!empty($orderby)) {
    $ordercond = "order by $orderby";
}
if (!empty($exam)) {
    $mainqry = mysqli_query($conn, "SELECT a.*, b.examcode FROM `accmast` a, demdmain b WHERE a.`accode` = b.`accode` and 
                            a.`grpcode` = '010' $citycond $centcond $medmcond  $examcond group by a.accode $ordercond ")or die(mysqli_error($conn));
} else {
    $mainqry = mysqli_query($conn, "SELECT accode, custcode, name, add1, add2, add3, mobile, city, pin,
                            tel, mobile from accmast where `grpcode` = '010' $citycond1 $centcond1
                             $medmcond1 group by accode $ordercond ")or die(mysqli_error($conn));
}

$num = mysqli_num_rows($mainqry);
if ($num > 0) {
    echo "<table class='table table-bordered table-hover myTable' width='100%'>";
    echo "<tr style='background-color: #6082BF;color: white;'>";
    echo "<td width='5%'><input type='checkbox' class='form-control acheck' id='acheck' style='width:50%'></td>";
//    echo "<td width='5%'></td>";
    echo "<td width='10%'><b>Code</b></td>";
    echo "<td width='25%'><b>Name</b></td>";
    echo "<td width='40%'><b>Address</b></td>";
    echo "<td width='10%'><b>city</b></td>";
    echo "</tr>";
    $sctr = 0;
    $ctr = 0;
    while ($row2 = mysqli_fetch_array($mainqry)) {
        $modcode = $row2['custcode'];
        $modname = $row2['name'];
        $maccode = $row2['accode'];
        $addr = $row2['add1'] . " " . $row2['add2'] . " " . $row2['add3'];
        $city = $row2['city'] . " - " . $row2['pin'];
        $ctr++;

        echo "<tr class='schltab'>";
        echo "<td><input type='checkbox' class='form-control mcheck'  name='chk_$ctr' value='1' style='width:50%'></td>";
        echo "<td><b>$modcode</b>
                                    <input type='hidden' style='border: none' class='scust' value='$modcode' name='custcode_$ctr' readonly>
                                            <input type='hidden' value='$row2[medmcode]' class='medium'>
                                            <input type='hidden' value='$maccode' class='accode'>
                                    </td>";
        echo "<td><b>$modname<input type='hidden' style='border: none' class='sname' value='$modname' name='' readonly></b></td>";
        echo "<td><b>$addr</b></td>";
        echo "<td><b>$city</b></td>";
        echo "</tr>";
        $sctr++;
    }
    echo"<input type='hidden' value='$ctr' name='count' class='form-control ' style='width:50%'>";
    echo "</table>";
} else {
    echo"<h1>No Record Found.</h1>";
}
//echo"<input type='hidden' value='$ctr' name='count' class='form-control ' style='width:50%'>";
//echo "</tbody>";
//echo "</table>";
//header("Content-type: application\json");
//echo json_encode($rows);
