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

$maindb = "member";
$formname = "Voter List";
$file = "voterlist_fr.php";

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

mysqli_select_db($conn, $zroot);
$date = $_POST["date"];
$family = $_POST["family"];
$surname = $_POST["surname"];
$gender = $_POST["gender"];
$area = $_POST["area"];
$city = $_POST["city"];
$pin = $_POST["pin"];
$village = $_POST["village"];
$taluka = $_POST["taluka"];
$district = $_POST["district"];
$native = $_POST["native"];
$occupation = $_POST["occupation"];
$profession = $_POST["profession"];
$mstatus = $_POST["mstatus"];
$qualification = $_POST["qualification"];
$phydisbaled = $_POST["phydis"];
$bloodgrp = $_POST["bloodgrp"];

 $surcond = $areacond = $citycond = $pincond = $villcond = $talukacond = $distcond = $nativecond = $datecond =  "";

if (!empty($surname)) {
    $surcond = " AND `surname`='$surname' ";
}
if (!empty($gender)) {
    $gendercond = " AND `gender`='$gender' ";
}
if (!empty($area)) {
    $areacond = " AND `area`='$area' ";
}
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' ";
}

$stdate = date('Y-m-d', strtotime($date. ' - 18 years'));


$getrecp = mysqli_query($conn, "SELECT *,DATE_FORMAT(`dob`, '%d-%m-%Y')as dob FROM `$maindb` WHERE 
                                         `age` >= 18  and gender = 'M' and dob >= $stdate
                                         $surcond  $areacond $pincond $citycond $pincond $villcond $talukacond 
                                         $distcond $nativecond  $mstatcond order by accode") or die(mysqli_error($conn));

$get = mysqli_num_rows($getrecp);

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

    include("includes/linkshead.php");
    // $mfdt = date("d-m-Y", strtotime($mfrmdate));
    // $mtdt = date("d-m-Y", strtotime($mtodate));
?>
    <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="pad">
            <?php include("includes/head_rep.php"); ?>
            <div class="row pad">
                <div class="col-lg-12 ">
                    <table style="background: white;width: 100%" class="table table-bordered table-condensed table-hover">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:5%">Member No.</th>
                                <th style="width:5%">Family ID</th>
                                <th style="width:5%">Surname</th>
                                <th style="width:10%">Name</th>
                                <th style="width:5%">Gender</th>
                                <th style="width:10%">Married Status</th>
                                <th style="width:7%">DOB</th>
                                <th style="width:3%">Age</th>
                                <th style="width:10%">Mobile /<br> Whatsapp</th>
                                <th style="width:7%">Qualification</th>
                                <th style="width:10%">Profession</th>
                                <th style="width:10%">Address</th>
                                <th style="width:5%">City</th>
                                <th style="width:5%">Taluka</th>
                                <th style="width:5%">District</th>
                                <th style="width:3%">Pincode</th>
                                <th style="width:5%">State</th>
                                <?php if ($phydisbaled == 'YES') { ?>
                                    <th style="width:10%">Physically Disabled</th>
                                    <th style="width:10%">Blood Group</th>
                                <?php } else { ?>
                                    <th style="width:10%">Blood Group</th>
                                <?php } ?>
                            </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'];
                                }

                                if ($rmain['phydisabled'] == 'YES') {
                                    $phydis = $rmain['phydisabledy'];
                                } else {
                                    $phydis = $rmain['phydisabled'];
                                }

                                $capsurname = strtoupper($rmain['surname']);
                                $capname = strtoupper($rmain['name'] . ' ' . $rmain['name2']);
                                $address1 = $rmain['add1'] . $rmain['add2'] . $rmain['add3'];
                                $address = str_replace(",", " ", $address1);

                                $ctr++;
                                echo "<tr id='boldfont'>";
                                echo "<td>$rmain[accode]</td>";
                                echo "<td>$rmain[familyid]</td>";
                                echo "<td>$capsurname</td>";
                                echo "<td>$capname</td>";
                                echo "<td>$gender</td>";
                                echo "<td>$mstatus</td>";
                                echo "<td>$dob</td>";
                                echo "<td>$rmain[age]</td>";
                                if ($rmain['mobile'] == $rmain['whatsapp']) {
                                    echo "<td>$rmain[mobile]</td>";
                                } else if ($rmain['mobile'] != $rmain['whatsapp']) {
                                    echo "<td>$rmain[mobile]<br>$rmain[whatsapp]</td>";
                                }
                                echo "<td>$rmain[qualification]</td>";
                                echo "<td>$rmain[profession]</td>";
                                echo "<td>$address</td>";
                                echo "<td>$rmain[city]</td>";
                                echo "<td>$rmain[taluka]</td>";
                                echo "<td>$rmain[district]</td>";
                                echo "<td>$rmain[pin]</td>";
                                echo "<td>$rmain[area]</td>";
                                if ($phydisbaled == 'YES') {
                                    echo "<td>$phydis</td>";
                                    echo "<td>$rmain[bloodgrp]</td>";
                                }else {
                                    echo "<td>$rmain[bloodgrp]</td>";
                                    
                                }
                                echo "</tr>";
                            }
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

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

    </html>
