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

$maindb = "member";
$formname = "Expired Member Register";
$file = "expiredreg_fr.php";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
$zroot = $_SESSION["zdbroot"];

mysqli_select_db($conn, $zroot);

$member = $_POST["member"];
$family = $_POST["family"];
$surname = $_POST["surname"];
$gender = $_POST["gender"];
$state = $_POST["state"];
$city = $_POST["city"];
$pin = $_POST["pin"];
$village = $_POST["village"];
$taluka = $_POST["taluka"];
$district = $_POST["district"];
$native = $_POST["native"];
$mstatus = $_POST["mstatus"];

$memcond = $famcond = $surcond = $gendercond = $areacond = $citycond = $pincond = $villcond = $talukacond = $distcond = $nativecond = $occucond = $profcond = $mstatcond = $qualcond = "";

if (!empty($member)) {
    $memcond = " AND `accode`='$member' ";
}
if (!empty($family)) {
    $famcond = " AND `familyid`='$family' ";
}
if (!empty($surname)) {
    $surcond = " AND `surname`='$surname' ";
}
if (!empty($gender)) {
    $gendercond = " AND `gender`='$gender' ";
}
if (!empty($state)) {
    $areacond = " AND `area`='$state' ";
}
if (!empty($city)) {
    $citycond = " AND `city`='$city' ";
}
if (!empty($pin)) {
    $pincond = " AND `pin`='$pin' ";
}
if (!empty($village)) {
    $villcond = " AND `village`='$village' ";
}
if (!empty($taluka)) {
    $talukacond = " AND `taluka`='$taluka' ";
}
if (!empty($district)) {
    $distcond = " AND `district`='$district' ";
}
if (!empty($native)) {
    $nativecond = " AND `native`='$native' ";
}
if (!empty($mstatus)) {
    $mstatcond = " AND `mstatus`='$mstatus' ";
}

$mfrmdate = trim($_POST["fdate"]);
$mtodate = trim($_POST["tdate"]);

$mfrmdatecond = "";
$mtodatecond = "";

if (!empty($mfrmdate)) {
    $mfrmdatecond = " AND `expired` >= '$mfrmdate' ";
}
if (!empty($mtodate)) {
    $mtodatecond = " AND `expired` <= '$mtodate' ";
}


