<?php 
/***
Auth: Vastvikta Nishad
Date: 22 JAN 2025
Description: Delete and Update bulk SMS email template
*/

$msg = '';
$id = isset($_GET['id']) ? base64_decode($_GET['id']) : '';
$bulkData = getbulkData($link, $id);

// Fetch data for the form fields
$name = isset($bulkData['name']) ? $bulkData['name'] : '';
$template_content = isset($bulkData['template_content']) ? $bulkData['template_content'] : '';
$type = isset($bulkData['type']) ? $bulkData['type'] : '';
$slug = isset($bulkData['slug']) ? $bulkData['slug'] : '';
?>

<div id="success"><?php echo $msg; ?></div>
<form id="mastersrcat" method="post" action="">
    <input name="id" id="id" type="hidden" value="<?= htmlspecialchars($id) ?>">
    <span class="breadcrumb_head" style="height:37px;padding:9px 16px"><?= (!empty($id) ? "Edit" : "New") ?> Bulk Email, Whatsapp, SMS Template</span>
    <div class="style2-table">
        <div class="table">
            <table class="tableview tableview-2 main-form">
                <tbody>
                <?php if ($msg != '') { ?>
                    <tr>
                        <td colspan="2" style="color:#ffffff; width:100%; background:#00CC66; padding:5px; border:1px solid #02944b; text-align:center;">
                            <?php echo $msg; ?>
                        </td>
                    </tr>
                <?php } ?>
                <tr>
                    <td>
                        <label>Template Name<em>*</em></label>
                        <div class="log-case">
                            <input name="template_name" id="template_name" type="text" value="<?= htmlspecialchars($name) ?>" class="input-style1">
                        </div>
                    
              
                    <td>
                        <label>Type<em>*</em></label>
                        <div class="log-case">
                            <select name="type" id="type" class="input-style1">
                                <option value="" disabled selected>Select Type</option>
                                <option value="SMS" <?= (isset($type) && $type === 'SMS') ? 'selected' : '' ?>>SMS</option>
                                <option value="Email" <?= (isset($type) && $type === 'Email') ? 'selected' : '' ?>>Email</option>
                                <option value="WhatsApp" <?= (isset($type) && $type === 'WhatsApp') ? 'selected' : '' ?>>WhatsApp</option>
                            </select>
                        </div>

                    </td>
                </tr>
                <tr>
                   <td>
                                      <!-- added code for text limit [vastvikta][31-12-2025] -->

                        <label>Template Content<em>*</em></label>
                        <div class="log-case">
                            <textarea 
                                name="template_content" 
                                id="template_content" 
                                style="height:120px;width:300px;"
                                maxlength="255"><?= htmlspecialchars($template_content) ?></textarea>
                        </div>

                        <div id="charCount">0 / 255</div>

                    </td>
                </tr>

                    <!-- <td>
                        <label>Slug</label>
                        <div class="log-case">
                            <input name="slug" id="slug" type="text" value="<?= htmlspecialchars($slug) ?>" class="input-style1">
                        </div>
                    </td> -->
                </tr>
                <tr>
                    <td colspan="4">
                            <center>
                                <?php if ($id != '') { ?>
                                    <input name="Update" type="submit" value="Update" class="button-orange1" id="submitclkbulk" style="float:inherit">
                                <?php } else { ?>
                                    <input name="addbtn" type="submit" value="Create" class="button-orange1" id="submitclkbulk" style="float:inherit">
                                <?php } ?>
                            </center>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
    
</form>
<script>
document.addEventListener("DOMContentLoaded", function () {
    const textarea = document.getElementById("template_content");
    const maxLength = 255;
    const counter = document.getElementById("charCount");

    function updateCount() {
        if (textarea.value.length > maxLength) {
            textarea.value = textarea.value.substring(0, maxLength);
        }
        counter.textContent = textarea.value.length + " / " + maxLength;
    }

    textarea.addEventListener("input", updateCount);

    // Initialize count on load (important if value already exists)
    updateCount();
});
</script>
