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

$maindb = "accmast";
$formname = "Patient Print";
$file = "patient_print.php";
$mgrpcode = "010";

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

$mob = $_POST["mob"];
$regndt = $_POST["regndt"];
$gender = $_POST["gender"];
$name = $_POST["name"];

$mobcond = "";
$regncond = "";
$gencond = "";
$namecond = "";

if (!empty($mob)) {
    $mobcond = " AND `mobile` = '$mob' ";
}
if (!empty($name)) {
    $namecond = " AND `accode`='$name' ";
}
if (!empty($regndt)) {
    $regncond = " AND `regndt` = '$regndt' ";
}
if (!empty($gender)) {
    $gencond = " AND `gender` = '$gender' ";
}

$getpmain = mysqli_query($conn, "SELECT *,DATE_FORMAT(`dob`,'%d-%m-%Y') as bday ,DATE_FORMAT(`regndt`,'%d-%m-%Y') as regn FROM `$maindb` WHERE `grpcode`='$mgrpcode' $namecond $mobcond $regncond $gencond ORDER BY `name`") or die(mysqli_error($conn));

if (isset($_POST["view"])) {
    include("includes/linkshead.php");
?>
    <!--<script src="MyScript/item.js" type="text/javascript"></script>-->
    <!--    <script type="text/javascript">
        function PrintDiv() {
            var divContents = document.getElementById("printdivcontent").innerHTML;
            var printWindow = window.open('', '', 'height=200,width=400');
            printWindow.document.write('<html><head><title>Print DIV Content</title>');
            printWindow.document.write('</head><body >');
            printWindow.document.write(divContents);
            printWindow.document.write('</body></html>');
            printWindow.document.close();
            printWindow.print();
        }
    </script> -->
    </head>

    <body>
        <div class="container">
            <?php include("includes/head_rep.php");
            ?>
            <div class="row" id="printdivcontent">

                <div class="col-lg-12 pad">
                    <table class="table table-condensed table-hover table-bordered">
                        <tr class="mySearchTr">
                            <th width="5%">Sr</th>
                            <th width="15%">Name</th>
                            <th width="25%">Address</th>
                            <th width="10%">Age</th>
                            <th width="10%">D.O.B</th>
                            <th width="10%">Mobile</th>
                            <th width="10%">Email</th>
                            <th width="10%">Gender</th>
                            <th width="10%">Regn. Date</th>
                        </tr>
                        <tr>
                            <!--<button type="button" name="print" onclick="PrintDiv();">PRINT TEST</button>-->
                        </tr>
                        <?php
                        $ctr = 1;
                        while ($pmain = mysqli_fetch_array($getpmain)) {

                            if ($pmain['gender'] == 'M') {
                                $gval = "Male";
                            } else if ($pmain['gender'] == 'F') {
                                $gval = "Female";
                            } else {
                                $gval = "Other";
                            }
                            echo "<tr class='bgwhite'>";
                            echo "<td>$ctr</td>";
                            echo "<td>$pmain[name]</td>";
                            echo "<td>$pmain[add1] $pmain[add2] $pmain[add3] </td>";
                            echo "<td>$pmain[age]</td>";
                            echo "<td>$pmain[bday]</td>";
                            echo "<td>$pmain[mobile]</td>";
                            echo "<td>$pmain[email]</td>";
                            echo "<td>$gval</td>";
                            echo "<td>$pmain[regn]</td>";
                            echo "</tr>";
                            $ctr++;
                        }
                        ?>
                    </table>
                </div>
            </div>
        </div>

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

    </html>
<?
}
if (isset($_POST["excel"])) {
    
    $contents .= "Sr., Name, Address, Age, D.O.B, Mobile, Email, Gender, Regn. Date \n";
                            
                        $ctr = 1;
                        while ($pmain = mysqli_fetch_array($getpmain)) {

                            if ($pmain['gender'] == 'M') {
                                $gval = "Male";
                            } else if ($pmain['gender'] == 'F') {
                                $gval = "Female";
                            } else {
                                $gval = "Other";
                            }
                            $contents .= "$ctr" . ",";
                            $contents .= "$pmain[name]" . ",";
                            $contents .= "$pmain[add1]" . " " . "$pmain[add2]" . " " . "$pmain[add3]" . ",";
                            $contents .= "$pmain[age]" . ",";
                            $contents .= "$pmain[bday]" . ",";
                            $contents .= "$pmain[mobile]" . ",";
                            $contents .= "$pmain[email]" . ",";
                            $contents .= "$gval" . ",";
                            $contents .= "$pmain[regn]" . "\n";
                            $ctr++;
                        }

    $contents = strip_tags($contents);

    // if ($_POST["dtype"] == 1) {
        header("Content-Disposition: attachment; filename=$formname" . "_Summary" . date('d-m-Y') . ".csv");
        print $contents;
    // } else {
    //     header("Content-Disposition: attachment; filename=$formname" . "_Detail" . date('d-m-Y') . ".csv");
    //     print $contents;
    // }
}
?>