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

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

      .clicked {
        background-color: beige;
      }
    </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">Kërkesat në Pritje</div>
          <div class="row">
            <div class="col-12">
              <div class="card">
                <div id="tabel12"></div>
                <div id="getpc" class="card-body">
                  <?php
              
                  $opts = [
                    "http" => [
                      "method" => "GET",
                      "header" => "api_token:" . $token,

                    ]

                  ];
                  $context = stream_context_create($opts);
                  if ($_SESSION['_session']['roli']==1) {
                    $sesioni=1;
                  }else{
                    $sesioni=  $_SESSION['_session']['institucioni'];
                  }

                  $str_data = file_get_contents($urlapi . "/getkerkesapritje/" . $sesioni, 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>ID</th>";

                  $temp .= "<th>Institucioni</th>";
                  $temp .= "<th>Personi pergjegjes </th>";
                  $temp .= "<th>Data</th>";
                  $temp .= "<th>Prioriteti</th>";
                  $temp .= "<th>Statusi</th>";
                  $temp .= "<th>Mundësia</th></tr></thead> <tbody>";

                  for ($i = 0; $i < $length; $i++) {
                    $temp .= "<td>" . $data[$i]["visitid"] . "</td>";

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

                    $temp .= "<td >" . $data[$i]["prioriteti"] . "</td>";
                    $temp .= "<td style=\"color: red\"> " . $data[$i]["emertimi"] . " </td>";
                    $temp .= "<td><button type=\"button\" style=\"padding: 4px\" onClick=\"clickid(" . $data[$i]["visitid"] . ")\" class=\"btn btn-info btn-xs\" data-toggle=\"modal\" data-target=\"#modal-see\"><i class=\"material-icons\">edit</i></button></td>";

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

    <!-- 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>
            <script>
              $("#closesee").click(function() {
                add = '<tr> <td> </td> <td></td> <td></td></tr>';
                document.getElementById("anshfaq").innerHTML = add;
              });
            </script>
          </div>
          <div class="modal-body">
            <div class="col-md-12">
              <div class="row" id="emridhembiemr">
                <div class="col-md-3">
                  <p>Emri Laboratorit<br> <strong>Qendra mjekesore medina</strong> </p>
                </div>
                <div class="col-md-3">
                  <p>Personi pergjegjes<br> <strong>Filan Fisteku</strong></p>
                </div>
                <div class="col-md-3">
                  <p>Qyteti<br> <strong>Prishtinë</strong> </p>
                </div>
                <div class="col-md-3">
                  <p>Prioriteti<br> <strong style="color: red">Urgjent</strong> </p>
                </div>
              </div>
            </div>
            <div class="col-md-12">
              <div class="row">
                <div class="col col-lg-12 col-md-12 col-sm-12">
                  <label>Përshkrimi i problemit nga klienti</label>
                  <textarea class="form-control border border-box" id="pershkrimi" style="border-radius: 10px; background:transparent" readonly>Përshkrimi i  Klientiti ... .. . problemi qendron tek gjilpera dhe versioni i sistemit </textarea>
                </div>


                <div class="col col-lg-6 col-md-12 col-sm-12">
                  <div class="card">
                    <div class="card-body table-responsive p-0">
                      <table class="table table-head-fixed text-wrap">
                        <thead>
                          <tr>
                            <th class="fixheader">Pajisjet</th>
                          </tr>
                        </thead>
                        <tbody id="pajisjet">

                        </tbody>
                      </table>
                    </div>
                  </div>
                </div>

                <div class="col col-lg-6 col-md-12 col-sm-12">
                  <div class="card">
                    <div class="card-body table-responsive p-0">
                      <table class="table table-head-fixed text-wrap">
                        <thead>
                          <tr>
                            <th class="fixheader">Serviset</th>
                          </tr>
                        </thead>
                        <tbody id="serviset">

                        </tbody>
                      </table>
                    </div>
                  </div>
                </div>




              </div> <!-- row -->
            </div> <!-- col-md-12 -->
          </div> <!-- body -->
          <script>
            clickedd = 0;

            function clickid(id) {
              clickedd = id;
              $.ajax({
                url: url + "/getbyvisit/" + id,
                headers: {
                  'api_token': token
                },
                type: 'GET',
                success: function(data, status) {
                  $('#emridhembiemr').empty();
                  $('#pershkrimi').empty();
                  $('#emridhembiemr').append(
                    ' <div class="col-md-3">' +
                    '<p>Emri Laboratorit<br> <strong>' + data[0].emri_institutit + '</strong> </p>' +
                    '</div>' +
                    '<div class="col-md-3">' +
                    '<p>Personi pergjegjes<br> <strong>' + data[0].emri + ' ' + data[0].mbiemri + '</strong></p>' +
                    '</div>' +
                    '<div class="col-md-3">' +
                    '<p>Qyteti<br> <strong>' + data[0].emri_qytetit + '</strong> </p>' +
                    '</div>' +
                    '<div class="col-md-3">' +
                    '<p>Prioriteti<br> <strong style="color: red">' + data[0].prioriteti + '</strong> </p>' +
                    '</div>'
                  );
                  $('#pershkrimi').val(data[0].pershkrimi);
                  $('#pershkrimi').attr('rows', data[0].pershkrimi.length / 90);
                }
              });
              $.ajax({
                url: url + "/getpaisjevisit/" + id,
                headers: {
                  'api_token': token
                },
                type: 'GET',
                success: function(data, status) {

                  i = '';
                  for (var r in data) {
                    i += '<tr  id="paisjaa' + r + '" class="">' +
                      '<td id="gr' + r + '" style="padding-top: 5px; padding-bottom: 5px" onclick="shfaqpaisje(' + id + ',' + data[r].paisjaid + ',' + r + ')">' + data[r].emri_pajisjes + '</td>' +
                      '</tr>';
                  }

                  document.getElementById("pajisjet").innerHTML = i;
                  $('#gr0').click();
                }
              });


            }
            paisjeid = 0;

            function shfaqpaisje(id, sid, r) {
              $('.clicked').removeClass("clicked");
              $('#paisjaa' + r).addClass("clicked");
              $.ajax({
                url: url + "/getservisit/" + id + "/" + sid,
                headers: {
                  'api_token': token
                },
                type: 'GET',
                success: function(data, status) {

                  i = '';
                  for (var r in data) {
                    i += '<tr id="ser' + r + '" class="">' +
                      '<td style="padding-top: 5px; padding-bottom: 5px">' + data[r].service_name + '</td>' +
                      '</tr>';
                  }

                  document.getElementById("serviset").innerHTML = i;

                }
              });
            }
          </script>
          <div id="refuzimi" class="col-sm-12 form-group bmd-form-group" style="display: none ;">
            <label class="form-floating bmd-label-static" style="background-color: white; left: 15px;">Komento arsyen e refuzimit</label>
            <textarea type="text" id="arsyearef" class="form-control" style="border: 1px solid green; border-radius: 6px; "></textarea>
          </div>
          <div class="modal-footer justify-content-between">
            <button class="btn btn-sm btn-danger" id="btnrefuzo">Refuzo kërkesën</button>
            <?php 
            if ($_SESSION['_session']['roli']==1||$_SESSION['_session']['roli']==2 ||$_SESSION['_session']['processing_service_requests']==2 ) {
            
            echo '<button class="btn btn-sm btn-success" id="btnprano" >Prano kërkesën</button>';
             # code...
            } ?>
          </div>
        </div>
      </div>
    </div>
    <!-- END modal for editin or viewing BRANDS & dEVICES and Services -->
    <script>
      $('#btnrefuzo').click(function() {
        $("#refuzimi").show();
        if($('#arsyearef').val().length != 0){
        $.ajax({
          url: url + "/refuzokerkesen/" + clickedd + "/" + <?php echo $_SESSION['_session']['id'] ?>,
          headers: {
            'api_token': token
          },
          data:{
            pershkrimianulimit:$('#arsyearef').val()
          },
          type: 'PUT',
          success: function(data, status) {
            toastr.success('Refuzuar');
            setTimeout(function() {
              window.location.replace("./Kerkesat_anuluara.php");
            }, 1000);
          },
        });
      }
      });
      $('#btnprano').click(function() {

        $.ajax({
          url: url + "/aprovokerkesen/" + clickedd + "/" + <?php echo $_SESSION['_session']['id'] ?>,
          headers: {
            'api_token': token
          },
          type: 'PUT',
          success: function(data, status) {
            toastr.success('pranuar');
            setTimeout(function() {
              window.location.replace("./Kerkesat_nprocesim.php");
            }, 1000);
          },
        });
      });
    </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>
      $('#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");
}
?>