<?php
}

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


    if ($phydisbaled == 'YES') {
        $contents .= "Member No.,Family ID,Surname,Name,Gender,Married Status,DOB,Age,Mobile,Qualification,Profession,Address,City,Taluka,District,Pin Code,State,Physically Disabled,Blood Group\n";
    } else {
        $contents .= "Member No.,Family ID,Surname,Name,Gender,Married Status,DOB,Age,Mobile,Qualification,Profession,Address,City,Taluka,District,Pin Code,State,Blood Group\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';
        }

        $current_year = date('Y');
        $dob = date_parse($rmain['dob']);
        $year = $dob["year"];
        $age = ($current_year - $year);

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

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

        $capsurname = strtoupper($rmain['surname']);
        $capname = strtoupper($rmain['name'] . ' ' . $rmain['name2']);
        $address1 = $rmain['add1'] . $rmain['add2'] . $rmain['add3'];
        $address = str_replace(",", " ", $address1);

        $ctr++;

        $contents .= "$rmain[accode]" . ",";
        $contents .= "$rmain[familyid]" . ",";
        $contents .= "$capsurname" . ",";
        $contents .= "$capname" . ",";
        $contents .= "$gender" . ",";
        $contents .= "$mstatus" . ",";
        $contents .= "$dob" . ",";
        $contents .= "$age1" . ",";
        $contents .= "$rmain[mobile]" . ",";
        $contents .= "$rmain[qualification]" . ",";
        $contents .= "$rmain[profession]" . ",";
        $contents .= "$address" . ",";
        $contents .= "$rmain[city]" . ",";
        $contents .= "$rmain[taluka]" . ",";
        $contents .= "$rmain[district]" . ",";
        $contents .= "$rmain[pin]" . ",";
        if ($phydisbaled == 'YES') {
            $contents .= "$rmain[area]" . ",";
            $contents .= "$rmain[bloodgrp]" . ",";
            $contents .= "$phydis" . "\n";
        } else {
            $contents .= "$rmain[area]" . ",";
            $contents .= "$rmain[bloodgrp]" . "\n";
        }
    }


    $contents = strip_tags($contents);

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


