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

//$zseries = "$_GET[series]";
//browse($zseries);
//$keycond = " AND `series`='$zseries' ";
$myyear = $_SESSION["tit_" . $dbname];
$keycond = "";
//$admin = $_SESSION["admin_" . $dbname];
//$zdb = $_SESSION["zdbyear_" . $dbname];
$usertype = $_SESSION["usertype"];
$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$zroot = $_SESSION["zdbroot"];

mysqli_select_db($conn, $zdb);


$success = false;

$maindb = 'attdmon';
$file = 'autoattendmonth.php';
$source = "DM";
$keyfld = "vouchno";

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


$cond = "";
if ($operator == "=") {
    if ($_REQUEST["sfield"] == "1") {
        $cond = "AND a.`deptname` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "2") {
        $cond = "AND b.`monname` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "2") {
        $cond = "AND a.`year` = '$searchTerm'";
    } 
} else {
    if ($_REQUEST["sfield"] == "1") {
        $cond = "AND a.`deptname` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "2") {
        $cond = "AND b.`monname` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "3") {
        $cond = "AND a.`year` LIKE '%$searchTerm%'";
    }
}


$sortcond = "ORDER BY a.`vouchno`";
if ($_REQUEST["sortBy"] == "1") {
    $sortcond = " ORDER BY a.`deptname`";
} else if ($_REQUEST["sortBy"] == "2") {
    $sortcond = " ORDER BY b.`monname`";
} else if ($_REQUEST["sortBy"] == "3") {
    $sortcond = " ORDER BY a.`year`";
}   


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

//$query = "SELECT a.vouchno, b.name ,c.name as deptname
//           FROM $maindb a, division b, dept c, WHERE a.divcode = b.code AND c.deptcode = b.code $cond $sortcond $ordcond  LIMIT $rangeFrom, $rangeTo";
//$result = mysqli_query($conn, $query) or die(mysqli_error($conn));

//$query = "SELECT a.vouchno, b.name, c.name as deptname FROM $maindb a, division b, dept c WHERE a.divcode = b.code AND a.deptcode = c.code $cond $sortcond $ordcond  LIMIT $rangeFrom, $rangeTo";
//$result = mysqli_query($conn, $query) or die(mysqli_error($conn));

mysqli_query($conn, "create temporary table attendmain SELECT a.vouchno, a.yearno as year, b.name as deptname, a.monname  FROM $maindb a,  dept b WHERE  a.deptcode = b.code
                ") or die(mysqli_error($conn));

$result = mysqli_query($conn, "SELECT a.*, b.monname as monthname FROM attendmain a, $zroot.`month` b WHERE a.monname = b.moncode  $cond $sortcond $ordcond  LIMIT $rangeFrom, $rangeTo") or die(mysqli_error($conn));

$rows = array();
$ctr = 0;
while ($r = mysqli_fetch_assoc($result)) {
    $rows[] = ['success' => true,
        'value' => $ctr,
        'deptname' => $r['deptname'],
        'monname' => $r['monthname'],
        'yearno' => $r['year'],
        'action' => "$file?mode=edit&$keyfld=$r[vouchno]",
    ];
    $ctr++;
}
//browse($rows);
header("Content-type: application\json");
echo json_encode($rows);
