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

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);
$zroot = $_SESSION["zdbroot"];
$file = 'classrep_ali_fr.php';
$formname = 'Class Report';
mysqli_set_charset($conn, "utf8mb4");

$saleexec = $_SESSION["saleexec"];
$usertype = $_SESSION["usertype"];
$username = strtoupper($_SESSION["username_" . $dbname]);

$tourcode = $_SESSION['tourcode'];
$tourname = $_SESSION['tourname'];

$name     = $_POST['name'];
$regnno   = $_POST['regnno'];
$paxno    = $_POST['paxno'];
$group    = $_POST['group'];
$class    = $_POST['class'];
$canc     = $_POST['canc'];
$prphoto  = $_POST['prphoto'];
$city     = $_POST['city'];
$state    = $_POST['state'];
$sex      = $_POST['sex'];
$ppexpiry = $_POST['ppexpiry'];
$ppinhand = $_POST['ppinhand'];
$depdate  = $_POST['depdate'];
$deppnr   = $_POST['deppnr'];
$airlines = $_POST['airlines'];
$agent    = $_POST['agent'];
$arrvdate = $_POST['arrvdate'];
$arrvpnr  = $_POST['arrvpnr'];
$infonly  = $_POST['inf'];
$canconly = $_POST['canc'];
$ticketchk = $_POST['ticketchk'];
$visachk   = $_POST['visachk'];
$landchk   = $_POST['landchk'];

// ── Language Setup ────────────────────────────────────────────
$language  = $_POST['lang'] ?? 'en';
if (file_exists("lang/" . $language . ".php")) {
    include "lang/" . $language . ".php"; // loads $lang array
}
$dir       = ($language === 'ur') ? 'rtl' : 'ltr';
$urdu_font = ($language === 'ur') ? "font-family:'Noto Nastaliq Urdu',serif;font-size:13px;" : '';
$ltr       = "dir='ltr'";

// ── tr() uses $lang array from language file ──────────────────
function tr($key)
{
    global $lang;
    return $lang[$key] ?? $key;
}

function convertToArabicNumerals($number)
{
    // Arabic numerals map
    $arabic_numerals = array(
        '0' => '٠',
        '1' => '١',
        '2' => '٢',
        '3' => '٣',
        '4' => '٤',
        '5' => '٥',
        '6' => '٦',
        '7' => '٧',
        '8' => '٨',
        '9' => '٩'
    );

    // Convert the number to a string to process each character
    $number = strval($number);

    // Replace each digit with its Arabic numeral
    $converted_number = '';
    for ($i = 0; $i < strlen($number); $i++) {
        if (isset($arabic_numerals[$number[$i]])) {
            $converted_number .= $arabic_numerals[$number[$i]];
        } else {
            // If it's not a digit, just keep the character as it is (this could be a separator like a space)
            $converted_number .= $number[$i];
        }
    }

    return $converted_number;
}

function smartUrdu($text)
{
    global $language;

    static $cache = [];

    if ($language !== 'ur') return $text;
    if (empty(trim($text))) return $text;

    if (isset($cache[$text])) return $cache[$text];

    $text = convertToArabicNumerals($text);

    if (preg_match('/^[٠-٩\d\s\-\/\+\(\)]+$/u', $text)) {
        return $text;
    }

    $url = "https://inputtools.google.com/request?text=" . urlencode($text) . "&itc=ur-t-i0-und&num=1";
    $response = @file_get_contents($url);
    $data = json_decode($response, true);

    $translated = $data[1][0][1][0] ?? $text;

    $cache[$text] = $translated;

    return $translated;
}

function num($val)
{
    global $language;
    return ($language === 'ur') ? convertToArabicNumerals($val) : $val;
}

function convertGroup($group)
{
    global $language;

    if ($language !== 'ur') return $group;

    $map = [
        'PREMIUM' => 'پریمیم',
        'BUDGET'  => 'بجٹ',
        'AFZAL'   => 'افضل',
        'AMEEN'   => 'امین',
        'NAEEM'   => 'نعیم',
        'CANCEL'  => 'منسوخ',
        '3'       => '۳' // Arabic numeral
    ];

    $group = strtoupper(trim($group));

    return $map[$group] ?? convertToArabicNumerals($group);
}

function convertSuffix($suffix)
{
    global $language;

    if ($language !== 'ur') return $suffix;

    $map = [
        'MR'   => 'جناب',
        'MRS'  => 'محترمہ',
        'MISS' => 'مس',
        'MS'   => 'محترمہ',
        'MSTR' => 'ماسٹر',
        'INF'  => 'بچہ'
    ];

    $suffix = strtoupper(trim($suffix));

    return $map[$suffix] ?? $suffix;
}

function convertTVL($chkticket, $chkvisa, $chkland)
{
    global $language;

    $result = [];

    if ($chkticket > 0) {
        $result[] = ($language === 'ur') ? 'ٹکٹ' : 'T';
    }

    if ($chkvisa > 0) {
        $result[] = ($language === 'ur') ? 'ویزا' : 'V';
    }

    if ($chkland > 0) {
        $result[] = ($language === 'ur') ? 'لینڈ' : 'L';
    }

    return implode(' ', $result);
}

function formatPassport($passport)
{
    global $language;

    if (empty($passport)) return '';

    if ($language === 'ur') {
        $passport = convertToArabicNumerals($passport);
        return $passport;
    }

    return $passport;
}

function convertCraft($craft)
{
    global $language;

    if ($language !== 'ur') return $craft;

    $map = [
        // IATA → Urdu name
        'SV'   => 'سعودیہ',
        'UK'   => 'وستارا',
        'QP'   => 'اکاسا ایئر',
        'XY'   => 'فلائی ناس',
        '6E'   => 'انڈیگو',
        'OV'   => 'سلم ایئر',
        'AI'   => 'ایئر انڈیا'
    ];

    $craft = strtoupper(trim($craft));

    return $map[$craft] ?? $craft;
}


// ── Query Conditions ──────────────────────────────────────────
$tourcond = $namecond = $regnocond = $paxcond = $groupcond = '';
$classcond = $canccond = $prphotocond = $citycond = $statecond = '';
$sexcond = $ppexpirycond = $ppinhandcond = $depdatecond = '';
$deppnrcond = $airlinescond = $agentcond = $arrvdatecond = $arrvpnrcond = '';

$tourcond1 = $namecond1 = $regnocond1 = $paxcond1 = $groupcond1 = '';
$classcond1 = $canccond1 = $prphotocond1 = $citycond1 = $statecond1 = '';
$sexcond1 = $ppexpirycond1 = $ppinhandcond1 = $depdatecond1 = '';
$deppnrcond1 = $airlinescond1 = $agentcond1 = $arrvdatecond1 = $arrvpnrcond1 = '';
$ticketchkcond = $visachkcond = $landchkcond = '';
$ticketchkcond1 = $visachkcond1 = $landchkcond1 = '';
$airlinecond = $airlinecond1 = '';
$agentcond = $agentcond1 = '';

if ($ticketchk == 1) {
    $ticketchkcond  = "AND b.chkticket = '1'";
    $ticketchkcond1 = "AND chkticket = '1'";
} elseif ($ticketchk == 2) {
    $ticketchkcond  = "AND b.chkticket = ''";
    $ticketchkcond1 = "AND chkticket = ''";
}

