<?php
include_once __DIR__ . '/../../shared/functionsinc.php';
include_once __DIR__ . '/../../include/auth/auth_functions.php';

if (session_status() === PHP_SESSION_NONE) session_start();
include_once __DIR__ .'/../../include/helper_functions/data_request.php';
include_once __DIR__ .'/../../db.php';
include 'signalwire_data.php';

$call_cost = "Loading...";
$call_duration = "Loading...";
$total_sms = "Loading...";
$total_sms_amount = "Loading...";
$messages_amount = "Loading...";
$total_messages = "Loading...";

$propertyId=$_SESSION['property'] ?? null;
$userId = $_SESSION['userid'];
$user_type_layer = $_SESSION['layer'];

// $rsponse_data = get_voice_calls_amount($propertyId);

// if(!empty($rsponse_data['calls'])){
//     $get_voice_calls_amount = $rsponse_data['calls'];
//     $call_cost = $get_voice_calls_amount['call_amount'] * 2;
//     $call_duration = $get_voice_calls_amount['call_duration'];
// }
// if(!empty($rsponse_data['messages'])){
//     $get_messages_amount = $rsponse_data['messages'];
//     $messages_amount = $get_messages_amount['messages_amount'];
//     $total_messages = $get_messages_amount['total_messages'];
// }

// Fetch users data: handle both primary and multi-property users
$usersData = fetchData('property_users', ['status' => 'registered', 'property_id' => $propertyId, 'status' => 'registered', 'deleted_at' => null]);
$multiPropUsers = fetchData('multi_prop_users', ['property_id' => $propertyId, 'status' => 'Active', 'deleted_at' => null]);


// Merge primary and multi-property users
if (!empty($multiPropUsers)) {
    foreach ($multiPropUsers as $multiUser) {
        $usersData[] = $multiUser;
    }
}


$propertiesData = fetchData('properties', ['id' => $propertyId]);
$doorsData = fetchData('gate_doors', ['property_id' => $propertyId]);
$hardwaresDoorsData = fetchData('visitors', ['property_id' => $propertyId]);
$callsData = fetchData('call_histories',['property_id' => $propertyId]);
$property_user = fetchDataSingle('property_users', ['email' => $_SESSION["email"], 'creator_type' => "users"]);
$is_property_user = empty($property_user) ? false : true;
// $is_property_admin = !empty($property_user) && $property_user[0]["role"] == 7 ? true : false;
$usersLength = is_array($usersData) ? count($usersData) : 0;
$propertiesLength = is_array($propertiesData) ? count($propertiesData) : 0;
$doorsDataLength = is_array($doorsData) ? count($doorsData) : 0;
$hardwaresDoorsDataLength = is_array($hardwaresDoorsData) ? count($hardwaresDoorsData) : 0;
$callsDataLength = is_array($callsData) ? count($callsData) : 0;
?>



