<?php
session_start();
if (isset($_SESSION['_session']['user']) &&  (time() - $_SESSION['_session']['LAST_ACTIVITY']  < $_SESSION['_session']['koha'])) {
    $_SESSION['_session']['LAST_ACTIVITY']  = time();

    if ($_SESSION['_session']['my_services_requests'] == 1 || $_SESSION['_session']['my_clinic_service_requests'] == 1) {
        require 'header.php';
?>
        <style>
            .dataTable>tbody>tr>td {
                padding: 1px !important;
            }

            .clicked {
                background-color: beige;
            }

            .cart-inform {
                /* border-style: outset; */
                border-color: #ff9800;
                box-shadow: 0 1px 4px 0 rgb(0 0 0 / 14%);
                border-radius: 10px;
                padding: 0px;
                padding-top: 15px;
                margin-left: 3px;
            }
        </style>
        <div class="content-wrapper">
            <div class="content-header">
                <div class="container-fluid" style="padding-top: 20px;">
                    <div class="alert alert-danger" role="alert" style="height: 40px; padding: 10px">Porositë e Anuluara</div>
                    <div class="row">
                        <div class="col-12">
                            <div class="card">
                                <div id="tabel12"></div>
                                <div id="getpc" class="card-body">

                                    <table id="example1" class="table table-bordered table-hover">
                                        <thead>
                                            <tr>
                                                <th>ID</th>
                                                <th>Institucioni</th>
                                                <th>Personi pergjegjes </th>
                                                <th>Data</th>
                                                <th>Qyteti</th>
                                                <th>Statusi</th>
                                                <th>Mundësia</th>
                                            </tr>
                                        </thead>
                                        <tbody>

                                            <?php
                                            $opts = [
                                                "http" => [
                                                    "method" => "GET",
                                                    "header" => "api_token:" . $token,

                                                ]

                                            ];
                                            $context = stream_context_create($opts);

                                            $str_data = file_get_contents($urlapi . "/getporosite/3", false, $context);
                                            $temp = '';

                                            $data = json_decode($str_data, true);

                                            if ($str_data == "[300]") {
                                            } else {
                                                $length = count($data);
                                                for ($i = 0; $i < $length; $i++) {
                                                    $temp .= "<tr><td>" . $data[$i]["Id"] . "</td>";

                                                    $temp .= "<td>" . $data[$i]["emri_institutit"] . "</td>";
                                                    $temp .= "<td>" . $data[$i]["emri"] . " " . $data[$i]["mbiemri"] . "</td>";

                                                    $temp .= "<td>" . $data[$i]["emri_qytetit"] . "</td>";
                                                    $temp .= "<td>" . $data[$i]["koha"] . "</td>";


                                                    // $temp .= "<td>" . $data[$i]["statusi"] . "</td>";
                                                    switch ($data[$i]["statusi"]) {
                                                        case 3:
                                                            $ngjyra = 'color:red;';

                                                            break;
                                                        case 0:
                                                            $ngjyra = 'color: hsl(47deg 52% 54%);';

                                                            break;
                                                        case 4:
                                                            $ngjyra = 'color:red;';

                                                            break;
                                                        case 2:
                                                            $ngjyra = 'color: green;';

                                                            break;
                                                        case 1:
                                                            $ngjyra = 'color: #ff9e0f;';

                                                            break;
                                                    }
                                                    $temp .= '<td style="' . $ngjyra . ' font-weight: 700">' . $data[$i]["emertimi"] . '</td>';

                                                    $temp .= ' <td><button type="button" onclick="shiko(' . $data[$i]["Id"] . ')"  style="padding: 4px"    class="btn btn-info btn-xs" data-toggle="modal" data-target="#modal-see"><i class="material-icons">remove_red_eye</i></button></td>';

                                                    $temp .= "</tr>";
                                                }
                                                echo $temp;
                                            }
                                            ?>




                                        </tbody>
                                    </table>

                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <script>
            function shiko(id) {

                $.ajax({
                    type: 'GET',
                    url: url + "/getporositepopup/" + id,

                    headers: {
                        'api_token': '<?php echo $token ?>'
                    },
                    success: function(data, textStatus, jqXHR) {
                        pj = '';
                        totali = 0.00;
                        for (var r in data) {
                            pj += '<div class="col-lg-12 cart-inform">' +
                                '<div class="row">' +
                                '<div class="col-sm-2">' +
                                '<p>' + data[r].sasia + 'x</p>' +
                                '</div>' +
                                '  <div class="col-sm-8">' +
                                ' <p style="font-size: 14px;">' + data[r].emri_pajisjes + '</p>' +
                                ' </div>' +
                                ' <div class="col-sm-2">' + data[r].cmimi + '€</div>' +
                                '</div>' +
                                ' </div>';

                            totali += parseFloat(data[r].cmimi);

                        }
                        $('#porositl').empty();
                        $(pj).appendTo('#porositl');
                        $('#totalicm').empty();
                        $('#totalicm').append(totali + '€');
                        $('#emrilab').empty();
                        $('#emrilab').append(data[0].emri_institutit);
                        $('#qytetilab').empty();
                        $('#qytetilab').append(data[0].emri_qytetit);
                        $('#emrimbiemrilab').empty();
                        $('#emrimbiemrilab').append(data[0].emri + ' ' + data[0].mbiemri);
                        $('#emaillab').empty();
                        $('#emaillab').append(data[0].perdoruesi);
                        $('#komenti').empty();
                        $('#komenti').append(data[0].komenti);



                        $('#datalf').empty();

                        dataliferimit = new Date(data[0].koha_liferimit);
                        ditalf = ("0" + dataliferimit.getDate()).slice(-2);
                        mujilf = ("0" + (dataliferimit.getMonth() + 1)).slice(-2);
                        datalif = dataliferimit.getFullYear() + "-" + (mujilf) + "-" + (ditalf);
                        if (data[0].koha_liferimit == null) {
                            $('#datalf').val(' ');
                            $('#koha_liferimit').attr('checked', false);

                        } else {

                            $('#datalf').val(datalif);
                            $('#koha_liferimit').attr('checked', true);
                        }


                    },

                    error: function(request, textStatus, errorThrown) {
                        toastr.error(request.responseJSON.message);

                    }

                });
            }
        </script>
        <!-- Modal for Editin or Viewing Brands & Devices And Services in it -->
        <div class="modal fade" id="modal-see">
            <div class="modal-dialog modal-lg">
                <div class="modal-content">
                    <div class="modal-header">
                        <h4 class="modal-title">Kërkesa</h4>
                        <button type="button" id="closesee" class="close" data-dismiss="modal" aria-label="Close">

                            <span aria-hidden="true">×</span>
                        </button>

                    </div>
                    <div class="modal-body">
                        <div class="col-md-12 responsive">
                            <div class="row responsive" id="emridhembiemr">
                                <div class="col-sm-3">
                                    <p>Emri Laboratorit<br> <strong id="emrilab">Qendra medinaaaaa</strong> </p>
                                </div>
                                <div class="col-sm-3">
                                    <p>Qyteti<br> <strong id="qytetilab">Ferizajj</strong> </p>
                                </div>
                                <div class="col-sm-3">
                                    <p>Personi përgjegjës<br> <strong id="emrimbiemrilab">Filan Fisteku</strong></p>
                                </div>
                                <div class="col-sm-3">
                                    <p>Emaili<br> <strong style="word-break: break-all;" id="emaillab">example@mail.com </strong></p>
                                </div>
                            </div>
                        </div>
                        <hr>
                        <div class="col-md-12" style="max-height: 420px; overflow-y: auto; overflow-x:hidden; " id="porositl">

                        </div><!-- col-md-12 -->
                        <hr>

                        <div class="col-md-12" style="justify-content: space-between;">
                            <div class="row">
                                <div class="col-sm-10">
                                    <strong> TOTAL:</strong>
                                </div>
                                <div class="col-sm-2"><strong id="totalicm"></strong></div>
                            </div>
                        </div>
                        <hr>
                        <div class="col-md-12">
                            <div class="row" id="rrethporosise">
                                <div class="col-sm-3">
                                    <label>
                                        <input type="checkbox" id="koha_liferimit" name="" disabled> Koha liferimit:
                                    </label>
                                    <!-- if isnt checked date remain disable -->
                                    <input type="date" id="datalf" class="form-control" max="9999-12-31" disabled>
                                </div>
                                <!-- koha liferimit ka me qen opsionale -->
                                <!-- komenti nese refuzohet patjeter duhet me komentu se pse po refuzohet si ktu si te kerkesa -->
                                <div class="col-sm-9 form-group bmd-form-group">
                                    <label class="form-floating bmd-label-static" style="background-color: white; left: 15px;" disabled>Komenti</label>
                                    <textarea type="text" id="komenti" class="form-control" style="border: 1px solid green; border-radius: 6px; " disabled></textarea>
                                </div>
                            </div>
                        </div>
                    </div> <!-- body -->
                </div>
            </div>
        </div>
        <!-- END modal for editin or viewing BRANDS & dEVICES and Services -->

        <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>
            $('#pershkrimi').each(function() {
                this.setAttribute('style', 'height:  auto  !important;overflow-y:hidden;');
            });
            $(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...",

                        "paginate": {
                            "first": "E para",
                            "last": "E fundit",
                            "next": "Tjetra",
                            "previous": "Para"
                        },
                    },
                    "pageLength": 10,
                    // "columnDefs": [{
                    //   "targets": [0],
                    //   "visible": false,
                    //   "order": [1, "desc"],
                    // }]

                });
                $('#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();
                });
            });
        </script>

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