/////////////////////////  PDF  //////////////////////////////////    
if (isset($_POST["subpdf"])) {

    require("library/fpdf/fpdf.php");

    class pdf extends fpdf
    {

        function Footer()
        {

            $this->SetY(-5);

            $this->SetFont('Arial', 'I', 8);
            $this->Cell(0, 5, 'Page ' . $this->PageNo() . '/{nb}', 0, 0, 'C');
        }
    }

    $pdf = new pdf('L', 'mm', 'A4');

    $pdf->AliasNbPages();
    //    $pdf->AddPage();
    $pdf->SetFont('Arial', 'B', 16);
    $pdf->SetTextColor(0, 0, 0);
    $ht = 6;

    $pdf->AddPage();
    $pdf->SetFont('Arial', '', 7);
    $pdf->Cell(10, $ht, "Member", 'TLR', 0, 'C');
    $pdf->Cell(9, $ht, "Family", 'TLR', 0, 'C');
    $pdf->Cell(11, $ht, "Surname", 'TLR', 0, 'C');
    $pdf->Cell(35, $ht, "Name", 'TLR', 0, 'L');
    $pdf->Cell(10, $ht, "Gender", 'TLR', 0, 'L');
    $pdf->Cell(10, $ht, "Status", 'TLR', 0, 'L');
    $pdf->Cell(11, $ht, "DOB", 'TLR', 0, 'L');
    $pdf->Cell(6, $ht, "Age", 'TLR', 0, 'L');
    $pdf->Cell(11, $ht, "Mobile", 'TLR', 0, 'L');
    $pdf->Cell(14, $ht, "Qualification", 'TLR', 0, 'C');
    $pdf->Cell(17, $ht, "Profession", 'TLR', 0, 'L');
    $pdf->Cell(74, $ht, "Address", 'TLR', 0, 'L');
    $pdf->Cell(10, $ht, "City", 'TLR', 0, 'L');
    $pdf->Cell(11, $ht, "Taluka", 'TLR', 0, 'L');
    $pdf->Cell(16, $ht, "District", 'TLR', 0, 'L');
    if ($phydisbaled == 'YES') {
        $pdf->Cell(15, $ht, "State", 'TLR', 0, 'L');
        $pdf->SetFont('Arial', '', 5);

        $pdf->Cell(10, $ht, "Physically", 'TLR', 0, 'L');
    } else {
        $pdf->SetFont('Arial', '', 7);

        $pdf->Cell(15, $ht, "State", 'TLR', 0, 'L');
    }
    $pdf->ln(3);
    $pdf->Cell(10, $ht, "No.", 'LBR', 0, 'L');
    $pdf->Cell(9, $ht, "ID", 'LBR', 0, 'L');
    $pdf->Cell(11, $ht, "", 'LBR', 0, 'L');
    $pdf->Cell(35, $ht, "", 'LBR', 0, 'L');
    $pdf->Cell(10, $ht, "", 'LBR', 0, 'L');
    $pdf->Cell(10, $ht, "", 'LBR', 0, 'L');
    $pdf->Cell(11, $ht, "", 'LBR', 0, 'L');
    $pdf->Cell(6, $ht, "", 'LBR', 0, 'L');
    $pdf->Cell(11, $ht, "", 'LBR', 0, 'L');
    $pdf->Cell(14, $ht, "", 'LBR', 0, 'L');
    $pdf->Cell(17, $ht, "", 'LBR', 0, 'L');
    $pdf->Cell(74, $ht, "", 'LBR', 0, 'L');
    $pdf->Cell(10, $ht, "", 'LBR', 0, 'L');
    $pdf->Cell(11, $ht, "", 'LBR', 0, 'L');
    $pdf->Cell(16, $ht, "", 'LBR', 0, 'L');
    if ($phydisbaled == 'YES') {
        $pdf->Cell(15, $ht, "", 'LBR', 0, 'L');
        $pdf->SetFont('Arial', '', 5);
        $pdf->Cell(10, $ht, "Disabled", 'LBR', 0, 'L');
    } else {
        $pdf->SetFont('Arial', '', 7);

        $pdf->Cell(15, $ht, "", 'LBR', 0, 'L');
    }
    $pdf->ln($ht);

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

        $current_year = date('Y');
        $dob = date_parse($rmain['dob']);
        $year = $dob["year"];
        $age = ($current_year - $year);

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

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

        $capsurname = strtoupper($rmain['surname']);
        $capname = strtoupper($rmain['name'] . ' ' . $rmain['name2']);
        $address1 = $rmain['add1'] . $rmain['add2'] . $rmain['add3'];
        $address = str_replace(",", " ", $address1);

        $ctr++;

        $pdf->SetFont('Arial', '', 6);
        $pdf->Cell(10, $ht, "$rmain[accode]", 1, 0, 'L');
        $pdf->Cell(9, $ht, "$rmain[familyid]", 1, 0, 'L');
        $pdf->Cell(11, $ht, "$capsurname", 1, 0, 'L');
        $pdf->SetFont('Arial', '', 5);
        $pdf->Cell(35, $ht, "$capname", 1, 0, 'L');
        $pdf->SetFont('Arial', '', 5);
        $pdf->Cell(10, $ht, "$gender", 1, 0, 'L');
        $pdf->Cell(10, $ht, "$mstatus", 1, 0, 'L');
        $pdf->Cell(11, $ht, "$dob", 1, 0, 'L');
        $pdf->Cell(6, $ht, "$age1", 1, 0, 'C');
        $pdf->Cell(11, $ht, "$rmain[mobile]", 1, 0, 'C');
        $pdf->Cell(14, $ht, "$rmain[qualification]", 1, 0, 'L');
        $pdf->Cell(17, $ht, "$rmain[profession]", 1, 0, 'L');
        $pdf->SetFont('Arial', '', 4);
        $pdf->Cell(74, $ht, "$address", 1, 0, 'L');
        $pdf->SetFont('Arial', '', 5);
        $pdf->Cell(10, $ht, "$rmain[city]", 1, 0, 'L');
        $pdf->Cell(11, $ht, "$rmain[taluka]", 1, 0, 'L');
        $pdf->Cell(16, $ht, "$rmain[district]", 1, 0, 'L');
        if ($phydisbaled == 'YES') {
            $pdf->Cell(15, $ht, "$rmain[area]", 1, 0, 'L');
            $pdf->Cell(10, $ht, "$phydis", 1, 0, 'L');
        } else {
            $pdf->Cell(15, $ht, "$rmain[area]", 1, 0, 'L');
        }
        $pdf->SetFont('Arial', '', 6);
        $pdf->ln($ht);
    }
    $pdf->Output("$formname" . ".pdf", "I");
}
?>