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

$maindb = "rationdonate";
$formname = 'Ramzan Ration Donation Register';
$file = "rationdonatereg_fr.php";

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

$memname = $_POST["memname"];
$gender = $_POST["gender"];
$occupation = $_POST["occupation"];

$mfrmdate = trim($_POST["fromdt"]);
$mtodate = trim($_POST["todt"]);

$memnamecond = "";
$gendercond = "";
$occupationcond = "";

if (empty($_POST["fromdt"])) {
    browse("From Date Cannot Be Empty");
    echo "<script>window.close()</script>";
}
if (empty($_POST["todt"])) {
    browse("To Date Cannot Be Empty");
    echo "<script>window.close()</script>";
}

if (!empty($memname)) {
    $memnamecond = " AND `memname`='$memname ' ";
}
if (!empty($gender)) {
    $gendercond = " AND `gender`='$gender ' ";
}
if (!empty($occupation)) {
    $occupationcond = " AND `occupation`='$occupation ' ";
}

$getpmain = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y')as vouchdt
                                    FROM $maindb WHERE vouchno != ''
                                    AND `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' $memnamecond 
                                    $gendercond $occupationcond order by vouchno,vouchdt")or die(mysqli_error($conn));
$get = mysqli_num_rows($getpmain);

include("includes/linkshead.php");
if (isset($_POST["view"])) {
    ?>
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold;
        }
    </style>

    </head>
    <body>
        <div class="container">
            <?php include("includes/head_rep.php"); ?>
            <div class="row">
                <div class="col-lg-12 pad">
                    <table style="background: white;width: 100%" class="table table-condensed table-hover">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:4%">Sr No.</th>
                                <th style="width:7%">Date</th>
                                <th style="width:12%;">Member Code</th>
                                <th style="width:12%;">Member Name</th>
                                <th style="width:12%;">Address</th>
                                <th style="width:12%;">Married Status</th>
                                <th style="width:5%">Mobile</th>
                                <th style="width:10%;">Blood Group</th>
                                <th style="width:10%;">Qualification</th>
                                <th style="width:10%;">Occupation</th>
                                <th style="width:10%;">Physically Disabled</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $mtqty = 0;
                            $mgtot = 0;

                            while ($pmain = mysqli_fetch_array($getpmain)) {

                                // $time = date('h:i A', strtotime($pmain['engtime']));

                                $address = $pmain['add1'] . $pmain['add2'] . $pmain['add3'];

                                $ctr++;
                                Echo "<tr id='boldfont'>";
                                Echo "<td>$ctr</td>";
                                Echo "<td>$pmain[vouchdt]</td>";
                                Echo "<td>$pmain[memno]</td>";
                                Echo "<td>$pmain[memname]</td>";
                                Echo "<td>$address</td>";
                                Echo "<td>$pmain[marstat]</td>";
                                Echo "<td>$pmain[mobile]</td>";
                                Echo "<td>$pmain[bloodgrp]</td>";
                                Echo "<td>$pmain[qualification]</td>";
                                Echo "<td>$pmain[occupation]</td>";
                                Echo "<td>$pmain[phydis]</td>";
                                Echo "</tr>";
                            }
                            
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <?php include("includes/linksfoot.php");
        ?>
    </body>
    </html>
    <?php
}
if (isset($_POST["excel"])) {

    $contents .= "Sr No.,Date,Member Code,Member Name,Address,Married Status,Mobile,Blood Group,Qualification,Occupation,Physically Disabled\n";

    $ctr = 0;
    $mtqty = 0;
    $mgtot = 0;

    while ($pmain = mysqli_fetch_array($getpmain)) {

        $address = $pmain['add1'] . $pmain['add2'] . $pmain['add3'];

        $ctr++;
        
        $contents .= $ctr . ",";
        $contents .= "$pmain[vouchdt]" . ",";
        $contents .= "$pmain[memno]" . ",";
        $contents .= "$pmain[memname]" . ",";
        $contents .= "$address" . ",";
        $contents .= "$pmain[mobile]" . ",";
        $contents .= "$pmain[bloodgrp]" . ",";
        $contents .= "$pmain[qualification]" . ",";
        $contents .= "$pmain[occupation]" . ",";
        $contents .= "$pmain[phydis]" . "\n";

    }

    $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;
    }
}
?>