<?php
/**
 * home-packages.php -- Home Packages
 */

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>Home Packages - 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>
    <!-- loader Start -->
    <div id="loading">
        <div id="loading-center">
        </div>
    </div>
    <!-- loader END -->
    <!-- Wrapper Start -->
    <div class="wrapper">
        <!-- Sidebar  -->
        <?php include 'navbar.php' ?>
        <!-- TOP Nav Bar END -->
        <!-- Page Content  -->
        <div id="content-page" class="content-page">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-sm-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;">Home Packages</h4>
                                </div>
                            </div>
                            <div class="iq-card-body">

                                <?php
                                if ( isset($_GET['edit'], $_GET['id']) ){
                                    $package_id = intval($_GET['id']);
                                    $getPackage = $db_con->query("SELECT * FROM home_packages WHERE package_id='$package_id'");
                                    $packageRow = $getPackage->fetch(PDO::FETCH_ASSOC);
                                ?>
                                    <form action="" method="post" enctype="multipart/form-data">
                                        <div class="col-sm-12 col-md-6">
                                            <div class="form-group col-sm-12">
                                                <label>Title:</label>
                                                <input type="text" class="form-control" name="p_title" value="<?= $packageRow['p_title']; ?>">
                                            </div>

                                            <div class="form-group col-sm-12">
                                                <label>Description:</label>
                                                <input type="text" class="form-control" name="p_description" value="<?= $packageRow['p_description']; ?>">
                                            </div>

                                            <div class="form-group col-sm-6">
                                                <label>Amount:</label>
                                                <input type="text" class="form-control" name="p_amount" value="<?= $packageRow['p_amount']; ?>">
                                            </div>

                                            <div class="form-group col-sm-12">
                                                <label>Option 1:</label>
                                                <input type="text" class="form-control" name="p_opt1" value="<?= $packageRow['p_opt1']; ?>">
                                            </div>
                                            <div class="form-group col-sm-12">
                                                <label>Option 2:</label>
                                                <input type="text" class="form-control" name="p_opt2" value="<?= $packageRow['p_opt2']; ?>">
                                            </div>
                                            <div class="form-group col-sm-12">
                                                <label>Option 3:</label>
                                                <input type="text" class="form-control" name="p_opt3" value="<?= $packageRow['p_opt3']; ?>">
                                            </div>
                                            <div class="form-group col-sm-12">
                                                <label>Option 4:</label>
                                                <input type="text" class="form-control" name="p_opt4" value="<?= $packageRow['p_opt4']; ?>">
                                            </div>
                                            <div class="form-group col-sm-12">
                                                <label>Option 5:</label>
                                                <input type="text" class="form-control" name="p_opt5" value="<?= $packageRow['p_opt5']; ?>">
                                            </div>
                                            <div class="form-group col-sm-12">
                                                <label>Option 6:</label>
                                                <input type="text" class="form-control" name="p_opt6" value="<?= $packageRow['p_opt6']; ?>">
                                            </div>
                                            <div class="form-group col-sm-12">
                                                <label>Option 7:</label>
                                                <input type="text" class="form-control" name="p_opt7" value="<?= $packageRow['p_opt7']; ?>">
                                            </div>
                                            <div class="form-group col-sm-12">
                                                <label>Option 8:</label>
                                                <input type="text" class="form-control" name="p_opt8" value="<?= $packageRow['p_opt8']; ?>">
                                            </div>

                                            <div class="form-group col-sm-6">
                                                <button type="submit" name="update_package" class="btn btn-primary float-right" value="Submit">Update</button>
                                            </div><br>
                                        </div>
                                    </form>
                                    <?php
                                    if ( isset($_POST['update_package']) ){
                                        error_reporting(E_ALL);

                                        $p_title = strip_tags($_POST['p_title']);
                                        $p_description = strip_tags(addslashes($_POST['p_description']));
                                        $p_amount = strip_tags(addslashes($_POST['p_amount']));

                                        $p_opt1 = strip_tags(addslashes($_POST['p_opt1']));
                                        $p_opt2 = strip_tags(addslashes($_POST['p_opt2']));
                                        $p_opt3 = strip_tags(addslashes($_POST['p_opt3']));
                                        $p_opt4 = strip_tags(addslashes($_POST['p_opt4']));
                                        $p_opt5 = strip_tags(addslashes($_POST['p_opt5']));
                                        $p_opt6 = strip_tags(addslashes($_POST['p_opt6']));
                                        $p_opt7 = strip_tags(addslashes($_POST['p_opt7']));
                                        $p_opt8 = strip_tags(addslashes($_POST['p_opt8']));

                                        $query = $db_con->prepare("UPDATE home_packages SET p_title='$p_title', p_description='$p_description', p_amount='$p_amount', p_opt1='$p_opt1', p_opt2='$p_opt2', p_opt3='$p_opt3', p_opt4='$p_opt4', p_opt5='$p_opt5', p_opt6='$p_opt6', p_opt7='$p_opt7', p_opt8='$p_opt8' WHERE package_id='".$packageRow['package_id']."'");
                                                
                                        if ( $query->execute() ){
                                    ?>
                                            <script>
                                                setTimeout(function(){
                                                    noticePop('Package Updated', 'success');
                                                }, 2000);
                                            </script>
                                    <?php
                                        } else {
                                    ?>
                                            <script>
                                                setTimeout(function(){
                                                    noticePop('Something Went Wrong. Try again.', 'error');
                                                }, 2000);
                                            </script>
                                    <?php
                                        }
                                    }
                                    ?>
                                <?php
                                } else {
                                ?>
                                    <div class="table-responsive">
                                        <div class="row justify-content-between">
                                            <div class="col-sm-12 col-md-6">
                                                <div id="user_list_datatable_info" class="dataTables_filter">
                                                    <form class="mr-3 position-relative">
                                                        <div class="form-group mb-0">
                                                            <input type="search" class="form-control" id="exampleInputSearch" placeholder="Search" aria-controls="user-list-table">
                                                        </div>
                                                    </form>
                                                </div>
                                            </div>
                                        </div>
                                        <table id="user-list-table" class="table table-striped table-bordered mt-4" role="grid" aria-describedby="user-list-page-info">
                                            <thead>
                                                <tr>
                                                    <th>Title</th>
                                                    <th>Description</th>
                                                    <th>Amount</th>
                                                    <th>Action</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                            <?php
                                            // Packages
                                            $packages = $db_con->prepare("SELECT * FROM home_packages");

                                            if ( $packages->execute() ){
                                                $i = 0;
                                                while ( $package = $packages->fetch(PDO::FETCH_ASSOC) ){
                                                    $i++;
                                            ?>
                                                    <tr>
                                                        <td><?= $package['p_title']; ?></td>
                                                        <td><?= $package['p_description']; ?></td>
                                                        <td><?= $package['p_amount']; ?></td>
                                                        <td class="text-center">
                                                            <div class="d-flex align-items-center list-user-action">
                                                                <a class="iq-bg-primary" data-toggle="tooltip" data-placement="top" title="" data-original-title="Edit" href="?edit&id=<?= $package['package_id']; ?>"><i class="ri-pencil-line"></i></a>
                                                            </div>
                                                        </td>
                                                    </tr>
                                            <?php
                                                }
                                            }
                                            ?>
                                            </tbody>
                                        </table>
                                    </div>
                                <?php } ?>
                            </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 = 'home-packages';
            });
        }

        function PublishTestimony( action_type, tid ){
            $.ajax({
                url: API,
                type: 'POST',
                //dataType: 'JSON',
                data: {
                    publish_testimony: 'publish_testimony',
                    action_type: action_type,
                    testimony_id: tid
                },
                cache: false
            }).done( function( result ){
                if ( result.error == false && result.notice == 'changed' ){
                    noticePop( result.message, 'success' );
                }
                else if ( result.error == true && result.notice == 'failed' ){
                    noticePop( result.message, 'error' );
                }
            });
        }
    </script>

</body>
</html>