<?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["getBaseCost"]) && ($_POST["getBaseCost"] == "yes")) {
        $invoice_date = "";
        if(isset($_POST['invoice_date']) && !empty($_POST['invoice_date'])){
            $invoice_date = $_POST['invoice_date'];
        }

        $base_sub = getBaseSubscription($pdo, $userId, $userLayer, $invoice_date);

        echo json_encode($base_sub);
    }

    if(isset($_POST['save_sub_form']) && $_POST['save_sub_form'] == "yes"){
        $edited_by = $_POST['edited_by']; 
        $sub_fee = $_POST['sub_fee'];
        $camera_add = $_POST['camera_add'];
        $number_add = $_POST['number_add'];
        $door_add = $_POST['door_add'];
        $msrp_sub_fee = $_POST['msrp_sub_fee'];
        $msrp_camera_add = $_POST['msrp_camera_add'];
        $msrp_number_add = $_POST['msrp_number_add'];
        $msrp_door_add = $_POST['msrp_door_add'];
        $cellular_sim = $_POST['cellular_sim'];
        $msrp_cellular_sim = $_POST['msrp_cellular_sim'];
        $subscription_type = $_POST['subscription_type'];
        $save_data = SaveBaseSubscription($pdo, $userId, $userLayer, $sub_fee, $camera_add, $number_add, $door_add, $msrp_sub_fee, $msrp_camera_add, $msrp_number_add, $msrp_door_add, $cellular_sim, $msrp_cellular_sim, $subscription_type, $edited_by);
        echo json_encode($save_data);
    }

    if(isset($_POST['save_balance_form']) && $_POST['save_balance_form'] == "yes"){
        $minimum_balance = $_POST['minimum_balance'];
        $save_data = SaveMinimumBalance($pdo, $userId, $userLayer, $minimum_balance);
        echo json_encode($save_data);
    
    }    
    if(isset($_POST['payment_proof']) && $_POST['payment_proof'] == 'yes'){
        if (isset($_FILES['payment_image']) && $_FILES['payment_image']['error'] == 0){
            $amount = $_POST['recharge_amount'];
            $payment_image = 'payment_image';
            $save_image = savePyamentProof($pdo, $userId, $userLayer, $payment_image, $amount);
            echo json_encode($save_image);
        }
        else{
            return 0;
        }
    }

    if(isset($_POST['get_payment_history']) && $_POST['get_payment_history'] == 'yes'){
        $payment_history = getPaymentHistory($pdo, $userId, $userLayer, $property_id);
        echo json_encode($payment_history);
    }

    if(isset($_POST['get_invoice_history']) && $_POST['get_invoice_history'] == 'yes'){
        $invoice_history = getInvoiceHistory($pdo, $userId, $userLayer, $property_id);
        echo json_encode($invoice_history);
    }

    if(isset($_POST['getPendingPayment']) && $_POST['getPendingPayment'] == 'yes'){
        $pending_payments = getPendingPayment($pdo, $userId, $userLayer);
        echo json_encode($pending_payments);
    }

    if(isset($_POST['approve_pending_payment']) && $_POST['approve_pending_payment'] == 'yes'){
        $payment_id = $_POST['payment_id']; 
        $amount = $_POST['amount']; 
        $approved = approvePendingPayment($pdo, $userId, $userLayer, $payment_id, $amount);
        echo json_encode($approved);
    }

    if(isset($_POST['add_sim']) && $_POST['add_sim'] == 'yes'){
        $qty = $_POST['qty']; 
        $add = addCellularSim($pdo, $userId, $userLayer, $qty);
        echo json_encode($add);
    }


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