<?php
include($_SERVER['DOCUMENT_ROOT'] . "/COMPONENTS/root/path.php");
echo "<h5 class='userId' style='display:none;'>" . $_GET['userId'] . "</h5>";
?>


<form action="#" class="pl-3 pr-3" id="form_deleteAdmin" enctype="multipart/form-data" autocomplete='off' validate>

    <h1 class="to-hide" style='font-size:15px; text-align:center;'>Please determine either to <span style='font-weight:600;' class='text-primary'>TERMINATE STAFF APPOINTMENT</span> or <span style='font-weight:600;' class='text-danger'>DELETE STAFF PERMANENTLY</span>.</h1>
    <h1 class="deletionInProgress" style='font-size:15px; text-align:center; display:none;'>Deletion in progress.</h1>
    <div class="col-md-12 mb-3 reason" style="display:none;">
        <label for="validationCustom02">REASON APPOINTMENT TERMINATION:</label>
        <input type="text" value="<?php echo $idd; ?>" class="idd" style="display:none;">
        <textarea class="form-control reasonForDischarge" id="exampleFormControlTextarea1" rows="3"></textarea>
        <div class="valid-feedback">
            Looks good!
        </div>
    </div>

    <?php
    echo '<input class="form-control userId" type="text" required="" name="unique_user_id" value=' . $_GET['userId'] . ' id="userId" placeholder="Enter description" style="display:none;">';
    ?>

    <div class="form-group text-center" style='margin-top:20px;'>
        <button type="button" class="btn btn-rounded btn-primary to-hide terminate">Terminate</button>
        <button type="button" class="btn btn-rounded btn-danger to-hide delAppointment" id="submit_deleteAdmin">Delete</button>

        <button type="button" class="btn btn-rounded btn-primary to-show terminateAppointment" style="display:none;">Proceed</button>
        <button type="button" class="btn btn-rounded btn-danger to-show delAppointment" id="deleteStaffInstead" style="display:none;">Delete staff instead</button>

    </div>

</form>



<script>
    $('.terminate').off().on("click", function() {
        $('.reason,.to-show').fadeIn();
        $('.to-hide').hide();
    });
    $('.terminateAppointment').off().on("click", function() {
        var terminated_by = token;
        var unique_user_id = $('#userId').val();
        var reason = $('.reasonForDischarge').val();
        $(this).attr("disabled", "disabled");
        $(this).html("<i class='fa fa-spinner fa-spin'></i> Terminating appointment, please wait!");
        $(".delAppointment").hide();
        if (reason != "") {
            $.get(domain_api + "/users/delete.php?terminated_by=" + terminated_by + "&unique_user_id=" + encodeURIComponent(unique_user_id) + "&reasonForTermination=" + encodeURIComponent(reason) + "", function(data) {
                $('#example').DataTable().ajax.reload();
                $("#myModal_actions").modal("hide");
                swal("Succes!", data.message, "success");
            });
        } else {
            alert("The reason for appointment termination cannot be empty.")
            $(this).attr("disabled", false);
            $(this).html("Proceed");
            $("#deleteStaffInstead").show();
        }


    });

    $('.delAppointment').off().on("click", function() {
        var unique_user_id = $('#userId').val();
        $(this).attr("disabled", "disabled");
        $(this).html("<i class='fa fa-spinner fa-spin'></i> Deleting staff, please wait!");
        $(".terminate,.terminateAppointment,.reason").hide();
        $(".deletionInProgress").show();
        $.get(domain_api + "/users/delete.php?deletingID=" + encodeURIComponent(unique_user_id) + "", function(data) {
            $('#example').DataTable().ajax.reload();
            $("#myModal_actions").modal("hide");
            swal("Succes!", data.message, "success");
        });
    });
</script>