if ($visachk == 1) {
    $visachkcond  = "AND b.chkvisa = '1'";
    $visachkcond1 = "AND chkvisa = '1'";
} elseif ($visachk == 2) {
    $visachkcond  = "AND b.chkvisa = ''";
    $visachkcond1 = "AND chkvisa = ''";
}

if ($landchk == 1) {
    $landchkcond  = "AND b.chkland = '1'";
    $landchkcond1 = "AND chkland = '1'";
} elseif ($landchk == 2) {
    $landchkcond  = "AND b.chkland = ''";
    $landchkcond1 = "AND chkland = ''";
}

if (!empty($tourcode)) {
    $tourcond  = " AND a.`tourno` = '$tourcode' ";
    $tourcond1 = " AND `tourno` = '$tourcode' ";
}
if (!empty($name)) {
    $namecond  = " AND b.`name` like '%$name%' ";
    $namecond1 = " AND `name` like '%$name%' ";
}
if (!empty($paxno)) {
    $paxcond  = " AND b.`vouchno`= '$paxno' ";
    $paxcond1 = " AND `vouchno`= '$paxno' ";
}
if (!empty($group)) {
    $groupcond  = " AND a.`groupno`= '$group' ";
    $groupcond1 = " AND `groupno`= '$group' ";
}

if (!empty($_POST['class'])) {
    foreach ($_POST['class'] as $cucheck2) {
        $ccheck2[$ccount2++]     .= "OR a.`batchno` = '" . $cucheck2 . "' ";
        $ccheckoth[$ccountoth++] .= "OR `batchno` = '" . $cucheck2 . "' ";
        $classprn = $classprn . $cucheck2 . "  /   ";
    }
    foreach ($ccheck2  as $g => $cusvalues2) {
        $cuscheck2   .= $cusvalues2;
    }
    foreach ($ccheckoth as $g => $valuesoth) {
        $cuscheckoth .= $valuesoth;
    }
    $classcond  = " AND (a.`batchno` = '' " . $cuscheck2   . " )";
    $classcond1 = " AND (`batchno` = '' "   . $cuscheckoth . " )";
    $classprn   = left($classprn, -4);
}

if (!empty($regnno)) {
    foreach ($regnno as $cucheckr2) {
        $ccheckr2[$ccountr2++]     .= "OR a.`regnno` = '" . $cucheckr2 . "' ";
        $ccheckroth[$ccountroth++] .= "OR `regnno` = '" . $cucheckr2 . "' ";
    }
    foreach ($ccheckr2  as $g => $cusvaluesr2) {
        $cuscheckr2  .= $cusvaluesr2;
    }
    foreach ($ccheckroth as $g => $valuesroth) {
        $cuscheckroth .= $valuesroth;
    }
    $regnocond  = " AND (a.`regnno` = '' " . $cuscheckr2   . " )";
    $regnocond1 = " AND (`regnno` = '' "   . $cuscheckroth . " )";
}

if (!empty($city)) {
    $citycond  = " AND a.`city`= '$city' ";
    $citycond1 = " AND `city`= '$city' ";
}
if (!empty($state)) {
    $statecond  = " AND a.`state` = '$state' ";
    $statecond1 = " AND `state` = '$state' ";
}
if ($sex != '') {
    $sexcond  = "AND left(b.`cmbgender`,1) = trim('$sex') ";
    $sexcond1 = "AND left(`cmbgender`,1) = trim('$sex') ";
}

if ($canconly > 0) {
    $canccond  = " and b.cancdate != '0000-00-00' ";
    $canccond1 = " and cancdate != '0000-00-00' ";
    $cancconda = " and a.cancdate != '0000-00-00' ";
} else {
    $canccond  = " and b.cancdate = '0000-00-00' ";
    $cancconda = " and a.cancdate = '0000-00-00' ";
    $canccond1 = " and cancdate = '0000-00-00' ";
}

if ($ppexpiry != 99) {
    $time     = date("Y-m-d");
    $mcurrmon = date("Y-m-d", strtotime($time . "+" . $ppexpiry . " month"));
    $ppexpirycond  = " and b.expdate != '0000-00-00' and b.expdate >= '$time' and b.expdate < '$mcurrmon' ";
    $ppexpirycond1 = " and expdate != '0000-00-00' and expdate >= '$time' and expdate < '$mcurrmon' ";
}

$depdate = $_POST['depdate'];
if (!empty($depdate)) {
    $depdatecond  = " and a.depdate = '$depdate' ";
    $depdatecond1 = " and depdate = '$depdate' ";
}

$arrvdate = $_POST['arrvdate'];
if (!empty($arrvdate)) {
    $arrvdatecond  = " and a.arrdate = '$arrvdate' ";
    $arrvdatecond1 = " and arrdate = '$arrvdate' ";
}

$airlines = $_POST['airlines'];
if ($airlines != '') {
    $airlinecond  = " and a.craft = '$airlines' ";
    $airlinecond1 = " and craft = '$airlines' ";
}

$agent = $_POST['agent'];
if ($agent != '') {
    $agentcond  = " and a.agent like '%$agent%' ";
    $agentcond1 = " and agent like '%$agent%' ";
}

if ($deppnr == 'P') {
    $deppnrcond  = " and a.deppnr = '' ";
    $deppnrcond1 = " and deppnr = '' ";
}
if ($arrpnr == 'P') {
    $arrpnrcond  = " and a.arrpnr = '' ";
    $arrpnrcond1 = " and arrpnr = '' ";
}

