<?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];
$fseries = $_SESSION["society_" . $dbname];
$success = false;
$maindb = 'medent';

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

$file = "medical.php";

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

$cond = "";
if ($operator == "=") {
    if ($_REQUEST["sfield"] == "1") {
        $cond = "AND `vouchdt` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "2") {
        $cond = "AND `manualno` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "3") {
        $cond = "AND `paxname` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "4") {
        $cond = "AND `mobile` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "5") {
        $cond = "AND `passport` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "6") {
        $cond = "AND `broker` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "7") {
        $cond = "AND `doctor` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "8") {
        $cond = "AND `fitness` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "9") {
        $cond = "AND `branch` = '$searchTerm'";
    }
} else {
    if ($_REQUEST["sfield"] == "1") {
        $cond = "AND `vouchdt` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "2") {
        $cond = "AND `manualno` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "3") {
        $cond = "AND `paxname` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "4") {
        $cond = "AND `mobile` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "5") {
        $cond = "AND `passport` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "6") {
        $cond = "AND `broker` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "7") {
        $cond = "AND `doctor` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "8") {
        $cond = "AND `fitness` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "9") {
        $cond = "AND `branch` LIKE '%$searchTerm%'";
    }
}

$sortcond = "ORDER BY `myvouch`";
if ($_REQUEST["sortBy"] == "1") {
    $sortcond = " ORDER BY `vouchdt`";
} else if ($_REQUEST["sortBy"] == "2") {
    $sortcond = " ORDER BY `manualno`";
} else if ($_REQUEST["sortBy"] == "3") {
    $sortcond = " ORDER BY `paxname`";
} else if ($_REQUEST["sortBy"] == "4") {
    $sortcond = " ORDER BY `mobile`";
} else if ($_REQUEST["sortBy"] == "5") {
    $sortcond = " ORDER BY `passport`";
} else if ($_REQUEST["sortBy"] == "6") {
    $sortcond = " ORDER BY `broker`";
} else if ($_REQUEST["sortBy"] == "7") {
    $sortcond = " ORDER BY `doctor`";
} else if ($_REQUEST["sortBy"] == "8") {
    $sortcond = " ORDER BY `fitness`";
} else if ($_REQUEST["sortBy"] == "9") {
    $sortcond = " ORDER BY `branch`";
}

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

//mysqli_query($conn,  "Create temporary table `med` SELECT Date_Format(`vouchdt`, '%d/%m/%Y') as mydt, myvouch,
//                        manualno, paxname, mobile,passport, broker,doctor FROM `$maindb` WHERE myvouch != ''  ")or die(mysqli_error($conn));

$srchresult = "SELECT *,date_Format(`vouchdt`, '%d/%m/%Y') as mydt  FROM $maindb WHERE myvouch != '' $cond $sortcond $ordcond  LIMIT $rangeFrom, $rangeTo"or die(mysqli_error($conn));

$result = mysqli_query($conn, $srchresult)or die(mysqli_error($conn));
//
//$query = "SELECT Date_Format(`vouchdt`, '%d/%m/%Y') as mydt,myvouch ,manualno, paxname, mobile,passport, broker,doctor FROM `medent`
//                 WHERE 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['fitness'] == 1) {
        $fitness = 'Fit (1)';
    } else if ($r['fitness'] == 2) {
        $fitness = 'Unfit (2)';
    } else if ($r['fitness'] == 3) {
        $fitness = 'Pending (3)';
    } else if ($r['fitness'] == 4) {
        $fitness = 'Repeat (4)';
    }


    $rows[] = ['success' => true,
        'value' => $ctr,
        'keyfld' => $keyfld,
        'mydt' => $r['mydt'],
        'manualno' => $r['manualno'],
        'paxname' => $r['paxname'],
        'mobile' => $r['mobile'],
        'passport' => $r['passport'],
        
        'broker' => $r['broker'],
        'doctor' => $r['doctor'],
        'branch' => $r['branch'],
        'fitness' => $fitness,
        'action' => "$file?mode=edit&myvouch=$r[myvouch]",
    ];
    $ctr++;
}

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