$getrecp = mysqli_query($conn, "SELECT *,DATE_FORMAT(`expired`, '%d-%m-%Y')as expiry,DATE_FORMAT(`dob`, '%d-%m-%Y')as dob FROM $maindb
                                  WHERE accode != '' and expired != '0000-00-00' $memcond $femcond $surcond $gendercond $areacond $pincond 
                                  $citycond $pincond $villcond $talukacond $distcond $nativecond  $mstatcond  
                                  $mfrmdatecond $mtodatecond order by accode") or die(mysqli_error($conn));
$get = mysqli_num_rows($getrecp);

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>
    <!--<script src="MyScript/item.js" type="text/javascript"></script>-->
    </head>
    <body>
        <div class="container" style="width:90%">
            <?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:7%">Expiry Date</th>
                                <th style="width:3%">Member</th>
                                <th style="width:3%">Family</th>
                                <th style="width:5%">Surname</th>
                                <th style="width:12%">Name</th>
                                <th style="width:5%">State</th>
                                <th style="width:5%">Taluka</th>
                                <th style="width:5%">City</th>
                                <th style="width:14%">Address</th>
                                <th style="width:6%">Mobile</th>
                                <th style="width:7%">Qualification</th>
                                <th style="width:7%">DOB</th>
                                <th style="width:3%">Age</th>
                                <th style="width:5%">Gender</th>
                                <th style="width:8%">Profession</th>
                                <th style="width:7%">Married Status</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            while ($rmain = mysqli_fetch_array($getrecp)) {

                                if ($rmain['mstatus'] == 'M') {
                                    $mstatus = 'MARRIED';
                                } elseif ($rmain['mstatus'] == 'S') {
                                    $mstatus = 'SINGLE';
                                } elseif ($rmain['mstatus'] == 'D') {
                                    $mstatus = 'DIVORCE';
                                } elseif ($rmain['mstatus'] == 'W') {
                                    $mstatus = 'WIDOW';
                                } else {
                                    $mstatus = '';
                                }

                                if ($rmain['gender'] == 'M') {
                                    $gender = 'MALE';
                                } elseif ($rmain['gender'] == 'F') {
                                    $gender = 'FEMALE';
                                }

                                if ($rmain['dob'] == '00-00-0000') {
                                    $dob = "";
                                } else {
                                    $dob = $rmain['dob'];
                                }

                                $capsurname = strtoupper($rmain['surname']);
                                $capname = strtoupper($rmain['name']);
                                $ctr++;
                                Echo "<tr id='boldfont'>";
                                Echo "<td>$rmain[expiry]</td>";
                                Echo "<td>$rmain[accode]</td>";
                                Echo "<td>$rmain[familyid]</td>";
                                Echo "<td>$capsurname</td>";
                                Echo "<td>$capname</td>";
                                Echo "<td>$rmain[area]</td>";
                                Echo "<td>$rmain[taluka]</td>";
                                Echo "<td>$rmain[city]</td>";
                                Echo "<td>$rmain[add1] $rmain[add2] $rmain[add3]</td>";
                                Echo "<td>$rmain[mobile]</td>";
                                Echo "<td>$rmain[qualification]</td>";
                                Echo "<td>$dob</td>";
                                Echo "<td>$age1</td>";
                                Echo "<td>$gender</td>";
                                Echo "<td>$rmain[profession]</td>";
                                Echo "<td>$mstatus</td>";
                                Echo "</tr>";
                            }
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

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

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

    $contents .= "Expiry,Member,Family,Surname,Name,City,Area,Taluka,Address,Mobile,Qualification,DOB,Gender,Profession,Married Status\n";

    $ctr = 0;
    while ($rmain = mysqli_fetch_array($getrecp)) {

        if ($rmain['mstatus'] == 'M') {
            $mstatus = 'MARRIED';
        } elseif ($rmain['mstatus'] == 'S') {
            $mstatus = 'SINGLE';
        } elseif ($rmain['mstatus'] == 'D') {
            $mstatus = 'DIVORCE';
        } elseif ($rmain['mstatus'] == 'W') {
            $mstatus = 'WIDOW';
        } else {
            $mstatus = '';
        }

        if ($rmain['gender'] == 'M') {
            $gender = 'MALE';
        } elseif ($rmain['gender'] == 'F') {
            $gender = 'FEMALE';
        }


        if ($rmain['dob'] == '00-00-0000') {
            $dob = "";
        } else {
            $dob = $rmain['dob'];
        }

        $capsurname = strtoupper($rmain['surname']);
        $capname = strtoupper($rmain['name']);
        $ctr++;

        $ctr++;

        $contents .= "$rmain[expiry]" . ",";
        $contents .= "$rmain[accode]" . ",";
        $contents .= "$rmain[familyid]" . ",";
        $contents .= "$capsurname" . ",";
        $contents .= "$capname" . ",";
        $contents .= "$rmain[area]" . ",";
        $contents .= "$rmain[taluka]" . ",";
        $contents .= "$rmain[city]" . ",";
        $contents .= "$rmain[add1]" . "$rmain[add2]" . "$rmain[add3]" . ",";
        $contents .= "$rmain[mobile]" . ",";
        $contents .= "$rmain[qualification]" . ",";
        $contents .= "$dob" . ",";
        $contents .= "$rmain[age]" . ",";
        $contents .= "$gender" . ",";
        $contents .= "$rmain[profession]" . ",";
        $contents .= "$mstatus" . "\n";
    }



    $contents = strip_tags($contents);

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