// ── Main Query ────────────────────────────────────────────────
$getpmain2 = mysqli_query($conn, "SELECT a.vouchno, a.regnno, a.vouchdt, DATE_FORMAT(a.`vouchdt`,'%d-%m-%Y') as mydt, a.haji, a.groupno, a.agent,
                                  a.fname, a.add1, a.add2, a.add3, a.tel, a.tel2, a.city, a.mobile, a.mobile2, a.pin, a.state, a.tel as infant, a.tcs as monno,
                                  a.fullamt, a.halfamt, a.infamt, a.atamt, a.stamt, a.fareamt, a.cancelchgamt, a.amount, a.discamt, a.tcs as ftnt,
                                  a.fullno, a.half2, a.half, a.halfno2, a.halfno, a.inf, a.infno, a.visachg, a.visachgno, a.cancelchg2, a.farediffamt,
                                  a.airtax, a.airtaxno, a.st, a.stno, a.farediff, a.farediffno, a.tcsamt, a.tcs, a.bookamt, a.bookamtno,
                                  a.cancelchg, a.cancelchgno, a.disc2, a.disc, a.discno, a.netamt, a.batchno, a.busno, a.craft, a.sector, a.depdate,
                                  a.deptime, a.deppnr, a.depflt, a.arrdate, a.arrtime, a.arrpnr, a.arrflt, a.tourno, a.rem1, a.rem2, a.rem3,
                                  a.discrem, a.email, a.halfpay, a.fullpay, a.cgstno, a.cgstamt, a.sgstno, a.sgstamt, a.nettot, a.agentcomm,
                                  a.mksingle, a.mkdouble, a.mktriple, a.mkquad, a.mkquint, a.mksuite, a.mdsingle, a.mddouble, a.mdtriple,
                                  a.mdquad, a.mdquint, a.mdsuite, b.vouchno as paxno, a.mkroom, a.mdroom, a.othroom,
                                  a.othsingle, a.othdouble, a.othtriple, a.othquad, a.othquint, a.othsuite, a.othnobed,
                                  b.srno, b.surname, b.name as paxname, b.suffix, b.midname, b.lastname,b.name_urdu, b.chkticket, b.chkvisa, b.chkland,
                                  b.vregnno, b.fhname, b.mehram, b.relation, b.caste, b.nation, b.accomp1, b.accomp2, b.comefrom, b.period, b.addindia, b.airline,
                                  b.draftno, b.dob, b.plbirth, b.mothname, b.spname, b.prof, b.qual, b.redypass, b.issdate, b.expdate, b.issplace,
                                  b.visano, b.visadoi, b.visapoi, b.applno, b.appldate, b.applplace, b.passoffice, b.photo, b.tourtype, b.roomno,
                                  b.policenqno, b.enqdate, b.nominee, b.nrelation, b.nadd1, b.nadd2, b.travsince, b.nopeople, b.fpeople, b.cpeople,
                                  b.at1, b.at1from, b.at1to, b.at2, b.at2from, b.at2to, b.at3, b.at3from, b.at3to, b.visaplace, b.visadtd, b.sex, b.tkttax,
                                  b.tktdate, b.mofano, b.bankname, b.appno, b.submdate, b.username, b.airsrno, b.haj_q, b.dum_q, b.oth_q, b.haj_b, b.dum_b, b.oth_b,
                                  b.bran, b.branregno, b.branvouch, b.noupd, b.tourdate, b.finyear, b.vbatch, b.tktamt, b.touramt, b.community,
                                  b.barcode, b.manual, b.panno, b.cmbgender, b.cancdate, b.chkchildwob,
                                  b.qurname, b.tour,
                                  (SELECT COUNT(*) FROM visaform v WHERE v.vregnno = a.regnno) as paxcount
                                   FROM `bookform` a, visaform b WHERE a.regnno = b.vregnno and a.tourno = b.tour
                                  $tourcond $namecond $regnocond $paxcond $groupcond $classcond $prphotocond $citycond $statecond $sexcond
                                  $ppexpirycond $ppinhandcond $depdatecond $deppnrcond $airlinecond $agentcond $arrvdatecond $arrvpnrcond $canccond $cancconda
                                  $ticketchkcond $visachkcond $landchkcond
                                  order by a.depdate, a.batchno, a.groupno, b.vregnno, b.vouchno") or die(mysqli_error($conn));

$getno2 = mysqli_num_rows($getpmain2);

// ── VIEW ──────────────────────────────────────────────────────
if (isset($_POST["view"]) || ($_GET["view"])) {
    include("includes/linkshead.php");
?>

    <head>
        <link href="https://fonts.googleapis.com/css2?family=Noto+Nastaliq+Urdu&display=swap" rel="stylesheet">
    </head>
    <style type="text/css">
        body {
            font-family: 'Noto Nastaliq Urdu', serif;
        }

        #tabital {
            font-style: italic;
        }

        #font1 {
            font-style: italic;
        }

        #boldfont {
            font-weight: bold;
        }
    </style>

    <body>
        <div class="container-fluid">
            <?php
            include("includes/report_head.php");
            if ($getno2 > 0) {
            ?>
                <table style="background:white;width:100%" dir="<?= $dir ?>" class="table table-condensed table-striped table-hover table-bordered">
                    <thead class="mySearchTr" dir="ltr">
                        <tr style='font-size:14px'>
                            <th><?= $lang['sr']       ?></th>
                            <th><?= $lang['regn']     ?></th>
                            <th><?= $lang['name']     ?></th>
                            <th><?= $lang['wob']      ?></th>
                            <th><?= $lang['group']    ?></th>
                            <th><?= $lang['tvl']      ?></th>
                            <th><?= $lang['sex']      ?></th>
                            <th><?= $lang['passport'] ?></th>
                            <th><?= $lang['craft']    ?></th>
                            <th><?= $lang['agent']    ?></th>
                            <th><?= $lang['makka']    ?></th>
                            <th><?= $lang['madina']   ?></th>
                            <th><?= $lang['shift']    ?></th>
                            <th><?= $lang['mobile']   ?></th>
                        </tr>
                    </thead>
                    <tbody dir="<?= $dir ?>" style="<?= $urdu_font ?>">
                        <?php
                        $pctr    = 0;
                        $s0 = $s1 = $s2 = $s3 = $s4 = $s5 = $s6 = 0;
                        $m0 = $m1 = $m2 = $m3 = $m4 = $m5 = $m6 = 0;
                        $o0 = $o1 = $o2 = $o3 = $o4 = $o5 = $o6 = 0;
                        $wob     = 0;
                        $inf     = 0;
                        $tregnno = -1;
                        $colflag = 0;
                        $actr    = 0;
                        $prem1 = $prem2 = $prem3 = '';
                        $pmobile = $pmobile2 = '';

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

                            $dob    = $pmain['dob'];
                            $srno    = $pmain['srno'];

                            $proceed = 1;
                            if ($infonly == 1) {
                                $curdate = date("Y-m-d");
                                $infmon  = date('Y-m-d', strtotime($dob . ' + 24 month'));
                                $proceed = ($infmon > $curdate) ? 1 : 0;
                            }

                            if ($proceed > 0) {

                                $voucher     = $pmain['vouchno'];
                                $vregnno     = $pmain['vregnno'];
                                $regnno      = $pmain['regnno'];
                                $paxno       = $pmain['paxno'];
                                $chkchildwob = $pmain['chkchildwob'];
                                $chkticket   = $pmain['chkticket'];
                                $chkvisa     = $pmain['chkvisa'];
                                $chkland     = $pmain['chkland'];

                                // ── Raw fields ────────────────────────────────────────
                                $surname  = trim($pmain['surname']);
                                $lastname = trim($pmain['lastname']);
                                $midname  = trim($pmain['midname']);
                                $groupno  = trim($pmain['groupno']);
                                $tel      = trim($pmain['tel']);
                                $mobile   = trim($pmain['mobile']);
                                $mobile2  = trim($pmain['mobile2']);
                                $redypass = trim($pmain['redypass']);
                                $craft    = trim($pmain['craft']);
                                $agent    = trim($pmain['agent']);
                                $rem1     = trim($pmain['rem1']);
                                $rem2     = trim($pmain['rem2']);
                                $rem3     = trim($pmain['rem3']);
                                $tvl = '';


                                // ── Translate or keep as-is ───────────────────────────
                                if ($language === 'ur') {
                                    $suffix      = convertSuffix($pmain['suffix']);
                                    $partynmvar      =  $suffix . $pmain['name_urdu'];
                                    $enursrno = convertToArabicNumerals($pmain['srno']);
                                    $telDisplay = convertToArabicNumerals($tel);
                                    $mobDisplay = convertToArabicNumerals($mobile);
                                    $mobile2 = convertToArabicNumerals($mobile2);
                                } else {
                                    $suffix   = trim($pmain['suffix']);
                                    $partynmvar      = $surname . ' ' . $lastname . ' ' . $midname . ' ' . $suffix;
                                    $enursrno = $pmain['srno'];
                                    $mobDisplay   = $mobile;
                                    $telDisplay   = $tel;
                                    $mobile2   = $mobile2;
                                }

                                $agentDisplay = smartUrdu($agent);
                                $rem1Display  = smartUrdu($rem1);
                                $rem2Display  = smartUrdu($rem2);
                                $rem3Display  = smartUrdu($rem3);
                                $tvl = convertTVL($chkticket, $chkvisa, $chkland);
                                $groupDisplayenur = convertGroup($groupno);
                                $passDisplay  = formatPassport($redypass);
                                $craftDisplay = convertCraft($craft);

                                // These never translate — always Roman/numeric


                                // ── Pax count ─────────────────────────────────────────
                                $paxcount = $pmain['paxcount'];
                                // ── Group change detection ────────────────────────────
                                if ($regnno != $tregnno) {
                                    if ($actr == 1) {
                                        echo "<tr><td colspan='9'></td><td colspan='3' $ltr>$prem1</td><td $ltr>$pmobile</td></tr>";
                                        if ($prem2 != '') $actr++;
                                    }
                                    if ($actr == 2) {
                                        echo "<tr><td colspan='9'></td><td colspan='3' $ltr>$prem2</td><td $ltr>$pmobile2</td></tr>";
                                        if ($prem3 != '') $actr++;
                                    }
                                    if ($actr == 3) {
                                        echo "<tr><td colspan='9'></td><td colspan='3' $ltr>$prem3</td><td></td></tr>";
                                    }
                                    if ($actr != 0) {
                                        echo "<tr><td colspan='14'><hr style='margin-top:-2px;margin-bottom:-6px;border-top:2px solid black'></td></tr>";
                                    }
                                    $backcol = ($colflag == 0)
                                        ? 'background-color:#F9F9F9'
                                        : 'background-color:rgba(131,238,255,0.5)';
                                    $colflag = 1 - $colflag;
                                    $tregnno = $regnno;
                                    $actr    = 1;
                                } else {
                                    $actr++;
                                }

                                // ── T/V/L ─────────────────────────────────────────────


                                // ── Gender ────────────────────────────────────────────
                                $gender = (left($pmain['cmbgender'], 1) == "M") ? tr('male') : tr('female');

                                if ($chkchildwob == 0) {
                                    $dispwob = '';

                                    // ── Makkah rooms ──────────────────────────────────
                                    $mkrm = '';
                                    if ($pmain['mksingle'] != 0) {
                                        $mkroom = $pmain['mksingle'];
                                        $mkrm   = tr('sgl') . ' - ' . $mkroom;
                                        if ($pmain['mk_singlepax'] > 0) $s1++;
                                    }
                                    if ($pmain['mkdouble'] > 0) {
                                        $mkroom = $pmain['mkdouble'];
                                        $mkrm   = ($mkrm != '') ? $mkrm . ' / ' . tr('dbl') . ' - ' . $mkroom : tr('dbl') . ' - ' . $mkroom;
                                        if ($pmain['mk_doublepax'] > 0) $s2++;
                                    }
                                    if ($pmain['mktriple'] != 0) {
                                        $mkroom = $pmain['mktriple'];
                                        $mkrm   = ($mkrm != '') ? $mkrm . ' / ' . tr('trp') . ' - ' . $mkroom : tr('trp') . ' - ' . $mkroom;
                                        if ($pmain['mk_triplepax'] > 0) $s3++;
                                    }
                                    if ($pmain['mkquad'] != 0) {
                                        $mkroom = $pmain['mkquad'];
                                        $mkrm   = ($mkrm != '') ? $mkrm . ' / ' . tr('quad') . ' - ' . $mkroom : tr('quad') . ' - ' . $mkroom;
                                        if ($pmain['mk_quadpax'] > 0) $s4++;
                                    }
                                    if ($pmain['mkquint'] != 0) {
                                        $mkroom = $pmain['mkquint'];
                                        $mkrm   = ($mkrm != '') ? $mkrm . ' / ' . tr('quint') . ' - ' . $mkroom : tr('quint') . ' - ' . $mkroom;
                                        if ($pmain['mk_quintpax'] > 0) $s5++;
                                    }
                                    if ($pmain['mksuite'] != 0) {
                                        $mkroom = $pmain['mksuite'];
                                        $mkrm   = ($mkrm != '') ? $mkrm . ' / ' . tr('suite') . ' - ' . $mkroom : tr('suite') . ' - ' . $mkroom;
                                        if ($pmain['mk_suitepax'] > 0) $s6++;
                                    }
                                    if ($pmain['mknobed'] != 0) {
                                        $mkrm = ($mkrm != '') ? $mkrm . ' / ' . tr('nobed') : tr('nobed');
                                        $s0++;
                                    }

                                    // ── Madina rooms ──────────────────────────────────
                                    $mdrm = '';
                                    if ($pmain['mdsingle'] != 0) {
                                        $mdroom = $pmain['mdsingle'];
                                        $mdrm   = tr('sgl') . ' - ' . $mdroom;
                                        $m1++;
                                    }
                                    if ($pmain['mddouble'] != 0) {
                                        $mdroom = $pmain['mddouble'];
                                        $mdrm   = ($mdrm != '') ? $mdrm . ' / ' . tr('dbl') . ' - ' . $mdroom : tr('dbl') . ' - ' . $mdroom;
                                        $m2++;
                                    }
                                    if ($pmain['mdtriple'] != 0) {
                                        $mdroom = $pmain['mdtriple'];
                                        $mdrm   = ($mdrm != '') ? $mdrm . ' / ' . tr('trp') . ' - ' . $mdroom : tr('trp') . ' - ' . $mdroom;
                                        $m3++;
                                    }
                                    if ($pmain['mdquad'] != 0) {
                                        $mdroom = $pmain['mdquad'];
                                        $mdrm   = ($mdrm != '') ? $mdrm . ' / ' . tr('quad') . ' - ' . $mdroom : tr('quad') . ' - ' . $mdroom;
                                        $m4++;
                                    }
                                    if ($pmain['mdquint'] != 0) {
                                        $mdroom = $pmain['mdquint'];
                                        $mdrm   = ($mdrm != '') ? $mdrm . ' / ' . tr('quint') . ' - ' . $mdroom : tr('quint') . ' - ' . $mdroom;
                                        $m5++;
                                    }
                                    if ($pmain['mdsuite'] != 0) {
                                        $mdroom = $pmain['mdsuite'];
                                        $mdrm   = ($mdrm != '') ? $mdrm . ' / ' . tr('suite') . ' - ' . $mdroom : tr('suite') . ' - ' . $mdroom;
                                        $m6++;
                                    }
                                    if ($pmain['mdnobed'] != 0) {
                                        $mdrm = ($mdrm != '') ? $mdrm . ' / ' . tr('nobed') : tr('nobed');
                                        $m0++;
                                    }

                                    // ── Other / Shifting rooms ────────────────────────
                                    $othrm = '';
                                    if ($pmain['othsingle'] != 0) {
                                        $othroom = $pmain['othsingle'];
                                        $othrm   = tr('sgl') . ' - ' . $othroom;
                                        $o1++;
                                    }
                                    if ($pmain['othdouble'] != 0) {
                                        $othroom = $pmain['othdouble'];
                                        $othrm   = ($othrm != '') ? $othrm . ' / ' . tr('dbl') . ' - ' . $othroom : tr('dbl') . ' - ' . $othroom;
                                        $o2++;
                                    }
                                    if ($pmain['othtriple'] != 0) {
                                        $othroom = $pmain['othtriple'];
                                        $othrm   = ($othrm != '') ? $othrm . ' / ' . tr('trp') . ' - ' . $othroom : tr('trp') . ' - ' . $othroom;
                                        $o3++;
                                    }
                                    if ($pmain['othquad'] != 0) {
                                        $othroom = $pmain['othquad'];
                                        $othrm   = ($othrm != '') ? $othrm . ' / ' . tr('quad') . ' - ' . $othroom : tr('quad') . ' - ' . $othroom;
                                        $o4++;
                                    }
                                    if ($pmain['othquint'] != 0) {
                                        $othroom = $pmain['othquint'];
                                        $othrm   = ($othrm != '') ? $othrm . ' / ' . tr('quint') . ' - ' . $othroom : tr('quint') . ' - ' . $othroom;
                                        $o5++;
                                    }
                                    if ($pmain['othsuite'] != 0) {
                                        $othroom = $pmain['othsuite'];
                                        $othrm   = ($othrm != '') ? $othrm . ' / ' . tr('suite') . ' - ' . $othroom : tr('suite') . ' - ' . $othroom;
                                        $o6++;
                                    }
                                    if ($pmain['othnobed'] != 0) {
                                        $othrm = ($othrm != '') ? $othrm . ' / ' . tr('nobed') : tr('nobed');
                                        $o0++;
                                    }
                                } else {
                                    $dispwob = tr('disp_wob');
                                    $wob++;
                                }

                                // ── Output row ────────────────────────────────────────
                                $pctr++;
                                echo "<tr data-href='bookform.php?mode=edit&vouchno=$voucher' class='clickable-row2' style='cursor:pointer;$backcol;'>";

                                echo ($srno > 0) ? "<td $ltr>" . num($enursrno) . "</td>" : "<td></td>";
                                echo "<td $ltr>" . num($regnno) . "</td>";

                                if ($pmain['cancdate'] != "0000-00-00") {
                                    echo "<td $ltr>$partynmvar</td>";
                                } else {
                                    if ($paxcount == 1) {
                                        echo "<td $ltr><b style='color:blue !important;'>$partynmvar</b></td>";
                                    } else {
                                        $curdate = date("Y-m-d");
                                        $infmon  = date('Y-m-d', strtotime($dob . ' + 24 month'));
                                        if ($infmon > $curdate) {
                                            echo "<td $ltr><b style='color:red !important;'>$partynmvar</b></td>";
                                            $inf++;
                                        } else {
                                            echo "<td $ltr>$partynmvar</td>";
                                        }
                                    }
                                }

                                echo ($dispwob != '') ? "<td style='color:red'>$dispwob</td>" : "<td></td>";
                                echo "<td $ltr>$groupDisplayenur</td>";
                                echo "<td $ltr>$tvl</td>";
                                echo "<td $ltr>$gender</td>";
                                echo "<td $ltr>$passDisplay</td>";
                                echo "<td $ltr>$craftDisplay</td>";

                                if ($actr == 1) {
                                    echo "<td $ltr>$agentDisplay</td>";
                                    echo "<td $ltr>$mkrm</td>";
                                    echo "<td $ltr>$mdrm</td>";
                                    echo "<td $ltr>$othrm</td>";
                                    echo "<td $ltr>$telDisplay</td>";
                                    echo "</tr>";
                                } else {
                                    if ($actr == 2) {
                                        echo "<td colspan='4' $ltr>$rem1Display</td><td $ltr>$mobDisplay</td>";
                                    }
                                    if ($actr == 3) {
                                        echo "<td colspan='4' $ltr>$rem2Display</td><td $ltr>$mobile2</td>";
                                    }
                                    if ($actr == 4) {
                                        echo "<td colspan='4' $ltr>$rem3Display</td><td></td>";
                                    }
                                    echo "</tr>";
                                }
                            }

                            // ── Save previous row values for remainder rows ────────────
                            $prem1    = $rem1Display ?? $rem1;
                            $prem2    = $rem2Display ?? $rem2;
                            $prem3    = $rem3Display ?? $rem3;
                            $pmobile  = $mobDisplay;
                            $pmobile2 = $mobile2;
                        }

                        // ── Final row flush ───────────────────────────────────────────
                        if ($actr >= 1) {
                            echo "<tr><td colspan='9'></td><td colspan='3' $ltr>$prem1</td><td $ltr>$pmobile</td></tr>";
                        }
                        if ($actr >= 2) {
                            echo "<tr><td colspan='9'></td><td colspan='3' $ltr>$prem2</td><td $ltr>$pmobile2</td></tr>";
                        }
                        if ($actr >= 3) {
                            echo "<tr><td colspan='9'></td><td colspan='3' $ltr>$prem3</td><td></td></tr>";
                        }
                        echo "<tr><td colspan='14'><hr style='margin-top:-2px;margin-bottom:-6px;border-top:2px solid black'></td></tr>";
                        ?>
                    </tbody>
                </table>

                <!-- ── Summary Table ── -->
                <table style='width:60%' class="table table-condensed table-striped table-hover table-bordered summ">
                    <tr style="background-color:#2EBFFE">
                        <?= ($language == 'ur') ? 'کل مسافر' : 'Total No. Of Pax' ?> -<?= num($getno2 - $inf) ?>
                        &nbsp;&nbsp;&nbsp;
                        <?= ($language == 'ur') ? 'بچہ' : 'Child - wob' ?> - <?= num($wob) ?>
                        &nbsp;&nbsp;&nbsp;
                        <?= ($language == 'ur') ? 'انفینٹ' : 'Infant' ?> - <?= num($inf) ?>
                    </tr>
                    <?php
                    $getpmain3 = mysqli_query($conn, "SELECT sum(mk_singlepax) as mkp1, sum(mk_doublepax) as mkp2, sum(mk_triplepax) as mkp3,
                                                          sum(mk_quadpax) as mkp4, sum(mk_quintpax) as mkp5, sum(mk_suitepax) as mkp6,
                                                          sum(md_singlepax) as mdp1, sum(md_doublepax) as mdp2, sum(md_triplepax) as mdp3,
                                                          sum(md_quadpax) as mdp4, sum(md_quintpax) as mdp5, sum(md_suitepax) as mdp6,
                                                          sum(oth_singlepax) as othp1, sum(oth_doublepax) as othp2, sum(oth_triplepax) as othp3,
                                                          sum(oth_quadpax) as othp4, sum(oth_quintpax) as othp5, sum(oth_suitepax) as othp6
                                                          FROM `bookform` WHERE regnno != ''
                                                          $tourcond1 $regnocond1 $groupcond1 $classcond1 $citycond1 $statecond1 $canccond1
                                                          $depdatecond1 $deppnrcond1 $airlinecond1 $agentcond1 $arrvdatecond1 $arrvpnrcond1
                                                          and regnno in (select vregnno from visaform where vregnno != ''
                                                          $namecond1 $paxcond1 $sexcond1 $ppexpirycond1 $canccond1 $ticketchkcond1 $visachkcond1 $landchkcond1)
                                                          ") or die(mysqli_error($conn));
                    $fetpmain3 = mysqli_fetch_array($getpmain3);

                    $s1 = $fetpmain3['mkp1'];
                    $s2 = $fetpmain3['mkp2'];
                    $s3 = $fetpmain3['mkp3'];
                    $s4 = $fetpmain3['mkp4'];
                    $s5 = $fetpmain3['mkp5'];
                    $s6 = $fetpmain3['mkp6'];
                    $m1 = $fetpmain3['mdp1'];
                    $m2 = $fetpmain3['mdp2'];
                    $m3 = $fetpmain3['mdp3'];
                    $m4 = $fetpmain3['mdp4'];
                    $m5 = $fetpmain3['mdp5'];
                    $m6 = $fetpmain3['mdp6'];
                    $o1 = $fetpmain3['othp1'];
                    $o2 = $fetpmain3['othp2'];
                    $o3 = $fetpmain3['othp3'];
                    $o4 = $fetpmain3['othp4'];
                    $o5 = $fetpmain3['othp5'];
                    $o6 = $fetpmain3['othp6'];

                    $sr1 = $s1;
                    $sr2 = round($s2 / 2, 2);
                    $sr3 = round($s3 / 3, 2);
                    $sr4 = round($s4 / 4, 2);
                    $sr5 = round($s5 / 5, 2);
                    $sr6 = round($s6 / 6, 2);
                    $sr0 = $s0;
                    $mr1 = $m1;
                    $mr2 = round($m2 / 2, 2);
                    $mr3 = round($m3 / 3, 2);
                    $mr4 = round($m4 / 4, 2);
                    $mr5 = round($m5 / 5, 2);
                    $mr6 = round($m6 / 6, 2);
                    $mr0 = $m0;
                    $or1 = $o1;
                    $or2 = round($o2 / 2, 2);
                    $or3 = round($o3 / 3, 2);
                    $or4 = round($o4 / 4, 2);
                    $or5 = round($o5 / 5, 2);
                    $or6 = round($o6 / 6, 2);
                    $or0 = $o0;

                    $srtot  = $sr1 + $sr2 + $sr3 + $sr4 + $sr5 + $sr6;
                    $mrtot  = $mr1 + $mr2 + $mr3 + $mr4 + $mr5 + $mr6;
                    $othtot = $or1 + $or2 + $or3 + $or4 + $or5 + $or6;
                    ?>
                    <tr style="background-color:#2EBFFE;font-size:12px;">
                        <th class="text-center"></th>
                        <th class="text-center"><?= ($language == 'ur') ? 'سنگل' : 'SINGLE' ?></th>
                        <th class="text-center"><?= ($language == 'ur') ? 'ڈبل' : 'DOUBLE' ?></th>
                        <th class="text-center"><?= ($language == 'ur') ? 'ٹرپل' : 'TRIPLE' ?></th>
                        <th class="text-center"><?= ($language == 'ur') ? 'کواڈ' : 'QUAD' ?></th>
                        <th class="text-center"><?= ($language == 'ur') ? 'کوئنٹ' : 'QUINT' ?></th>
                        <th class="text-center"><?= ($language == 'ur') ? 'سویٹ' : 'SUITE' ?></th>
                        <th class="text-center"><?= ($language == 'ur') ? 'بغیر بیڈ' : 'NO BED' ?></th>
                        <th class="text-center"><?= ($language == 'ur') ? 'کل' : 'TOTAL' ?></th>
                    </tr>
                    <tr style="background-color:#F7F3B5;color:black !important;">
                        <td class="text-center"><?= ($language == 'ur') ? 'مکہ' : 'MAKKAH' ?></td>
                        <td class="text-center"><?= num($sr1); ?></td>
                        <td class="text-center"><?= num($sr2); ?></td>
                        <td class="text-center"><?= num($sr3); ?></td>
                        <td class="text-center"><?= num($sr4); ?></td>
                        <td class="text-center"><?= num($sr5); ?></td>
                        <td class="text-center"><?= num($sr6); ?></td>
                        <td class="text-center"><?= num($sr0); ?></td>
                        <td class="text-center"><?= num($srtot); ?></td>
                    </tr>
                    <tr style="background-color:lightgreen;color:black !important;">
                        <td class="text-center"><?= ($language == 'ur') ? 'مدینہ' : 'MADINA' ?></td>
                        <td class="text-center"><?= num($mr1); ?></td>
                        <td class="text-center"><?= num($mr2); ?></td>
                        <td class="text-center"><?= num($mr3); ?></td>
                        <td class="text-center"><?= num($mr4); ?></td>
                        <td class="text-center"><?= num($mr5); ?></td>
                        <td class="text-center"><?= num($mr6); ?></td>
                        <td class="text-center"><?= num($mr0); ?></td>
                        <td class="text-center"><?= num($mrtot); ?></td>
                    </tr>
                    <tr style="background-color:#6dbfb8;color:black !important;">
                        <td class="text-center"><?= ($language == 'ur') ? 'دیگر / شفٹنگ' : 'OTHER/SHIFTING' ?></td>
                        <td class="text-center"><?= num($or1); ?></td>
                        <td class="text-center"><?= num($or2); ?></td>
                        <td class="text-center"><?= num($or3); ?></td>
                        <td class="text-center"><?= num($or4); ?></td>
                        <td class="text-center"><?= num($or5); ?></td>
                        <td class="text-center"><?= num($or6); ?></td>
                        <td class="text-center"><?= num($or0); ?></td>
                        <td class="text-center"><?= num($othtot); ?></td>
                    </tr>
                </table>
            <?php } ?>
        </div>
        <?php include("includes/linksfoot.php"); ?>
    </body>

    </html>
