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

$maindb = "complaint";
$formname = "Complaint";
$file = "complaint_print.php";
//$mgrpcode = "010";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
$zroot = $_SESSION["zdir"];
$zcomp = $_SESSION["comp_" . $dbname];
$fseries = $_SESSION["society_" . $dbname];
//$getcomp = mysqli_query($conn, "SELECT * FROM $zroot.`society` where `cono` = '$fseries'")or die(mysqli_error($conn));
$comp = mysqli_fetch_array($getcomp);

mysqli_select_db($conn, $zdb);

    $name = $_POST['name'];
    $status = $_POST['status'];
    $flatno = $_POST['flatno'];
    $subject = $_POST['subject'];

    $namecond = "";
    $statuscond = "";
    $flatnocond = "";
    $subjectcond = "";

    if (!empty($name)) {
        $namecond = " AND `vouchno`='$name'";
    }
    if (!empty($status)) {
        $statuscond = " AND `status`='$status'";
    }
    if (!empty($flatno)) {
        $flatnocond = " AND `flatno`='$flatno' ";
    }
    if (!empty($subject)) {
        $subjectcond = " AND `subject`='$subject' ";
    }
    
    $getpmain = mysqli_query($conn, "SELECT *,DATE_FORMAT(`complete`,'%d-%m-%Y') as mydt FROM `$maindb` WHERE `vouchno`!='' $namecond $statuscond $flatnocond $subjectcond")or die(mysqli_error($conn));

if (isset($_POST["view"])) {
    include("includes/linkshead.php");
    ?>
    <!--<script src="MyScript/complaints.js" type="text/javascript"></script>-->
    <script type="text/javascript">

        $(document).ready(function () {
            $(".dtlhide").hide();

            //        left side
            $("body").on('dblclick', '.getdtl', getDetails);
            $("body").on('dblclick', '.dtlhide', hideDetails);
            function getDetails() {
                var trid = $(this).closest('tr').attr('id');
                var idval = trid.split('_')[1];
                $("#dtl_" + idval).show();
                $("#dtlcomp_" + idval).show();
            }
            function hideDetails() {
                var trid = $(this).closest('tr').attr('id');
                var idval = trid.split('_')[1];
                $("#dtl_" + idval).hide();
                $("#dtlcomp_" + idval).hide();

            }
        })
    </script>
    </head>
    <body>
        <div class="container">
            <?php include("includes/head_rep.php"); ?>
            <div class="row">
                <div class="col-lg-12 pad">
                    <table class="table table-condensed table-bordered">
                        <tr class="mySearchTr">
                            <th width="3%">Sr</th>
                            <th width="2%">Complaint</th>
                            <th width="8%">Flat No.</th>
                            <th width="20%">Name</th>
                            <th width="20%">Subject</th>
                            <th width="10%">Status</th>
                            <th width="10%">Complete Date</th>
                        </tr>
                        <?php
                        $ctr = 1;
                        $mctr = 0;
                        while ($pmain = mysqli_fetch_array($getpmain)) {

                            $name = $pmain['name'];
                            $compno = $pmain['vouchno'];
                            $subject = $pmain['subject'];
                            $status1 = $pmain['status'];
                            $flatno = $pmain['flatno'];
                            $city = $pmain['city'];
                            $pin = $pmain['pin'];
                            $tel = $pmain['tel'];
                            $email = $pmain['email'];
                            $complby = $pmain['complby'];
                            $complete = $pmain['mydt'];


                            switch ($complete) {
                                case "00-00-0000";
                                    $complete = '';
                                    break;
                            }

                            switch ($status1) {
                                case "U":
                                    $status = 'Unattended';
                                    break;
                                case "P":
                                    $status = 'Pending';
                                    break;
                                case "C":
                                    $status = 'Completed';
                                    break;
                            }
                            Echo "<tr class='getdtl' id='getdtl_$mctr'>";
                            Echo "<td>$ctr</td>";
                            Echo "<td>$compno</td>";
                            Echo "<td>$flatno</td>";
                            Echo "<td>$name<br><i>Complaint By - $complby</i></td>";
                            Echo "<td>$subject</td>";
                            Echo "<td>$status</td>";
                            Echo "<td>$complete</td>";
                            Echo "</tr>";
                            $ctr++;




                            $gstaff = mysqli_query($conn, "SELECT code,name FROM `comprecdby` where code = '$pmain[recdby]' order by `name`") or die(mysqli_error($conn));
                            $fstaff = mysqli_fetch_array($gstaff);

                            $compldate = $pmain['vouchdt'];
                            $descrip = $pmain['descrip'];
                            $reason = $pmain['reason'];
                            $verifyby = $pmain['verifyby'];
                            $recdby = $fstaff['name'];

                            echo "<tr class='dtlhide' id='dtlcomp_$mctr' style='background-color: #3E065F;'>";
                            Echo "<th style='background-color: #3E065F;width:3%;'></th>";
                            Echo "<th style='background-color: #3E065F;width:8%;'>Complaint Date</th>";
                            Echo "<th style='background-color: #3E065F;width:8%;'>Received By</th>";
                            Echo "<th style='background-color: #3E065F;width:20%;'>Description</th>";
                            Echo "<th style='background-color: #3E065F;width:20%;'>Reason</th>";
                            Echo "<th style='background-color: #3E065F;width:10%;'>Verified By</th>";
                            Echo "<th style='background-color: #3E065F;width:4%;'></th>";
                            Echo "</tr>";



                            Echo "<tr class='dtlhide' id='dtl_$mctr'>";
                            Echo "<td></td>";
                            Echo "<td>$compldate</td>";
                            Echo "<td>$recdby</td>";
                            Echo "<td>$descrip</td>";
                            Echo "<td>$reason</td>";
                            Echo "<td>$verifyby</td>";
                            Echo "</tr>";
                            Echo "<tr style='height:10px;'></tr>";
                            $mctr++;
                        }
                        ?>
                    </table>
                </div>
            </div>
        </div>
        <?php include("includes/linksfoot.php"); ?>
    </body>
    </html>
    <?
}

