<!-- =====================================================
     DELETE / CANCEL EVENT MODAL
===================================================== -->

<div id="deleteModal" class="modal">

    <div class="deleteBox">

        <!-- ICON -->
      <div class="deleteIcon">
    <i class="fa-solid fa-triangle-exclamation"></i>
</div>

        <!-- TITLE -->
        <h2 id="modalTitle">
            Delete Event
        </h2>

        <!-- MESSAGE -->
        <p id="deleteMessage">
            Are you sure you want to delete this event?
        </p>

        <!-- BUTTONS -->
        <div class="deleteActions">

            <button
                type="button"
                id="confirmBtn"
                onclick="confirmDelete()">

                Yes, Delete
            </button>

            <button
                type="button"
                onclick="closeModal('deleteModal')">

                Cancel
            </button>

        </div>

    </div>

</div>



<script>
/* =====================================================
   DELETE JS
===================================================== */

let deleteEventId = 0;
let deleteMode = "delete";


async function openDeleteModal(id) {

    deleteEventId = id;

    const f = new FormData();
    f.append("check_participant", 1);
    f.append("event_id", id);

    let data =
        await postJSON(
            "event_action.php",
            f
        );

    const title =
        document.getElementById("modalTitle");

    const msg =
        document.getElementById("deleteMessage");

    const btn =
        document.getElementById("confirmBtn");


    if (data.hasParticipant) {

        deleteMode = "cancel";

        title.innerText =
            "Cancel Event";

        msg.innerText =
            "This event already has participants. Cancel event instead?";

        btn.innerText =
            "Yes, Cancel";

    } else {

        deleteMode = "delete";

        title.innerText =
            "Delete Event";

        msg.innerText =
            "Are you sure you want to permanently delete this event?";

        btn.innerText =
            "Yes, Delete";
    }

    openModal("deleteModal");
}


async function confirmDelete() {

    const f = new FormData();

    f.append(
        "delete_event",
        1
    );

    f.append(
        "event_id",
        deleteEventId
    );

    f.append(
        "mode",
        deleteMode
    );

    let data =
        await postJSON(
            "event_action.php",
            f
        );

    showToast(
        data.msg,
        data.status
    );

    if (
        data.status ===
        "success"
    ) {

        closeModal(
            "deleteModal"
        );

        setTimeout(()=>{
            location.reload();
        },1200);
    }
}
</script>