<?php
include("core/init.php");
include("session.php");
/***********Table Used**********/
$mainTable = "quick_inquries";
$pageName = "quick-inquiries";
/****************Search Filter Start Here***********/
if (!Session::exists("filterQuery")) {
    Session::put("filterQuery", '');
}
if (isset($_POST["filter_province"])) {
    Session::delete("Rfilter_province");
    $filterQuery = "";
    $filterQuery .= 'WHERE ';
    if (Input::getInput("filter_province") != "") {
        Session::put("Rfilter_province", Input::getInput("filter_province"));
        $filterQuery .= " location= '" . Input::getInput("filter_province") . "'";
    } else {
        $filterQuery = "";
    }
    Session::put("filterQuery", $filterQuery);
}
/****************Delete**********************/
if (Input::particularInputExists("get", "deleteId")) {
    if (Db::getInstance()->num_rows("select id from {$mainTable} where id='" . Input::getInput("deleteId") . "'") > 0) {
        Db::getInstance()->delete($mainTable, array("id" => Input::getInput("deleteId")));

        Session::flash("message", '<div class="alert-success" style="padding: 5px;">Quick Inquiry Deleted.</div><Br>');
        Redirect::to($pageName);
    } else {
        Redirect::to($pageName);
    }
}
$dbProvinces = Db::getInstance()->get_results("select id,province from province where isActive=1");

$provinces = array();
if (count($dbProvinces)) foreach ($dbProvinces as $dbProvince) {
    $provinces[$dbProvince["id"]] = $dbProvince["province"];
}
/****************Delete Ends**********************/
?>
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Quick Inquiries - <?php echo Config::get("project/site_title"); ?></title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
    <link rel="shortcut icon" href="dist/img/admin-panel.png">
    <!-- Bootstrap 3.3.6 -->
    <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css">
    <!-- Ionicons -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/ionicons/2.0.1/css/ionicons.min.css">
    <!-- Theme style -->
    <link rel="stylesheet" href="plugins/datatables/dataTables.bootstrap.css">


    <link rel="stylesheet" href="dist/css/AdminLTE.min.css">
    <!-- AdminLTE Skins. Choose a skin from the css/skins folder instead of downloading all of them to reduce the load. -->
    <link rel="stylesheet" href="dist/css/skins/_all-skins.min.css">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
  <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
  <![endif]-->


    <Style>
        #result {
            margin-left: 5px;
        }

        .short {
            color: #FF0000;
        }

        .weak {
            color: #E66C2C;
        }

        .good {
            color: #2D98F3;
        }

        .strong {
            color: #006400;
        }
    </Style>

</head>