if (isset($_POST["excel"])) {
    $contents .= "Sr,Complaint No.,Flat No.,Name,Complaint By,Subject,Status,Complete Date\n";
                        
    
                        $ctr = 1;
                        $mctr = 0;
                        while ($pmain = mysqli_fetch_array($getpmain)) {

                            $name = $pmain['name'];
                            $compno = $pmain['vouchno'];
                            $subject = $pmain['subject'];
                            $status1 = $pmain['status'];
                            $flatno = $pmain['flatno'];
                            $city = $pmain['city'];
                            $pin = $pmain['pin'];
                            $tel = $pmain['tel'];
                            $email = $pmain['email'];
                            $complby = $pmain['complby'];
                            $complete = $pmain['mydt'];


                            switch ($complete) {
                                case "00-00-0000";
                                    $complete = '';
                                    break;
                            }

                            switch ($status1) {
                                case "U":
                                    $status = 'Unattended';
                                    break;
                                case "P":
                                    $status = 'Pending';
                                    break;
                                case "C":
                                    $status = 'Completed';
                                    break;
                            }
                            $contents .= "$ctr"      . ",";
                            $contents .= "$compno"   . ",";
                            $contents .= "$flatno"   . ",";
                            $contents .= "$name"     . ",";
                            $contents .= "$complby"  . ",";
                            $contents .= "$subject"  . ",";
                            $contents .= "$status"   . ",";
                            $contents .= "$complete" . "\n";
                            $ctr++;

//                            $gstaff = mysqli_query($conn, "SELECT code,name FROM `comprecdby` where code = '$pmain[recdby]' order by `name`") or die(mysqli_error($conn));
//                            $fstaff = mysqli_fetch_array($gstaff);
//
//                            $compldate = $pmain['vouchdt'];
//                            $descrip = $pmain['descrip'];
//                            $reason = $pmain['reason'];
//                            $verifyby = $pmain['verifyby'];
//                            $recdby = $fstaff['name'];
//
//                            echo "<tr class='dtlhide' id='dtlcomp_$mctr' style='background-color: #3E065F;'>";
//                            Echo "<th style='background-color: #3E065F;width:3%;'></th>";
//                            Echo "<th style='background-color: #3E065F;width:8%;'>Complaint Date</th>";
//                            Echo "<th style='background-color: #3E065F;width:8%;'>Received By</th>";
//                            Echo "<th style='background-color: #3E065F;width:20%;'>Description</th>";
//                            Echo "<th style='background-color: #3E065F;width:20%;'>Reason</th>";
//                            Echo "<th style='background-color: #3E065F;width:10%;'>Verified By</th>";
//                            Echo "<th style='background-color: #3E065F;width:4%;'></th>";
//                            Echo "</tr>";
//
//
//
//                            Echo "<tr class='dtlhide' id='dtl_$mctr'>";
//                            Echo "<td></td>";
//                            Echo "<td>$compldate</td>";
//                            Echo "<td>$recdby</td>";
//                            Echo "<td>$descrip</td>";
//                            Echo "<td>$reason</td>";
//                            Echo "<td>$verifyby</td>";
//                            Echo "</tr>";
//                            Echo "<tr style='height:10px;'></tr>";
//                            $mctr++;
                        }

    $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;
    }
}
//if (isset($_POST["subpdf"])) {
//    $name = $_POST[name];
//    $country = $_POST[country];
//    $city = $_POST[city];
//
//    $namecond = "";
//    $countrycond = "";
//    $citycond = "";
//
//    if (!empty($name)) {
//        $namecond = " AND `accode`='$name' ";
//    }
//    if (!empty($country)) {
//        $countrycond = " AND `country` LIKE '%$country%' ";
//    }
//    if (!empty($city)) {
//        $citycond = " AND `city` LIKE '%$city%' ";
//    }
//    $getpmain = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `grpcode`='$mgrpcode' $namecond $citycond $countrycond ")or die(mysqli_error($conn));
//
//    require("library/fpdf/fpdf.php");
//
//    class pdf extends fpdf {
//
//        function Footer() {
//
////Position at 1.5 cm from bottom
//
//            $this->SetY(-15);
//
////Arial italic 8
//
//            $this->SetFont('Arial', 'I', 8);
//
////Page number
//
//            $this->Cell(0, 10, 'Page ' . $this->PageNo() . '/{nb}', 0, 0, 'C');
//        }
//
//    }
//
//    $pdf = new pdf('L', 'mm', 'A4');
//
//    $pdf->AliasNbPages();
//
//    $pdf->AddPage();
//    $pdf->SetFont('Arial', '', 12);
//    $pdf->SetFillColor(210, 204, 204);
//    $pdf->SetTextColor(000, 000, 000);
//    if ($dtype == 1) {
//        $rtype = " - Summary";
//    } elseif ($dtype == 2) {
//        $rtype = " - Detail";
//    } elseif ($dtype == '') {
//        $rtype = '';
//    }
//    if ($mfdt != '') {
//        $mfdt = "From Date - $mfdt";
//    }if ($mtdt != '') {
//        $mtdt = "To Date - $mtdt";
//    }
//    $pdf->MultiCell(278, 7, "$comp[compname]\n$comp[address]\n$formname $rtype " . "                                                                                                   " . "Report Date-" . Date("d-m-Y") . "\n" . $mfdt . "          " . $mtdt, Alignment . LEFT, false, 1, 0, '', 1);
//    $pdf->SetFont('Arial', '', 1, 0);
//
//    $pdf->SetTextColor(0, 0, 0);
//    $pdf->SetFont('Arial', 'I', 10);
//    $pdf->SetTextColor(238, 61, 111);
//    $pdf->SetFillColor(249, 244, 245);
//    $pdf->Cell(278, 7, "Date - " . Date("d-m-y"), 1, 0, '', 1, 0, 'L');
//    $pdf->Ln();
//    $pdf->Cell(278, 7, "Print - " . $formname, 1, 0, '', 1, 0, 'L');
//    $pdf->SetFont('Arial', '', 1, 0);
//
//    $pdf->SetTextColor(0, 0, 0);
//    $pdf->Ln();
//
//    $pdf->SetFont('Arial', 'I', 10);
//    $pdf->SetTextColor(000, 000, 000);
//    $pdf->SetFillColor(239, 228, 176);
//    $pdf->Cell(18, 7, "Sr", 1, 0, '', 1);
//    $pdf->Cell(80, 7, "Name", 1, 0, '', 1);
//    $pdf->Cell(30, 7, "Block", 1, 0, '', 1);
//    $pdf->Cell(20, 7, "Wing", 1, 0, '', 1);
//    $pdf->Cell(30, 7, "Floor", 1, 0, '', 1);
//    $pdf->Cell(20, 7, "Flat No.", 1, 0, '', 1);
//    $pdf->Cell(30, 7, "City", 1, 0, '', 1);
//    $pdf->Cell(30, 7, "Pin", 1, 0, '', 1);
//    $pdf->Cell(20, 7, "Tel", 1, 0, '', 1);
//    $pdf->SetFont('Arial', '', 1, 0);
//
//    $pdf->SetTextColor(0, 0, 0);
//    $pdf->Ln();
//
//    $ctr = 0;
//    while ($pmain = mysqli_fetch_array($getpmain)) {
//        $name = $pmain['name'];
//        $block = $pmain['block'];
//        $wing = $pmain['wing'];
//        $floor = $pmain['floor'];
//        $flatno = $pmain['flatno'];
//        $city = $pmain['city'];
//        $pin = $pmain['pin'];
//        $tel = $pmain['tel'];
//
//        $ctr++;
//        $pdf->SetFont('Arial', '', 10);
//        $pdf->Cell(18, 7, "$ctr", 1);
//        $pdf->Cell(80, 7, "$name", 1);
//        $pdf->Cell(30, 7, "$block", 1);
//        $pdf->Cell(20, 7, "$wing", 1);
//        $pdf->Cell(30, 7, "$floor", 1);
//        $pdf->Cell(20, 7, "$flatno", 1);
//        $pdf->Cell(30, 7, "$city", 1);
//        $pdf->Cell(30, 7, "$pin", 1);
//        $pdf->Cell(20, 7, "$tel", 1);
//
//        $pdf->Ln();
//    }
//    $pdf->Output("Member.pdf", "D");
//}
?>

