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

  if ($_SESSION['_session']['all_service_requests'] == 1 || $_SESSION['_session']['my_clinic_service_requests'] == 1 || $_SESSION['_session']['my_services_requests'] == 1) {
    require 'header.php';
?>
    <style>
      .dataTable>tbody>tr>td {
        padding: 0px !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-success" role="alert" style="height: 40px; padding: 10px">Kërkesat</div>
          <div class="row">
            <div class="col-12">
              <div class="card">
                <div id="tabel12"></div>
                <div id="getpc" class="card-body">
                  <?php
                  if($_SESSION['_session']['roli']==1){
                    $postdata = http_build_query(
                      array(
                        'veprimi' => 'admin_selectnpritje',
                       
                      )
                    );
                  }else if($_SESSION['_session']['roli']==2 || $_SESSION['_session']['roli']==3 ){
                    $postdata = http_build_query(
                      array(
                        'veprimi' => 'all_selectnpritje',
                        'pranuesi_id' =>   $_SESSION['_session']['institucioni'],
                      )
                    );
                  }else{
                    $postdata = http_build_query(
                      array(
                        'veprimi' => 'user_selectnpritje',
                        'pranuesi_id' =>   $_SESSION['_session']['id'],
                      )
                    );
                  }
                  
                  $opts = [
                    "http" => [
                      "method" => "GET",
                      "header" => "api_token:" . $token,

                    ]
                  ];
                  $context = stream_context_create($opts);

                  $delegostr = file_get_contents($urlapi . "/getmyusers/" . 1, false, $context);
                  $delego = json_decode($delegostr, true);

                  $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>Pranuar nga</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>" . $data[$i]["statusi"] . "</td>";
                    switch ($data[$i]["statusi"]) {
                      case 3:
                        $ngjyra = 'color:red;';
                        $per = "";
                        break;
                      case 0:
                        $ngjyra = 'color: hsl(47deg 52% 54%);';
                        $per = "";
                        break;
                      case 4:
                        $ngjyra = 'color:red;';
                        $per = "";
                        break;
                      case 2:
                        $ngjyra = 'color: green;';
                        $per = "<a type=\"button\" title=\"Printo\"  style=\"padding: 6px\" class=\"btn btn-info btn-xs\" href=\"Raport.php?vid=". $data[$i]["visitid"] . "\"><i class=\"material-icons\">print</i></a></td>";
                        break;
                      case 1:
                        $ngjyra = 'color: #ff9e0f;';
                        $per = "";
                        break;
                    }

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

                    $temp .= "<td><button title=\"Shiko\" type=\"button\" style=\"padding: 6px\" class=\"btn btn-success btn-xs\" onClick=\"clickid(" . $data[$i]["visitid"] . ")\" data-toggle=\"modal\" data-target=\"#modal-see\"><i class=\"material-icons\">remove_red_eye</i></button>";
                  // $temp .= '<td><button type="button" style="padding: 6px" class="btn btn-success btn-xs" onclick="clickid(37)" data-toggle="modal" data-target="#modal-see"><i class="material-icons">remove_red_eye</i></button><a type="button" style="padding: 6px" class="btn btn-info btn-xs" href="Raport.php?vid=37"><i class="material-icons">print</i></a></td>';
                  if ($data[$i]["statusi"] != 2 && $_SESSION['_session']['roli'] == 1) {
                    $temp .= "<button  title=\"Delego\"  type=\"button\" style=\"padding: 6px\" class=\"btn  btn-warning btn-xs\"  onclick=\"clickidd=" . $data[$i]["visitid"] . "\" data-toggle=\"modal\" data-target=\"#delegomodal\"><i class=\"material-icons\">send</i></button>";
                }
                    $temp .= $per;
              
                    $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">Rreth Kërkesës</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>
            </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" disabled></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" 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">Serviset</th>
                          </tr>
                        </thead>
                        <tbody id="serviset">

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

              </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);
                  $('#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();
                }
              });
              $.ajax({
                url: url + "/showkomentimin/" + id,
                headers: {
                  'api_token': token
                },
                type: 'GET',
                success: function(data, status) {
              if(data!="[300]"){
                  i = '';
                  ngjyra='';
                  for (var r in data) {
                    switch (data[r].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;
                    }
                    i += '<tr><td>'+data[r].data+'</td><td>'+data[r].emri+' '+data[r].mbiemri+'</td>' +
                      '<td style="'+ngjyra+' font-weight:500">'+data[r].emertimi+'</td><td>'+data[r].Komenti+'</td>' +
                      '</tr>';
                      
                  }

                  document.getElementById("kmnt").innerHTML = i;
                }
                }
              });

            }

            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 class="container-fluid">
              <div class="row">
                <div class="col-md-12">
                  <div class="card">
                    <table class="table">
                      <thead>
                        <tr>
                          <th>Data</th>
                          <th>Përdoruesi</th>
                          <th>Statusi i kërkesës</th>
                          <th>Komenti</th>

                        </tr>
                      </thead>
                      <tbody id="kmnt">
                        <tr>
                          <td>2021-10-11 10:34:41</td>
                          <td>Demo</td>
                          <!-- <td>1</td> -->
                          <td style="color: #ff9800; font-weight:500">Kërkesa në procesim</td>
                          <td>test </td>
                        </tr>
                      </tbody>
                    </table>
                  </div>
                </div>
              </div>
            </div>
          </div> <!-- footer end -->
        </div>
      </div>
    </div>

    <!-- END modal for editin or viewing BRANDS & dEVICES and Services -->
    <div class="modal fade" id="delegomodal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="exampleModalLabel">Jeni duke e deleguar kërkesën</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body">
                        Zgjedh perdoruesin:
                        <select id="select-delegues" class="form-control selectized select2" style="width:100%;" autocomplete="off" tabindex="-1">
                            <option value="" selected="selected">-Zgjedh Perdoruesin-</option>
                            <?php
                            for ($i = 0; $i < count($delego); $i++) {
                                $instopt .= '<option value="' . $delego[$i]['id'] . '">' . $delego[$i]["emri"] . ' ' . $delego[$i]["mbiemri"] . '</option>';
                            }
                            echo $instopt;
                            ?>
                        </select>
                        <script>
                            $('#select-delegues').select2({
                                dropdownParent: $('#delegomodal')
                            });
                        </script>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-sm btn-secondary" data-dismiss="modal">Mbyll</button>
                        <button type="button" id="delegouser" class="btn btn-sm btn-success ">Ruaj</button>
                        
                    </div>
                    <div class="mszh" style="text-align: center;"></div>
                </div>
               
            </div>
        </div>
        <script>
          clickidd=0;
            $('#delegouser').click(function() {
                if ($('#select-delegues').val() != '') {

                    $.ajax({
                        type: 'PUT',
                        url: url + "/delegouser",
                        headers: {
                            'api_token': token
                        },
                        data: {
                            deleguesi: $('#select-delegues').val(),
                            id: clickidd
                        },
                        success: function(data, status) {
                            toastr.success('Kërkesa u delegua me sukses');
                            $('#delegomodal').modal('hide');
                        },
                    });
                }else{
                    $('#delegomodal').find('.mszh').append('<span class="alert alert-danger">Zgjedhni Deleguesin !</span>').delay('1500').fadeOut();
                }
            });
        </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...",
            //"search":         "Kërko:",
            "paginate": {
              "first": "E para",
              "last": "E fundit",
              "next": "Tjetra",
              "previous": "Para"
            },
          },
          "pageLength": 10,
          // "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();
        });
      });
    </script>

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