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

$maindb = 'visaform';

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

$zroot = $_SESSION["zdir"];
$compcd = $_SESSION["comp_" . $dbname];
$myyear = $_SESSION[$myyear];
$tourcode = $_SESSION['tourcode'];
$tourname = $_SESSION['tourname'];

mysqli_select_db($conn, $zdb);

$class = $_POST["class"];
$frmregnno = $_POST["frmregnno"];
$toregnno = $_POST["toregnno"];
$frmpaxno = $_POST["frmpaxno"];
$topaxno = $_POST["topaxno"];

$ccond = "";
if (!empty($_POST["class"])) {
    $ccond = " AND b.`batchno`='$class' ";
}


$regnnocond = "";
if (!empty($_POST["frmregnno"]) && !empty($_POST["toregnno"])) {
    $regnnocond = " AND a.`vregnno` BETWEEN '$frmregnno' AND '$toregnno'";
}

$paxcond = "";
if (!empty($_POST["frmpaxno"]) && !empty($_POST["topaxno"])) {
    $paxcond = " AND a.`vouchno` BETWEEN '$frmpaxno' AND '$topaxno'";
}

$docfilter = $_POST['docfilter'] ?? '';
$doccond = "";

switch ($docfilter) {

    case 'complete':
        $doccond = "
            AND COALESCE(a.chkpassport,0)=1
            AND COALESCE(a.chkpan,0)=1
            AND COALESCE(a.chkaadhaar,0)=1
            AND COALESCE(a.chkhealth,0)=1
            AND COALESCE(a.chkvisarec,0)=1
            AND COALESCE(a.chkphoto,0)=1
        ";
        break;

    case 'incomplete':
        $doccond = "
            AND (
                COALESCE(a.chkpassport,0)=0 OR
                COALESCE(a.chkpan,0)=0 OR
                COALESCE(a.chkaadhaar,0)=0 OR
                COALESCE(a.chkhealth,0)=0 OR
                COALESCE(a.chkvisarec,0)=0 OR
                COALESCE(a.chkphoto,0)=0
            )
        ";
        break;

    case 'passport':
        $doccond = " AND COALESCE(a.chkpassport,0)=0 ";
        break;

    case 'pan':
        $doccond = " AND COALESCE(a.chkpan,0)=0 ";
        break;

    case 'aadhaar':
        $doccond = " AND COALESCE(a.chkaadhaar,0)=0 ";
        break;

    case 'health':
        $doccond = " AND COALESCE(a.chkhealth,0)=0 ";
        break;

    case 'visa':
        $doccond = " AND COALESCE(a.chkvisarec,0)=0 ";
        break;

    case 'photo':
        $doccond = " AND COALESCE(a.chkphoto,0)=0 ";
        break;
}


$orderby = " ORDER BY a.vouchno ";

$query = "SELECT 
                a.vouchno,
                a.vregnno,
                a.name,

                a.chkpassport,
                a.chkpan,
                a.chkaadhaar,
                a.chkhealth,
                a.chkvisarec,
                a.chkphoto,

                CASE WHEN COALESCE(a.chkpassport,0)=1 THEN '✔' ELSE '✖' END AS passport,
                CASE WHEN COALESCE(a.chkpan,0)=1 THEN '✔' ELSE '✖' END AS pan,
                CASE WHEN COALESCE(a.chkaadhaar,0)=1 THEN '✔' ELSE '✖' END AS aadhaar,
                CASE WHEN COALESCE(a.chkhealth,0)=1 THEN '✔' ELSE '✖' END AS health,
                CASE WHEN COALESCE(a.chkvisarec,0)=1 THEN '✔' ELSE '✖' END AS visa,
                CASE WHEN COALESCE(a.chkphoto,0)=1 THEN '✔' ELSE '✖' END AS photo

            FROM `visaform` a, `bookform` b
            WHERE a.vregnno = b.regnno
              AND a.tour = b.tourno
              AND a.`name` != ''
              AND b.tourno = '$tourcode'
              $regnnocond $paxcond
              $ccond $doccond
              $orderby";


$cmain = mysqli_query($conn, $query) or die(mysqli_error($conn));

// $cmain = mysqli_query($conn, "SELECT  a.*,b.tel,b.mobile,b.add1,b.add2,b.add3,b.city,b.pin,b.state  FROM `visaform` a, `bookform` b  
//                               WHERE  a.vregnno = b.regnno AND a.tour = b.tourno AND a.`name` != ''  AND b.tourno = '$tourcode'  $regnnocond $paxcond
//                               $ccond $orderby ") or die(mysqli_error($conn));

$paxno = mysqli_num_rows($cmain);

$getcomp = mysqli_query($conn, "SELECT * FROM $zroot.`company` WHERE `code`= '$compcd'") or die(mysqli_error($conn));
$comp = mysqli_fetch_array($getcomp);

if (mysqli_num_rows($cmain) <= 0) {
    browse('Record Not Found');
    echo "<script>"
        . "window.close();"
        . "</script>;";
}

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

    include("includes/linkshead.php");
    ?>
    <style>
        .missing-row {
            background-color: #ffe6e6;
            /* light red */
        }

        .tick {
            color: green;
            font-weight: bold;
        }

        .cross {
            color: red;
            font-weight: bold;
        }
    </style>

    </head>

    <body>
        <div class="container">
            <?php
            include("includes/report_head.php");
            ?>
            <table class="table table-bordered table-sm">
                <thead class="table-dark">
                    <tr class="mySearchTr">
                        <th>Sr. No.</th>
                        <th>Regn. No.</th>
                        <th>Pax No.</th>
                        <th>Pax Name</th>
                        <th>Passport</th>
                        <th>PAN</th>
                        <th>Aadhaar</th>
                        <th>Health</th>
                        <th>Visa</th>
                        <th>Photo</th>
                    </tr>
                </thead>
                <tbody>

                    <?php
                    $ctr = 0;

                    while ($row = mysqli_fetch_assoc($cmain)) {
                        
                        $ctr++;
                        // detect missing document
                        $hasMissing =
                            empty($row['chkpassport']) ||
                            empty($row['chkpan']) ||
                            empty($row['chkaadhaar']) ||
                            empty($row['chkhealth']) ||
                            empty($row['chkvisarec']) ||
                            empty($row['chkphoto']);

                        $rowClass = $hasMissing ? 'missing-row' : '';
                        ?>
                        <tr class="<?= $rowClass ?>">
                            <td><?= $ctr ?></td>
                            <td><?= ($row['vregnno']) ?></td>
                            <td><?= ($row['vouchno']) ?></td>
                            <td><?= htmlspecialchars($row['name']) ?></td>

                            <td class="<?= ($row['passport'] == '✔') ? 'tick' : 'cross' ?>"><?= $row['passport'] ?></td>
                            <td class="<?= ($row['pan'] == '✔') ? 'tick' : 'cross' ?>"><?= $row['pan'] ?></td>
                            <td class="<?= ($row['aadhaar'] == '✔') ? 'tick' : 'cross' ?>"><?= $row['aadhaar'] ?></td>
                            <td class="<?= ($row['health'] == '✔') ? 'tick' : 'cross' ?>"><?= $row['health'] ?></td>
                            <td class="<?= ($row['visa'] == '✔') ? 'tick' : 'cross' ?>"><?= $row['visa'] ?></td>
                            <td class="<?= ($row['photo'] == '✔') ? 'tick' : 'cross' ?>"><?= $row['photo'] ?></td>
                        </tr>
                    <?php } ?>

                </tbody>
            </table>



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

    </html>
    <?php
}
?>