<?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];
$success = false;

$maindb = "hallbook";
$file = "hallbooking.php";

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

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

$cond = "";
if ($operator == "=") {
    if ($_REQUEST["sfield"] == "1") {
        $cond = "AND `bookno` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "2") {
        $cond = "AND `vouchdt`  = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "3") {
        $cond = "AND `hall` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "4") {
        $cond = "AND `eventdt` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "5") {
        $cond = "AND `formem` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "6") {
        $cond = "AND `name` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "7") {
        $cond = "AND `add1` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "8") {
        $cond = "AND `mobile` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "9") {
        $cond = "AND `amount` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "10") {
        $cond = "AND `staff` = '$searchTerm'";
    }
} else {
    if ($_REQUEST["sfield"] == "1") {
        $cond = "AND `bookno` LIKE '%$searchTerm%'";
    } elseif ($_REQUEST["sfield"] == "2") {
        $cond = "AND `vouchdt` LIKE '%$searchTerm%'";
    } elseif ($_REQUEST["sfield"] == "3") {
        $cond = "AND `hall` LIKE '%$searchTerm%'";
    } elseif ($_REQUEST["sfield"] == "4") {
        $cond = "AND `eventdt` LIKE '%$searchTerm%'";
    } elseif ($_REQUEST["sfield"] == "5") {
        $cond = "AND `formem` LIKE '%$searchTerm%'";
    } elseif ($_REQUEST["sfield"] == "6") {
        $cond = "AND `name` LIKE '%$searchTerm%'";
    } elseif ($_REQUEST["sfield"] == "7") {
        $cond = "AND `add1` LIKE '%$searchTerm%'";
    } elseif ($_REQUEST["sfield"] == "8") {
        $cond = "AND `mobile` LIKE '%$searchTerm%'";
    } elseif ($_REQUEST["sfield"] == "9") {
        $cond = "AND `amount` LIKE '%$searchTerm%'";
    } elseif ($_REQUEST["sfield"] == "10") {
        $cond = "AND `staff` LIKE '%$searchTerm%'";
    }
}

$sortcond = "ORDER BY `bookno`";
if ($_REQUEST["sortBy"] == "1") {
    $sortcond = " ORDER BY `bookno`";
} else if ($_REQUEST["sortBy"] == "2") {
    $sortcond = " ORDER BY `vouchdt";
} else if ($_REQUEST["sortBy"] == "3") {
    $sortcond = " ORDER BY `hall`";
} else if ($_REQUEST["sortBy"] == "4") {
    $sortcond = " ORDER BY `eventdt`";
} else if ($_REQUEST["sortBy"] == "5") {
    $sortcond = " ORDER BY `formem`";
} else if ($_REQUEST["sortBy"] == "6") {
    $sortcond = " ORDER BY `name`";
} else if ($_REQUEST["sortBy"] == "7") {
    $sortcond = " ORDER BY `add1`";
} else if ($_REQUEST["sortBy"] == "8") {
    $sortcond = " ORDER BY `mobile`";
} else if ($_REQUEST["sortBy"] == "9") {
    $sortcond = " ORDER BY `amount`";
} else if ($_REQUEST["sortBy"] == "10") {
    $sortcond = " ORDER BY `staff`";
}

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

$srchresult = "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y')as vouchdt, DATE_FORMAT(`eventdt`, '%d-%m-%Y')as eventdt FROM `$maindb`  WHERE  $keycond 
                      $cond $sortcond $ordcond LIMIT $rangeFrom, $rangeTo";
$result = mysqli_query($conn, $srchresult) or die(mysqli_error($conn));


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

    $qry = mysqli_query($conn,"SELECT code, name FROM hallname WHERE code = $r[hall]") or die(mysqli_error($conn));
    $naam = mysqli_fetch_array($qry);
    
    $rows[] = [
        'success' => true,
        'value' => $ctr,
        'keyfld' => $keyfld,
        'vouchno'  => $r['bookno'],
        'vouchdt'    => $r['vouchdt'],
        'hall'    => $naam['name'],
        'eventdt'    => $r['eventdt'],
        'formem'  => $r['formem'],
        'name'   => $r['name'],
        'add1'   => $r['add1'] ,
        'mobile'   => $r['mobile'],
        'amount'   => $r['amount'],
        'staff'   => $r['staff'],
        'action'  => "$file?mode=edit&vouchno=$r[vouchno]",
    ];
    $ctr++;
}
header("Content-type: application\json");
echo json_encode($rows);
