<?php
require_once "includes/config.inc.php";

$conn = OpenCon();
$arr = array('status' => 0, 'errorMessage' => "Warning: No data found ");
$data_json = file_get_contents('php://input');
$data_json = json_decode($data_json, true);
$errorMessage = "";
$listname = $_POST['listname'];

if (isset($_POST['frm_mode']))
{
	$sql = "SELECT * FROM " . $listname;
	$result = mysqli_query($conn, $sql);
	if($result->num_rows>0) 
    {
    	$view = mysqli_fetch_array($result);
    	$output = array('title' => $view['title']);
    	$arr['status'] = 1;
        $arr['retData'] = $output;
        $arr['errorMessage'] = $errorMessage;
    }
    else{
    $arr['status'] = 0;
    $arr['errorMessage'] = 'No list found';
    }
}

if (isset($data_json['frm_mode']) && $data_json['frm_mode'] == 'doLogin') {
    $username = (isset($data_json['username'])) ? $data_json['username'] : '';
    $passphrase = (isset($data_json['passphrase'])) ? $data_json['passphrase'] : '0';

     $sql = "select * from AGENT_LIST where    (AGENT_USERNAME='" . $username . "' AND AGENT_PASSWORD='" . $passphrase . "'    ) ";
     
    $result = mysqli_query($con, $sql);
   
    if($result->num_rows>0) 
    {
        $view = mysqli_fetch_array($result);
        if($view['AGENT_STATUS']=='Active')
            {
                $sql = "select * from BANK_LIST where BID='{$view['BID']}'";
                $result = mysqli_query($con, $sql);
                if($result->num_rows>0) 
                    {      
                        $viewBank = mysqli_fetch_array($result);

                        $output = array(
                            'BNAME' => $viewBank['BNAME'], 
                            'BEMAIL' => $viewBank['BEMAIL'], 
                            'AGENT_CODE' => $view['AGENT_CODE'], 
                            'AGENT_USERNAME' => $view['AGENT_USERNAME'], 
                            'AGENT_NAME' => $view['AGENT_NAME'], 
                            'AGENT_EMAIL' => $view['AGENT_EMAIL']
                             
                        );

                        $arr['status'] = 1;
                        $arr['retData'] = $output;
        
                        $arr['errorMessage'] = $errorMessage;
                    }
                
        
            }else{

                
                $arr['status'] = 0;
                $arr['errorMessage'] = 'Your account is suspended, Please contact your bank manager';
           }
    } else {
        
             $arr['status'] = 0;
             $arr['errorMessage'] = 'Invalid login credentials';

    }
}

echo json_encode($arr);
die();
CloseCon($con);
