<?php

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

$maindb = "visamain";
$trandb = "visatran";
$trandb1 = "category";
$formname = "Backup";
$file = "backup_excel.php";
// $mgrpcode = "010";
$entfile = "entry.php";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);
//mysqli_set_charset($conn,"utf8");
//echo "Current character set is: " . mysqli_character_set_name($conn);
$getpmain = mysqli_query($conn, "SELECT a.*,a.category as catmain, b.*,b.category as cattran FROM `visamain` a ,visatran  b WHERE a.myvouch=b.myvouch ORDER BY `a`.`myvouch` ASC")or die(mysqli_error($conn));
//print table($getpmain);
//die;
//die();
$contents .= "Sr., Work , Place, File Date ,File No., Party, Visa No., Id no., Category, Sponsor, Visa Qty, Email, Paid By, FE ID, FE Password, Hijri, Passport,"
        . " Pax Name, Emiigration Status, Stamp Date, Mofa No.1, Mofa Date, Mofa No.2, Candidate Mobile, Remark, Flights Detail, DOB,"
        . " Religion, POB, POI, Stay Period, PP DOI, PP DOE, Submission Date, Category, Passport Delivery, DL, MED, Exit, Saudi Party, Saudi Sector, Salary,"
        . " Sex, Passport Received \n";
mysqli_select_db($conn, $zdb);

    $ctr = 0;
while ($pmain = mysqli_fetch_assoc($getpmain)) {
    $work = $pmain['visatype'];
    $place = str_replace("," , " " , $pmain['sector']);
    $vouchdt = $pmain['vouchdt'];
    $manualno = $pmain['manualno'];
    $party = str_replace("," , " " , $pmain['broker']);
    $visano = $pmain['visano'];
    $idno = $pmain['idno'];
    $catmain = str_replace("," , " " , $pmain['catgmain']);
    $sponsor = str_replace("," , " " , $pmain['sponsor']);
    $visaqty = $pmain['visaqty'];
    $email = str_replace("," , " " , $pmain['email']);
    $paidby = $pmain['paidby'];
    $feid = $pmain['feid'];
    $fepass = $pmain['fepass'];
    $hijridate = $pmain['hijridate'];
    $passport = $pmain['passport'];
    $paxname = str_replace("," , " " , $pmain['paxname']);
    $passrecd = $pmain['passrecd'];
    $issplace = $pmain['issplace'];
    $issdate = $pmain['issdate'];
    $stampdate = $pmain['stampdate'];
    $carrier = $pmain['carrier'];
    $fltno = $pmain['fltno'];
    $fltdate = $pmain['fltdate'];
    $flttime = $pmain['flttime'];
    $pnrno = $pmain['pnrno'];
    $mofano = $pmain['mofano'];
    $mofa_date = $pmain['mofa_date'];
    $mofano2 = $pmain['mofano2'];
    $arndate = $pmain['arndate'];
    $arn = $pmain['arn'];
    $jobdesig = $pmain['jobdesig'];
    $imigdate = $pmain['imigdate'];
    $imigstatus = $pmain['imigstatus'];
    $cattran = $pmain['cattran'];
    $passdeliver = $pmain['passdeliver'];
    $remtran = $pmain['remtran'];
    $dl = $pmain['dl'];
    $med = $pmain['med'];
    $exit = $pmain['exit'];
    $sdparty = $pmain['sdparty'];
    $sdsector = $pmain['sdsector'];
    $salary = $pmain['salary'];
    $canmobile = $pmain['canmobile'];
    $fltdtl = $pmain['fltdtl'];
    $jobid = $pmain['jobid'];
    $tfile = $pmain['tfile'];
    $plbirth = $pmain['plbirth'];
    $sex = $pmain['sex'];
    $dob = $pmain['dob'];
    $religion = $pmain['religion'];
    $doe = $pmain['doe'];
    $submission_date = $pmain['submission_date'];
//    $mofa_date = $pmain['mofa_date'];
    $periodstay = $pmain['periodstay'];
    $delvstat = $pmain['delvstatus'];
    
    
    
    if($delvstat == "N"){
        $delvstatus = "ECNR";
    }
    elseif($delvstat == "E"){
        $delvstatus = "ECR";
    }
    if($delvstat == ""){
        $delvstatus = "";
    }



    $ctr++;
    $contents .= $ctr . ",";
    $contents .= "$work" . ",";
    $contents .= "$place" . ",";
    $contents .= "$vouchdt" . ",";
    $contents .= "$manualno" . ",";
    $contents .= "$party" . ",";
    $contents .= "$visano" . ",";
    $contents .= "$idno" . ",";
    $contents .= "$catmain" . ",";
    $contents .= "$sponsor" . ",";
    $contents .= "$visaqty" . ",";
    $contents .= "$email" . ",";
    $contents .= "$paidby" . ",";
    $contents .= "$feid" . ",";
    $contents .= "$fepass" . ",";
    $contents .= "$hijridate" . ",";
    $contents .= "$passport" . ",";
    $contents .= "$paxname" . ",";
    $contents .= "$delvstatus" . ",";
    $contents .= "$stampdate" . ",";
    $contents .= "$mofano" . ",";
    $contents .= "$mofa_date" . ",";
    $contents .= "$mofano2" . ",";
    $contents .= "$canmobile" . ",";
    $contents .= "$remtran" . ",";
    $contents .= "$fltdtl" . ",";
    $contents .= "$dob" . ",";
    $contents .= "$religion" . ",";
    $contents .= "$plbirth" . ",";
    $contents .= "$issplace" . ",";
    $contents .= "$periodstay" . ",";
    $contents .= "$issdate" . ",";
    $contents .= "$doe" . ",";
    $contents .= "$submission_date" . ",";
    $contents .= "$category" . ",";
    $contents .= "$passdeliver" . ",";
    $contents .= "$dl" . ",";
    $contents .= "$med" . ",";
    $contents .= "$exit" . ",";
    $contents .= "$sdparty" . ",";
    $contents .= "$sdsector" . ",";
    $contents .= "$salary" . ",";
    $contents .= "$sex" . ",";
    $contents .= "$passrecd" . "\n";
}



$contents = strip_tags($contents);
header('Content-Type: application/vnd.ms-excel');
header("Content-Disposition: attachment;filename=$formname" . "_Detail" . date('d-m-Y') . ".csv");
print chr(255) . chr(254).mb_convert_encoding($contents, 'UTF-16LE', 'UTF-8');
