<?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'];
$services = $_SESSION['services'];
$edit_message = $_SESSION['edit_message'];
$id = $_REQUEST['message_id'];

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

if (isset($_POST['title'])) {
    $title = $_POST['title'];
    $base_url = base_url();
    $base_url = str_replace('backend/', '', $base_url);
    $media_file_url = '';
    $preacher = $_POST['preacher'];
    $service_date = $_POST['service_date'];
    $uploaded_by = $_POST['uploaded_by'];
    $updated_by = $_POST['updated_by'];;
    $img_url = '';
    $service = $_POST['updated_service'];


    if (isset($_FILES['img_url'])) {
        $filename = $_FILES['img_url']['name'];
        $extension = pathinfo($filename, PATHINFO_EXTENSION);
        $fn = preg_replace('/\s+/', '_', $title);
        $file_name = $fn."_".$service_date.".".$extension;
        $img_url = $base_url . 'uploads/' . $file_name;
        $file_size = $_FILES['img_url']['size'];
        $file_tmp = $_FILES['img_url']['tmp_name'];
        move_uploaded_file($file_tmp, "../uploads/" . $file_name);
    }

    if (isset($_FILES['media_file_url'])) {
        $filename = $_FILES['media_file_url']['name'];
        $extension = pathinfo($filename, PATHINFO_EXTENSION);
        $fn = preg_replace('/\s+/', '_', $title);
        $file_name = $fn."_".$service_date.".".$extension;
        $media_file_url = $base_url . 'audio/' . $file_name;
        $file_size = $_FILES['media_file_url']['size'];
        $file_tmp = $_FILES['media_file_url']['tmp_name'];
        move_uploaded_file($file_tmp, "../audio/" . $file_name);
    }

    $msg_response = $CI->Message_archive->update_msg($id, $title, $media_file_url, $preacher, $service_date, $uploaded_by, $updated_by, $img_url, $service);
    $msg = $msg_response['message'];
    if ($msg_response['data']) {
        $error = 'confirmation';
    } else {
        $error = 'error';
    }
    header("location: messages?msg=" . $msg . "!&type=" . $error."");
exit();
}
?>
<script type="text/javascript">
    function changeHandlerImg() {
        var fullPath = document.getElementById('img_url').value;
        if (fullPath) {
            var startIndex = (fullPath.indexOf('\\') >= 0 ? fullPath.lastIndexOf('\\') : fullPath.lastIndexOf('/'));
            var filename = fullPath.substring(startIndex);
            if (filename.indexOf('\\') === 0 || filename.indexOf('/') === 0) {
                filename = filename.substring(1);
            }
            document.getElementById('image_url').setAttribute("value", filename);
        }
    }

    function changeHandlerMedia() {
        var fullPath = document.getElementById('media_file_url').value;
        if (fullPath) {
            var startIndex = (fullPath.indexOf('\\') >= 0 ? fullPath.lastIndexOf('\\') : fullPath.lastIndexOf('/'));
            var filename = fullPath.substring(startIndex);
            if (filename.indexOf('\\') === 0 || filename.indexOf('/') === 0) {
                filename = filename.substring(1);
            }
            document.getElementById('media_url').setAttribute("value", filename);
        }
    }
    
        function changeServiceCat() {
        var serviceDate = document.getElementById('service').value;
            document.getElementById('updated_service').setAttribute("value", serviceDate);
            }
</script>

<!-- 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-5 col-lg-6 col-md-4">
            <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">Edit Sermon</h1>
                                </div>
                                <form enctype="multipart/form-data" 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">
                                        <input type="text" class="form-control form-control-user" name="title" id="title" placeholder="Sermon Title" value="<?php echo $edit_message->title; ?>" required>
                                    </div>
                                    <div class="form-group">
                                        <input type="text" class="form-control form-control-user" id="preacher" name="preacher" placeholder="Preacher"
                                        value="<?php echo $edit_message->preacher; ?>"required>
                                    </div>
                                    <div class="form-group">Service Date
                                        <input type="date" class="form-control form-control-user" name="service_date" id="service_date" placeholder="Service Date" value="<?php echo $edit_message->service_date; ?>" required>
                                    </div>
                                    <div class="form-group">Image Thumbnail<br>
                                        <img src="<?php echo $edit_message->img_url; ?>" alt="Girl in a jacket" width="80" height="80">
                                    </div>
                                    <div class="form-group">Select New Thumbnail
                                        <input type="file" class="form-control form-control-file" name="img_url" id="img_url" placeholder="Image Thumbnail" accept=".jpg,.png" onchange="changeHandlerImg()">
                                    </div>
                                     <div class="form-group">Media File
                                        <input type="text" class="form-control form-control-user" value="<?php echo $edit_message->media_file_url; ?>" readonly>
                                    </div>
                                    <div class="form-group">Sermon Audio File
                                        <input type="file" class="form-control form-control-file" name="media_file_url" id="media_file_url" placeholder="Sermon Audio File" accept=".wma,.mp3,.mp4"  onchange="changeHandlerMedia()">
                                    </div>
                                    <div class="form-group">
                                        <select name="service" class="form-control form-control-select" name="service" id="service" placeholder="Category" onchange="changeServiceCat()">
                                            <option value="">Select Service Category...</option>
                                            <?php
                                            foreach ($services as $service) {
                                            ?>
                                                <option value="<?php echo $service->id; ?>"><?php echo $service->name; ?></option>
                                            <?php } ?>
                                        </select>
                                    </div>
                                    <div class="form-group">
                                        <input type="text" class="form-control form-control-user" id="updated_by" name="updated_by" value="<?php echo $user; ?>" hidden>
                                        <input type="text" class="form-control form-control-user" id="uploaded_by" name="uploaded_by" value="<?php echo $edit_message->uploaded_by; ?>" hidden>
                                        <input type="text" class="form-control form-control-user" id="updated_service" name="updated_service" value="<?php echo $edit_message->service; ?>" hidden>
                                    </div>
                                    <hr>
                                    <button class="btn btn-primary btn-user btn-block" type="Submit">
                                        Submit
                                    </button>
                                    <button class="btn btn-primary btn-user btn-block" type="Reset">
                                        Reset
                                    </button>
                                </form>
                                <hr />
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

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