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

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

$cond = "";
if ($operator == "=") {
    if ($_REQUEST["sfield"] == "1") {
        $cond = "AND a.`vouchdt` = '$searchTerm'";
    } elseif ($_REQUEST["sfield"] == "2") {
        $cond = "AND b.`product_name` = '$searchTerm'";
    } elseif ($_REQUEST["sfield"] == "3") {
        $cond = "AND c.`color_name` = '$searchTerm'";
    } elseif ($_REQUEST["sfield"] == "4") {
        $cond = "AND d.`process_name` = '$searchTerm'";
    } elseif ($_REQUEST["sfield"] == "5") {
        $cond = "AND e.`name` = '$searchTerm'";
    } elseif ($_REQUEST["sfield"] == "6") {
        $cond = "AND a.`qty` = '$searchTerm'";
    } elseif ($_REQUEST["sfield"] == "7") {
        $cond = "AND a.`vouchno` = '$searchTerm'";
    }
} else {
        if ($_REQUEST["sfield"] == "1") {
            $cond = "AND a.`vouchdt` LIKE '%$searchTerm%'";
        } elseif ($_REQUEST["sfield"] == "2") {
            $cond = "AND b.`product_name` LIKE '%$searchTerm%'";
        } elseif ($_REQUEST["sfield"] == "3") {
            $cond = "AND c.`color_name` LIKE '%$searchTerm%'";
        } elseif ($_REQUEST["sfield"] == "4") {
            $cond = "AND d.`process_name` LIKE '%$searchTerm%'";
        } elseif ($_REQUEST["sfield"] == "5") {
            $cond = "AND e.`name` LIKE '%$searchTerm%'";
        } elseif ($_REQUEST["sfield"] == "6") {
            $cond = "AND a.`qty` LIKE '%$searchTerm%'";
        } elseif ($_REQUEST["sfield"] == "6") {
            $cond = "AND a.`vouchno` LIKE '%$searchTerm%'";
        }
    }

    $sortcond = "ORDER BY a.`vouchdt`";
    if ($_REQUEST["sortBy"] == "1") {
        $sortcond = " ORDER BY a.`vouchdt`";
    } elseif ($_REQUEST["sortBy"] == "2") {
        $sortcond = " ORDER BY b.`product_name`";
    } elseif ($_REQUEST["sortBy"] == "3") {
        $sortcond = " ORDER BY c.`color_name`";
    } elseif ($_REQUEST["sortBy"] == "4") {
        $sortcond = " ORDER BY d.`process_name`";
    } elseif ($_REQUEST["sortBy"] == "5") {
        $sortcond = " ORDER BY e.`name`";
    } elseif ($_REQUEST["sortBy"] == "6") {
        $sortcond = " ORDER BY a.`qty`";
    } elseif ($_REQUEST["sortBy"] == "7") {
        $sortcond = " ORDER BY a.`vouchno`";
    }

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

    $result = mysqli_query($conn, "SELECT a.*,b.code,b.name as product_name,c.code,c.name as color_name,"
            . "d.code, d.name as process_name,e.accode, e.name as karigar_name "
            . "FROM `jobgiven` a, `item` b, `color` c,`process` d,`accmast` e "
            . "WHERE b.code = a.prodcode AND c.code = a.colorname AND d.code = a.process "
            . "AND e.accode = a.karigar $cond $sortcond $ordcond")or die(mysqli_error($conn));

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

        $rows[] = [ 'success' => true,
            'value' => $ctr,
            'vouchno' => $r['vouchno'],
            'vouchdt' => $r['vouchdt'],
            'product' => $r['product_name'],
            'color'   => $r['color_name'],
            'process' => $r['process_name'],
            'karigar' => $r['karigar_name'],
            'qty' => $r['qty'],
            'action' => "$file?mode=edit&vouchno=$r[vouchno]",
        ];
        $ctr++;
    }
    header("Content-type: application\json");
    echo json_encode($rows);
    