<?php
/**
 * upgrade-requests.php -- Upgrade Requests
 */

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;
}

?>

<!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>Upgrade Requests - 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 Requests Table</h4>
                                </div>
                            </div>
                            <div class="iq-card-body">
                                <p>All Transactions are saved here for refrencing. You can also <code>print</code> Tranactions</p>
                                <div class="table-responsive">
                                    <table id="datatable" class="table table-striped table-bordered">
                                        <thead>
                                            <tr>
                                                <th>Username</th>
                                                <th>Current Plan</th>
                                                <th>New Plan</th>
                                                <th>Proof</th>
                                                <th>Narration</th>
                                                <th>Date</th>
                                                <th>Action</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                        <?php
                                        //$transactions = $db_con->prepare("SELECT * FROM user_transactions WHERE type='Deposit' AND status='Pending' ORDER BY created DESC");
                                        //$upgradePlanFR = $db_con->prepare("SELECT * FROM user_upgrade_plans_feerequest WHERE status='Pending' ORDER BY requested_datetime DESC");
                                        /*$upgradePlans = $db_con->query(
                                            "SELECT up.up_id, up.plan_name, upfr.upfr_id, upfr.up_id, upfr.user_id, upfr.fee
                                            FROM user_upgrade_plans up 
                                            LEFT JOIN user_upgrade_plans_feerequest upfr
                                            ON up.up_id=upfr.up_id
                                            WHERE upfr.user_id='".$userRow['id']."' AND publish='Yes'"
                                        );*/
                                        $upgradePlanFR = $db_con->query(
                                            "SELECT upfr.*, up.up_id, up.plan_name, upfr.upfr_id
                                            FROM user_upgrade_plans_feerequest upfr
                                            LEFT JOIN user_upgrade_plans up
                                            ON upfr.up_id=up.up_id
                                            WHERE status='Pending'
                                            ORDER BY requested_datetime DESC"
                                        );

                                        if ( $upgradePlanFR->execute() ){
                                            while ( $upfr = $upgradePlanFR->fetch(PDO::FETCH_ASSOC) ){
                                                $users = $db_con->query("SELECT username, current_plan FROM users WHERE id='".$upfr['user_id']."'");
                                                $user = $users->fetch(PDO::FETCH_ASSOC);
                                        ?>
                                                <tr id="tr-<?= $upfr['upfr_id']; ?>">
                                                    <td><?= $user['username']; ?></td>
                                                    <td><?= $user['current_plan']; ?></td>
                                                    <td><?= $upfr['plan_name']; ?></td>
                                                    <td>
                                                        <?php if ( !empty($upfr['file']) ){ ?>
                                                            <a href="../wp-user/uploads/<?= $upfr['file']; ?>" target="_blank">View Proof</a>
                                                        <?php } else { echo 'Not Available'; } ?>
                                                    </td>
                                                    <td><?= $upfr['narration']; ?></td>
                                                    <td>
                                                        <!--<?= date('h:iA', strtotime($upfr['requested_datetime'])); ?>-->
                                                        <!--<input type="date" class="form-control" id="ddate-<?= $upfr['upfr_id']; ?>" value="<?= $upfr['requested_datetime']; ?>">-->
                                                        <input type="text" class="form-control" id="ddate-<?= $upfr['upfr_id']; ?>" value="<?= date('Y/m/d', strtotime($upfr['requested_datetime'])); ?>">
                                                    </td>
                                                    <td>
                                                        <button class="btn btn-sm btn-primary" type="button" onclick="AppDec('approve', <?= $upfr['upfr_id']; ?>, <?= $upfr['up_id']; ?>, '<?= $upfr['plan_name']; ?>', <?= $upfr['user_id']; ?>, $('#ddate-<?= $upfr['upfr_id']; ?>').val())">Approve</button>
                                                        <button class="btn btn-sm btn-primary" type="button" onclick="AppDec('decline', <?= $upfr['upfr_id']; ?>, <?= $upfr['up_id']; ?>, '<?= $upfr['plan_name']; ?>', <?= $upfr['user_id']; ?>, $('#ddate-<?= $upfr['upfr_id']; ?>').val())">Decline</button>
                                                    </td>
                                                </tr>
                                        <?php
                                            }
                                        }
                                        ?>
                                        </tbody>
                                        <tfoot>
                                            <tr>
                                                <th>Username</th>
                                                <th>Current Plan</th>
                                                <th>New Plan</th>
                                                <th>Proof</th>
                                                <th>Narration</th>
                                                <th>Date</th>
                                                <th>Action</th>
                                            </tr>
                                        </tfoot>
                                    </table>
                                    <button type="button" class="btn btn-primary mr-2" onclick="window.print()"><i class="las la-download"></i>Print Transactions</button>
                                </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, type ){
            swal({
                html: '<span style="font-size:18px!important;">'+ `${value}` +'</span>',
                type: `${type}`,
                timer: 4000,
                showConfirmButton: false,
                allowOutsideClick: false,
                allowEscapeKey: false,
            }).then(function(){
                //window.location.href = 'pending-deposits';
            });
        }

        function AppDec( action, upfrid, upid, plan_name, uid, requested_datetime ){
            //var requested_datetime = $('#ddate-'+upfrid).val();
            $.ajax({
                url: API,
                type: 'POST',
                //dataType: 'JSON',
                data: {
                    upgrade_request : 'upgrade_request',
                    action_type : action,
                    upfr_id : upfrid,
                    up_id : upid,
                    plan_name : plan_name,
                    user_id : uid,
                    requested_datetime : requested_datetime
                },
                cache: false
            }).done( function( result ){
                if ( result.error == false && result.notice == 'approved' ){
                    document.getElementById('tr-'+upfrid).style.display = 'none'; // hide table row (tr)
                    noticePop( result.message, 'success' );
                }
                else if ( result.error == false && result.notice == 'declined' ){
                    document.getElementById('tr-'+upfrid).style.display = 'none'; // hide table row (tr)
                    noticePop( result.message, 'success' );
                }
                else if ( result.error == true && result.notice == 'failed' ){
                    noticePop( result.message, 'error' );
                }
            });
        }
    </script>

</body>
</html>