<?php
}

// ── EXCEL EXPORT ──────────────────────────────────────────────
if (isset($_POST["excel"]) || ($_GET["excel"])) {
    if ($getno2 > 0) {
        $contents  = "Sr., Regn., Name, Pax #, Sex, Passport, Craft, Agent, Makka, Room, Madina, Room, Group \n";
        $pctr      = 0;
        $s0 = $s1 = $s2 = $s3 = $s4 = $s5 = $s6 = 0;
        $m0 = $m1 = $m2 = $m3 = $m4 = $m5 = $m6 = 0;

        while ($pmain = mysqli_fetch_array($getpmain2)) {
            $dob     = $pmain['dob'];
            $proceed = 1;
            if ($infonly == 1) {
                $curdate = date("Y-m-d");
                $infmon  = date('Y-m-d', strtotime($dob . ' + 24 month'));
                $proceed = ($infmon > $curdate) ? 1 : 0;
            }
            if ($proceed > 0) {
                $ptyped  = ($pmain['paidby'] == "C") ? "CASH" : "CREDIT";
                $voucher = $pmain['vouchno'];
                $vregnno = $pmain['vregnno'];
                $paxno   = $pmain['paxno'];
                $partynm = trim($pmain['surname']) . ' ' . trim($pmain['lastname']) . ' ' . trim($pmain['midname']);
                $mobile  = $pmain['mobile'];
                $mkroom  = $pmain['mkroom'];
                $mdroom  = $pmain['mdroom'];
                $gender  = (left($pmain['cmbgender'], 1) == "M") ? "Male" : "Female";

                if ($pmain['mksingle'] != 0) {
                    $mkroom = $pmain['mksingle'];
                    $mk_rtype = 'SGL';
                    $s1++;
                }
                if ($pmain['mkdouble'] > 0) {
                    $mkroom = $pmain['mkdouble'];
                    $mk_rtype = 'DBL';
                    $s2++;
                }
                if ($pmain['mktriple'] != 0) {
                    $mkroom = $pmain['mktriple'];
                    $mk_rtype = 'TRP';
                    $s3++;
                }
                if ($pmain['mkquad']   != 0) {
                    $mkroom = $pmain['mkquad'];
                    $mk_rtype = 'QUAD';
                    $s4++;
                }
                if ($pmain['mkquint']  != 0) {
                    $mkroom = $pmain['mkquint'];
                    $mk_rtype = 'QUINT';
                    $s5++;
                }
                if ($pmain['mksuite']  != 0) {
                    $mkroom = $pmain['mksuite'];
                    $mk_rtype = 'SUITE';
                    $s6++;
                }
                if ($pmain['mknobed']  != 0) {
                    $mkroom = $pmain['mknobed'];
                    $mk_rtype = 'NOBED';
                    $s0++;
                }

                if ($pmain['mdsingle'] != 0) {
                    $mdroom = $pmain['mdsingle'];
                    $md_rtype = 'SGL';
                    $m1++;
                }
                if ($pmain['mddouble'] != 0) {
                    $mdroom = $pmain['mddouble'];
                    $md_rtype = 'DBL';
                    $m2++;
                }
                if ($pmain['mdtriple'] != 0) {
                    $mdroom = $pmain['mdtriple'];
                    $md_rtype = 'TRP';
                    $m3++;
                }
                if ($pmain['mdquad']   != 0) {
                    $mdroom = $pmain['mdquad'];
                    $md_rtype = 'QUAD';
                    $m4++;
                }
                if ($pmain['mdquint']  != 0) {
                    $mdroom = $pmain['mdquint'];
                    $md_rtype = 'QUINT';
                    $m5++;
                }
                if ($pmain['mdsuite']  != 0) {
                    $mdroom = $pmain['mdsuite'];
                    $md_rtype = 'SUITE';
                    $m6++;
                }
                if ($pmain['mdnobed']  != 0) {
                    $mdroom = $pmain['mdnobed'];
                    $md_rtype = 'NOBED';
                    $m0++;
                }

                $paxqry   = mysqli_query($conn, "SELECT vouchno FROM `visaform` WHERE `vregnno` = '$regnno' ") or die(mysqli_error($conn));
                $paxcount = mysqli_num_rows($paxqry);

                $pctr++;
                $contents .= "$pctr,$vregnno,$partynm,$paxno,$gender,{$pmain['redypass']},{$pmain['craft']},{$pmain['agent']},$mk_rtype,$mkroom,$md_rtype,$mdroom,{$pmain['groupno']}\n";
            }
        }

        $sr1 = $s1;
        $sr2 = round($s2 / 2, 2);
        $sr3 = round($s3 / 3, 2);
        $sr4 = round($s4 / 4, 2);
        $sr5 = round($s5 / 5, 2);
        $sr6 = round($s6 / 6, 2);
        $sr0 = $s0;
        $mr1 = $m1;
        $mr2 = round($m2 / 2, 2);
        $mr3 = round($m3 / 3, 2);
        $mr4 = round($m4 / 4, 2);
        $mr5 = round($m5 / 5, 2);
        $mr6 = round($m6 / 6, 2);
        $mr0 = $m0;
        $srtot = $sr1 + $sr2 + $sr3 + $sr4 + $sr5 + $sr6;
        $mrtot = $mr1 + $mr2 + $mr3 + $mr4 + $mr5 + $mr6;

        $contents .= "Total No. Of Pax - $pctr\n";
        $contents .= ",SINGLE,DOUBLE,TRIPLE,QUAD,QUINT,SUITE,NO BED,TOTAL\n";
        $contents .= "MAKKAH,$sr1,$sr2,$sr3,$sr4,$sr5,$sr6,$sr0,$srtot\n";
        $contents .= "MADINA,$mr1,$mr2,$mr3,$mr4,$mr5,$mr6,$mr0,$mrtot\n";
    }

    $contents = strip_tags($contents);
    $fname = ($_POST["dtype"] == 1 || $_POST["dtype"] == 3) ? "_Summary" : "_Detail";
    header("Content-Disposition: attachment;filename=$formname{$fname}" . date('d-m-Y') . ".csv");
    print $contents;
}

