<?php

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

$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);
$admin = $_SESSION["admin_" . $dbname];
$fsociety = $_SESSION["society_" . $dbname];
$success = false;
$maindb = 'hotelpmain';

// $fseries = $_SESSION["series_" . $dbname];
// $keycond = " and `series`='$fseries'";

$file = "hotelpurch.php";

$keyfld = "vouchno";
$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.`vouchdt` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "3") {
        $cond = "AND d.`cityname` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "4") {
        $cond = "AND b.`hotelname` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "5") {
        $cond = "AND c.`categname` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "6") {
        $cond = "AND e.`partyname` = '$searchTerm'";
    } 
} else {
    if ($_REQUEST["sfield"] == "1") {
        $cond = "AND a.`vouchno` LIKE '%$searchTerm%'";
    }  else if ($_REQUEST["sfield"] == "2") {
        $cond = "AND a.`vouchdt` LIKE '%$searchTerm%'";
    }  else if ($_REQUEST["sfield"] == "3") {
        $cond = "AND d.`cityname` LIKE '%$searchTerm%'";
    }  else if ($_REQUEST["sfield"] == "4") {
        $cond = "AND b.`hotelname` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "5") {
        $cond = "AND c.`categname` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "6") {
        $cond = "AND e.`partyname` LIKE '%$searchTerm%'";
    } 
}

$sortcond = "ORDER BY a.`vouchno`";
if ($_REQUEST["sortBy"] == "1") {
    $sortcond = " ORDER BY a.`vouchno`";
}  else if ($_REQUEST["sortBy"] == "2") {
    $sortcond = " ORDER BY a.`vouchdt`";
}  else if ($_REQUEST["sortBy"] == "3") {
    $sortcond = " ORDER BY d.`cityname`";
}  else if ($_REQUEST["sortBy"] == "4") {
    $sortcond = " ORDER BY b.`hotelname`";
} else if ($_REQUEST["sortBy"] == "5") {
    $sortcond = " ORDER BY c.`categname`";
} else if ($_REQUEST["sortBy"] == "6") {
    $sortcond = " ORDER BY e.`partyname`";
}
 
$ordcond = "DESC";
if ($_REQUEST["orderAs"] == "desc") {
    $ordcond = " DESC";
} else {
    $ordcond = " ASC";
}

$query = "SELECT a.*,DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y')as vouchdt, b.code , b.hotelnm as hotelname, c.code, c.name as categname, d.code, d.name as cityname
                FROM $maindb a, hotels b , hotelcatg c, city d
                WHERE a.`vouchno` != '' and a.hotelnm = b.code and a.hotelcatg = c.code and a.city = d.code 
                $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)) {

    
    // $mhotel = mysqli_query($conn, "SELECT code,hotelnm FROM hotels where code ='$r[hotelnm]'") or die(mysqli_error($conn));
    // $thotel = mysqli_fetch_array($mhotel);

    // $mhotcatg = mysqli_query($conn, "SELECT code,name FROM hotelcatg where code ='$r[hotelcatg]'") or die(mysqli_error($conn));
    // $thotcatg = mysqli_fetch_array($mhotcatg);

    // $mhotcity = mysqli_query($conn, "SELECT code,name FROM city where code ='$r[city]'") or die(mysqli_error($conn));
    // $thotcity = mysqli_fetch_array($mhotcity);

     $magent = mysqli_query($conn, "SELECT accode,name FROM accmast where accode ='$r[agent]'") or die(mysqli_error($conn));
     $lagent = mysqli_fetch_array($magent);


    $rows[] = [ 'success' => true,
        'value'        => $ctr,
        'keyfld'       => $keyfld,
        'vouchno'      => $r['vouchno'],
        'vouchdt'      => $r['vouchdt'],
        'city'         => $r['cityname'],
        'hotelnm'      => $r['hotelname'],
        'hotelcatg'    => $r['categname'],
        'agent'        => $lagent['name'],
        'action'       => "$file?mode=edit&vouchno=$r[vouchno]"
    ];
    $ctr++;
}

header("Content-type: application\json");
echo json_encode($rows);
?>