<?php
session_start();
if (isset($_SESSION['_session']['user']) &&  (time() - $_SESSION['_session']['LAST_ACTIVITY']  < $_SESSION['_session']['koha'])) {
    $_SESSION['_session']['LAST_ACTIVITY']  = time();
    require 'header.php';
?>
    <link rel="stylesheet" type="text/css" href="plugins/select2/css/select2.min.css">
    <script src="plugins/select2/js/select2.min.js"></script>
    <style type="text/css">
        @media only screen and (max-width: 600px) {
            .control-label {
                display: show;
            }

            .checkbox {
                margin-left: 0;
            }

            .edit {
                flex-wrap: nowrap;
                padding-bottom: 10px;
            }
        }

        .nav li:hover>a,
        .nav li .dropdown-menu a:hover,
        .nav li .dropdown-menu a:focus,
        .nav li.active>[data-toggle="collapse"] {
            background-color: rgba(200, 200, 200, 0.2);
            /* color: #ff7575; */
            box-shadow: none;
        }

        .nav li a,
        .nav li .dropdown-menu a {
            margin: 10px 15px 0;
            border-radius: 3px;
            color: #3C4858;
            padding-left: 10px;
            padding-right: 10px;
            text-transform: capitalize;
            font-size: 13px;
            padding: 10px 15px;
        }

        button.iksi {
            cursor: pointer;
            margin-top: 10px;
        }

        span.iksi {
            border: none !important;
            padding: 0 8px;
        }
    </style>

    <div class="content-wrapper">
        <div class="content-header">
            <div class="container-fluid" style="padding-top: 20px;">
                <div class="row">
                    <div class="col-12">
                        <div class="card">
                            <div class="card-header clearfix">
                                <button type="button" class="btn btn-info btn-sm" data-toggle="modal" data-target="#modal-stoku"> <i class="material-icons">add</i> Regjistro Stokun </button>
                                <button type="button" class="btn btn-info btn-sm" data-toggle="modal" data-target="#modal-reagents"> <i class="material-icons">add</i> Regjistro Reagjenta </button>


                            </div>

                            <!-- <div id="tabel12"></div> -->
                            <div id="getpc" class="card-body">
                                <?php
                                $opts = [
                                    "http" => [
                                        "method" => "GET",
                                        "header" => "api_token:" . $token,
                                    ]
                                ];
                                $context = stream_context_create($opts);
                                $str_data = file_get_contents($urlapi . "/tbl_stock", false, $context);
                                $data = json_decode($str_data, true);
                                $length = count($data);

                                $temp = "<table id=\"example1\" class=\"table table-bordered table-hover\">";
                                $temp .= "<thead><tr ><th type=\"hidden\">ID</th>";
                                $temp .= "<th>Pajisja</th>";
                                $temp .= "<th>Firma</th>";
                                $temp .= "<th>Sasia ne stok</th>";
                                $temp .= "<th> çmimi </th>";
                                $temp .= "<th>Mundësia</th></tr></thead> <tbody>";

                                for ($i = 0; $i < $length; $i++) {
                                    $temp .= "<td type=\"hidden\">" . $data[$i]["Id"] . "</td>";
                                    $temp .= "<td>" . $data[$i]["emri_pajisjes"] . "</td>";
                                    $temp .= "<td>" . $data[$i]["emri_prodhuesit"] . "</td>";
                                    $temp .= "<td>" . $data[$i]["sasia"] . "</td>";
                                    $temp .= "<td>" . $data[$i]["cmimi"] . "</td>";
                                    $temp .= "<td>";

                                    $temp .= "<button style=\"padding: 6px\" 
                class=\"btn btn-info btn-xs edit\" 
                onClick=\"selectuser_click(" . $data[$i]["Id"] . ")\"
                data-toggle=\"modal\" 
                data-target=\"#EditStock\">
                <i class=\"material-icons dp48\">edit</i>";
                                    $temp .= "</td>";
                                    $temp .= "</tr>";
                                }
                                $temp .= "</tbody></table>";
                                echo $temp;

                                ?>

                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>




    <!-- modal for stock stoku stoku stoku  -->
    <div class="modal fade" id="modal-stoku">
        <div class="modal-dialog" style="max-width: 600px">
            <div class="modal-content ">
                <div class="modal-header">
                    <h4 class="modal-title">Regjistro stokun</h4>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <div class="input-group mb-12 edit">
                        <label class="col col-sm-3 col-md-3 control-label">Zgjedh Brand</label>
                        <div class="col col-sm-9 col-md-9" id="brand_stock">
                            <script>
                                function brand_stock() {
                                    $.ajax({
                                        type: 'POST',

                                        url: url + "/firma",
                                        headers: {
                                            'api_token': token
                                        },
                                        data: {
                                            veprimi: 'select',
                                            emri_brendit: ' ',
                                            id: 0,
                                            emri_firmes: ' ',
                                            pershkrim: ' ',
                                            logoimg: ' ',
                                            userid: 0,
                                            ord: 0
                                        },
                                        success: function(data, status) {
                                            y = "<select  id=\"get_brand\" class=\"form-control selectized select2\" autocomplete=\"off\" tabindex=\"-1\">";
                                            y += "<option value=\"\" selected=\"selected\">-Zgjedh Brandin-</option>";

                                            for (i = 0; i < data.length; i++) {
                                                y += '<option value="' + data[i].id + '">' + data[i].emri_prodhuesit + '</option> ';
                                            }

                                            // $('#brand_stock').append(z);
                                            document.getElementById("brand_stock").innerHTML = y;


                                            $("#get_brand").change(function() {
                                                $.ajax({
                                                    url: url + "/paisjeforfirma/" + $("#get_brand").val(),
                                                    headers: {
                                                        'api_token': token
                                                    },
                                                    type: 'GET',
                                                    success: function(data, status) {
                                                        y = "<select  id=\"select-pajisjee-stoku\" class=\"form-control selectized select2\" autocomplete=\"off\" tabindex=\"-1\">";
                                                        y += "<option value=\"\" selected=\"selected\">-Zgjedh Pajisjen-</option>";

                                                        for (i = 0; i < data.length; i++) {
                                                            y += '<option value="' + data[i].id + '">' + data[i].emri_pajisjes + '</option> ';

                                                        }

                                                        document.getElementById("getPajisjestock").innerHTML = y;
                                                        $('#getPajisjeclient1').show();
                                                    }
                                                });
                                            });
                                        }
                                    });

                                }

                                brand_stock();
                            </script>
                        </div>
                    </div>


                    <div class="input-group mb-12 edit" id="getPajisjeclient1" style="display:none;">
                        <label class="col-sm-3 col-md-3 control-label">Pajisja</label>
                        <div class="col-sm-9 col-md-9" id="getPajisjestock">
                            <script>


                            </script>
                        </div>
                    </div>



                    <!-- Jo domosdosmerisht -->
                    <div class="input-group mb-12">
                        <label class="col-sm-3 col-md-3 control-label">Sasia</label>
                        <div class="col-sm-9 col-md-9">
                            <input type="number" class="form-control" placeholder="Sasia në stok" id="sasia">

                        </div>
                    </div>
                    <div class="input-group mb-12">
                        <label class="col-sm-3 col-md-3 control-label">Porosi</label>
                        <div class="col-sm-9 col-md-9">
                            <input type="number" class="form-control" placeholder="Maksimumi për porosi" id="maxporosi">

                        </div>
                    </div>
                    <div class="input-group mb-12">
                        <label class="col-sm-3 col-md-3 control-label">NR.Sasis Limituar</label>
                        <div class="col-sm-9 col-md-9">
                            <input type="number" class="form-control" placeholder="Numri i stokut kur është sasia limituar" id="nrsasialimituar">

                        </div>
                    </div>
                    <div class="input-group mb-12">
                        <label class="col-sm-3 col-md-3 control-label">Sasia Limituar</label>
                        <div class="col-sm-9 col-md-9">
                            <input type="number" class="form-control" placeholder="Maksimumi për porosi kur është e limituar" id="sasialimituar">

                        </div>
                    </div>
                    <div class="input-group mb-12">
                        <label class="col-sm-3 col-md-3 control-label">TVSH</label>
                        <div class="col-sm-9 col-md-9">
                            <span class="bmd-form-group"><input type="number" class="form-control" placeholder="Perqindja e tvsh-së" id="tvsh"></span>

                        </div>
                    </div>
                    <div class="input-group mb-12">
                        <label class="col-sm-3 col-md-3 control-label">Çmimi</label>
                        <div class="col-sm-4 col-md-4">
                            <input type="number" class="form-control" placeholder="Çmimi Produktit" id="cmimi">
                        </div>

                        <label for="cmimicheck" style="padding-top:12px;" class="col-sm-3 col-md-3 control-label">Shfaq çmimin</label>
                        <div style="padding-top:12px;" class="col-sm-2 col-md-2">
                            <input type="checkbox" class="checkbox" style="margin-left:-15px;" placeholder="Çmimi Produktit" id="cmimicheck">
                        </div>

                        <div class="input-group mb-12">
                            <label class="col-sm-3 col-md-3 control-label">Fotoja e Produktit</label>
                            <div class="col-sm-9 col-md-9 row">
                                <div class="col-sm-6 col-md-6">
                                    <input id="stokufoto" type="file" style="display:none;">
                                    <label for="stokufoto" class="btn btn-warning" style=" height: 35px"><i class="material-icons">add</i>Shto logo</label>
                                </div>
                                <div class="col-sm-6 col-md-6 row" style="padding-top: 15px;">
                                    <label for="fotocheck" style="margin-left:-10px;" class="col-md-8 control-label">Shfaq Foton e paisjes</label>
                                    <input class="col-md-4" style="margin-left:10px;margin-top: 10px;" type="checkbox" placeholder="Çmimi Produktit" id="fotocheck">

                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer justify-content-between">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Jo</button>
                    <button type="button" id="btn-ruaj-stock" onclick="" class="btn btn-success">Ruaj</button>
                </div>
            </div>
        </div>
    </div>

    <!-- ////////////////////////////////////////////////////////////////////////////////////////////////////////// -->
    <div class="modal fade" id="modal-stoku-edit">
        <div class="modal-dialog" style="max-width: 600px">
            <div class="modal-content ">
                <div class="modal-header">
                    <h4 class="modal-title">Edito stokun</h4>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <div class="input-group mb-12 edit">
                        <label class="col col-sm-3 col-md-3 control-label">Brendi</label>
                        <div class="col col-sm-9 col-md-9" id="brendssa">
                            <input type="text" class="form-control" placeholder="Brendi" id="Brendied" disabled>
                        </div>
                    </div>


                    <div class="input-group mb-12 edit" id="getPajisjeclien221">
                        <label class="col-sm-3 col-md-3 control-label">Pajisja</label>
                        <div class="col-sm-9 col-md-9" id="sss">
                            <input type="text" class="form-control" placeholder="Pajisja" id="Pajisjaedit" disabled>
                        </div>
                    </div>



                    <!-- Jo domosdosmerisht -->
                    <div class="input-group mb-12">
                        <label class="col-sm-3 col-md-3 control-label">Sasia</label>
                        <div class="col-sm-9 col-md-9">
                            <input type="number" class="form-control" placeholder="Sasia në stok" id="sasiaED">

                        </div>
                    </div>
                    <div class="input-group mb-12">
                        <label class="col-sm-3 col-md-3 control-label">Porosi</label>
                        <div class="col-sm-9 col-md-9">
                            <input type="number" class="form-control" placeholder="Maksimumi për porosi" id="maxporosiED">

                        </div>
                    </div>
                    <div class="input-group mb-12">
                        <label class="col-sm-3 col-md-3 control-label">NR.Sasis Limituar</label>
                        <div class="col-sm-9 col-md-9">
                            <input type="number" class="form-control" placeholder="Numri i stokut kur është sasia limituar" id="nrsasialimituarED">

                        </div>
                    </div>
                    <div class="input-group mb-12">
                        <label class="col-sm-3 col-md-3 control-label">Sasia Limituar</label>
                        <div class="col-sm-9 col-md-9">
                            <input type="number" class="form-control" placeholder="Maksimumi për porosi kur është e limituar" id="sasialimituarED">

                        </div>
                    </div>
                    <div class="input-group mb-12">
                        <label class="col-sm-3 col-md-3 control-label">TVSH</label>
                        <div class="col-sm-9 col-md-9">
                            <span class="bmd-form-group"><input type="number" class="form-control" placeholder="Perqindja e tvsh-së" id="tvshED"></span>

                        </div>
                    </div>
                    <div class="input-group mb-12">
                        <label class="col-sm-3 col-md-3 control-label">Çmimi</label>
                        <div class="col-sm-4 col-md-4">
                            <input type="number" class="form-control" placeholder="Çmimi Produktit" id="cmimiED">
                        </div>

                        <label for="cmimicheck" style="padding-top:12px;" class="col-sm-3 col-md-3 control-label">Shfaq çmimin</label>
                        <div style="padding-top:12px;" class="col-sm-2 col-md-2">
                            <input type="checkbox" class="checkbox" style="margin-left:-15px;" placeholder="Çmimi Produktit" id="cmimicheckED">
                        </div>

                        <div class="input-group mb-12">
                            <label class="col-sm-3 col-md-3 control-label">Fotoja e Produktit</label>
                            <div class="col-sm-9 col-md-9 row">
                                <div class="col-sm-6 col-md-6">
                                    <input id="stokufotoED" onchange="fotopostget(this)" type="file" style="display:none;">
                                    <label for="stokufotoED" class="btn btn-warning" style=" height: 35px"><i class="material-icons">add</i>Shto logo</label>
                                </div>

                                <div class="col-sm-6 col-md-6 row" style="padding-top: 15px;">
                                    <label for="fotocheck" style="margin-left:-10px;" class="col-md-8 control-label">Shfaq Foton e paisjes</label>
                                    <input class="col-md-4" style="margin-left:10px;margin-top: 10px;" type="checkbox" placeholder="Çmimi Produktit" id="fotocheckED">

                                </div>
                                <img id="fotoed" width="100" height="70" />
                            </div>

                        </div>
                    </div>
                </div>
                <div class="modal-footer justify-content-between">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Jo</button>
                    <button type="button" id="edit-ruaj-stock" onclick="" class="btn btn-success">Ruaj</button>
                </div>
            </div>
        </div>
    </div>
    <!-- ////////////////////////////////////////////////////////////////////////////////////////////////////////// -->
    <!-- ////////////////////////////////////////////////////////////////////////////////////////////////////////// -->
    <!-- ////////////////////////////////////////////////////////////////////////////////////////////////////////// -->
    <div class="modal fade" id="modal-reagents">
        <div class="modal-dialog" style="max-width: 600px">
            <div class="modal-content ">
                <div class="modal-header">
                    <h4 class="modal-title">Regjistro Reagjenta</h4>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">

                    <p style="text-align: center"> REAGJENT</p>

                    <div class="input-group mb-12 edit" id="">
                        <label class="col-sm-3 col-md-3 col-2 control-label">Brandi</label>
                        <div class="col-sm-9 col-md-9 col-10" id="getreagjentbrend">

                            <script>
                                $.ajax({
                                    type: 'POST',

                                    url: url + "/firma",
                                    headers: {
                                        'api_token': token
                                    },
                                    data: {
                                        veprimi: 'select',
                                        emri_brendit: ' ',
                                        id: 0,
                                        emri_firmes: ' ',
                                        pershkrim: ' ',
                                        logoimg: ' ',
                                        userid: 0,
                                        ord: 0
                                    },
                                    success: function(data, status) {
                                        y = "<select  id=\"regentsBrend\" class=\"form-control selectized select2\" autocomplete=\"off\" tabindex=\"-1\">";
                                        y += "<option value=\"\" selected=\"selected\">-Zgjedh Brandin-</option>";

                                        for (i = 0; i < data.length; i++) {
                                            y += '<option value="' + data[i].id + '">' + data[i].emri_prodhuesit + '</option> ';
                                        }

                                        // $('#brand_stock').append(z);
                                        document.getElementById("getreagjentbrend").innerHTML = y;


                                        $("#regentsBrend").change(function() {
                                            $.ajax({
                                                url: url + "/paisjeforfirma/" + $("#regentsBrend").val(),
                                                headers: {
                                                    'api_token': token
                                                },
                                                type: 'GET',
                                                success: function(data, status) {
                                                    y = "<select  id=\"paisjereagjent\" class=\"form-control selectized select2\" autocomplete=\"off\" tabindex=\"-1\">";
                                                    y += "<option value=\"\" selected=\"selected\">-Zgjedh Pajisjen-</option>";

                                                    for (i = 0; i < data.length; i++) {
                                                        y += '<option value="' + data[i].id + '">' + data[i].emri_pajisjes + '</option> ';

                                                    }

                                                    document.getElementById("getPajisjestockreagjent").innerHTML = y;
                                                    $('#reagjentpaisje').show();
                                                }
                                            });
                                        });
                                    }
                                });
                            </script>
                        </div>
                    </div>

                    <div class="input-group mb-12 edit" id="reagjentpaisje">
                        <label class="col-sm-3 col-md-3 col-2 control-label">Pajisja</label>
                        <div class="col-sm-9 col-md-9 col-10" id="getPajisjestockreagjent">


                        </div>
                    </div>
                    <div class="col-12" id="shto_Ketu">
                        <div class="row" style="margin-top: 10px; margin-bottom: -10px">
                            <div class="col-md-11 col-10">
                                <p> Shto fushë </p>
                            </div>
                            <div class="col-md-1 col-2 btnShtoHyrje" style="float: right; margin-left: -12px;">
                                <button id="shtoRresht" class="btn btn-sm btn-outline-secondary rounded-circle" style="width:30px;height: 30px;padding: 6px;" data-toggle="tooltip" data-placement="top" title="Shto fushë"><i class="material-icons dp48" id="">add</i></button>
                            </div>
                        </div>
                        <hr>

                        <div class="row inputat" id="old_inputs">
                            <div class="col-sm-7 col-md-7 col-10 hapsira-row-g-2">
                                <input type="text" class="form-control emrirea" id="EmriReagjentit" name="EmriReagjentit" style="margin-right: -15px; width: 100%; border: none;" placeholder="Shkruani emrin e reagjentit" value="" required>
                            </div>
                            <div class="col-sm-2 col-md-2 col-10 hapsira-row-g-2">
                                <input type="text" class="form-control sasiarea" id="sasiar" name="sasiar" style="margin-right: -15px; width: 100%; border: none;" placeholder="Sasia" value="" required>
                            </div>
                            <div class="col-sm-2 col-md-2 col-10 hapsira-row-g-2">
                                <input type="text" class="form-control cmimirea" id="cmimir" name="cmimir" style="margin-right: -15px; width: 100%; border: none;" placeholder="Cmimi" value="" required>
                            </div>
                            <div class="col-md-1 col-1 hapsira-row-g-2">
                                <!-- <button type="button" id="" class="close iksi" onclick="$('#old_inputs').remove();"><span class="iksi" aria-hidden="true">&times;</span></button> -->
                            </div>
                        </div>

                    </div>
                </div>

                <div class="modal-footer ">
                    <div class="col-12">
                        <div class="row">
                            <div class="col-12">
                                <!-- <p class="" style="color: red; font-weight:400;">Reagjenti mund te jetë tashmë i regjistruar</p> -->
                            </div>
                            <div class="col-12 ">
                                <button type="button" class="btn btn-default" data-bs-dismiss="modal">Jo</button>
                                <button type="button" id="btn-reagjent" onclick="" class="btn btn-success">Ruaj</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        $('#btn-reagjent').click(function(e) {
            e.preventDefault();
            tru = true;
            $('.emrirea').keyup(function(e) {

                $('.inputat').each(function(index, element) {
                    if ($(this).find('.emrirea').val() == '') {
                        tru = false;
                        if ( $(this).find('.errori').length==0) {
                        $(this).find('.emrirea').after('<span class="errori" style="color:red;">Kjo fush është obligative</span>');
                            
                        }
                    } else {
                        $(this).find('.errori').remove();
                    }

                });
            });
            $('.emrirea').keyup();
            if (tru) {


                emri = [];
                sasia = [];
                cmimi = [];
                $('.inputat').each(function(index, element) {
                    emri.push($(this).find('.emrirea').val());
                    sasia.push($(this).find('.sasiarea').val());
                    cmimi.push($(this).find('.cmimirea').val());

                });
                data = {
                    brand_id: $("#regentsBrend").val(),
                    pajisja_id: $("#paisjereagjent").val(),
                    emri: emri,
                    sasia: sasia,
                    cmimi: cmimi
                };
                $.ajax({
                    type: 'POST',
                    url: url + "/regreagjent",
                    data: data,
                    headers: {
                        'api_token': '<?php echo $token ?>'
                    },
                    success: function(data, textStatus, jqXHR) {
                        // location.reload();
                        $('#modal-reagents').modal('hide');
                    },
                    error: function(request, textStatus, errorThrown) {
                        toastr.error(request.responseJSON.message);
                    }
                });
                console.log(data);
            }
        });
        editid = 0;

        function selectuser_click(id) {
            editid = id;

            // tbl_stock
            $.ajax({
                type: 'GET',
                url: url + "/tbl_stock",
                data: {
                    id: id
                },
                headers: {
                    'api_token': '<?php echo $token ?>'
                },
                success: function(data, textStatus, jqXHR) {
                    $('#Brendied').val(data[0].emri_prodhuesit);
                    $('#Pajisjaedit').val(data[0].emri_pajisjes);
                    $('#sasiaED').val(data[0].sasia);
                    $('#maxporosiED').val(data[0].maxporosi);
                    $('#nrsasialimituarED').val(data[0].sasialimituar);
                    $('#sasialimituarED').val(data[0].minporosi);
                    $('#tvshED').val(data[0].tvsh);
                    $('#cmimiED').val(data[0].cmimi);
                    fotoed = data[0].foto;
                    if (data[0].foto != "") {
                        $('#fotoed').show();
                        $('#fotoed').attr('src', data[0].foto);
                    } else {
                        $('#fotoed').hide();
                    }


                    data[0].shfaqcmimin == 1 ? $('#cmimicheckED').prop('checked', true) : $('#cmimicheckED').prop('checked', false);
                    data[0].fotocheck == 1 ? $('#fotocheckED').prop('checked', true) : $('#fotocheckED').prop('checked', false);

                    //    $('#fotocheckED').val(data[0].emri_pajisjes);

                },

            });
            $('#modal-stoku-edit').modal('show');
        }

        $("#btn-ruaj-stock").click(function() {
            if ($('#cmimicheck').is(':checked')) {
                cmcheck = 1;
            } else {
                cmcheck = 0;
            }
            if ($('#fotocheck').is(':checked')) {
                ftcheck = 1;
            } else {
                ftcheck = 0;
            }


            if (($('#fotocheck').is(':checked') || $('#stokufoto')[0].files.length != 0) &&
                ($("#get_brand").val() != null) &&
                ($("#select-pajisjee-stoku").val() != undefined) &&
                ($("#sasia").val() != null) &&
                ($("#maxporosi").val() != null)
            ) {
                if ($('#fotocheck').is(':checked')) {
                    ajaxdata = {
                        fotocheck: ftcheck,
                        foto: null,
                        pajisja_id: $("#select-pajisjee-stoku").val(),
                        brand_id: $("#get_brand").val(),
                        sasia: $("#sasia").val(),
                        max: $("#maxporosi").val(),
                        nrsasislimit: $("#nrsasialimituar").val(),
                        maxsasilimit: $("#sasialimituar").val(),
                        cmimi: $("#cmimi").val(),
                        cmcheck: cmcheck,
                        tvsh: $("#tvsh").val(),
                        userid: <?php echo $_SESSION['_session']['id']; ?>
                    };
                    $.ajax({
                        type: 'POST',
                        url: url + "/regstokun",
                        data: ajaxdata,
                        headers: {
                            'api_token': '<?php echo $token ?>'
                        },
                        success: function(data, textStatus, jqXHR) {
                            location.reload();
                        },
                        error: function(request, textStatus, errorThrown) {
                            toastr.error(request.responseJSON.message);
                        }
                    });
                } else {
                    var file = document.querySelector('#stokufoto').files[0];
                    var reader = new FileReader();
                    reader.readAsDataURL(file);
                    reader.onload = function() {
                        base64file = reader.result;
                        ajaxdata = {
                            fotocheck: ftcheck,
                            foto: base64file,
                            pajisja_id: $("#select-pajisjee-stoku").val(),
                            brand_id: $("#get_brand").val(),
                            sasia: $("#sasia").val(),
                            max: $("#maxporosi").val(),
                            nrsasislimit: $("#nrsasialimituar").val(),
                            maxsasilimit: $("#sasialimituar").val(),
                            cmimi: $("#cmimi").val(),
                            cmcheck: cmcheck,
                            tvsh: $("#tvsh").val(),
                            userid: <?php echo $_SESSION['_session']['id']; ?>
                        };
                        $.ajax({
                            type: 'POST',
                            url: url + "/regstokun",
                            data: ajaxdata,
                            headers: {
                                'api_token': '<?php echo $token ?>'
                            },
                            success: function(data, textStatus, jqXHR) {
                                location.reload();
                            },
                            error: function(request, textStatus, errorThrown) {
                                toastr.error(request.responseJSON.message);
                            }
                        });
                    };
                }
            } else {
                toastr.error("Ju lutem plotsoi fushat");

            }

        });

        // editmodali////////////////////////////////////////////
        $("#edit-ruaj-stock").click(function() {
            if ($('#cmimicheckED').is(':checked')) {
                cmcheck = 1;
            } else {
                cmcheck = 0;
            }
            if ($('#fotocheckED').is(':checked')) {
                ftcheck = 1;
            } else {
                ftcheck = 0;
            }


            if (($("#sasiaED").val() != null) &&
                ($("#maxporosiED").val() != null)
            ) {
                ajaxdata = {
                    id: editid,
                    fotocheck: ftcheck,
                    foto: fotoed,
                    sasia: $("#sasiaED").val(),
                    max: $("#maxporosiED").val(),
                    nrsasislimit: $("#nrsasialimituarED").val(),
                    maxsasilimit: $("#sasialimituarED").val(),
                    cmimi: $("#cmimiED").val(),
                    cmcheck: cmcheck,
                    tvsh: $("#tvshED").val(),
                    userid: <?php echo $_SESSION['_session']['id']; ?>
                };
                console.log(ajaxdata);
                $.ajax({
                    type: 'PUT',
                    url: url + "/edregstokun",
                    data: ajaxdata,
                    headers: {
                        'api_token': '<?php echo $token ?>'
                    },
                    success: function(data, textStatus, jqXHR) {
                        $('#modal-stoku-edit').modal('hide');
                    },
                    error: function(request, textStatus, errorThrown) {
                        // toastr.error(request.responseJSON.message);
                    }
                });
            }
        });
        ///////////////////////////////

        extraRresht = 0;
        $("#shtoRresht").click(function() {
            $("#shto_Ketu").append(
                '<div class="row inputat" id="extra_input' + extraRresht + '">' +

                '<div class="col-sm-7 col-md-7 col-10 hapsira-row-g-2">' +
                '<input type="text" class="form-control emrirea" id="EmriReagjentit' + extraRresht + '" name="EmriReagjentit' + extraRresht + '" style="margin-right: -15px; width: 100%; border: none;" placeholder="Shkruani emrin e reagjentit" value="" required>' +
                '</div>' +
                '<div class="col-sm-2 col-md-2 col-10 hapsira-row-g-2">' +
                '<input type="text" class="form-control sasiarea" id="sasiar' + extraRresht + '" name="sasiar' + extraRresht + '" style="margin-right: -15px; width: 100%; border: none;" placeholder="Sasia" value="" required>' +
                '</div>' +
                '<div class="col-sm-2 col-md-2 col-10 hapsira-row-g-2">' +
                '<input type="text" class="form-control cmimirea" id="cmimir' + extraRresht + '" name="cmimir' + extraRresht + '" style="margin-right: -15px; width: 100%; border: none;" placeholder="Cmimi" value="" required>' +
                '</div>' +
                '<div class="col-md-1 col-1 hapsira-row-g-2">' +
                ' <button type="button"  class="close iksi" onclick="$(\'#extra_input' + extraRresht + '\').remove();"><span class="iksi" aria-hidden="true">&times;</span></button>' +
                '</div>' +
                '</div>'
            );
            extraRresht += 1;
        });
    </script>
    <!-- end modal stoku stock stoku stoku  -->

    <script>

    </script>

    <!-- perfondon edito modalin -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

    <script src="plugins/datatables/jquery.dataTables.min.js"></script>
    <script src="plugins/datatables-bs4/js/dataTables.bootstrap4.min.js"></script>
    <script src="plugins/datatables-responsive/js/dataTables.responsive.min.js"></script>
    <script src="plugins/datatables-responsive/js/responsive.bootstrap4.min.js"></script>
    <script src="plugins/datatables-buttons/js/dataTables.buttons.min.js"></script>
    <script src="plugins/datatables-buttons/js/buttons.bootstrap4.min.js"></script>
    <script src="plugins/jszip/jszip.min.js"></script>
    <script src="plugins/pdfmake/pdfmake.min.js"></script>
    <script src="plugins/pdfmake/vfs_fonts.js"></script>
    <script src="plugins/datatables-buttons/js/buttons.html5.min.js"></script>
    <script src="plugins/datatables-buttons/js/buttons.print.min.js"></script>
    <script src="plugins/datatables-buttons/js/buttons.colVis.min.js"></script>
    <script src="plugins/moment/moment.min.js"></script>

    <script>
        $(function() {

            // start of oTable
            oTable = $("#example1").DataTable({

                "responsive": true,
                "lengthChange": false,
                "autoWidth": false,
                "buttons": ["copy", "csv", "excel", "pdf", "print", "colvis"],
                "order": [
                    [0, "desc"]
                ],
                "language": {
                    "search": "",
                    "searchPlaceholder": "Kërko",
                    "lengthMenu": "Shfaq _MENU_ të dhëna për faqe",
                    "zeroRecords": "Nuk u gjet asgjë",
                    "info": "Shfaqur faqja _PAGE_ prej _PAGES_",
                    "infoEmpty": "Nuk ka të dhëna",
                    "infoFiltered": "(Filtruar _MAX_ të gjithave)",

                    "loadingRecords": "Loading...",
                    "processing": "Duke procesuar...",
                    //"search":         "Kërko:",
                    "paginate": {
                        "first": "E para",
                        "last": "E fundit",
                        "next": "Tjetra",
                        "previous": "Para"
                    },
                },
                "pageLength": 15,
                "columnDefs": [{
                    "targets": [0],
                    "visible": false,

                }]
            });
            $('#example1_wrapper .col-md-6:eq(0)').append('<div class="row" ' +
                '<div class="input-group col-3">' +
                '<div class="col"><input class=form-control date" type="date" id="min" /></div>' +
                '<div class="col"><input class=form-control date" type="date" id="max" /></div>' +
                '<div class="col"><i class="material-icons dp48" id="searchbydate">search</i></div>' +
                '</div></div>').ready(function() {
                $('#min').val(new Date().toISOString().split('T')[0]);
                $('#max').val(new Date().toISOString().split('T')[0]);
            });
            $('#searchbydate').on('click', function() {
                $.fn.dataTable.ext.search.push(function(settings, data, dataIndex) {
                    var min = $('#min').val();
                    var max = $('#max').val();
                    var date = new Date(data[3]);
                    date = JSON.stringify(date);
                    date = date.slice(1, 11);

                    if ((min === '' && max === '') || (min === '' && date <= max) || (min <= date && max === '') || (min <= date && date <= max)) {
                        return true;
                    }
                    return false;
                });

                oTable.draw();
            });
        }); // end of oTable

        fotoed = '';

        function fotopostget(input) {
            if (input.files && input.files[0]) {
                var edit = new FileReader();

                edit.onload = function(e) {
                    fotoed = e.target.result;
                    $('#fotoed').attr('src', e.target.result);
                    $('#fotoed').show();
                };


                edit.readAsDataURL(input.files[0]);
            }
        }
    </script>


<?php require 'footer.php';
} else {
    header("location:../logout.php");
}
?>