<?php 
/**
 * Bulletin edit Page
 * Author: Ritu modi
 * Date: 13-03-2024
 * This page is used for managing bulletins. It allows users, particularly administrators, to create new bulletins or edit existing ones.
 */
// updated code for datetimepicker [vastvikta][11-06-2025]
// Encode 'Edit_Bulletin' to base64
$Edit_Bulletin = base64_encode('Edit_Bulletin'); 
// Decode 'id' from the GET parameter
$id = base64_decode($_GET['id']);

// Include common functions
include_once("common_function.php");

// Initialize userId
$userId = '';

// Check if user is admin
if($_SESSION['user_group'] == '0000' && $_SESSION['logged'] == 'Admin'){
    // Set userId if user is admin
    $userId = $_SESSION['userid'];
}

// Initialize data variable
$data = '';

// Check if id is set
if($id){
    // Get bulletin record
    $Bulletin = new common_function;
    $data = $Bulletin->get_bulletin_record($id);
}
?>
<style>
/* Hide other calendar libraries */
.ui-datepicker,
.ui-timepicker-div,
.flatpickr-calendar,
.datepicker-dropdown {
    display: none !important;
}

/* DO NOT block pointer-events */
</style>


<!-- Bulletin Form -->
<form name="registration" method="post" id="registration">
    <!-- Hidden inputs for userId and bulletin_id -->
    <input type="hidden" name="userid" value="<?php echo $userId;?>" id="userid">
    <input type="hidden" name="bulletin_id" class="bulletin_id" value="<?php echo $data['id'];?>" id="<?php $data['id']; ?>">

    <!-- Bulletin Header -->
    <span class="breadcrumb_head" style="height:37px;padding:9px 16px"><?= ($data['id'] != '') ? "Edit" : "New" ?> Bulletin </span>

    <!-- Bulletin Form Content -->
    <div class="style2-table">
        <div class="table">
            <table class="tableview tableview-2 main-form" width="100%">
                <tbody>
                    <!-- Message Input -->
                    <tr>
                        <td><label>Message<em>*</em></label></td>
                        <td  class="left boder0-right">
                            <div class="log-case">
                                <textarea name="M_Description" id="M_Description" cols="" rows="4" class="text-area1"><?php if(isset($data['id'])){ echo $data['Message']; }?></textarea>
                            </div>
                        </td>
                    </tr>

                    <?php
$startdate = !empty($data['d_startDate']) 
    ? date("d-m-Y H:i:s", strtotime($data['d_startDate'])) 
    : date("d-m-Y H:i:s");

$enddate = !empty($data['d_endDate']) 
    ? date("d-m-Y H:i:s", strtotime($data['d_endDate'])) 
    : date("d-m-Y H:i:s");
?>

<tr>
    <td><label>Start Date<em>*</em></label></td>
    <td class="left boder0-right">
        <div class="log-case">
            <input type="text" id="startdatetime" name="startdatetime"
                   class="date_class dob1" value="<?= $startdate ?>" autocomplete="off" />
        </div>
    </td>
</tr>

<tr>
    <td><label>End Date<em>*</em></label></td>
    <td class="left boder0-right">
        <div class="log-case">
            <input type="text" id="enddatetime" name="enddatetime"
                   class="date_class dob1" value="<?= $enddate ?>" autocomplete="off" />
        </div>
    </td>
</tr>

                    <!-- Message Type Select -->
                    <tr>
                        <td><label>Message Type<em>*</em></label></td>
                        <td class="left boder0-right">
                            <div class="log-case">
                                <select name="msg_type" class="select-styl1" id="msg_type">
                                    <option value=''>Select Type</option>
                                    <option value='0' <?php if(isset($data['id']) && $data['i_msgType'] == '0'){ echo 'selected'; }?>>Normal</option>
                                    <option value='1' <?php if(isset($data['id']) && $data['i_msgType'] == '1'){ echo 'selected'; }?>>Important</option>
                                </select>
                            </div>
                        </td>
                    </tr>

                    <!-- Submit Button -->
                    <tr>
                        <td class="left boder0-right" colspan="4">
                            <center> 
                                <?php if ($data['id'] != '') { ?>
                                    <!-- Update Button -->
                                    <input name="Update_bulletin" type="submit" value="Update" class="button-orange1 Update_bulletin" style="float:inherit">
                                <?php } else { ?>
                                    <!-- Create Button -->
                                    <input name="addbtn" type="submit" value="Create" class="button-orange1 Update_bulletin" style="float:inherit">
                                <?php } ?>
                            </center>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</form>

<!-- Include common JavaScript file -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.1/jquery.validate.min.js"></script>
<script src="<?=$SiteURL?>public/js/common.js"></script>

<script>
$(document).ready(function () {
    $('#startdatetime, #enddatetime').datetimepicker({
        format: 'd-m-Y H:i:s',
        step: 1,
        timepicker: true,
        datepicker: true,
        scrollMonth: false,
        scrollInput: false
    });
});
</script>
