<?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"];
$searchTerm2 = $_REQUEST["ssearch2"];
$operator = $_REQUEST["soperator"];

$cond = "";

if ($searchTerm) {
//    $cond = "AND (a.`manualno` LIKE '%$searchTerm%' OR a.`visano` LIKE '%$searchTerm%' OR b.`mofano` LIKE '%$searchTerm%')";
    $cond = "AND CONCAT(a.manualno, a.visano, b.mofano) LIKE '%$searchTerm%'";
}

if ($searchTerm2) {
//    $cond = "AND (a.`manualno` LIKE '%$searchTerm%' OR a.`visano` LIKE '%$searchTerm%' OR b.`mofano` LIKE '%$searchTerm%')";
    $cond = "AND CONCAT(a.manualno, a.visano, b.mofano) LIKE '%$searchTerm2%'";
}

//browse($cond);


$sortcond = "ORDER BY a.`vouchdt`";


$ordcond = "DESC";

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

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

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