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

  if ($_SESSION['_session']['declined_service_requests'] == 1) {
    require 'header.php';
?>
    <style>
      .dataTable>tbody>tr>td {
        padding: 1px !important;
      }
    </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 e Anuluara</div>
          <div class="row">
            <div class="col-12">
              <div class="card">
                <div id="tabel12"></div>
                <div id="getpc" class="card-body">
                  <?php
                  $postdata = http_build_query(
                    array(
                      'veprimi' => 'mine_selectanuluara',
                      'pranuesi_id' =>   $_SESSION['_session']['id'],
                    )
                  );
                  $opts = [
                    "http" => [
                      "method" => "GET",
                      "header" => "api_token:" . $token,

                    ]

                  ];
                  $context = stream_context_create($opts);

                  $str_data = file_get_contents($urlapi . "/crudrequests?" . $postdata, 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>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 type=\"hidden\">" . $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; font-weight: 700">' . $data[$i]["emertimi"] . '</td>';
                    $temp .= '<td><button type="button" style="padding: 6px" class="btn btn-danger btn-xs"  onclick="ktheje(' . $data[$i]["visitid"] . ')" title="Ktheje porosinë në pranim"><i class="material-icons">replay</i></button></td>';
                    //spo di a kur refuzohet me kthy ne pritje apo me refuzu komplet me ja jap dy mundesi ???

                    $temp .= "</tr>";
                  }
                  $temp .= "</tbody></table>";
                  echo $temp;
                  ?>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <script>
      function ktheje(id) {
        $.confirm({
          title: 'Rikthe!',
          content: 'Deshironi ta riktheni kerkesen!',
          buttons: {
            Anulo: {
              btnClass: 'btn-red',
            },
            Rikthe: function() {
              $.ajax({
                    url: url + "/kthejepritje/" + id + "/" + <?php echo $_SESSION['_session']['id'] ?>,
                    headers: {
                        'api_token': token
                    },
                    data: {
                        pershkrimianulimit: ' ',
                    },
                    type: 'PUT',
                    success: function(data, status) {
                 
                            window.location.replace("./Kerkesat_npritje.php");
                      
                    },
                });
            },

          }
        });
      }
    </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">SHIKO ose Edito</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">
              <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" style="padding: 2px; max-height: 300px; width: 100%; flex-basis: auto; overflow: overlay;">
                  <label>Përshkrimi i problemit nga klienti</label>
                  <textarea class="form-control border border-box" style="border-radius: 10px; background:transparent" readonly></textarea>
                </div>

                <div class="col col-lg-6 col-md-12 col-sm-12" style="padding: 2px; max-height: 300px; width: 100%; flex-basis: auto; overflow: overlay;">
                  <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" style="padding: 2px; max-height: 300px; width: 100%; flex-basis: auto; overflow: overlay;">
                  <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="serviset">
                          <tr id="ServiceName" class="clicked">
                            <td class="active" style="padding-top: 5px; padding-bottom: 5px" onclick="">Kontrrollë</td>
                          </tr>
                          <tr id="ServiceName" class="clicked">
                            <td class="active" style="padding-top: 5px; padding-bottom: 5px" onclick="">Kontrrollaa</td>
                          </tr>
                        </tbody>
                      </table>
                    </div>
                  </div>
                </div>
                <script>
                  function clickid(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 / 120);
                      }
                    });
                    $.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();
                      }
                    });


                  }

                  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> <!-- row -->
            </div> <!-- col-md-12 -->
          </div> <!-- body -->
          <script>
            function clickid(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);

                }
              });
              $.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();
                }
              });


            }

            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 class="modal-footer justify-content-between"> </div>
        </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>
      $(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");
}
?>