<?php

$para = $_REQUEST["para"];
$famid = $_REQUEST["familyid"];


if ($para == "F") {
    $famcond = "and `famcheck`='1'";
    require("includes/check_auth_user.php");
} elseif ($para == "M") {
    require("includes/check_auth_user.php");
    $famcond = "and `famcheck`!='1'";
} else {
    require("includes/check_auth_mem.php");
    $famcond = "and `familyid`='$famid'";
}
require("includes/dbconnect.php");
include('includes/function.php');

$zroot = $_SESSION["zdbroot"];
$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zroot);
$admin = $_SESSION["admin_" . $dbname];
$success = false;
$maindb = 'member';
$society = $_SESSION["society_" . $dbname];
$file = "member.php";
$keyfld = "accode";
$rangeFrom = $_REQUEST["rangeFrom"];
$rangeTo = $_REQUEST["rangeTo"];
$searchTerm = $_REQUEST["ssearch"];
$operator = $_REQUEST["soperator"];


$cond = "";
if ($operator == "=") {
    if ($_REQUEST["sfield"] == "1") {
        $cond = "AND `name` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "2") {
        $cond = "AND `taluka` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "3") {
        $cond = "AND `accode` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "4") {
        $cond = "AND `familyid` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "5") {
        $cond = "AND `surname` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "6") {
        $cond = "AND `aadhaar` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "7") {
        $cond = "AND `mobile` = '$searchTerm'";
    } else if ($_REQUEST["sfield"] == "8") {
        $cond = "AND `add1` = '$searchTerm'";
    }
} else {
    if ($_REQUEST["sfield"] == "1") {
        $cond = "AND `name` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "2") {
        $cond = "AND `taluka` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "3") {
        $cond = "AND `accode` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "4") {
        $cond = "AND `familyid` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "5") {
        $cond = "AND `surname` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "6") {
        $cond = "AND `aadhaar` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "7") {
        $cond = "AND `mobile` LIKE '%$searchTerm%'";
    } else if ($_REQUEST["sfield"] == "8") {
        $cond = "AND `add1` LIKE '%$searchTerm%'";
    } 
}

$sortcond = "ORDER BY accode";
if ($_REQUEST["sortBy"] == "1") {
    $sortcond = " ORDER BY name";
} else if ($_REQUEST["sortBy"] == "2") {
    $sortcond = " ORDER BY `taluka`";
} else if ($_REQUEST["sortBy"] == "3") {
    $sortcond = " ORDER BY `accode`";
} else if ($_REQUEST["sortBy"] == "4") {
    $sortcond = " ORDER BY `familyid`";
} else if ($_REQUEST["sortBy"] == "5") {
    $sortcond = " ORDER BY `surname`";
} else if ($_REQUEST["sortBy"] == "6") {
    $sortcond = " ORDER BY `aadhaar`";
} else if ($_REQUEST["sortBy"] == "7") {
    $sortcond = " ORDER BY `mobile`";
} else if ($_REQUEST["sortBy"] == "8") {
    $sortcond = " ORDER BY `add1`";
}

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

$query = "SELECT *,DATE_FORMAT(`dob`, '%d-%m-%Y') as dob FROM `$maindb` WHERE `accode` != ''  $cond $famcond
          $sortcond $ordcond  LIMIT $rangeFrom, $rangeTo";
$result = mysqli_query($conn, $query) or die(mysqli_error($conn));
// print table($result);
$rows = array();
$ctr = 0;
while ($r = mysqli_fetch_assoc($result)) {

    $name = strtoupper($r['name']);
    $name2 = strtoupper($r['name2']);
    $surname = strtoupper($r['surname']);

    $rows[] = [
        'success' => true,
        'value' => $ctr,
        'keyfld' => $keyfld,
        'accode' => $r['accode'],
        'familyid' => $r['familyid'],
        'surname' => $surname,
        'name' => $name,
        'taluka' => $r['taluka'],
        'aadhaar' => $r['aadhaar'],
        'mobile' => $r['mobile'],
        'add1' => $r['add1'],
        'area' => $r['area'],
        'action' => "$file?mode=edit&accode=$r[accode]&para=$para",
    ];
    $ctr++;
}
header("Content-type: application\json");
echo json_encode($rows);
