<?php
/**
 * userUpgradePlan.php -- User selected Upgrade Plan
 */

include '../inc/config.inc.php'; // Configuration

ob_start();
session_start();

//date_default_timezone_set('Africa/Lagos');

error_reporting(E_ALL);
ini_set('display_errors', 1);

if ( !isset($_SESSION['admin']) || $_SESSION["loggedin"] !== true || trim($_SESSION['admin']) == '' ){
	//header('Location: ' . $BASE_URL . 'wp-admin/');
    header('Location: ' . $BASE_URL . 'signin');
	exit;
}

if ( isset($_GET['uid']) AND isset($_GET['upid']) ){
    $user_id = intval($_GET['uid']);
    $plan_id = intval($_GET['upid']);

    // Users
    $users = $db_con->query("SELECT id, fullname, currency, current_plan FROM users WHERE id='$user_id'");
    $user = $users->fetch(PDO::FETCH_ASSOC);

    // Plans
    $plans = $db_con->query("SELECT * FROM user_upgrade_plans WHERE up_id='$plan_id'");
    $plan = $plans->fetch(PDO::FETCH_ASSOC);

?>

<!doctype html>
<html lang="en">
<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title><?= $plan['plan_name']; ?> | User Upgrade Plans - Alert Signals Nation</title>
    <!-- Favicon -->

    <link rel="shortcut icon" href="../../img/Signals-Alert-3.png" />
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <!-- Typography CSS -->
    <link rel="stylesheet" href="css/typography.css">
    <!-- Style CSS -->
    <link rel="stylesheet" href="css/style.css">
    <!-- Responsive CSS -->
    <link rel="stylesheet" href="css/responsive.css">

    <script src="../plugins/sweetalerts/promise-polyfill.js"></script>
    <link href="../plugins/sweetalerts/sweetalert.css" rel="stylesheet" type="text/css" />

    <link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,600,700" rel="stylesheet" />
    <style>
        /*------ Google Font Style ------ */
        body {font-family:'Roboto'!important;}
    </style>

</head>
<body class="sidebar-main-active right-column-fixed">
    <!-- loader Start -->
    <!--<div id="loading">
        <div id="loading-center">
        </div>
    </div>-->
    <!-- loader END -->
    <!-- Wrapper Start -->
    <div class="wrapper">
        <!-- Sidebar  -->
        <?php include 'navbar.php' ?>
        <!-- Page Content  -->
        <div id="content-page" class="content-page">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-sm-12 col-lg-12">
                        <div class="iq-card">
                            <div class="iq-card-header d-flex justify-content-between">
                                <div class="iq-header-title">
                                    <h4 class="card-title" style="font-family: 'Roboto'!important;">Upgrade Plan "<?= $plan['plan_name']; ?>" - Current Plan: <?= $user['current_plan']; ?></h4>
                                </div>
                            </div>
                            <div class="iq-card-body">
                                <div class="row">
                                    <div class="col-sm-9">
                                        <div class="tab-content mt-0" id="v-pills-tabContent">
                                            <div class="tab-pane fade show active" id="v-pills-home" role="tabpanel" aria-labelledby="v-pills-home-tab">
                                                
                                            <?php
                                            // Get User Upgrade Plans
                                            $userUP = $db_con->query("SELECT * FROM user_upgrade_plans_feerequest WHERE up_id='$plan_id' AND user_id='$user_id'");
                                            $curPlan = $userUP->fetch(PDO::FETCH_ASSOC);

                                            if ( $userUP->rowCount() == 0 ){ // User has no Plan at all. List all Plans to add one for user
                                            ?>
                                                <div class="form-group">
                                                    <label>"<?= $plan['plan_name']; ?>" Fee (<?= $user['currency']; ?>)</label>
                                                    <input type="text" class="form-control" id="fee-<?= $plan['up_id']; ?>" placeholder="0.00">
                                                </div>
                                                <button type="button" onclick="UpdateUpFee('new', 0, <?= $plan['up_id']; ?>, <?= $user_id; ?>, $('#fee-<?= $plan['up_id']; ?>').val())" class="btn btn-primary">Add New Plan "<?= $plan['plan_name']; ?>"</button>
                                            <?php
                                            } else { // User has at least one Plan
                                            ?>
                                                <div class="form-group">
                                                    <label>"<?= $plan['plan_name']; ?>" Fee (<?= $user['currency']; ?>)</label>
                                                    <input type="text" class="form-control" id="fee-<?= $curPlan['up_id']; ?>" value="<?= $curPlan['fee']; ?>">
                                                </div>
                                                <button type="button" onclick="UpdateUpFee('update', <?= $curPlan['upfr_id']; ?>, <?= $curPlan['up_id']; ?>, <?= $user_id; ?>, $('#fee-<?= $curPlan['up_id']; ?>').val())" class="btn btn-primary">Update "<?= $plan['plan_name']; ?>"</button><br>
                                                <hr>
                                                Allow Withdraw? <button type="button" onclick="AllowWithdraw('Yes', <?= $curPlan['upfr_id']; ?>, <?= $curPlan['up_id']; ?>, <?= $user_id; ?>)" class="btn btn-primary" <? if ($curPlan['allow_withdraw']=='Yes') echo 'disabled'; ?>>Yes</button> <button type="button" onclick="AllowWithdraw('No', <?= $curPlan['upfr_id']; ?>, <?= $curPlan['up_id']; ?>, <?= $user_id; ?>)" class="btn btn-primary" <? if ($curPlan['allow_withdraw']=='No') echo 'disabled'; ?>>No</button>
                                            <?php
                                            }
                                            ?>

                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!-- Wrapper END -->
    <!-- Footer -->
    <?php include 'footer.php' ?>
    <!-- color-customizer END -->
    <!-- Optional JavaScript -->
    <script src="../plugins/jquery-3.1.1.min.js"></script>
    <script src="../plugins/sweetalerts/sweetalert2.min.js"></script>
    <script src="https://unpkg.com/@popperjs/core@2/dist/umd/popper.js"></script>

    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="js/jquery.min.js"></script>
    <script src="js/popper.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <!-- Appear JavaScript -->
    <script src="js/jquery.appear.js"></script>
    <!-- Countdown JavaScript -->
    <script src="js/countdown.min.js"></script>
    <!-- Counterup JavaScript -->
    <script src="js/waypoints.min.js"></script>
    <script src="js/jquery.counterup.min.js"></script>
    <!-- Wow JavaScript -->
    <script src="js/wow.min.js"></script>
    <!-- Apexcharts JavaScript -->
    <script src="js/apexcharts.js"></script>
    <!-- Slick JavaScript -->
    <script src="js/slick.min.js"></script>
    <!-- Select2 JavaScript -->
    <script src="js/select2.min.js"></script>
    <!-- Owl Carousel JavaScript -->
    <script src="js/owl.carousel.min.js"></script>
    <!-- Magnific Popup JavaScript -->
    <script src="js/jquery.magnific-popup.min.js"></script>
    <!-- Smooth Scrollbar JavaScript -->
    <script src="js/smooth-scrollbar.js"></script>
    <!-- lottie JavaScript -->
    <script src="js/lottie.js"></script>
    <!-- am core JavaScript -->
    <script src="js/core.js"></script>
    <!-- am charts JavaScript -->
    <script src="js/charts.js"></script>
    <!-- am animated JavaScript -->
    <script src="js/animated.js"></script>
    <!-- am kelly JavaScript -->
    <script src="js/kelly.js"></script>
    <!-- am maps JavaScript -->
    <script src="js/maps.js"></script>
    <!-- am worldLow JavaScript -->
    <script src="js/worldLow.js"></script>
    <!-- Style Customizer -->
    <script src="js/style-customizer.js"></script>
    <!-- Chart Custom JavaScript -->
    <script src="js/chart-custom.js"></script>
    <!-- Custom JavaScript -->
    <script src="js/custom.js"></script>

    <script>
        // BASE URL and API Inclusion
        const BASE_URL = '<?= $BASE_URL; ?>';
        const API = `${BASE_URL}api/admin.php`;

        function noticePop( value ){
            swal({
                html: '<span style="font-size:18px!important;">Successfully updated "'+ `${value}` +'" <br>for <b><?= $user['fullname']; ?></b></span>',
                type: 'success',
                timer: 4000,
                showConfirmButton: false,
                allowOutsideClick: false,
                allowEscapeKey: false,
            }).then(function(){
                window.location.href = 'userUpgradePlan?uid=<?= $user['id']; ?>&upid=<?= $plan['up_id']; ?>';
            });
        }

        function UpdateUpFee( action_type, upfrid, upid, uid, fee ){
            $.ajax({
                url: API,
                type: 'POST',
                //dataType: 'JSON',
                data: {
                    update_up_fee : 'update_up_fee',
                    action_type : action_type,
                    upfr_id : upfrid,
                    up_id : upid,
                    user_id : uid,
                    fee: fee
                },
                cache: false
            }).done( function( result ){
                if ( result.error == false && result.notice == 'inserted' ){
                    noticePop( result.message, 'success' );
                }
                else if ( result.error == false && result.notice == 'updated' ){
                    noticePop( result.message, 'success' );
                }
                else if ( result.error == true && result.notice == 'failed' ){
                    noticePop( result.message, 'error' );
                }
            });
        }

        function AllowWithdraw( action_type, upfrid, upid, uid ){
            $.ajax({
                url: API,
                type: 'POST',
                //dataType: 'JSON',
                data: {
                    allow_withdraw_plan: 'allow_withdraw_plan',
                    action_type: action_type,
                    upfr_id : upfrid,
                    up_id : upid,
                    user_id : uid
                },
                cache: false
            }).done( function( result ){
                if ( result.error == false && result.notice == 'updated' ){
                    noticePop( result.message, 'success' );
                }
                else if ( result.error == true && result.notice == 'failed' ){
                    noticePop( result.message, 'error' );
                }
            });
        }
    </script>

</body>
</html>

<?php
}
?>