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

$paxdb = "visaform";
$bookdb = "bookform";
$formname = "Discount Register";

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

$namecond = $regnocond = $paxnocond = $groupcond = $crnocond = $classcond = $tournocond = $citycond = $statecond = $infcond = $cancond =
    $addcond = $ppexpcond = $ppinhand = $dpdtcond = $dppnrcond = $aircond = $prregnocond = $prphcond = $agentcond = $arrvdatecond =
    $arrvpnrcond = $buscond = $roomcond = "";

$name     = $_POST['name'];
$regno    = $_POST['vregnno'];
$paxno    = $_POST['paxno'];
$group    = $_POST['group'];
$crno     = $_POST['crno'];
$class    = $_POST['class'];
$tourno   = $_POST['tourno'];
$city     = $_POST['city'];
$state    = $_POST['state'];
$inf      = $_POST['inf'];
$canc     = $_POST['canc'];
$address  = $_POST['address'];
$ppexpiry = $_POST['ppexpiry'];
$ppinhand = $_POST['ppinhand'];
$depdate  = $_POST['depdate'];
$deppnr   = $_POST['deppnr'];
$airlines = $_POST['airlines'];
$prregno  = $_POST['prregno'];
$prphoto  = $_POST['prphoto'];
$agent    = $_POST['agent'];
$arrvdate = $_POST['arrvdate'];
$arrvpnr  = $_POST['arrvpnr'];
$busno    = $_POST['busno'];
$roomno   = $_POST['roomno'];

if (!empty($name)) {
    $namecond = "AND `haji` = '$name' ";
}

$mainqry = mysqli_query($conn, "SELECT * FROM `bookform` WHERE `vouchno` != '' ")or die(mysqli_error($conn));
$m1 = mysqli_num_rows($mainqry);
$m2 = mysqli_num_rows($mainqry2);
$m = $m1 + $m2;
if ($m <= 0) {
    // $a = 0;
}
if ($a == 0) {
    // echo "<script>"
    //     . "window.close()"
    //     . "</script>";
}
if (isset($_POST["view"])) {


    include("includes/linkshead.php");
?>
    <style type="text/css">
        #tabital {
            font-style: italic;
        }

        #font1 {
            font-style: italic;
        }

        #boldfont {
            font-weight: bold;
        }
    </style>
    </head>

    <body>
        <div class="container" style="width:100%">
            <?php include("includes/head_rep.php"); ?>
            <div class="row">
                <div class="col-lg-12 pad">
                    <table style="background: white;width: 100%" class="table table-bordered table-condensed table-hover">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:5%">Sr.</th>
                                <th style="width:10%" class="text-right">Regn.</th>
                                <th style="width:40%">Name</th>
                                <th style="width:10%" class="text-right">No. of Pax.</th>
                                <th style="width:10%" class="text-right">Discount</th>
                                <th style="width:10%" class="text-right">Tour Amount</th>
                                <th style="width:15%">Agent</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $discamt = 0;
                            $totamt = 0;

                            while ($fmain = mysqli_fetch_array($mainqry)) {

                                $gpaxno = mysqli_query($conn, "SELECT * FROM $paxdb WHERE `vregnno`='$fmain[vouchno]' ") or die(mysqli_error($conn));
                                $paxnum = mysqli_num_rows($gpaxno);

                                $ctr++;
                                echo "<tr id='boldfont'>";
                                echo "<td>$ctr</td>";
                                echo "<td class='text-right'>$fmain[vouchno]</td>";
                                echo "<td>$fmain[haji]</td>";
                                echo "<td class='text-right'>$paxnum</td>";
                                echo "<td class='text-right'>$fmain[discamt]</td>";
                                echo "<td class='text-right'>$fmain[netamt]</td>";
                                echo "<td>$fmain[agent]</td>";
                                echo "</tr>";
                                $discamt += $fmain['discamt'];
                                $totamt += $fmain['netamt'];
                            }
                            $colr = "red";
                            echo "<tr style='color:$colr'>";
                            echo "<td class='text-right' colspan='4'><h4><b>Total</b></h4></td>";
                            echo "<td class='text-right'><h4><b>$discamt</b></h4></td>";
                            echo "<td class='text-right'><h4><b>$totamt</b></h4></td>";
                            echo "<td class='text-right'><h4><b></b></h4></td>";
                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <?php include("includes/linksfoot.php");
        ?>
    </body>

    </html>
<?php
}


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


    $contents .= "Sr., Regn., Name, No. of Pax, Discount, Tour Amount, Agent \n";

    $ctr = 0;
                            $discamt = 0;
                            $totamt = 0;

                            while ($fmain = mysqli_fetch_array($mainqry)) {

                                $gpaxno = mysqli_query($conn, "SELECT * FROM $paxdb WHERE `vregnno`='$fmain[vouchno]' ") or die(mysqli_error($conn));
                                $paxnum = mysqli_num_rows($gpaxno);

                                $ctr++;
                                $contents .= "$ctr" . ",";
                                $contents .= "$fmain[vouchno]" . ",";
                                $contents .= "$fmain[haji]" . ",";
                                $contents .= "$paxnum" . ",";
                                $contents .= "$fmain[discamt]" . ",";
                                $contents .= "$fmain[netamt]" . ",";
                                $contents .= "$fmain[agent]" . "\n";

                                $discamt += $fmain['discamt'];
                                $totamt += $fmain['netamt'];
                            }
                            $colr = "red";

                            $contents .= "" . ",";
                            $contents .= "" . ",";
                            $contents .= "" . ",";
                            $contents .= "Total" . ",";
                            $contents .= "$discamt" . ",";
                            $contents .= "$totamt" . ",";
                            $contents .= "" . "\n";

                            $contents = strip_tags($contents);

        header("Content-Disposition: attachment; filename=$formname" . date('d-m-Y') . ".csv");
        print $contents;
  
}
?>