// ── GEN VIEW ──────────────────────────────────────────────────
if (isset($_POST["genview"])) {
    $j = 0;
    $i = 0;
    while ($pmain = mysqli_fetch_array($getpmain2)) {
        $dob    = $pmain['dob'];
        $paxno  = $pmain['paxno'];
        $set    = 0;

        if ($pmain['cancdate'] != "0000-00-00") {
        } else {
            if ($dob != "0000-00-00") {
                $curdate = date("Y-m-d");
                $infmon  = date('Y-m-d', strtotime($dob . ' + 24 month'));
                if ($infmon > $curdate) {
                    $set = 0;
                    $inf++;
                } else {
                    $i++;
                    $set = 1;
                }
            }
        }
        if ($set > 0) {
            mysqli_query($conn, "UPDATE visaform SET srno = '$i' WHERE vouchno = '$paxno' ") or die(mysqli_error($conn));
        } else {
            mysqli_query($conn, "UPDATE visaform SET srno = 0 WHERE vouchno = '$paxno' ") or die(mysqli_error($conn));
        }
    }
    browse("Processing Complete");
    echo "<script>window.close()</script>";
}

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

    if ($getno2 > 0) {
        $contents .= "Sr., Regn., Name, Pax #, Sex, Passport, Craft, Agent, Makka, Room, Madina, Room, Group \n";

        $pctr = 0;
        $pchtypecond = "";
        $s1 = $s2 = $s3 = $s4 = $s5 = $s6 = $s0;
        $m1 = $m2 = $m3 = $m4 = $m5 = $m6 = $m0;

        while ($pmain = mysqli_fetch_array($getpmain2)) {
            $dob = $pmain['dob'];

            $proceed = 1;
            if ($infonly == 1) {
                $curdate = date("Y-m-d");
                $infmon = date('Y-m-d', strtotime($dob . ' + 24 month'));
                if ($infmon > $curdate) {
                    $proceed = 1;
                } else {
                    $proceed = 0;
                }
            }
            if ($proceed > 0) {
                if ($pmain['paidby'] == "C") {
                    $ptyped = "CASH";
                } else {
                    $ptyped = "CREDIT";
                }

                $voucher = $pmain['vouchno'];
                $vregnno = $pmain['vregnno'];
                $paxno = $pmain['paxno'];

                $partynm = trim($pmain['surname']) . ' ' . trim($pmain['lastname']) . ' ' . trim($pmain['midname']);
                $invno = $pmain['invno'];
                $totalqty = $pmain['totalqty'];
                $mydt = $pmain['mydt'];
                $mobile = $pmain['mobile'];
                $mkroom = $pmain['mkroom'];
                $mdroom = $pmain['mdroom'];

                $gender = '';
                if (left($pmain['cmbgender'], 1) == "M") {
                    $gender = "Male";
                } else {
                    $gender = "Female";
                }

                if ($pmain['mksingle'] != 0) {
                    $mkroom = $pmain['mksingle'];
                    $mk_rtype = 'SGL';
                    $s1++;
                }
                if ($pmain['mkdouble'] > 0) {
                    $mkroom = $pmain['mkdouble'];
                    $mk_rtype = 'DBL';
                    $s2++;
                }
                if ($pmain['mktriple'] != 0) {
                    $mkroom = $pmain['mktriple'];
                    $mk_rtype = 'TRP';
                    $s3++;
                }
                if ($pmain['mkquot'] != 0) {
                    $mkroom = $pmain['mkquot'];
                    $mk_rtype = 'QUAD';
                    $s4++;
                }
                if ($pmain['mkquint'] != 0) {
                    $mkroom = $pmain['mkquint'];
                    $mk_rtype = 'QUINT';
                    $s5++;
                }
                if ($pmain['mksuite'] != 0) {
                    $mkroom = $pmain['mksuite'];
                    $mk_rtype = 'SUITE';
                    $s6++;
                }
                if ($pmain['mknobed'] != 0) {
                    $mkroom = $pmain['mknobed'];
                    $mk_rtype = 'NOBED';
                    $s0++;
                }

                if ($pmain['mdsingle'] != 0) {
                    $mdroom = $pmain['mdsingle'];
                    $md_rtype = 'SGL';
                    $m1++;
                }
                if ($pmain['mddouble'] != 0) {
                    $mdroom = $pmain['mddouble'];
                    $md_rtype = 'DBL';
                    $m2++;
                }
                if ($pmain['mdtriple'] != 0) {
                    $mdroom = $pmain['mdtriple'];
                    $md_rtype = 'TRP';
                    $m3++;
                }
                if ($pmain['mdquot'] != 0) {
                    $mdroom = $pmain['mdquot'];
                    $md_rtype = 'QUAD';
                    $m4++;
                }
                if ($pmain['mdquint'] != 0) {
                    $mdroom = $pmain['mdquint'];
                    $md_rtype = 'QUINT';
                    $m5++;
                }
                if ($pmain['mdsuite'] != 0) {
                    $mdroom = $pmain['mdsuite'];
                    $md_rtype = 'SUITE';
                    $m6++;
                }
                if ($pmain['mdnobed'] != 0) {
                    $mdroom = $pmain['mdnobed'];
                    $md_rtype = 'NOBED';
                    $m0++;
                }

                $paxcount = 0;
                $paxqry = mysqli_query($conn, "SELECT vouchno FROM `visaform` WHERE `vregnno` = '$regnno' ") or die(mysqli_error($conn));
                $paxcount = mysqli_num_rows($paxqry);

                $pctr++;
                $contents .= "$pctr" . ",";
                $contents .= "$pmain[vregnno]" . ",";

                if ($pmain['cancdate'] != "0000-00-00") {
                    $contents .= "$partynm" . ",";
                } else {
                    if ($paxcount == 1) {
                        $contents .= "$partynm" . ",";
                    } else {
                        $curdate = date("Y-m-d");
                        $infmon = date('Y-m-d', strtotime($dob . ' + 24 month'));

                        if ($infmon > $curdate) {
                            $contents .= "$partynm" . ",";
                        } else {
                            $contents .= "$partynm" . ",";
                        }
                    }
                }

                $contents .= "$paxno" . ",";
                $contents .= "$gender" . ",";
                $contents .= "$pmain[redypass]" . ",";
                $contents .= "$pmain[craft]" . ",";
                $contents .= "$pmain[agent]" . ",";
                $contents .= "$mk_rtype" . ",";
                $contents .= "$mkroom" . ",";
                $contents .= "$md_roomtype" . ",";
                $contents .= "$mdroom" . ",";
                $contents .= "$pmain[group]" . "\n";
            }
        }
        $stot = $s1 + $s2 + $s3 + $s4 + $s5 + $s6;
        $mtot = $m1 + $m2 + $m3 + $m4 + $m5 + $m6;

        $sr1 = $s1;
        $sr2 = round($s2 / 2, 2);
        $sr3 = round($s3 / 3, 2);
        $sr4 = round($s4 / 4, 2);
        $sr5 = round($s5 / 5, 2);
        $sr6 = round($s6 / 6, 2);
        $sr0 = $s0;

        $mr1 = $m1;
        $mr2 = round($m2 / 2, 2);
        $mr3 = round($m3 / 3, 2);
        $mr4 = round($m4 / 4, 2);
        $mr5 = round($m5 / 5, 2);
        $mr6 = round($m6 / 6, 2);
        $mr0 = $m0;

        $srtot = $sr1 + $sr2 + $sr3 + $sr4 + $sr5 + $sr6;
        $mrtot = $mr1 + $mr2 + $mr3 + $mr4 + $mr5 + $mr6;

        $contents .= "Total No. Of Pax - $stot \n";

        $contents .= " , SINGLE, DOUBLE, TRIPLE, QUAD, QUINT, SUITE, NO BED, TOTAL \n";

        $contents .= "MAKKAH" . ",";
        $contents .= "$sr1" . ",";
        $contents .= "$sr2" . ",";
        $contents .= "$sr3" . ",";
        $contents .= "$sr4" . ",";
        $contents .= "$sr5" . ",";
        $contents .= "$sr6" . ",";
        $contents .= "$sr0" . ",";
        $contents .= "$srtot" . "\n";

        $contents .= "MADINA" . ",";
        $contents .= "$mr1" . ",";
        $contents .= "$mr2" . ",";
        $contents .= "$mr3" . ",";
        $contents .= "$mr4" . ",";
        $contents .= "$mr5" . ",";
        $contents .= "$mr6" . ",";
        $contents .= "$mr0" . ",";
        $contents .= "$mrtot" . "\n";
    }

    $contents = strip_tags($contents);

    if ($_POST["dtype"] == 1 || $_POST["dtype"] == 3) {
        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["genview"])) {
    $j = 0;
    $i = 0;
    while ($pmain = mysqli_fetch_array($getpmain2)) {
        $dob = $pmain['dob'];
        $depdate = $pmain['depdate'];
        $paxno = $pmain['paxno'];
        $set = 0;

        if ($pmain['cancdate'] != "0000-00-00") {
        } else {
            if ($dob != "0000-00-00") {
                $curdate = date("Y-m-d");
                $infmon = date('Y-m-d', strtotime($dob . ' + 24 month'));

                if ($infmon > $curdate) {
                    $set = 0;
                    $inf++;
                } else {
                    $i = $i + 1;
                    $set = 1;
                }
            }
        }
        if ($set > 0) {
            mysqli_query($conn, "update visaform set srno = '$i' where vouchno = '$paxno' ") or die(mysqli_error($conn));
        } else {
            mysqli_query($conn, "update visaform set srno = 0 where vouchno = '$paxno' ") or die(mysqli_error($conn));
        }
    }

    browse("Processing Complete");
    echo "<script>window.close()</script>";
}
?>