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

//$zseries = "$_GET[series]";
//browse($zseries);
$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);

//$zroot = $_SESSION["zdbroot"];

$success = false;


$ztit = $_SESSION["tit_" . $dbname];

$maindb = 'visamain';
$trandb = 'visatran';
$trandb2 = 'category';

$file = 'entry.php';

$keyfld = "myvouch";

$series = $_REQUEST["series"];
$rangeFrom = $_REQUEST["rangeFrom"];
$rangeTo = $_REQUEST["rangeTo"];
$searchTerm = $_REQUEST["ssearch"];
$operator = $_REQUEST["soperator"];



$cond = "";
if ($operator == "=") {
    if ($_REQUEST["sfield"] == "1") {
        $cond = "AND a.`vouchno` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "2") {
        $cond = "AND a.`visano` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "3") {
        $cond = "AND b.`passport` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "4") {
        $cond = "AND a.`sponsor` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "5") {
        $cond = "AND a.`broker` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "6") {
        $cond = "AND a.`sector` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "7") {
        $cond = "AND a.`manualno` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "8") {
        $cond = "AND b.`paxname` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "9") {
        $cond = "AND a.`idno` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "10") {
        $cond = "AND a.`visaqty` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "11") {
        $cond = "AND b.`cateng` = '$searchTerm'";
    }
} else {

    if ($_REQUEST["sfield"] == "1") {
        $cond = "AND a.`vouchno` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "2") {
        $cond = "AND a.`visano` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "3") {
        $cond = "AND b.`passport` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "4") {
        $cond = "AND a.`sponsor` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "5") {
        $cond = "AND a.`broker` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "6") {
        $cond = "AND a.`sector` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "7") {
        $cond = "AND a.`manualno` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "8") {
        $cond = "AND b.`paxname` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "9") {
        $cond = "AND a.`idno` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "10") {
        $cond = "AND a.`visaqty` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "11") {
        $cond = "AND b.`cateng` LIKE '%$searchTerm%'";
    }
}


$sortcond = "ORDER BY a.`vouchdt` DESC,a.manualno,b.cateng,a.broker,b.paxname";
if ($_REQUEST["sortBy"] == "1") {
    $sortcond = " ORDER BY a.`vouchno`";
} else if ($_REQUEST["sortBy"] == "2") {
    $sortcond = " ORDER BY a.`visano`";
} else if ($_REQUEST["sortBy"] == "3") {
    $sortcond = " ORDER BY b.`passport`";
} else if ($_REQUEST["sortBy"] == "4") {
    $sortcond = " ORDER BY a.`sponsor`";
} else if ($_REQUEST["sortBy"] == "5") {
    $sortcond = " ORDER BY a.`broker`";
} else if ($_REQUEST["sortBy"] == "6") {
    $sortcond = " ORDER BY a.`sector`";
} else if ($_REQUEST["sortBy"] == "7") {
    $sortcond = " ORDER BY a.`manualno`";
} else if ($_REQUEST["sortBy"] == "8") {
    $sortcond = " ORDER BY b.`paxname`";
} else if ($_REQUEST["sortBy"] == "9") {
    $sortcond = " ORDER BY a.`idno`";
} else if ($_REQUEST["sortBy"] == "10") {
    $sortcond = " ORDER BY a.`visaqty`";
} else if ($_REQUEST["sortBy"] == "11") {
    $sortcond = " ORDER BY b.`cateng`";
}


$ordcond = "DESC";
if ($_REQUEST["orderAs"] == "desc") {
    $ordcond = " DESC";
} else {
    $ordcond = " ASC";
}



//$query = "SELECT a.vouchno, date_format(a.`vouchdt`,'%d-%m-%Y') as mydt, a.myvouch, a.partyname, a.sponsor, a.broker, a.visatype, a.manualno, b.passport 
//           FROM `$maindb` a, `$trandb` b where a.myvouch=b.myvouch $cond $sortcond $ordcond  LIMIT $rangeFrom, $rangeTo";
$query = "SELECT a.vouchno, date_format(a.`vouchdt`,'%d-%m-%Y') as mydt, a.myvouch, a.sector, b.paxname, a.sponsor, a.broker, a.visatype, a.manualno, a.visano,a.idno,a.visaqty,
          b.passport,b.cateng FROM `$maindb` a LEFT JOIN `$trandb` b 
          ON a.myvouch = b.myvouch where a.`myvouch` !=''
          $cond $sortcond $ordcond  LIMIT $rangeFrom, $rangeTo";
$result = mysqli_query($conn, $query) or die(mysqli_error($conn));

$rows = array();
$ctr = 0;
while ($r = mysqli_fetch_assoc($result)) {

    if ($r['sector'] == "New DELHI"){
        $colr = "style='color:red'";
    }else{
        $colr = "";
    }

    $rows[] = ['success' => true,
        'value' => $ctr,
        'vouchno' => $r['vouchno'],
        'myvouch' => $r['myvouch'],
        'vouchdt' => $r['mydt'],
        'partyname' => $r['paxname'],
        'sector' => $r['sector'],
        'passport' => $r['passport'],
        'sponsor' => $r['sponsor'],
        'broker' => $r['broker'],
        'visatype' => $r['visatype'],
        'manualno' => $r['manualno'],
        'visano' => $r['visano'],
        'visaqty' => $r['visaqty'],
        'idno' => $r['idno'],
        'cateng' => $r['cateng'],
        'colr' => $colr,
        'action' => "$file?mode=edit&myvouch=$r[myvouch]",
    ];
    $ctr++;
}
//browse($rows);
header("Content-type: application\json");
echo json_encode($rows);
