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

<style>
    .dose h1,
    .dose {
        margin-top: 12px;
        text-align: left;
        font-size: 15px;
        color: green;
    }
</style>

<form action="#" class="pl-3 pr-3" id="form_update" enctype="multipart/form-data" autocomplete='off' validate>
    <?php
    echo '<input class="form-control userId" type="text"  name="unique_user_id" value=' . $_GET['userId'] . ' id="name" placeholder="Enter description" style="display:none;">';
    echo '<input class="form-control profile_photo" type="text" name="profile_photo"  value="" id="profile_photo" placeholder="Enter description" style="display:none;">';
    ?>

    <h1 style='font-size:18px; text-align:center; color:black;'><u>EDIT USER DETAILS</u></h1>


    <div class="form-group secondInput">
        <label for="bank" class="agency_name">User Name</label>
        <input class="form-control" type="text" name="fullname" id="fullname" placeholder="User Name" required>
    </div>

    <div class="form-group secondInput">
        <label for="receipt_no">Email Address</label>
        <input class="form-control" type="text" name="email" id="email" placeholder="Email Address" required>
    </div>

    <div class="form-group secondInput">
        <label for="bank">Mobile Phone Number</label>
        <input class="form-control" type="text" name="phone" id="phone" placeholder="Mobile Phone Number" required>
    </div>

    <div class="form-group secondInput">
        <label for="receipt_no">Nationality</label>
        <select class="form-control" name="nationality" id="nationality" autofocus required>

        </select>
    </div>

    <div class="form-group secondInput">
        <label for="bank">Living Town</label>
        <input class="form-control" type="text" name="living_town" id="living_town" placeholder="Enter new password" required>
    </div>

    <div class="form-group secondInput">
        <label for="bank">Office Address</label>
        <textarea class="form-control" name="address" id="address" placeholder="Enter other comments if any"></textarea>
    </div>


    <div class="form-group secondInput">
        <label for="name" id="Description">Status</label>
        <select class="form-control" id="status" name="status" required autofocus>

        </select>
    </div>

    <p style='margin-top:-8px; width:100%; text-align:center;'><u style='color:#8b7272;'>Select user accessible features</u></p>
            <?php
            echo "<table style='width:100%; text-align:left; border:none;'>";
            echo "<thead>";
            echo "<tr class='headerr' style='background:linear-gradient(#ffffff, #ebf2f4, #f4f4f4);'>";
            echo "<th class='text-center' style='width:40px;'><input type='checkbox'  class='checkall-items'></th>";
            echo "<th style='text-align:left;'>Select All</th>";
            echo "</tr>";
            echo "</thead>";
            echo "<tbody>";

            $query1 = mysqli_query($business_conn, "SELECT * FROM businesses_menu");
            while ($spec1 = mysqli_fetch_assoc($query1)) {
                $query2 = mysqli_query($conn, "SELECT * FROM user_accessible_features WHERE unique_user_id = '".$_GET['userId']."' AND Category = '" . $spec1["Category"] . "' AND MenuItemId = '" . $spec1["MenuItem"] . "'");
                $affected_row = mysqli_affected_rows($conn);
                if ($affected_row > 0) {
                    while ($spec2 = mysqli_fetch_assoc($query2)) {
                        echo "<tr style=''>";
                        echo "<td style='border: 1px solid #ededed;' class='text-center'><input type='checkbox' checked name='menuItem[]' class='selected-items' data-category='" . $spec1['Category'] . "' data-icon='" . $spec1['Icon'] . "' data-menu-item='" . $spec1['MenuItem'] . "' data-menu-item-file-name='" . $spec1['MenuItemFileName'] . "'  data-menu-item-file-name-mobile='" . $spec1['MenuItemFileNameMobile'] . "' data-progress='" . $spec1['Progress'] . "' data-status='" . $spec1['Status'] . "' value='" . $spec1['id'] . "'></td>";
                        echo "<td style='text-align:left;'>" . $spec1["MenuItem"] . "</td>";
                        echo "</tr>";
                    }
                } else {
                    echo "<tr style=''>";
                    echo "<td style='border: 1px solid #ededed;' class='text-center'><input type='checkbox' name='menuItem[]' class='selected-items' data-category='" . $spec1['Category'] . "' data-icon='" . $spec1['Icon'] . "' data-menu-item='" . $spec1['MenuItem'] . "' data-menu-item-file-name='" . $spec1['MenuItemFileName'] . "'  data-menu-item-file-name-mobile='" . $spec1['MenuItemFileNameMobile'] . "' data-progress='" . $spec1['Progress'] . "' data-status='" . $spec1['Status'] . "' value='" . $spec1['id'] . "'></td>";
                    echo "<td style='text-align:left;'>" . $spec1["MenuItem"] . "</td>";
                    echo "</tr>";
                }
            }

            echo "</tbody>";
            echo "</table>";
            ?>


    <div class="form-group text-center" style='margin-top:20px;'>
        <button type="button" class="btn btn-rounded btn-secondary closeModal">Cancel</button>
        <button type="submit" class="btn btn-rounded btn-success" id="submit_update"><span class='submitLoader'><i class="fa fa-check"></i> Update</span></button>

    </div>

