<?php
session_start();

try {
    //code...
    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["submitneworg"])) {
            $orgName = $_POST["orgname"];
            $orgAddress = $_POST["orgaddress"];
            $orgCity = $_POST["orgcity"];
            $orgState = $_POST["orgstate"];
            $orgZip = $_POST["orgzip"];
            $orgContact = $_POST["orgcontact"];
            $orgContactPhone = $_POST["orgcontactphone"];
            $dealerId = $_SESSION['userid'];
    
            newOrganization($pdo, $orgName, $orgAddress, $orgCity, $orgState, $orgZip, $orgContact, $orgContactPhone, $dealerId);
            header("Location: ../../dashboard.php?tab=my_properties");
        }
    
        if (isset($_POST["getDoorsGate"])) {
            $base_url = getBaseUrl();
            $data['base_url'] = "../../../data/audio_messages/";
            $data['phone_numbers'] = getMyNumbers($pdo, $userId, $userLayer, $property_id);
            $property_id = $_SESSION['property'];
            $data['my_call_flow'] = getMyCallFlow($pdo, $userId, $userLayer, $property_id);
            $data['property_users_numbers'] = getPropertyNumbers($pdo, $userId, $userLayer, $property_id);
            header("Content-Type: application/json");
            sendJsonResponse(200, $data);
        }


    } else {
        header("location: ../../login.php");
        exit();
    }
} catch (\Throwable $th) {
    header("Content-Type: application/json");
    sendJsonResponse(500, "An unexpected error occurred.");
}
