<?php
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

if (!isset($_SESSION['username'])) {
    header("location: /login");
}

$user = $_SESSION['user'];

$CI = &get_instance();
$CI->load->model('Post_model');

if (isset($_POST['title'])) {
    $title = $_POST['title'];
    $posttext = $_POST['posttext'];
    $upload_date = $_POST['upload_date'];
    $uploaded_by = $_POST['uploaded_by'];
    
    if($posttext == ""){
        $error = 'error';
        $msg = 'Post cannot be empty!';
    header("location: add_post?msg=" . $msg . "!&type=" . $error."");
    }
    $msg_response = $CI->Post_model->save_post($title, $posttext, $upload_date, $uploaded_by);
    $msg = $msg_response['message'];
    if ($msg_response['data']) {
        $error = 'confirmation';
    } else {
        $error = 'error';
    }
    header("location: add_post?msg=" . $msg . "!&type=" . $error."");
    exit();
}

?>
<!-- Begin Page Content -->
<div class="container-fluid">

    <div class="d-sm-flex align-items-center justify-content-between mb-4">
        <h1 class="h3 mb-0 text-gray-800">Users</h1>
        <a href="#" class="d-none d-sm-inline-block btn btn-sm btn-primary shadow-sm"><i class="fas fa-download fa-sm text-white-50"></i> Generate Report</a>
    </div>

    <!-- Content Row -->
    <div class="row">

        <!-- Earnings (Monthly) Card Example -->
        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card border-left-primary shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-primary text-uppercase mb-1">
                                Total Users</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo $_SESSION['countUsers'] ?></div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-user fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Earnings (Monthly) Card Example -->
        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card border-left-success shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-success text-uppercase mb-1">
                                Sermons</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo $_SESSION['countSermon'] ?></div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-book fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Earnings (Monthly) Card Example -->
        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card border-left-info shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-success text-uppercase mb-1">
                                Categories</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo $_SESSION['countServices'] ?></div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-clipboard-list fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Pending Requests Card Example -->
        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card border-left-warning shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-warning text-uppercase mb-1">
                                Last Login</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo $_SESSION['user_login'] ?></div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-clock fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-xl-10 col-lg-12 col-md-8">
            <div class="card o-hidden border-0 shadow-lg my-5">
                <div class="card-body p-0">
                    <!-- Nested Row within Card Body -->
                    <div class="row">
                        <div class="col-lg-12">
                            <div class="p-5">
                                <div class="text-center">
                                    <h1 class="h4 text-gray-900 mb-4">Upload Pastor's Desk</h1>
                                </div>
                                <form class="user" method="post" action="">
                                    <p> <?php
                                        if (isset($_REQUEST['msg']) && isset($_REQUEST['type'])) {

                                            if ($_REQUEST['type'] == "error")
                                                $msg = "<div class='col-lg-6 mb-4'><div class='card bg-danger text-white shadow'><div class='card-body'>" . $_REQUEST['msg'] . "<div class='text-white-50 small'></div></div></div></div>";
                                            else if ($_REQUEST['type'] == "warning")
                                                $msg = "<div class='col-lg-6 mb-4'><div class='card bg-warning text-white shadow'><div class='card-body'>" . $_REQUEST['msg'] . "<div class='text-white-50 small'></div></div></div></div>";
                                            else if ($_REQUEST['type'] == "confirmation")
                                                $msg = "<div class='col-lg-6 mb-4'><div class='card bg-success text-white shadow'><div class='card-body'>" . $_REQUEST['msg'] . "<div class='text-white-50 small'></div></div></div></div>";
                                            else if ($_REQUEST['type'] == "information")
                                                $msg = "<div class='col-lg-6 mb-4'><div class='card bg-info text-white shadow'><div class='card-body'>" . $_REQUEST['msg'] . "<div class='text-white-50 small'></div></div></div></div>";

                                            echo $msg;
                                        }
                                        ?>
                                    </p>
                                    <div class="form-group col-lg-6">
                                        <input type="text" class="form-control form-control-user" name="title" id="title" placeholder="Title" required>
                                    </div>
                                    <div class="form-group">
                                        <textarea class="form-control form-control-user" id="posttext" name="posttext" required></textarea>
                                    </div>
                                    <div class="form-group col-lg-2">
                                        <input type="date" class="form-control form-control-user" name="upload_date" id="upload_date" required />
                                    </div>
                                    <span id="type_message" style="color:red"> </span> <br>
									<input type="text" class="form-control form-control-user" name="uploaded_by" id="uploaded_by" value="<?php echo $user; ?>" hidden />
                                    <hr>
                                    <button class="btn btn-primary btn-user btn-block col-lg-2" type="Submit">
                                        Submit
                                    </button>
                                    <button class="btn btn-primary btn-user btn-block col-lg-2" type="Reset">
                                        Reset
                                    </button>
                                </form>
                                <hr />
                            </div>
                        </div>
                    </div> 
                </div>
            </div>
        </div>
    </div>

</div>
<!-- /.container-fluid -->