<body class="hold-transition skin-blue sidebar-mini">
    <div class="wrapper">




        <?php include("includes/header.php"); ?>






        <?php include("includes/sidebar.php"); ?>


        <!-- Content Wrapper. Contains page content -->
        <div class="content-wrapper">
            <!-- Content Header (Page header) -->
            <section class="content-header">
                <h1>
                    Quick Inquiries
                    <small></small>
                </h1>
                <ol class="breadcrumb">
                    <li><a href="Dashboard"><i class="fa fa-dashboard"></i> Home</a></li>
                    <li class="active">Quick Inquiries</li>
                </ol>
            </section>

            <!-- Main content -->
            <section class="content">

                <div class="row">

                    <div class="col-md-12">

                        <?php echo Session::flashDisplay("message"); ?>

                        <div class="box">
                            <div class="box-header">

                                <div class="col-md-12 col-md-offset-3">
                                    <?php
                                    $newArray = array_combine(array_values($provinces), array_values($provinces));
                                    $newProvinceArray = array("" => "All Locations");
                                    $provinces = $newProvinceArray + $newArray;
                                    ?>
                                    <form id="_filterform" action="<?php echo $pageName; ?>" method="post">
                                        <div class="col-md-3">
                                            <select name="filter_province" class="form-control">
                                                <?php echo Input::getSelected("filter_province", Session::get("Rfilter_province") ? Session::get("Rfilter_province") : '', $provinces); ?>
                                            </select>
                                        </div>

                                        <div class="col-md-3">
                                            <button type="submit" class="btn btn-primary" onclick="filterResults();">Filter</button>
                                        </div>

                                    </form>
                                </div>



                            </div>
                            <!-- /.box-header -->
                            <div class="box-body">
                                <table id="example1" class="table table-bordered table-striped">
                                    <thead>
                                        <tr>
                                            <th>ID</th>
                                            <th>Name</th>
                                            <th>Email</th>
                                            <th>Phone Number</th>
                                            <th>Location</th>
                                            <th>Package Type</th>
                                            <th>Package</th>
                                            <th>Created At</th>
                                            <th>Action</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                        $dbFields = Db::getInstance()->get_results("select * from {$mainTable} " . Session::get("filterQuery") . " ORDER by id DESC");
                                        if (count($dbFields)) {
                                            $dateTimeFrmt = Config::get("formats/dt_tm_report");
                                            foreach ($dbFields as $dbField) {
                                                $createdAt = new DateTime($dbField["date_time"]);
                                                echo '<tr>';
                                                echo '<td>' . $dbField["id"] . '</td>';
                                                echo '<td>' . $dbField["name"] . '</td>';
                                                echo '<td><a href="mailto:' . $dbField["email"] . '">' . $dbField["email"] . '</a></td>';
                                                echo '<td>' . $dbField["phone"] . '</td>';
                                                echo '<td>' . $dbField["location"] . '</td>';
                                                echo '<td>' . $dbField["package_type"] . '</td>';
                                                echo '<td>' . $dbField["package"] . '</td>';
                                                echo '<td>' . $createdAt->format($dateTimeFrmt) . '</td>';
                                                echo '<td><a href="' . $pageName . '?deleteId=' . $dbField["id"] . '" onclick="return confirm(\'Are you sure you want to delete Quick Inquiry?\');" title="Delete"> <i class="fa fa-trash fa-lg" aria-hidden="true"></i></a>&nbsp;&nbsp;<a href="javascript:void(0)" onclick=openViewDetailPopup(' . $dbField["id"] . ') title="View"> <i class="fa fa-eye fa-lg" aria-hidden="true"></i></a></td>';
                                        ?>
                                        <?php
                                                echo '</tr>';
                                            }
                                        }
                                        ?>
                                    </tbody>
                                </table>
                            </div>
                            <!-- /.box-body -->
                        </div>

                    </div>










                </div>





            </section>
            <!-- /.content -->
        </div>
        <!-- /.content-wrapper -->

        <?php include("includes/footer.php"); ?>


        <!-- Add the sidebar's background. This div must be placed
       immediately after the control sidebar -->
        <div class="control-sidebar-bg"></div>
    </div>
    <!-- ./wrapper -->

    <!-- jQuery 2.2.3 -->
    <script src="plugins/jQuery/jquery-2.2.3.min.js"></script>
    <!-- Bootstrap 3.3.6 -->
    <script src="bootstrap/js/bootstrap.min.js"></script>
    <!-- Slimscroll -->
    <script src="plugins/slimScroll/jquery.slimscroll.min.js"></script>
    <!-- FastClick -->
    <script src="plugins/fastclick/fastclick.js"></script>
    <!-- AdminLTE App -->
    <script src="dist/js/app.min.js"></script>
    <!-- AdminLTE for demo purposes -->
    <script src="dist/js/demo.js"></script>
    <script src="plugins/datatables/jquery.dataTables.min.js"></script>
    <script src="plugins/datatables/dataTables.bootstrap.min.js"></script>
    <script>
        $(function() {
            $("#example1").DataTable({
                "order": [
                    [0, "desc"]
                ] // Sort by first column (index 0)
            });
        });

        function filterResults() {
            $("#_filterform").submit();
            return true;
        }

        // View Detail
        function openViewDetailPopup(id) {
            const url = 'quick_inquiry_detail.php?id=' + encodeURIComponent(id);
            window.open(
                url,
                'popupWindow',
                'width=650,height=500,scrollbars=yes,resizable=yes'
            );
        }
    </script>

</body>

</html>