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

ini_set('display_errors', 1);
error_reporting(E_ERROR);

$maindb = "accmast";
$formname = "Karigar Print";
$file = "karigar_print.php";
$mgrpcode = "009";

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

if (isset($_POST["view"])) {
    $name = $_POST['name'];
    $country = $_POST['country'];
    $city = $_POST['city'];
    $active = $_POST['active'];
    $process = $_POST['process'];
    $gender = $_POST['gender'];
    $grpcod = $_POST['grpcod'];

    $namecond = "";
    $countrycond = "";
    $citycond = "";
    $activecond = "";
    $proccond = "";
    $gendercond = "";
    $grpcond = "";


    if (!empty($_POST["dept"])) {
        $dept = trim($_POST["dept"]);
        $deptcond = "AND `deptcode` = '$dept'";
    }
    if (!empty($_POST["grpcod"])) {

        $grpcond = "AND `refcodenew` = '$grpcod'";
    }
    if (!empty($_POST["gender"])) {
        $gendercond = "AND `gender` = '$gender'";
    }


    if ($active == 'A') {
        $activecond = " AND `active`= 1 ";
    } elseif ($active == 'I') {
        $activecond = " AND `active` != 1 ";
    }

    if (!empty($name)) {
        $namecond = " AND `accode`='$name' ";
    }
    if (!empty($country)) {
        $countrycond = " AND `country` LIKE '%$country%' ";
    }
    if (!empty($city)) {
        $citycond = " AND `city` LIKE '%$city%' ";
    }
    if (!empty($process)) {
        $proccond = " AND `process`='$process' ";
    }
    $getpmain = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE type='K' and `grpcode`!='$mgrpcode' 
                             $grpcond $namecond $activecond $citycond $countrycond $proccond $deptcond $gendercond")or die(mysqli_error($conn));

    include("includes/linkshead.php");
    ?>
    <!--<script src="MyScript/item.js" type="text/javascript"></script>-->
    </head>
    <body>
        <div class="container-fluid">
            <div class="row">
                <?php include("includes/head_rep.php"); ?>
            </div>
            <div class="row">
                <div class="col-lg-12 pad">
                    <?php
                    if ($_POST["dtype"] == 1) {
                        ?>
                        <table class="table table-condensed table-bordered table-hover">
                            <tr  class="mySearchTr">
                                <th style="width: 150px;">FMI Code</th>
                                <th style="width: 150px">Group Code</th>
                                <th style="width: 150px;">Name</th>
                                <th style="width: 150px;">Mobile</th>
                                <th style="width: 150px;">Gender</th>
                                <th style="width: 150px;">Emergency Contact Person</th>
                                <th style="width: 150px;">Emergency Contact Person No.</th>
                                <th style="width: 150px;">Process</th>
                                <th style="width: 150px;">Image</th>
                                <th style="width: 150px;">Status</th>
                            </tr>
                            <?php
                            $ctr = 0;
                            while ($pmain = mysqli_fetch_array($getpmain)) {

                                if ($pmain['active'] == 0) {
                                    $activeflag = "Unactive";
                                } else {
                                    $activeflag = "Active";
                                }

                                $getkariimg = mysqli_query($conn, "SELECT * FROM `kariimg` WHERE `code` = '$pmain[accode]' ") or die(mysqli_error($conn));
                                $kariimg = mysqli_fetch_array($getkariimg);

                                Echo "<tr style='background-color: white'>";
                                Echo "<td>$pmain[custcode]</td>";
                                Echo "<td>$pmain[refcodenew]</td>";
                                Echo "<td>$pmain[name]</td>";
                                Echo "<td>$pmain[mobile]</td>";
                                Echo "<td>$pmain[gender]</td>";
                                Echo "<td>$pmain[emgcontpers]</td>";
                                Echo "<td>$pmain[emgcontno]</td>";
                                Echo "<td>$process[name]</td>";
                                Echo "<td><img src='$kariimg[imgpath]' alt='Image Not Found' style='height: 120px; width: 100px;'/></td>";
                                Echo "<td>$activeflag</td>";
                                Echo "</tr>";
                                $ctr++;
                            }
                        }
                        ?>
                    </table>
                </div>
            </div>
            <div class="row">
                <div class="col-lg-12 pad" >
                    <?php
                    if ($_POST["dtype"] == 2) {
                        ?>

                        <table class="table table-condensed table-hover table-bordered">
                            <tr  class="mySearchTr">
                                <th width="5%">Code</th>
                                <th width="5%">Ref Code</th>
                                <th width="5%">Group Code</th>
                                <th width="10%">Name</th>
                                <th width="10%">Department</th>
                                <th width="25%">Address</th>
                                <th width="5%">Gender</th>
                                <th width="5%">Emergency Contact Name & No.</th>
                                <th width="5%">Status</th>
                                <th width="5%">Pan No.</th>
                                <th width="5%">Aadhaar No.</th>
                                <th width="5%">Bank Name</th>
                                <th width="5%">Account No.</th>
                                <th width="5%">IFSC CODE</th>
                                <th width="10%">Mobile</th>
                                <th width="20%">Image</th>
                            </tr>
                            <?php
                            $ctr = 1;
                            while ($pmain = mysqli_fetch_array($getpmain)) {

                                if ($pmain['active'] == 0) {
                                    $activeflag = "Unactive";
                                } else {
                                    $activeflag = "Active";
                                }

                                $getkariimg = mysqli_query($conn, "SELECT * FROM `kariimg` WHERE `code` = '$pmain[accode]' ") or die(mysqli_error($conn));
                                $kariimg = mysqli_fetch_array($getkariimg);

                                $getdept = mysqli_query($conn, "SELECT `name`,`code` FROM `dept` where code = '$pmain[deptcode]'")or die(mysqli_error($conn));
                                $fdept = mysqli_fetch_array($getdept);

                                Echo "<tr style='background-color: white'>";
                                Echo "<td>$pmain[custcode]</td>";
                                Echo "<td>$pmain[refcode]</td>";
                                Echo "<td>$pmain[refcodenew]</td>";
                                Echo "<td>$pmain[name]</td>";
                                Echo "<td>$fdept[name]</td>";
                                Echo "<td>$pmain[add1] $pmain[add2] $pmain[add3] , $pmain[city] , $pmain[pin] , $pmain[country]</td>";
                                Echo "<td>$pmain[gender]</td>";
                                Echo "<td>$pmain[emgcontpers] - $pmain[emgcontno]</td>";
                                Echo "<td>$activeflag</td>";
                                Echo "<td>$pmain[pancard]</td>";
                                Echo "<td>$pmain[aadhar]</td>";
                                Echo "<td>$pmain[bankname]</td>";
                                Echo "<td>$pmain[acno]</td>";
                                Echo "<td>$pmain[ifsc]</td>";
//                                Echo "<td>$pmain[pin]</td>";
                                Echo "<td>$pmain[mobile]</td>";
//                                Echo "<td>$pmain[fax]</td>";
                                Echo "<td><img src='$kariimg[imgpath]' alt='Image Not Found' style='height: 120px; width: 100px;'/></td>";
                                Echo "</tr>";
                                $ctr++;
                            }
                        }
                        ?>
                    </table>
                </div>
            </div>
        </div>
        <?php include("includes/linksfoot.php"); ?>
    </body>
    </html>
    <?php
}
if (isset($_POST["excel"])) {
    $name = $_POST['name'];
    $country = $_POST['country'];
    $city = $_POST['city'];
    $active = $_POST['active'];


    if (!empty($active)) {
        $activecond = " AND `active`= 1 ";
    }

    $namecond = "";
    $activecond = "";
    $countrycond = "";
    $citycond = "";

    if (!empty($active)) {
        $activecond = " AND `active`= 1 ";
    }

    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' and type='K' $namecond $citycond $countrycond order by `custcode`+0")or die(mysqli_error($conn));

    $contents .= "FMI Code,Group Code,Ref Code ,Name,Name As On Govt.Id,Gender,Location,Address,City,Pin,Station,Country,Mobile,Telephone,Email,Emergency Contact Person,Emergency Contact Mobile,Contact Person1,Pan Card No,Aadhar Card No,Opening Balance,Department, Process,Last A/c Date,P.Fund,ESIC,P.T,Basic,HRA,DA/MMA,TA/CA,OA,Total Salary,Active,Start Time,End Time,Bank Name,IFSC,Account Number,Kariger Image,Aadhar Front Image,Aadhar Back Image,Pan Card\n";
    $ctr = 0;
    while ($pmain = mysqli_fetch_array($getpmain)) {
        if ($pmain['active'] == 0) {
            $activeflag = "Unactive";
        } else {
            $activeflag = "Active";
        }

        $getkariimg = mysqli_query($conn, "SELECT * FROM `kariimg` WHERE `code` = '$pmain[accode]' ") or die(mysqli_error($conn));
        $kariimg = mysqli_fetch_array($getkariimg);

        $getprocess = mysqli_query($conn, "SELECT `code`, `name` FROM `process` WHERE `code` = '$pmain[process]' ") or die(mysqli_error($conn));
        $process = mysqli_fetch_array($getprocess);
        
        $getloc = mysqli_query($conn, "SELECT `code`, `name` FROM `location` WHERE `code` = '$pmain[location]' ") or die(mysqli_error($conn));
        $location = mysqli_fetch_array($getloc);

        $getdept = mysqli_query($conn, "SELECT `code`, `name` FROM `dept` WHERE `code` = '$pmain[deptcode]' ") or die(mysqli_error($conn));
        $dept = mysqli_fetch_array($getdept);
        $deptname = $dept['name'];

//        $getkariimg = mysqli_query($conn, "SELECT * FROM `kariimg` WHERE `code` = '$pmain[accode]' ") or die(mysqli_error($conn));
//        $kariimg = mysqli_fetch_array($getkariimg);

        if (file_exists($kariimg['imgpath']) != '') {
            $kariimage = "YES";
        } else if (file_exists($kariimg['imgpath']) == '') {
            $kariimage = "NO";
        } else {
            $kariimage = "";
        }
        if (file_exists($kariimg['aadharfront']) != '') {
            $aadharfront = "YES";
        } else if (file_exists($kariimg['aadharfront']) == '') {
            $aadharfront = "NO";
        } else {
            $aadharfront = "";
        }
        if (file_exists($kariimg['aadharback']) != '') {
            $aadharback = "YES";
        } else if (file_exists($kariimg['aadharback']) == '') {
            $aadharback = "NO";
        } else {
            $aadharback = "";
        }
        if (file_exists($kariimg['panimg']) != '') {
            $panimg = "YES";
        } else if (file_exists($kariimg['panimg']) == '') {
            $panimg = "NO";
        } else {
            $panimg = "";
        }


        $pname = $pmain['name'];
        $pmobile = $pmain['mobile'];
        $pprname = $process['name'];
        $add1 = $pmain['add1'];

        $paddress = $add1 . $add2 . $add3;
        $address = str_replace(",", " ", "$paddress");
        $tel = str_replace(",", "/", "$pmain[tel]");

//       $gdImage = imagecreatefromjpeg('');
        $contents .= $pmain['custcode'] . ",";
        $contents .= $pmain['refcode'] . ",";
        $contents .= $pmain['refcodenew'] . ",";
        $contents .= $pname . ",";
        $contents .= $pmain['nickname'] . ",";
        $contents .= $pmain['gender'] . ",";
        $contents .= $location['name'] . ",";
        $contents .= $address . ",";
        $contents .= $pmain['city'] . ",";
        $contents .= $pmain['pin'] . ",";
        $contents .= $pmain['station'] . ",";
        $contents .= $pmain['country'] . ",";
        $contents .= $pmain['mobile'] . ",";
        $contents .= $tel . ",";
        $contents .= $pmain['email'] . ",";
        $contents .= $pmain['emgcontpers'] . ",";
        $contents .= $pmain['emgcontno'] . ",";
        $contents .= $pmain['contpers'] . ",";
        $contents .= $pmain['pancard'] . ",";
        $contents .= $pmain['aadhar'] . ",";
        $contents .= $pmain['openbal'] . ",";
        $contents .= $deptname . ",";
        $contents .= $process['name'] . ",";
        $contents .= $pmain['acdate'] . ",";
        $contents .= $pmain['pfund'] . ",";
        $contents .= $pmain['esic'] . ",";
        $contents .= $pmain['p_tax'] . ",";
        $contents .= $pmain['basic'] . ",";
        $contents .= $pmain['hra'] . ",";
        $contents .= $pmain['da'] . ",";
        $contents .= $pmain['ta'] . ",";
        $contents .= $pmain['oa'] . ",";
        $contents .= $pmain['total'] . ",";
        $contents .= $activeflag . ",";
        $contents .= $pmain['starttime'] . ",";
        $contents .= $pmain['endtime'] . ",";
        $contents .= $pmain['bankname'] . ",";
        $contents .= $pmain['ifsc'] . ",";
        $contents .= $pmain['acno'] . ",";
        $contents .= $kariimage . ",";
        $contents .= $aadharfront . ",";
        $contents .= $aadharback . ",";
        $contents .= $panimg . "\n";
//        $contents .= $kariimg[imgpath] . "\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;
    }
}
?>