<?php
session_start();
if (isset($_SESSION['userid'])) {
    require_once('../../db.php');
    require_once('payment_functions.php');
    require_once('../dashboard/signalwire_data.php');
    $userId = $_SESSION['userid'];
    $userLayer = $_SESSION['layer'];
    $property_id = $_SESSION['property'];
    if (isset($_POST["getdata"])) {
        $page = $_POST['page'];
        $payment_date = $_POST['payment_date'];
        if($page != ""){
            $payment_date = format_date($page, $payment_date);
        }
        $data['user_layer'] = $userLayer;
        $data['user'] = $_SESSION;
        $dealer = "";
        $get_dealer_property = getDealaer($property_id, $userLayer);
        $property = $get_dealer_property['property'];
        $dealer = $get_dealer_property['dealer'];
        $property = $get_dealer_property['property'];
        
        $s_date = $payment_date . "-01";
        $e_date = $payment_date . date('-t');
        $data['payment_date'] = $payment_date;
        $f_date = DateTime::createFromFormat('Y-m', $payment_date);
        $data['formatted_date'] = $f_date->format('F Y');
        $call_message_data = get_voice_calls_amount($property_id, $s_date, $e_date);
        if($call_message_data != 0){
            $data['calls_duration'] = $call_message_data['calls']['call_duration'];
            $data['calls_amount'] = $call_message_data['calls']['call_amount'];
            $data['call_unit_price'] = $call_message_data['calls']['call_unit_price'];
            $data['messages_count'] = $call_message_data['messages']['total_messages'];
            $data['messages_amount'] = $call_message_data['messages']['messages_amount'];       
            $data['message_unit_amount'] = $call_message_data['messages']['message_unit_amount'];  
        }
        else{
            $data['calls_duration'] = 0;
            $data['calls_amount'] = 0;
            $data['call_unit_price'] = 0;
            $data['messages_count'] = 0;
            $data['messages_amount'] = 0;       
            $data['message_unit_amount'] = 0; 
        }

        $data['price_1'] = get_payment_data($property_id, '1');
        $data['price_2'] = get_payment_data($property_id, '2');
        $data['price_3'] = get_payment_data($property_id, '3');
        $data['price_4'] = get_payment_data($property_id, '4');
        $data['price_5'] = get_payment_data($property_id, '5');
        $data['price_6'] = get_payment_data($property_id, '6');
        $data['price_7'] = get_payment_data($property_id, '7');
        $data['dealer'] = $dealer;
        $data['property'] = $property;


        echo json_encode($data);
    }

    if (isset($_POST["getvariablecostdata"])) {
        $page = $_POST['page'];
        $payment_date = $_POST['payment_date'];
        if($page != ""){
            $payment_date = format_date($page, $payment_date);
        }
        $data['user_layer'] = $userLayer;
        $data['user'] = $_SESSION;
        $dealer = "";
        $get_dealer_property = getDealaer($property_id, $userLayer);
        $property = $get_dealer_property['property'];
        $data['property'] = $property;
        // $dealer = $get_dealer_property['dealer'];
        
        $s_date = $payment_date . "-01";
        $e_date = $payment_date . date('-t');
        $data['payment_date'] = $payment_date;
        $f_date = DateTime::createFromFormat('Y-m', $payment_date);
        $data['formatted_date'] = $f_date->format('F Y');
        $call_message_data = get_voice_calls_amount($property_id, $s_date, $e_date);
        
        if($call_message_data != 0){
            $data['calls_duration'] = $call_message_data['calls']['call_duration'];
            $data['calls_amount'] = $call_message_data['calls']['call_amount'];
            $data['call_unit_price'] = $call_message_data['calls']['call_unit_price'];
            $data['messages_count'] = $call_message_data['messages']['total_messages'];
            $data['messages_amount'] = $call_message_data['messages']['messages_amount'];       
            $data['message_unit_amount'] = $call_message_data['messages']['message_unit_amount'];  
        }
        else{
            $data['calls_duration'] = 0;
            $data['calls_amount'] = 0;
            $data['call_unit_price'] = 0;
            $data['messages_count'] = 0;
            $data['messages_amount'] = 0;       
            $data['message_unit_amount'] = 0; 
        }
        $data['property'] = $property;
        echo json_encode($data);
    }

    if(isset($_POST['update_price'])){
        if(empty($property_id)){
            return "Please select property";
        }
        $product_id = $_POST['product_id'];
        $dealer_price = $_POST['prodct_unit_price'];
        $end_user_price = $_POST['msrp_price'];
        $product_qty = $_POST['product_qty'];
        $update_price = update_product_price($property_id, $product_id, $dealer_price, $end_user_price, $product_qty);
        return $update_price;
    }
    

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

