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

$paxdb = "visaform";
$bookdb = "bookform";
$formname = "Agewise 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' ";
}

$date1 = date('Y-m-d');
mysqli_query($conn, "drop temporary table if exists agedata") or die(mysqli_error($conn));
mysqli_query($conn, "create temporary table agedata
                            SELECT DATEDIFF('$date1', dob)/365 AS age, year(dob) as ageyear FROM `visaform`  ") or die(mysqli_error($conn));

$mainqry = mysqli_query($conn, "SELECT ageyear, round(age) as age, count(*) as nopax FROM agedata group by round(age) order by round(age) ") or die(mysqli_error($conn));
//$mainqry = mysqli_query($conn, "SELECT * FROM agedata ") or die(mysqli_error($conn));
//print table($mainqry) ;
//die();

$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:60%">
            <?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:10%" class="text-right">Sr.</th>
                                <th style="width:30%" class="text-right">Born Year</th>
                                <th style="width:30%" class="text-right">Age</th>
                                <th style="width:30%" class="text-right">No. Of Pax</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            while ($fage = mysqli_fetch_array($mainqry)) {
                                $ctr++;
                                echo "<tr id='boldfont'>";
                                echo "<td class='text-right'>$ctr</td>";
                                echo "<td class='text-right'>$fage[ageyear]</td>";
                                echo "<td class='text-right'>$fage[age]</td>";
                                echo "<td class='text-right'>$fage[nopax]</td>";
                                echo "</tr>";
                            }
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

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

    </html>
<?php
}


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

$contents .="Sr, Born Year, Age, No Of Pax\n";

$ctr = 0;
while ($fage = mysqli_fetch_array($mainqry)) {
    $ctr++;

    $contents .="$ctr" . ",";
    $contents .="$fage[ageyear]" . ",";
    $contents .="$fage[age]" . ",";
    $contents .="$fage[nopax]" . "\n";

}

    $contents = strip_tags($contents);

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