<?php
include('../db/model.php');
include('../config/config.php');
$obj = new Model();
// $tableHeight='';
$forUpdate = false;
$id = 0;

if (isset($_POST['id'])) {
    $id = (int)$_POST['id'];
    if (is_numeric($id) && $id > 0 && $id != '' && $id != null) {
        $forUpdate = $id > 0;
        $eventType = $obj->GetEventTypeById($id);
    }
}

if (isset($_POST['type'])) {
    if ($_POST['type']=='add') {
        $result = $obj->AddEventType($_POST);
        header('Content-Type: application/json');

        if (strlen($result)==1) {
            echo json_encode(['success' => true, 'message' => 'Event Type Successfully Added']);
        }else{
            echo json_encode(['success' => false, 'message' => $result]);
        }
    }
    else if ($_POST['type']=='update') {
        $result = $obj->UpdateEventType($_POST);
        header('Content-Type: application/json');

        if (strlen($result)==1) {
            echo json_encode(['success' => true, 'message' => 'Event Type Successfully Updated']);
        }else{
            echo json_encode(['success' => false, 'message' => $result]);
        }
    }
    exit;
}

?>
<div class="modal fade" id="modalModal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="modalModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header text-center">
                <h1 class="modal-title fs-5 w-100" id="modalModalLabel"><?php if ($forUpdate) {
                                                                            echo 'Update Event Type';
                                                                        } else {
                                                                            echo 'Add New Event Type';
                                                                        } ?></h1>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div id="custom-form-light">
                <div class="modal-body">

                    <input type="hidden" name="id" id="id" value="<?php if ($forUpdate) {
                                                                        echo $eventType['id'];
                                                                    } ?>">

                    <div class="input-group">
                        <div class="col-12 col-sm-12 col-md-6 mb-3 pe-md-2">
                            <label for="name">Event Type</label>
                            <input type="text" name="name" id="name" value="<?php if ($forUpdate) {
                                                                                echo $eventType['name'];
                                                                            } ?>" class="form-control">
                        </div>

                        <div class="col-12 col-sm-12 col-md-6 mb-3 ms-auto me-0 ps-md-2">
                            <label for="eventTypeStatus">Status</label>
                            <div class="input-group">
                                <div class="form-check ps-5">
                                    <input class="form-check-input" type="radio" name="eventTypeStatus" id="eventTypeStatusActive" <?php if ($forUpdate) {
                                                                                                                                if ($eventType['status'] == 1) {
                                                                                                                                    echo 'checked';
                                                                                                                                }
                                                                                                                            } ?>>
                                    <label class="form-check-label" for="eventTypeStatusActive">
                                        Active
                                    </label>
                                </div>
                                <div class="form-check ps-5">
                                    <input class="form-check-input" type="radio" name="eventTypeStatus" id="eventTypeStatusDeActive" <?php if ($forUpdate) {
                                                                                                                                    if ($eventType['status'] == 0) {
                                                                                                                                        echo 'checked';
                                                                                                                                    }
                                                                                                                                } ?>>
                                    <label class="form-check-label" for="eventTypeStatusDeActive">
                                        De-Active
                                    </label>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>

                    <?php if ($forUpdate) { ?>
                        <button type="submit" name="payment" class="btn btn-primary" onclick="UpdateEventType()">Update</button>
                    <?php } else { ?>
                        <button type="submit" name="payment" class="btn btn-primary" onclick="AddEventType()">Add</button>
                    <?php } ?>

                </div>
            </div>
            <!-- </form> -->
        </div>
    </div>
</div>