<?php
session_start();
include('../include/connection.php');
include('../include/app_config.php');
if(isset($_GET['load']) && $_GET['load']==1)
{
    $sql = "select b.*,s.Name as StateName,l.LGA AS LgaName,st.Name As StateResidence from count b left outer join state s on b.stateorigin=s.StateID
                    left outer join localgovt l on b.lgaorigin=l.LGAID left outer join state st on b.stateresidence=st.StateID";
    $fields = array();
    $conditions = array();
    $result = array();
    if($_GET['state'] !="") {
        $conditions[] = "b.stateorigin=:state";
        $fields[':state']=$_GET['state'];
    }
    
    if($_GET['lga'] !="") {
        $conditions[] = "b.lgaorigin=:lga";
        $fields[':lga']=$_GET['lga'];
    }
    
    if($_GET['employ'] !="") {
        $conditions[] = "b.employment=:employ";
        $fields[':employ']=$_GET['employ'];
    }
    
    if($_GET['age'] !="") {
        if($_GET['age'] == 1)
        {
            $conditions[] = "b.age<=14";
        }
        else if($_GET['age'] == 2)
        {
            $conditions[] = "b.age>=15 AND b.age<=21";
        }
        else if($_GET['age'] == 3)
        {
            $conditions[] = "b.age>=22 AND b.age<=35";
        }
        else if($_GET['age'] == 4)
        {
            $conditions[] = "b.age>=36 AND b.age<=55";
        }
        else if($_GET['age'] == 5)
        {
            $conditions[] = "b.age>=55";
        }
    }
    
    if($_GET['disable'] !="") {  
        $conditions[] = "b.disable=:disable";
        $fields[':disable']=$_GET['disable'];
    }
    
    if($_GET['gender'] !="") {
        $conditions[] = "b.sex=:sex";
        $fields[':sex']=$_GET['gender'];
    }
    
    if($_GET['stater'] !="") {
        $conditions[] = "b.stateresidence=:stater";
        $fields[':stater']=$_GET['stater'];
    }
    
    if($_GET['lgar'] !="") {
        $conditions[] = "b.lgaresidence=:lgar";
        $fields[':lgar']=$_GET['lgar'];
    }
    
    if (count($conditions) > 0) {
        $sql .= " WHERE " . implode(' AND ', $conditions). " order by b.lastname";
    }
    $result['text'] = "";
    $q=$con->select_query($sql,$fields);
    $sn = 1;
    $count = 0;
    if(count($q) > 0)
    {
        foreach($q as $r)
        {
            $result['text'].= '<tr>
                <td style="font-size:12px">'.$sn.'</td>
                <td>'.$r['title'].' '.$r['lastname'].' '.$r['firstname'].' '.$r['othername'].'</td>
                <td>'.$r['StateName'].'</td>
                <td>'.$r['LgaName'].'</td>
                <td>'.$r['StateResidence'].'</td>
                <td>'.$r['sex'].'</td>
                <td>'.$r['age'].'</td></tr>';
            $sn++;
            $count++;
        } 
    }
    $result['count'] = $count;
    $result = json_encode($result);
    echo $result;
}
?>