<?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 = "grpmast";
$file = "patient.php";
$action = "patient";

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

$cond = "";
if ($operator == "=") {
    if ($_REQUEST["sfield"] == "1") {
        $cond = "AND `name` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "2") {
        $cond = "AND `mobile` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "3") {
        $cond = "AND `accode` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "4") {
        $cond = "AND `add1` . `add2` . `add3`= '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "5") {
        $cond = "AND `age` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "6") {
        $cond = "AND  `dob` = '$searchTerm'";
    }
} else {
    if ($_REQUEST["sfield"] == "1") {
        $cond = "AND `name` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "2") {
        $cond = "AND `mobile` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "3") {
        $cond = "AND `accode` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "4") {
        $cond = "AND `add1` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "5") {
        $cond = "AND `age` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "6") {
        $cond = "AND `dob` LIKE '%$searchTerm%'";
    }
}

$sortcond = "ORDER BY `name`";
if ($_REQUEST["sortBy"] == "1") {
    $sortcond = " ORDER BY `name`";
} else if ($_REQUEST["sortBy"] == "2") {
    $sortcond = " ORDER BY `mobile`";
} else if ($_REQUEST["sortBy"] == "3") {
    $sortcond = " ORDER BY `accode`";
} else if ($_REQUEST["sortBy"] == "4") {
    $sortcond = " ORDER BY `add1`";
} else if ($_REQUEST["sortBy"] == "5") {
    $sortcond = " ORDER BY `age`";
} else if ($_REQUEST["sortBy"] == "6") {
    $sortcond = " ORDER BY `dob`";
}

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

$query = "SELECT *, DATE_FORMAT(`dob`, '%d-%m-%Y') as bday FROM `accmast` WHERE (`grpcode`= '010' OR grpcode = '022') and accode != 51 $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)) {
$balance = 0;

    $getpay = mysqli_query($conn, "SELECT sum(amount) as tamt FROM `actran` WHERE accode = '$r[accode]'") or die(mysqli_error($conn));
    if (!empty($getpay)) {

        $mpay = mysqli_fetch_array($getpay);

        $balance = $mpay['tamt'];
    }

    if ($r['gender'] == 'M') {
        $gender = "Male";
    } else if ($r['gender'] == 'F') {
        $gender = "Female";
    } else {
        $gender = "Other";
    }
    $rows[] = [
        'success' => true,
        'value' => $ctr,
        'keyfld' => $keyfld,
        'accode' => $r['accode'],
        'name' => $r['name'],
        'add1' => $r['add1'],
        'add2' => $r['add2'],
        'add3' => $r['add3'],
        'age' => $r['age'],
        'dob' => $r['bday'],
        'email' => $r['email'],
        'gender' => $gender,
        'mobile' => $r['mobile'],
        'balance' => $balance,
        'action' => "$file?mode=edit&accode=$r[accode]",
    ];
    $ctr++;
}

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