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

$maindb = "finallist";
$formname = "Voter List Print";
$file = "finallist_print.php";
$mgrpcode = "";

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

$name = $_POST['name'];
$area = $_POST['area'];
$city = $_POST['city'];
$pin = $_POST['pin'];
$brno = $_POST['brno'];
$address = $_POST['address'];
$member = $_POST['member'];
$groupmast = $_POST['groupmast'];
$cbsno = $_POST['cbsno'];
$oldno = $_POST['oldno'];
$orderby = $_POST['orderby'];
$region = $_POST['region'];
$yearno = $_POST['yearno'];
$removdup = $_POST['removdup'];
$namecond = "";
$membercond = "";
$areacond = "";
$brnocond = "";
$citycond = "";
$pincond = "";
$addresscond = "";
$groupmastcond = "";
$orderbycond = "";
$cbscond = "";
$oldcond = "";
$regioncond = "";

if (!empty($groupmast)) {
    $groupmastcond = " and accode in (select memcode from grouptran where code != '' and `code`='$groupmast')";
}
if (!empty($name)) {
    $namecond = " AND `accode`='$name' ";
}
if (!empty($member)) {
    $membercond = " AND `name` LIKE '%$member%' ";
}
if (!empty($brno)) {
    $brnocond = " AND `brno` LIKE '%$brno%' ";
}
if (!empty($yearno)) {
    $yearnocond = " AND `yearno` = '$yearno' ";
}
if (!empty($address)) {
    $addresscond = "  AND ( `add1` LIKE '%$address%' or  `add2` LIKE '%$address%' or  `add3` LIKE '%$address%' )";
}
if (!empty($area)) {
    $areacond = " AND `station` LIKE '%$area%' ";
}
if (!empty($region)) {
    $regioncond = " AND `region` LIKE '%$region%' ";
}
if (!empty($brno)) {
    $brnocond = " AND `brno` LIKE '%$brno%' ";
}
if (!empty($pin)) {
    $pincond = " AND `pin` LIKE '%$pin%' ";
}
if (!empty($city)) {
    $citycond = " AND `city` LIKE '%$city%' ";
}
if (!empty($cbsno)) {
    $cbscond = " AND `memno` LIKE '%$cbsno%' ";
}
if (!empty($oldno)) {
    $oldcond = " AND `oldno` LIKE '%$oldno%' ";
}
if (!empty($orderby)) {
    if ($orderby == "1") {
        $orderbycond = " order by name";
    } else {
        $orderbycond = " order by oldno";
    }
}
if ($removdup == "1") {
    $getpmain = mysqli_query($conn, "SELECT accode,memno,yearno,joindate,shtactid,oldno,brno,region,station,name,add1,add2,add3,country,city,email,pin,tel FROM `$maindb` WHERE `accode`!='' $regioncond $cbscond $oldcond $namecond $addresscond $brnocond $membercond $pincond $citycond $areacond $yearnocond  $groupmastcond group by brno $orderbycond")or die(mysqli_error($conn));
} else {

    $getpmain = mysqli_query($conn, "SELECT accode,memno,yearno,joindate,shtactid,oldno,brno,region,station,name,add1,add2,add3,country,city,email,pin,tel FROM `$maindb` WHERE `accode`!='' $regioncond $cbscond $oldcond $namecond $addresscond $brnocond $membercond $pincond $citycond $areacond $yearnocond $groupmastcond $orderbycond")or die(mysqli_error($conn));
}
if (isset($_POST["view"])) {

    include("includes/linkshead.php");
    ?>
    <script src="MyScript/item.js" type="text/javascript"></script>
    </head>
    <body>
        <div class="container">
            <div class="row">
                <div class="col-lg-6">
                    <label class="label-control marg">Date - </label>
                    <label class="label-control marg"><?php Echo Date("d-m-Y"); ?></label>
                </div>
                <div class="col-lg-5">
                    <label class="label-control marg">Print - </label>
                    <label class="label-control marg"><?php Echo $formname; ?></label>
                </div>
                <div class="col-lg-1">
                    <a onclick="window.close()"  class="btn btn-primary">Go Back</a>
                </div>
            </div>
            <div class="row">
                <div class="col-lg-12 pad">
                    <table class="table table-condensed table-hover">
                        <tr class="mySearchTr"  style="">
                            <th>Sr</th>
                            <th>Cust No.</th>
                            <th>Old No.</th>
                            <th>Br No.</th>
                            <?php if ($removdup != '1') { ?>
                                <th>Year No.</th>
                            <?php } ?>
                            <th>Join Date</th>
                            <th>Sht Act Id</th>
                            <th>Name</th>
                            <th>Address</th>
                            <th>Region</th>
                            <th>Area</th>
                            <th>Email</th>
                            <th>Pin</th>
                            <th>Tel/Mobile</th>
                        </tr>
                        <?php
                        $ctr = 1;
//                        browse($ctr);
                        while ($pmain = mysqli_fetch_array($getpmain)) {

                            Echo "<tr>";
                            Echo "<td>$ctr</td>";
                            Echo "<td>$pmain[memno]</td>";
                            Echo "<td>$pmain[oldno]</td>";
                            Echo "<td>$pmain[brno]</td>";
                            if ($removdup != '1') {
                                Echo "<td>$pmain[yearno]</td>";
                            }
                            Echo "<td>$pmain[joindate]</td>";
                            Echo "<td>$pmain[shtactid]</td>";
                            Echo "<td>$pmain[name]</td>";
                            Echo "<td>$pmain[add1] $pmain[add2] $pmain[add3] </td>";
                            Echo "<td>$pmain[region]</td>";
                            Echo "<td>$pmain[station]</td>";
                            Echo "<td>$pmain[email]</td>";
                            Echo "<td>$pmain[pin]</td>";
                            Echo "<td>$pmain[tel]  $pmain[mobile]</td>";
                            Echo "</tr>";
                            $ctr++;
                        }
                        ?>
                    </table>
                </div>
            </div>
        </div>

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

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


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

    class pdf extends fpdf {

        function Header() {
            
        }

        function Footer() {

            //Position at 1.5 cm from bottom
            $this->SetY(-15);
            //Arial italic 8
            $this->SetFont('Arial', 'I', 7);
            //Page number
            $this->Cell(0, 10, 'Page ' . $this->PageNo() . '/{nb}', 0, 0, 'C');
        }

    }

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

    $pdf->AliasNbPages();

    $pdf->AddPage();
//    $pdf->SetMargins(10, 10, 10);
    //  $pdf->SetXY(7, 5);

    $pdf->SetFont('Arial', 'B', 7);
    $pdf->SetTextColor(255, 255, 255);
    $pdf->SetFillColor(96, 130, 191);

    $pdf->Cell(8, 7, 'SR.', 1, 0, '', 1);
    $pdf->Cell(16, 7, 'Cust No', 1, 0, '', 1);
    $pdf->Cell(16, 7, 'Old No', 1, 0, '', 1);
    $pdf->Cell(16, 7, 'Sht Act Id.', 1, 0, '', 1);
    $pdf->Cell(16, 7, 'BR.No', 1, 0, '', 1);
    $pdf->Cell(55, 7, 'Name', 1, 0, '', 1);
    $pdf->Cell(125, 7, 'Address', 1, 0, '', 1);
    $pdf->Cell(15, 7, 'Area', 1, 0, '', 1);
    $pdf->Cell(27, 7, 'Email', 1, 0, '', 1);
    $pdf->Cell(13, 7, 'Pin', 1, 0, '', 1);
    $pdf->Cell(20, 7, 'Tel', 1, 0, '', 1);
    $pdf->Cell(24, 7, 'Region', 1, 0, '', 1);

    $pdf->SetFont('Arial', '', 1, 0);
    $pdf->SetTextColor(0, 0, 0);
    $pdf->Ln();

    $ctr = 0;
    while ($pmain = mysqli_fetch_array($getpmain)) {
        $ctr++;

        $pdf->SetFont('Arial', '', 7);

        $pdf->Cell(8, 7, $ctr, 1, 0);
        $pdf->Cell(16, 7, $pmain['memno'], 1, 0);
        $pdf->Cell(16, 7, $pmain['oldno'], 1, 0);
        $pdf->Cell(16, 7, $pmain['shtactid'], 1, 0);
        $pdf->Cell(16, 7, $pmain['brno'], 1, 0);
        $pdf->Cell(55, 7, $pmain['name'], 1, 0);
        $pdf->Cell(125, 7, $pmain['add1'] . $pmain['add2'] . $pmain['add3'], 1, 0);
        $pdf->Cell(15, 7, $pmain['station'], 1, 0);
        $pdf->Cell(27, 7, $pmain['email'], 1, 0);
        $pdf->Cell(13, 7, $pmain['pin'], 1, 0, 'R');
        $pdf->Cell(20, 7, $pmain['tel'] . $pmain['mobile'], 1, 0);
        $pdf->Cell(24, 7, $pmain['region'], 1, 0);
        $pdf->Ln();
    }

    $pdf->SetFont('Arial', 'B', 7);
    $pdf->Output("Member Print.pdf", "D");
}

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

    $contents .= "Sr, Cust No., Old No.,BR No., Sht Act Id., Name, Address, Area, Region, Email, Pin, Tel \n";

    $ctr = 1;

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

        $paddress = $pmain['add1'] . " " . $pmain['add2'] . " " . $pmain['add3'];

        $pname = str_replace(",", "  ", "$pmain[name]");
        $paddress = str_replace(",", "  ", "$paddress");

        $contents .= $ctr . ",";
        $contents .= $pmain['memno'] . ",";
        $contents .= $pmain['oldno'] . ",";
        $contents .= $pmain['brno'] . ",";
        $contents .= $pmain['shtactid'] . ",";
        $contents .= $pmain['name'] . ",";
        $contents .= $paddress . ",";
        $contents .= $pmain['station'] . ",";
        $contents .= $pmain['region'] . ",";
        $contents .= $pmain['email'] . ",";
        $contents .= $pmain['pin'] . ",";
        $contents .= $pmain['tel'] . $pmain['mobile'] . "\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;
    }
}
?>