</form>

<script>
    var userId = $('.userId').html();
    $.getJSON(domain_api + "/users/users.php?userId=" + encodeURIComponent(userId), function(data) {

        $("#profile_photo").val(data.user_information[0].profile_photo);
        $("#fullname").val(data.user_information[0].fullname);
        $("#nationality").empty();
        $("#nationality").append("<option selected value='" + data.user_information[0].nationality + "'>" + data.user_information[0].nationality + "</option>");
        $("#living_town").val(data.user_information[0].living_town);
        $("#email").val(data.user_information[0].email);
        $("#phone").val(data.user_information[0].phone);
        $("#address").val(data.user_information[0].address);
        $("#status").empty();
        $("#status").append(
            "<option value='" + data.user_information[0].status + "'>" + data.user_information[0].status + "</option>" +
            "<option value='Active'>Active</option>" +
            "<option value='In-active'>In-active</option>"
        );

    });

    $('.checkall-items').off().on('change', function(e) {
        $('.selected-items').prop("checked", $(this).prop("checked"));
        e.preventDefault();
    })


    $("#nationality").empty();
    $("#nationality").append('<option value="" selected></option>');
    $.get(domain_api + "/other-configs/configs.php", function(data) {
        var results = data;
        $.each(results.data.countries, function(key, value) {
            $("#nationality").append("<option value='" + value + "'>" + value + "</option>");
        });
    });

    $("#submit_update").on("click", function() {
        $("#form_update").submit(function(e) {
            $.ajax({
                url: domain_api + "/users/update.php",
                type: "POST",
                data: new FormData(this),
                contentType: false,
                processData: false,
                beforeSend: function() {
                    $("#submit_update").attr("disabled", "disabled");
                    $(".submitLoader").html("<i class='fa fa-spinner fa-spin'></i>")
                },
                success: function(data) {
                    $("#myModal_actions").modal("hide");
                    var ActivePage = sessionStorage.getItem("ActivePage");
                    $(".submitLoader").html("<i class='fas fa-check'></i> Approve");
                    if (data.code == 200) {
                        $("#myModal_actions").modal("hide");
                        $("#example").hide();
                        setTimeout(function() {
                            $(".container-fluid").load(ActivePage, function(data) {});
                        }, 100)

                        swal("Succes!", data.message, "success");
                    } else {
                        swal("Oops!", data.message, "error");
                    }
                    $("#submit_update").attr("disabled", false);
                },
                error: function(request, error) {
                    swal("Oops!", "Update was not successful, you should try again!", "error");
                },
            });
            e.preventDefault();
        });
        $(this).off();
    });
</script>