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

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);
$zyear = right($zdb, 4);

$zroot = $_SESSION["zdbroot"];
$formname = 'School List of Demand Form - Centrewise';
$maindb = 'accmast';
$trandb = 'demdmain';

if (isset($_POST["view"])) {

    $school = $_POST["school"];
    $center = $_POST["center"];
    $medium = $_POST["medium"];
    $section = $_POST["section"];
    $session = $_POST["session"];
    $country = $_POST["country"];
    $city = $_POST["city"];
    $exam = $_POST["exam"];
    $dtype = $_POST["dtype"];
    $orderby = $_POST["orderby"];

    $ordcond = '';
    $centcond = '';
    $centcond1 = '';

    $examcond1 = "";
    $examcond = "";

    $medmcond1 = "";
    $medmcond = "";

    $schlcond1 = "";
    $schlcond = "";

    $sessncond1 = "";
    $sessncond = "";

    $sectcond1 = "";
    $sectcond = "";

    $countrycond = "";
    $citycond = "";

    if ($orderby == '1') {
        $ordcond = 'order by centcode, name';
    } elseif ($orderby == '2') {
        $ordcond = 'order by centcode, custcode';
    }

    if (!empty($exam)) {
        $examcond1 = " AND b.`examcode` = '$exam' ";
        $examcond = " AND `examcode` = '$exam' ";
        $gexam = mysqli_query($conn, "SELECT `code`,`name` FROM `exam` WHERE `code` = '$exam'") or die(mysqli_error($conn));
        $fexam = mysqli_fetch_array($gexam);
        $exname = $fexam['name'];
    }

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

    if (!empty($school)) {
        $schlcond1 = " AND b.`accode` = '$school' ";
        $schlcond = " AND `accode` = '$school' ";
    }
    if (!empty($medium)) {
        $medmcond = " AND `medmcode` = '$medium' ";
    }

    if (!empty($session)) {
        $sessncond1 = " AND b.`session` = '$session' ";
        $sessncond = " AND `session` = '$session' ";
    }

    if (!empty($section)) {
        $sectcond1 = " AND b.`section` = '$section' ";
        $sectcond = " AND `section` = '$section' ";

        if ($section == 'P') {
            $msec = 'Primary';
        } elseif ($section == 'S') {
            $msec = 'Secondary';
        } else {
            $msec = 'Preliums';
        }
    }

    if (!empty($city)) {
        $citycond = " AND `city` like '%$city%' ";
    }

    $comp = mysqli_query($conn, "SELECT * FROM $zroot.`company`") or die(mysqli_error($conn));
    $zcomp = mysqli_fetch_assoc($comp);

    include("includes/linkshead.php");
    ?>
    <link href="MyStyle/rep_format.css" rel="stylesheet">
    <style>

        .mtable{
            border:1px solid black;
            boder-collapse:collapse;
        }

        .mtd{
            border:1px solid black;
            boder-collapse:collapse;
            text-align:right;
        }

        .padd{
            padding:3px;
        }

        .padd2{
            padding:5px;
        }

        .border{
            border-top-style:solid;
            border-right-style:none;
            border-left-style:none;
            border-bottom-style:solid;
            border-width:2px;
        }
        .page {
            width: 210mm;
            height: auto;
            padding: 1mm;
            margin: 10mm auto;
            border: 1px #D3D3D3 solid;
            border-radius: 5px;
            background: white;
            /*overflow: auto;*/

        }
        .fontsize{
            font-size: 15px;
        }

        .fontsize2{
            font-size: 13px;
        }

        table{
            width:100%;
        }
        hr { 
            border-style: inset;
            border-width: 2px;
        } 

    </style>
    </head>
    <body>    
        <div class="container">

            <div class="col-lg-10"></div>
            <div class="col-lg-1">
                <a  class="btn btn-primary goback"  onclick="window.close()">Go Back</a>
            </div>
            <div class = "page" >
                <!--<div class="subpage" style="margin:5px;width:100%;">-->
                <div class="row">
                    <?php
                    echo "<h3 class='text-center' style='margin:0px'>$zcomp[compname]</h3>";
                    echo "<h5 class='text-center'>$zcomp[add1] $zcomp[add1] </h5>";
                    echo "<h5 class='text-center'>$zcomp[add3]</h5>";
                    ?>
                </div>
                <table style="width:100%">
                    <tr>
                        <td style="width:100%"><h3><b><?php echo $formname; ?> </b></h3></td>
                    </tr>
                    <?php
                    if ($_POST["dtype"] == 2) {
                        $reptype = 'Detail';
                    } else {
                        $reptype = 'Summary';
                    }
                    ?>
                </table>               
                <?php
                $gmain = mysqli_query($conn, "SELECT * from `accmast` where accode != ''  $schlcond $centcond $citycond  and 
                                  accode in (select accode from `demdmain` where vouchno != '' and year = '$myyear'
                                  $sessncond $medmcond $examcond $sectcond ) $ordcond") or die(mysqli_error($conn));

                $getno = mysqli_num_rows($gmain);

                if ($getno == 0) {
                    browse("No Record Found");
                    echo "<script>window.close()</script>";
                }
                ?>            
                <table>
                    <?php
                    $tcentcode = '';
                    $texamcode = '';
                    $tmedmcode = '';
                    $ctr = 0;
                    $a = 0;
                    while ($pmain = mysqli_fetch_array($gmain)) {
                        $centcode = $pmain['centcode'];
//                        $examcode = $exam;
                        $medmcode = $pmain["medmcode"] ;
                        
                        $gsemi = mysqli_query($conn, "SELECT semi FROM `$trandb` where custcode = '$pmain[custcode]'  $sessncond $examcond $sectcond") or die(mysqli_error($conn));
                        $fsemi = mysqli_fetch_object($gsemi);
                        $semi = $fsemi->semi;

//                        if ($centcode != $tcentcode || $examcode != $texamcode || $medmcode != $tmedmcode) {
                        
                        if ($centcode != $tcentcode ) {
                            $gcenter = mysqli_query($conn, "SELECT name FROM `center` where centcode = '$centcode' ") or die(mysqli_error($conn));
                            $fcent = mysqli_fetch_object($gcenter);
                            $centname = $fcent->name;
                            $tcentcode = $centcode;
//                            $texamcode = $examcode;
//                            $gmedm = mysqli_query($conn, "SELECT name FROM `medium` where code = '$medmcode' ") or die(mysqli_error($conn));
//                            $fmedm = mysqli_fetch_object($gmedm);
//                            $medmname = $fmedm->name;
//                            $tmedmcode = $medmcode;
                            



                            if ($a = 1) {
                                echo "<tr style='page-break-after:always;'>";
                                echo "</tr>";
                            }
                            ?>

                            <thead>
                                <!--<tr>-->
                                    <?php // if ($exam != '') { ?>
                                        <!--<td colspan="3" ><h4><b>Exam</b>&nbsp;&nbsp;:&nbsp;&nbsp;<?php echo $exname; ?></h4></td>-->
                                    <?php // } if ($section != '') { ?>
                                        <!--<td ><h4><b>Section</b>&nbsp;&nbsp;:&nbsp;&nbsp;<?php echo $msec; ?></h4></td>-->
                                    <?php // } if ($medmcode != '') { ?>
                                        <!--<td colspan="2" ><h4><b>Medium</b>&nbsp;&nbsp;:&nbsp;&nbsp;<?php echo $medmname; ?></h4></td>-->
                                    <? // } ?>
                                <!--</tr>-->
                            <?php // if ($center != '') { ?>
<!--                                <tr>
                                    <td colspan="5"><h4><b>Center</b>&nbsp;&nbsp;:&nbsp;&nbsp;<?php echo $centname; ?></h4></td>
                                </tr>-->
                            <?php // } ?>
<!--                                <tr>
                                    <td colspan="5"><h4><b><?= $reptype ?></b></h4></td>
                                </tr>-->

                                <tr class="border">
                                    <td style="width:5%; text-align:left" class="padd fontsize"><b>Sr.</b></td>
                                    <td style="width:50%; text-align:left " class="padd fontsize"><b>Name</b></td>
                                    <td style="width:10%" class="padd fontsize"><b>Ref.Code</b></td>
                                    <td style="width:24%" class="padd fontsize"><b>Tel.No.</b></td>
                                    <td style="width:10%" class="padd fontsize"><b>Semi</b></td>
                                </tr>
                            </thead>

                            <?php
                            $ctr = 0;
                            $a = 1;
                        }
                        $ctr = $ctr + 1;
                        echo "<tr>";
                        echo "<td style='text-align:left' class='padd fontsize'>$ctr</td>";
                        echo "<td class='padd fontsize'>$pmain[name]</td>";
                        echo "<td class='padd fontsize'>$pmain[custcode]</td>";
                        if ($pmain['mobile'] != '') {
                            echo "<td class='padd fontsize'>$pmain[tel]/$pmain[mobile]</td>";
                        } else {
                            echo "<td class='padd fontsize'>$pmain[tel]</td>";
                        }
                        echo "<td class='padd fontsize'>$semi</td>";
                        echo "</tr>";

                        if ($_POST["dtype"] == 2) {
                            echo "<tr>";
                            echo "<td style='text-align:right' class=''></td>";
                            echo "<td class='padd fontsize2'>$pmain[add1]<br></td>";
                            echo "<td style='width:20%' class='padd fontsize'></td>";
                            echo "<td style='width:20%' class='padd fontsize'></td>";
                            echo "</tr>";

                            echo "<tr>";
                            echo "<td style='text-align:right' class=''></td>";
                            echo "<td class='padd fontsize2'>$pmain[add2]<br></td>";
                            echo "<td style='width:20%' class='padd fontsize'></td>";
                            echo "<td style='width:20%' class='padd fontsize'></td>";
                            echo "</tr>";
                            if ($pmain['add3'] != '') {
                                echo "<tr>";
                                echo "<td style='text-align:right' class=''></td>";
                                echo "<td class='padd fontsize2'>$pmain[add3]<br></td>";
                                echo "<td style='width:20%' class='padd fontsize'></td>";
                                echo "<td style='width:20%' class='padd fontsize'></td>";
                                echo "</tr>";
                            }
                            echo "<tr>";
                            echo "<td style='text-align:right' class=''></td>";
                            echo "<td class='padd fontsize2'>$pmain[city] $pmain[pin]<br></td>";
                            echo "<td style='width:20%' class='padd fontsize'></td>";
                            echo "<td style='width:20%' class='padd fontsize'></td>";
                            echo "</tr>";
                        }
                    }
                    ?>

                </table>    
            </div>
        </div>
        <!--</div>-->
        <?php include('includes/linksfoot.php'); ?>
    </body>
    </html>
<?php }
?>