<?php
include($_SERVER['DOCUMENT_ROOT'] . "/COMPONENTS/root/path.php");
echo "<h5 class='invoiceNo' style='display:none;'>" . $_GET['invoiceNo'] . "</h5>";
echo "<h5 class='userId' style='display:none;'>" . $_GET['id'] . "</h5>";
?>
<style>
    .dose h1,
    .dose {
        margin-top: 12px;
        text-align: left;
        font-size: 15px;
        color: green;
    }
</style>

<form action="#" id="form_update" enctype="multipart/form-data" autocomplete='off' validate>
    <?php
    echo '<input class="form-control userId" type="text"  name="id" value=' . $_GET['id'] . ' id="name" placeholder="Enter description" style="display:none;">';
    ?>
    <h1 style='font-size:18px; text-align:center; color:black;'><u>WAREHOUSE ADDED STOCK</u></h1>
    <div class="form-group secondInput">
        <label for="receipt_no">Select Warehouse</label>
        <select class="form-control" name="warehouse" id="warehouse" required>
            <?php
            $query = mysqli_query($conn, "SELECT * FROM config WHERE config_type_id = '5'");
            while ($spec = mysqli_fetch_assoc($query)) {
                echo "<option style='text-align: center;'  value='" . $spec['name'] . "'>" . $spec['name'] . "</option>";
            }
            ?>
        </select>
    </div>

    <div class="form-group text-center">
        <button type="button" class="btn btn-rounded btn-secondary closeModal">Cancel</button>
        <button type="submit" class="btn btn-rounded btn-success" id="submit_update1"><span class='submitLoader'><i class="fa fa-check"></i> Update</span></button>
    </div>
</form>


<script>
    var userId = $('.userId').html();
    $(document).off().on("click", "#submit_update1", function() {
        $("#form_update").on("submit", function(e) {
            $.ajax({
                url: domain_api + "/stock/update.php",
                type: "POST",
                data: new FormData(this),
                contentType: false,
                processData: false,
                beforeSend: function() {
                    $("#submit_update1").attr("disabled", "disabled");
                    $(".submitLoader").html("<i class='fa fa-spinner fa-spin'></i>")
                },
                success: function(data) {
                    var invoiceNo = $('.invoiceNo').html();
                    $("#myModal_warehouse").modal("hide");
                    // var ActivePage = sessionStorage.getItem("ActivePage");
                    $(".submitLoader").html("<i class='fas fa-check'></i> Approve");
                    if (data.code == 200) {
                        $("#myModal_warehouse").modal("hide");
                        setTimeout(function() {
                            $(".profileView").load(domain + "/COMPONENTS/sub-pages/view-item-profile.php?invoiceNo=" + encodeURIComponent(invoiceNo) + "", function(data) {});
                        }, 100)
                        swal("Succes!", data.message, "success");
                    } else {
                        swal("Oops!", data.message, "error");
                    }
                    $("#submit_update1").attr("disabled", false);
                },
                error: function(request, error) {
                    swal("Oops!", "Update was not successful, you should try again!", "error");
                },
            });
            e.preventDefault();
        });
        //     $(this).off();
    });
</script>