<?php

session_start();

if (isset($_SESSION['userid'])) {
    require_once('../../db.php');
    require_once('../../shared/functionsinc.php');
    $userId = $_SESSION['userid'];
    $userLayer = $_SESSION['layer'];
    $property_id = $_SESSION['property'];

    if (isset($_POST["getdata"])) {
        $data['purchased_numbers'] = getPurchasedNumbers($pdo, $userId, $userLayer, $property_id);

        $data['user_layer'] = $userLayer;
        $data['dealers'] = getDealers($pdo, $userId, $userLayer);
        echo json_encode($data);
    }
    if(isset($_POST['dealerProperties'])){
        $dealer_id = $_POST['dealer_id'];
        $data['properties'] = numberDealerProperties($pdo, $dealer_id);
        echo json_encode($data);
    }
    if(isset($_POST["seachNumber"])){
        if(!empty($property_id)){
            $number_type = $_POST['seachNumber'];
            $contains = $_POST['contains'];
            $in_region = $_POST['in_region'];
            $area_code = $_POST['area_code'];
            $number_terms = $_POST['number_terms'];
            
            $get_data = getAvailableNumbers($property_id, $pdo, $userId, $userLayer, $number_type, $contains, $in_region, $area_code, $number_terms);
            if(!empty($get_data)){
                $data['searched_numbers'] = $get_data;
                $data['layer'] = $userLayer;
            }
            else{
                $data = 0;
            }
        }
        else{
            $data = 0;
        }
        echo json_encode($data);

    }
    if(isset($_POST["purchase_number"]) && $_POST["purchase_number"] == 'yes'){
        $number = $_POST['number'];
        // if($userLayer == "dealer"){
        $property_id = (int)$_POST['property_id'];
        $number_type = $_POST['number_type'];
        $data = purchaseNumber($pdo, $userId, $userLayer, $number, $property_id, $number_type);
        // }
        // else{
        //     $data = purchaseNumber($pdo, $userId, $userLayer, $number);
        // }
        echo $data;            
    }
    if(isset($_POST['assign_dealer_number']) && $_POST['assign_dealer_number'] == 'yes'){
        $phone_number = trim($_POST['phone_number']);
        $dealer_id = (int)$_POST['dealer_id'];
        $number_id = (int)$_POST['number_id'];
        $property_id = (int)$_POST['property_id'];
        if (!preg_match('/^\+?[0-9\- ]{7,20}$/', $phone_number)) {
            echo "<h4 class='alert alert-danger'>Invalid phone number format.</h4>";
            return;
        }
        $data = assignDelaerNumber($pdo, $userId, $phone_number, $dealer_id, $number_id, $property_id);
        return $data;
    }
    if(isset($_POST['assignDealerProject'])){
        $project_id = $_POST['project_id'];
        $space_url = $_POST['space_url'];
        $api_token = $_POST['api_token'];
        $number_limit = $_POST['number_limit'];
        $dealer_id = $_POST['dealer_id'];
        $data = assignDealerProject($pdo, $userId, $userLayer, $project_id, $space_url, $api_token, $dealer_id, $number_limit);
        echo json_encode($data);
    }
    if(isset($_POST['check_limit_for_number'])){
        $data['properties'] = checkLimitNumber($pdo, $userId, $userLayer);
        $data['user_role'] = $userLayer;
        if($userLayer == "dealer"){
            $data['calculate_number'] = calculateNumber($pdo, $userId, $userLayer);
        }
        echo json_encode($data);   
    }

    if(isset($_POST['getSgProDt'])){
        $user_id = $_POST['user_id'];
        $data = checkProDetail($pdo, $user_id, $userLayer);
        echo json_encode($data);   
    }

    if(isset($_POST['checkPropertyDoorHardware'])){
        $check_prop = checkPropertyDoorHardware($property_id);
        echo $check_prop;
    }

    if(isset($_POST['deletesgnumber'])){
        $phone_number = $_POST['number'];
        $delete_number = deleteSgNumber($property_id, $phone_number, $userId);
        echo $delete_number;
    }


} else {
    header("location: ../../login.php");
    exit();
}