<script src="js/dashboard/cameraImages.js"></script>
<div>
    <!-- <div id="cards_error" style="display: none;"></div> -->
    <div class="row pt-3">
        <?php if(!$is_property_user): ?>
        <div class="col-12 col-sm-6 col-md-3 " id="registered_users_container">
            <div class="info-box cursor_pointer" onclick="redirect_to('/dashboard.php?tab=users')">
                <!-- <span class="info-box-icon bg-info elevation-1"><i class="fas fa-cog"></i></span> -->
                <span class="info-box-icon bg-info elevation-1"><i class="fa fa-user"></i></span>
                <div class="info-box-content">
                    <span class="info-box-text">Registered Users</span>
                    <span class="info-box-number" id="registered_users">
                        <?php echo $usersLength ?? 0; ?>
                    </span>
                </div>
            </div>
        </div>
        <?php endif; ?>
        <!-- /.col -->
        <?php if(!$is_property_user): ?>
        <div class="col-12 col-sm-6 col-md-3" id="units_number_container">
            <div class="info-box mb-3 cursor_pointer" onclick="redirect_to('/dashboard.php?tab=my_properties')">
                <span class="info-box-icon bg-success elevation-1"><i class="fa fa-building"></i></span>
                <div class="info-box-content">
                    <span class="info-box-text">Units</span>
                    <span class="info-box-number" id="units_number"><?php echo $propertiesLength ?? 0; ?></span>
                </div>
                <!-- /.info-box-content -->
            </div>
            <!-- /.info-box -->
        </div>
        <?php endif; ?>
        <!-- /.col -->
        <?php if(!$is_property_user): ?>
        <div class="col-12 col-sm-6 col-md-3" id="Door_Gate_number_container">
            <div class="info-box mb-3 cursor_pointer" onclick="redirect_to('dashboard.php?tab=property_builder',true)">
                <span class="info-box-icon bg-primary elevation-1"><i class="fa fa-building"></i></span>
                <div class="info-box-content">
                    <span class="info-box-text">Door/Gate Releases</span>
                    <span class="info-box-number" id="Door_Gate_number"><?php echo $doorsDataLength ?? 0; ?></span>
                </div>
                <!-- /.info-box-content -->
            </div>
            <!-- /.info-box -->
        </div>
        <?php endif; ?>
        <!-- /.col -->

        <!-- fix for small devices only -->
        <div class="clearfix hidden-md-up"></div>


        <div class="col-12 col-sm-6 col-md-3" id="invisible_keys_number_container">
            <div class="info-box mb-3 cursor_pointer" onclick="redirect_to('dashboard.php?tab=visitors')">
                <span class="info-box-icon bg-danger elevation-1"><i class="fa fa-key"></i></span>
                <div class="info-box-content">
                    <span class="info-box-text">Active Invisible keys</span>
                    <span class="info-box-number" id="invisible_keys_number"><?php echo $hardwaresDoorsDataLength ?? 0; ?></span>
                </div>
                <!-- /.info-box-content -->
            </div>
            <!-- /.info-box -->
        </div>
        <!-- /.col -->
        <div class="col-12 col-sm-6 col-md-3" id="calls_number_container">
            <div class="info-box mb-3 cursor_pointer" onclick="redirect_to('/events.php?active=4')">
                <span class="info-box-icon bg-warning elevation-1"><i class="fa fa-phone"></i></span>
                <div class="info-box-content">
                    <span class="info-box-text">Calls from visitors</span>
                    <span class="info-box-number" id="calls_number"><?php echo $callsDataLength ?? 0; ?></span>
                </div>
                <!-- /.info-box-content -->
            </div>
            <!-- /.info-box -->
        </div>
        <!-- /.col -->
        <?php if(!$is_property_user): ?>
        <div class="col-12 col-sm-6 col-md-3" id="voice_call_log_cost_container">
            <div class="info-box cursor_pointer mb-3" onclick="redirect_to('../../events.php?active=4')">
                <a href="../../events.php?active=4">
                    <span class="info-box-icon bg-info elevation-1 " style="height: 100%; text-decoration: none;"><i class="fa fa-phone"></i></span>
                </a>
                <div class="info-box-content">
                    <span class="info-box-text">Voice Call Amount </span>
                    <span class="info-box-number" id="voice_call_log_cost"><?php echo $call_cost ?? 0; ?></span>
                </div>
                
                <!-- /.info-box-content -->
            </div>
            <!-- /.info-box -->
        </div>
        <?php endif; ?>
        <!-- /.col -->
        <?php if(!$is_property_user): ?>
        <div class="col-12 col-sm-6 col-md-3" id="voice_calls_log_duration_container">
            <div class="info-box mb-3 cursor_pointer" onclick="redirect_to('../../events.php?active=4')">
                <a href="../../events.php?active=4">
                    <span class="info-box-icon bg-default elevation-1" style="height: 100%; text-decoration: none;"><i class="fa fa-phone"></i></span>
                </a>
                <div class="info-box-content">
                    <span class="info-box-text">Voice Call Duration</span>
                    <span class="info-box-number" id="voice_calls_log_duration"><?php echo $call_duration ?? 0; ?></span>
                </div>
                <!-- /.info-box-content -->
            </div>
            <!-- /.info-box -->
        </div>
        <?php endif; ?>
        <!-- /.col -->
        <div class="col-12 col-sm-6 col-md-3" id="total_dashboard_sms_container">
            <div class="info-box cursor_pointer mb-3" onclick="redirect_to('../../events.php?active=5')">
                <a href="../../events.php?active=5">
                    <span class="info-box-icon bg-warning elevation-1" style="height: 100%; text-decoration: none;"><i class="fa fa-message"></i></span>
                </a>
                <div class="info-box-content">
                    <span class="info-box-text">Total SMS </span>
                    <span class="info-box-number" id="total_dashboard_sms"><?php echo $messages_amount ?? 0; ?></span>
                </div>
                <!-- /.info-box-content -->
            </div>
            <!-- /.info-box -->
        </div>
        <!-- /.col -->
        <?php if(!$is_property_user): ?>
        <div class="col-12 col-sm-6 col-md-3" id="total_dashboard_sms_amount_container">
            <div class="info-box cursor_pointer mb-3" onclick="redirect_to('../../events.php?active=5')">
                <a href="../../events.php?active=5">
                    <span class="info-box-icon bg-warning elevation-1" style="height: 100%; text-decoration: none;"><i class="fa fa-message"></i></span>
                </a>
                <div class="info-box-content">
                    <span class="info-box-text">Total SMS Amount</span>
                    <span class="info-box-number" id="total_dashboard_sms_amount"><?php echo $messages_amount ?? 0; ?></span>
                </div>
                <!-- /.info-box-content -->
            </div>
            <!-- /.info-box -->
        </div>
        <?php endif;
            if($user_type_layer === "admin" || $user_type_layer === "super_admin" || $user_type_layer === "dealer"){ 
        ?>
            <div class="col-12 col-sm-6 col-md-3" id="total_dashboard_sms_container">
                <div class="info-box cursor_pointer mb-3" onclick="redirect_to('../../settings.php')">
                    <a>
                        <span class="info-box-icon bg-info elevation-1" style="height: 100%; text-decoration: none;"><i class="fa-solid fa-dollar-sign"></i></span>
                    </a>
                    <div class="info-box-content">
                        <span class="info-box-text">Dealer Base Cost </span>
                        <span class="info-box-number" id="dashboard_dealer_cost">Loading...</span>
                    </div>
                    <!-- /.info-box-content -->
                </div>
                <!-- /.info-box -->
            </div>
        <?php } 
            if($user_type_layer !== "user"){
        ?>
        <div class="col-12 col-sm-6 col-md-3" id="total_dashboard_sms_container">
            <div class="info-box cursor_pointer mb-3" onclick="redirect_to('../../settings.php')">
                <a>
                    <span class="info-box-icon bg-info elevation-1" style="height: 100%; text-decoration: none;"><i class="fa-solid fa-dollar-sign"></i></span>
                </a>
                <div class="info-box-content">
                    <span class="info-box-text">MSRP Base Cost </span>
                    <span class="info-box-number" id="dashboard_msrp_cost">Loading...</span>
                </div>
                <!-- /.info-box-content -->
            </div>
            <!-- /.info-box -->
        </div>

        <div class="col-12 col-sm-6 col-md-3" id="total_dashboard_sms_container">
            <div class="info-box cursor_pointer mb-3" onclick="redirect_to('../../settings.php')">
                <a>
                    <span class="info-box-icon bg-info elevation-1" style="height: 100%; text-decoration: none;"><i class="fa-solid fa-dollar-sign"></i></span>
                </a>
                <div class="info-box-content">
                    <span class="info-box-text">Property Balance </span>
                    <span class="info-box-number" id="dashboard_property_balance">Loading...</span>
                </div>
                <!-- /.info-box-content -->
            </div>
            <!-- /.info-box -->
        </div>
        <?php } ?>
        <!-- /.col -->
    </div>

    <div class="" id="streaming">
    </div>
</div>