<?php
include "../../db/dBase.php";
$dbase = new dBase();
$tcon = $dbase->con;
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

$response = array('status' => 'fail', 'items' => [], 'total_count' => 0);
$data = json_decode(file_get_contents("php://input"));


if (isset($data->API_KEY)) {

    $key_ = mysqli_real_escape_string($tcon, $data->API_KEY);
    $key = md5($key_);

    $api_n = $dbase->queryto_fetch_api("SELECT COUNT(*) AS `count`,`userID` FROM `access_log` WHERE `token`='$key' ");
    if ($api_n['count'] == 1) {

        $ctUser = $api_n['userID'];
        $api_row = $dbase->queryto_fetch_api("SELECT user.rank,user.userID,agent.pf_id FROM `user` LEFT JOIN `agent` ON agent.userID=user.userID WHERE user.userID='$ctUser' ");
        $cUser = $api_row['userID'];
        $pf_id = $api_row['pf_id'];


        if (isset($data->send)) {

            $send = json_decode($data->send);
            if (isset($send->mode)) {
                
                if ($send->mode == 'list-all') {
                    
                    $filter=$send->filter;
                    
                $category_query = '';
                if (isset($filter->category)) {
                     $category = mysqli_real_escape_string($tcon, $filter->category);
                    if($category=='sale'){
                        $category_query_by = " AND ( LOWER(_bayut_listing.category) = 'sale' OR LOWER( _bayut_listing.category) = 'buy' ) ";
                        $category_query_pf = " AND ( LOWER(_pf_listing.category) =  'sale' OR LOWER( _pf_listing.category)='buy' ) ";
                     }
                     if($category=='rent'){
                        $category_query_by = " AND _bayut_listing.category = 'rent'";
                        $category_query_pf = " AND _pf_listing.category = 'rent'";
                     }
                }
                
                $type_query = '';
                    if (isset($filter->type)) {
                    $type = mysqli_real_escape_string($tcon, $filter->type);
                        $type_query_by = " AND _bayut_listing.type = '" . $type . "'";
                        $type_query_pf = " AND _pf_listing.type = '" . $type . "'";
                }
            
               $bedroom_query = '';
                    if (isset($filter->bedroom)) {
                    $bedroom = mysqli_real_escape_string($tcon, $filter->bedroom);
                        $bedroom_query_by = " AND _bayut_listing.bedroom = '" . $bedroom . "'";
                        $bedroom_query_pf = " AND _pf_listing.bedroom = '" . $bedroom . "'";
                }
                $limit = 1000;
                    if (isset($send->limit)) {
                    $limit = mysqli_real_escape_string($tcon, $send->limit);
                    
                }
            
            
            
            
                    
                    $pf_list = $dbase->queryToWhile_2("SELECT user.userID,_pf_listing.changed,_pf_listing.ref,user.job,user.dp,area.title AS `community`, user.name,_pf_listing.bedroom, 'pf' AS `source`,_pf_listing.price,_pf_listing.category,_pf_listing.type,_pf_listing.details,_pf_listing.listing_level,_pf_listing.dp AS `pdp` ,_pf_listing.title,_pf_listing.ref,_pf_listing.status FROM `_pf_listing` LEFT JOIN `agent` ON agent.pf_id= _pf_listing.agent LEFT JOIN `user` ON user.userID=agent.userID LEFT JOIN `area` ON area.id = _pf_listing.community WHERE _pf_listing.status='ACTIVE' ".$type_query_pf." ".$category_query_pf." ".$bedroom_query_pf." GROUP BY _pf_listing.ref ORDER BY _pf_listing.id DESC LIMIT $limit");
                    $pf_count = $dbase->queryto_fetch_api("SELECT COUNT(*) AS `count` FROM `_pf_listing`  WHERE _pf_listing.status='ACTIVE' ".$bedroom_query_pf." ".$category_query_pf." ".$type_query_pf." ");
                    $by_list = $dbase->queryToWhile_2("SELECT user.userID,_bayut_listing.changed,_bayut_listing.ref,user.job,user.dp,area.title AS `community`, user.name,_bayut_listing.bedroom, 'by' AS `source`,_bayut_listing.price,_bayut_listing.category,_bayut_listing.status,'' AS `listing_level` ,_bayut_listing.type,_bayut_listing.dp AS `pdp` ,_bayut_listing.title,_bayut_listing.ref,_bayut_listing.status FROM `_bayut_listing` LEFT JOIN `user` ON user.userID=_bayut_listing.agent LEFT JOIN `area` ON area.id = _bayut_listing.community WHERE _bayut_listing.status = 'live' AND _bayut_listing.mode <> 'history' ".$type_query_by." ".$category_query_by." ".$bedroom_query_by." GROUP BY  _bayut_listing.ref ORDER BY _bayut_listing.id DESC LIMIT $limit");
                    $by_count = $dbase->queryto_fetch_api("SELECT COUNT(*) AS `count` FROM `_bayut_listing`  WHERE _bayut_listing.status = 'live' AND _bayut_listing.mode <> 'history' ".$type_query_by." ".$category_query_by." ".$bedroom_query_by." ");
                    
                    $response = array(
                        'CU'=>$api_row,
                        'pf' => array_filter($pf_list),
                        'by' => array_filter($by_list),
                        'by_count'=>$by_count['count'],
                        'pf_count'=>$pf_count['count']
                    );
                }
                
                
                if ($send->mode == 'list') {

                    $portal = mysqli_real_escape_string($tcon, $send->portal);


                    $items = [];$agent_query='';$status_query='';
                    if ($portal == 'pf') {
                        $agent_query = " AND _pf_listing.agent  = '$pf_id' ";
                        $status_query = " AND _pf_listing.status  = 'ACTIVE' ";
                        $items = $dbase->queryToWhile_2("SELECT _pf_listing.details,_pf_listing.listing_level,_pf_listing.dp,_pf_listing.title,_pf_listing.ref,_pf_listing.status FROM `_pf_listing` WHERE _pf_listing.id<>'' " . $agent_query . " " . $status_query . " ORDER BY _pf_listing.id DESC ");
                        $total_count = $dbase->queryto_fetch_api("SELECT COUNT(*) AS `count` FROM `_pf_listing`  WHERE _pf_listing.id <> '' " . $status_query . " " . $agent_query . " ");
                    } elseif ($portal == 'by') {
                        $agent_query = " AND _bayut_listing.agent  = '$cUser' ";
                        $status_query = " AND _bayut_listing.status  = 'live' ";
                        $items = $dbase->queryToWhile_2("SELECT _bayut_listing.status,'' AS `listing_level` ,_bayut_listing.dp,_bayut_listing.title,_bayut_listing.ref,_bayut_listing.status FROM `_bayut_listing` WHERE _bayut_listing.id<>'' " . $agent_query . " " . $status_query . " ORDER BY _bayut_listing.id DESC ");
                        $total_count = $dbase->queryto_fetch_api("SELECT COUNT(*) AS `count` FROM `_bayut_listing`  WHERE _bayut_listing.id <> '' " . $status_query . " " . $agent_query . " ");
                    }


                    if (sizeof($items) > 0) {
                        $items = $dbase->utf8_converter($items);
                    }


                    $response = array(
                        'items' => array_filter($items),
                        'count' => $total_count['count']
                    );
                }
                if ($send->mode == 'view') {

                    if (isset($send->ref)) {

                        $portal = mysqli_real_escape_string($tcon, $send->portal);
                        $ref = mysqli_real_escape_string($tcon, $send->ref);
                        if ($portal == 'pf') {
                            $item = $dbase->queryto_fetch_api("SELECT user.phone,user.email,user.job,user.dp AS `udp`,user.userID,user.name AS `agent_name`,area.title AS `comName` ,_pf_listing.category, _pf_listing.type, _pf_listing.bedroom, _pf_listing.bathroom, _pf_listing.price, _pf_listing.completion, _pf_listing.title, _pf_listing.status, _pf_listing.changed AS `modified` , _pf_listing.listing_level, _pf_listing.details, _pf_listing.thumb, _pf_listing.dp FROM `_pf_listing` LEFT JOIN `area` ON _pf_listing.community=area.id LEFT JOIN `agent` ON agent.pf_id=_pf_listing.agent LEFT JOIN `user` ON user.userID = agent.userID  WHERE _pf_listing.ref='$ref' ");
                            
                            $b64image = 'data:image/jpeg'.';base64,'.base64_encode(file_get_contents($item['dp']));

                        } elseif ($portal == 'by') {
                            $item = $dbase->queryto_fetch_api("SELECT user.phone,user.email,user.job,user.dp AS `udp`, user.userID,user.name AS `agent_name`,area.title AS `comName` ,_bayut_listing.category, _bayut_listing.type, _bayut_listing.bedroom, _bayut_listing.bathroom, _bayut_listing.price, _bayut_listing.completion, _bayut_listing.title, _bayut_listing.status AS `listing_level` , _bayut_listing.modified, _bayut_listing.mode, _bayut_listing.details, _bayut_listing.dp FROM `_bayut_listing` LEFT JOIN `area` ON _bayut_listing.community=area.id LEFT JOIN `user` ON user.userID = _bayut_listing.agent WHERE _bayut_listing.ref='$ref' ");

                        }
                        
                          $lead_data=$dbase->queryToWhile_2("SELECT lead.status,_status_list.title AS `status_name` ,lead.id,lead.phone,lead.added,lead.name FROM `lead` LEFT JOIN `lead_profile` ON lead.id =lead_profile.leadID LEFT JOIN `_status_list` ON lead.status=_status_list.code WHERE lead_profile.listing_refID ='$ref' ORDER BY lead.added DESC LIMIT 30 ");
    

                        $lead_check = $dbase->queryto_fetch_api("SELECT COUNT(*) AS `count` FROM `lead_profile` WHERE lead_profile.listing_refID = '$ref'   ");

                        $response = array(
                            'item' => $item,
                            'lead_data'=>array_filter($lead_data),
                            'lead_count' => $lead_check['count'],
                            'img1'=>$b64image,
                            'CU'=>$api_row
                        );
                    }
                }
                
                
                if ($send->mode == 'suggestion') {

                    if (isset($send->leadID)) {
                        
                    $leadID=mysqli_real_escape_string($tcon,$send->leadID);
                    $profile_data = $dbase->queryto_fetch_api("SELECT `property_type`,`bedroom` FROM `lead_profile` WHERE lead_profile.leadID = '$leadID'   ");
                    $lead_data = $dbase->queryto_fetch_api("SELECT `rent_sale`,`com` FROM `lead` WHERE lead.id = '$leadID'   ");
                    $property_type = $profile_data['property_type'];
                    $rent_sale = $lead_data['rent_sale'];
                    $com = $lead_data['com'];
                        
                    
                    $category_query = '';
                
                    $category = mysqli_real_escape_string($tcon, $rent_sale);
                    if($category=='sale'){
                        $category_query_by = " OR ( LOWER(_bayut_listing.category) = 'sale' OR LOWER( _bayut_listing.category) = 'buy' ) ";
                        $category_query_pf = " OR ( LOWER(_pf_listing.category) =  'sale' OR LOWER( _pf_listing.category)='buy' ) ";
                     }
                     if($category=='rent'){
                        $category_query_by = " OR _bayut_listing.category = 'rent'";
                        $category_query_pf = " OR _pf_listing.category = 'rent'";
                     }
                
                
                    $type_query = '';
                    
                    $type = mysqli_real_escape_string($tcon, $property_type);
                    if($type!=''){
                    $type_query_by = " OR _bayut_listing.type = '" . $type . "'";
                    $type_query_pf = " OR _pf_listing.type = '" . $type . "'";
                    }
                    
                   
                    
                    $com = mysqli_real_escape_string($tcon, $com);
                    if($com!=''){
                    $com_query_by = " OR _bayut_listing.community = '" . $com . "'";
                    $com_query_pf = " OR _pf_listing.community = '" . $com . "'";
                    }
                
                    $bedroom = mysqli_real_escape_string($tcon, $bedroom);
                    if($bedroom!=''){
                    $bedroom_query_by = " OR _bayut_listing.bedroom = '" . $bedroom . "'";
                    $bedroom_query_pf = " OR _pf_listing.bedroom = '" . $bedroom . "'";
                    }
                        
                        
                        
                                    
                    
                    
                   $pf_list=[];
                    $by_list=[];
                    
                    if($bedroom_query_by!=''  OR $bedroom_query_by!='' OR $type_query_by!='' OR $category_query_by!='' ){
                    $by_list = $dbase->queryToWhile_2("SELECT user.userID,_bayut_listing.changed,_bayut_listing.ref,user.job,user.dp,area.title AS `community`, user.name,_bayut_listing.bedroom, 'by' AS `source`,_bayut_listing.price,_bayut_listing.category,_bayut_listing.status,'' AS `listing_level` ,_bayut_listing.type,_bayut_listing.dp AS `pdp` ,_bayut_listing.title,_bayut_listing.ref,_bayut_listing.status FROM `_bayut_listing` LEFT JOIN `user` ON user.userID=_bayut_listing.agent LEFT JOIN `area` ON area.id = _bayut_listing.community WHERE _bayut_listing.category = '' AND _bayut_listing.status = 'live' AND _bayut_listing.mode <> 'history' ".$type_query_by." ".$com_query_by." ".$category_query_by." ".$bedroom_query_by." GROUP BY  _bayut_listing.ref ORDER BY _bayut_listing.id DESC LIMIT 8");    
                    }
                    
                    if($bedroom_query_pf!=''  OR $bedroom_query_pf!='' OR $type_query_pf!='' OR $category_query_pf!='' ){
                     $pf_list = $dbase->queryToWhile_2("SELECT user.userID,_pf_listing.changed,_pf_listing.ref,user.job,user.dp,area.title AS `community`, user.name,_pf_listing.bedroom, 'pf' AS `source`,_pf_listing.price,_pf_listing.category,_pf_listing.type,_pf_listing.details,_pf_listing.listing_level,_pf_listing.dp AS `pdp` ,_pf_listing.title,_pf_listing.ref,_pf_listing.status FROM `_pf_listing` LEFT JOIN `agent` ON agent.pf_id= _pf_listing.agent LEFT JOIN `user` ON user.userID=agent.userID LEFT JOIN `area` ON area.id = _pf_listing.community WHERE _pf_listing.status='ACTIVE' ".$type_query_pf." ".$com_query_pf." ".$category_query_pf." ".$bedroom_query_pf." GROUP BY _pf_listing.ref ORDER BY _pf_listing.id DESC LIMIT 8");
                    }
                        
                         $response = array(
                        'CU'=>$api_row,
                        'pf' => array_filter($pf_list),
                        'by' => array_filter($by_list)
                    );
                        
                        
                    }
                    
                    
                }
                
                
                
            }
        }
    }
}

echo json_encode($response);
