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

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

      .clicked {
        background-color: beige;
      }

      label {
        all: unset;
      }
    </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 e Përfunduara</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_selectnperfundim',
                      '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:green; font-weight: 700">' . $data[$i]["emertimi"] . '</td>';
                    $temp .= "<td><button type=\"button\" onClick=\"clickid(" . $data[$i]["visitid"] . ")\"    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>";
                    $temp .= "<a type=\"button\"  style=\"padding: 6px\" class=\"btn btn-info btn-xs\" href=\"Raport.php?vid=" . $data[$i]["visitid"] . "\"><i class=\"material-icons\">print</i></a></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" style="max-width: 1200px">
        <div class="modal-content">
          <div class="modal-header">
            <h4 class="modal-title">Edito</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" s>
            <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> </textarea>
                </div>

                <!-- Kjo ktu ka me qen me while, ne menyre me u perserit per secilen pajisje nga nje dic i caktuar -->
                <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">
                          <tr id="DeviceName" class="clicked">
                            <td style="padding-top: 5px; padding-bottom: 5px" onclick="">SFRI H18 3 Diff- Hematology Analyzer</td>
                          </tr>

                        </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 class="col col-md-12">
                  <div class="row" id="devicescheckbox">
                    <div class="col-md-2"><label><input type="checkbox" id="Kontrate" name="" disabled> Kontratë</label></div>
                    <div class="col-md-2"><label><input type="checkbox" id="Falas" name="" disabled> Falas </label></div>
                    <div class="col-md-2"><label><input type="checkbox" id="Garancion" name="" disabled> Garancion</label></div>
                    <div class="col-md-4"><label><input type="checkbox" id="Reagjentesh" name="" disabled> Përdorim Reagjentësh</label></div>
                    <div class="col-md-2"><label><input type="checkbox" id="Qera" name="" disabled> Qera</label></div>
                  </div>
                </div>



                <div class="col col-md-12">
                  <h6 style="margin: 0">Përshkrimi i problemit</h6>
                  <hr style="margin: 0">
                  <div class="row" id="checkboxforproblem">
                    <div class="col-md-2"><label><input type="checkbox" class="perprob" id="Teknik" name="" disabled> Teknik</label></div>
                    <div class="col-md-2"><label><input type="checkbox" class="perprob" id="Kimik" name="" disabled> Kimik </label></div>
                    <div class="col-md-2"><label><input type="checkbox" class="perprob" id="operimi" name="" disabled> Programi/ operimi</label></div>
                    <div class="col-md-2"><label><input type="checkbox" class="perprob" id="Procedurë" name="" disabled> Procedurë</label></div>
                    <div class="col-md-2"><label><input type="checkbox" class="perprob" id="Tjetër" name="" disabled> Tjetër</label></div>
                    <div class="col-md-2"><label><input type="checkbox" class="perprob" id="problem" name="" disabled> Nuk u raportua problem</label></div>
                  </div>
                </div>



                <div class="col col-md-12">
                  <h6 style="margin: 0">Lloji i vizitës</h6>
                  <hr style="margin: 0">
                  <div class="row" id="llojivizites">
                    <div class="col-md-2"><label><input type="checkbox" class="vizita" id="Mirëmbajtje" name="" disabled> Mirëmbajtje</label></div>
                    <div class="col-md-2"><label><input type="checkbox" class="vizita" id="Urgjent" name="" disabled> Urgjent </label></div>
                    <div class="col-md-2"><label><input type="checkbox" class="vizita" id="Trajnim" name="" disabled> Trajnim</label></div>
                    <div class="col-md-2"><label><input type="checkbox" class="vizita" id="Instalim" name="" disabled> Instalim</label></div>
                    <div class="col-md-4"><label><input type="checkbox" class="vizita" id="Tjetërvz" name="" disabled> Tjetër <input type="text" class="form-control" style="border: inherit"></label> </div>
                  </div>
                </div>



                <div class="col col-md-12">
                  <h6 style="margin: 0">Detaje</h6>
                  <hr style="margin: 0">
                  <div class="row" id="detaje">
                    <div class="col-md-2">Data sot?<input type="date" id="datasot" class="form-control" name="" disabled></div>
                    <div class="col-md-2">Koha e ardhjes<input type="text" id="kohaardhjes" class="form-control" name="" disabled></div>
                    <div class="col-md-2">Koha e largimit<input type="text" id="kohalarg" class="form-control" name="" disabled></div>
                    <div class="col-md-2">Trajnim<input type="text" id="trajnimdata" class="form-control" name="" disabled> </div>
                    <div class="col-md-2">Instalim<input type="text" id="instalimdata" class="form-control" name="" disabled> </div>
                    <div class="col-md-2">Tjetër<input type="text" id="tjeterdata" class="form-control" name="" disabled></div>
                  </div>
                  <!-- <button onClick="$('#getDiv').toggle()" class="btn btn-clipboard" title="" data-bs-original-title="Shto rubrikë për detaje"><i class="material-icons">add</i></button> -->
                </div>

                <!-- <button onClick="$('#getDiv').toggle()" class="btn btn-xs"><i class="material-icons">add</i></button>  -->
                <!-- jquery me shfaq -->


                <!-- shto edhe nje div nese ka nevoje -->
                <div class="col col-md-12" id="getDiv" style="display: none;">
                  <h6 style="margin: 0">Detaje</h6>
                  <hr style="margin: 0">
                  <div class="row" id="detaje">
                    <div class="col-md-2">Data sot?<input type="date" class="form-control" name="" disabled></div>
                    <div class="col-md-2">Koha e ardhjes<input type="text" class="form-control" name="" disabled></div>
                    <div class="col-md-2">Koha e largimit<input type="text" class="form-control" name="" disabled></div>
                    <div class="col-md-2">Trajnim<input type="text" class="form-control" name="" disabled> </div>
                    <div class="col-md-2">Instalim<input type="text" class="form-control" name="" disabled> </div>
                    <div class="col-md-2">Tjetër<input type="text" class="form-control" name="" disabled></div>
                  </div>
                </div>



                <div class="col col-md-12">
                  <div class="row" id="detaje2">
                    <div class="col-md-2">Koha në kompani<input type="text" id="Kohakompani" class="form-control" name="" disabled></div>
                    <div class="col-md-2">Koha në laborator<input type="text" id="Kohalab" class="form-control" name="" disabled></div>
                    <div class="col-md-2">Koha e udhëtimit<input type="text" id="Kohaudhe" class="form-control" name="" disabled></div>
                    <div class="col-md-2">Faturime<input type="text" id="Faturime" class="form-control" name="" disabled> </div>
                    <div class="col-md-2">TT<input type="text" id="TT" class="form-control" name="" disabled> </div>
                    <div class="col-md-2">Numri i faturës<input type="text" id="Numrifatures" class="form-control" name="" disabled></div>
                  </div>
                </div>



                <div class="col col-md-12">
                  <h6 style="margin: 0">Përshkrimi i punimeve</h6>
                  <hr style="margin: 0">
                  <div class="row">
                    <div class="col-md-7">
                      <textarea id="pershkrimipunes" class="form-control" disabled> </textarea>
                    </div>
                    <div class="col-md-3">
                      <textarea id="Testetekryera" class="form-control" disabled>  </textarea>
                    </div>
                    <div class="col-md-2">
                      <textarea id="kohapershkrim" class="form-control" disabled>  </textarea>
                    </div>
                    <div class="col col-md-12" style="padding-top: 5px">
                      <input id="Aparaticheck" type="checkbox" disabled> Aparati u dorëzua në gjendje të mirë pune
                    </div>
                  </div>
                </div>

                <div class="col col-md-12">
                  <div class="row">
                    <div class="col-md-6">
                      <textarea id="Shenimevizit" class="form-control resizable" disabled></textarea>
                    </div>
                    <div class="col-md-6">
                      <textarea id="materialekonsumi" class="form-control resizable" name="text" disabled> </textarea>
                    </div>
                  </div>
                </div>

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

            function clickid(id) {
              clickidd = 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) {
              paisjeid = sid;
              $('.clicked').removeClass("clicked");
              $('#paisjaa' + r).addClass("clicked");
              $.ajax({
                url: url + "/getservisit/" + id + "/" + sid,
                headers: {
                  'api_token': token
                },
                type: 'GET',
                success: function(data, status) {
                  if (data[0].Kontrat === 1) {
                    $('#Kontrate').prop("checked", true);
                  } else {
                    $('#Kontrate').prop("checked", false);
                  }
                  if (data[0].Falas === 1) {
                    $('#Falas').prop("checked", true);
                  } else {
                    $('#Falas').prop("checked", false);
                  }
                  if (data[0].Garancion === 1) {
                    $('#Garancion').prop("checked", true);
                  } else {
                    $('#Garancion').prop("checked", false);
                  }
                  if (data[0].Reagjentesh === 1) {
                    $('#Reagjentesh').prop("checked", true);
                  } else {
                    $('#Reagjentesh').prop("checked", false);
                  }
                  if (data[0].Qera === 1) {
                    $('#Qera').prop("checked", true);
                  } else {
                    $('#Qera').prop("checked", false);
                  }
                  if (data[0].pershkrimiproblemit.includes('Teknik')) {
                    $('#Teknik').prop("checked", true);
                  } else {
                    $('#Teknik').prop("checked", false);
                  }
                  if (data[0].pershkrimiproblemit.includes('Kimik')) {
                    $('#Kimik').prop("checked", true);
                  } else {
                    $('#Kimik').prop("checked", false);
                  }
                  if (data[0].pershkrimiproblemit.includes('operimi')) {
                    $('#operimi').prop("checked", true);
                  } else {
                    $('#operimi').prop("checked", false);
                  }
                  if (data[0].pershkrimiproblemit.includes('Procedurë')) {
                    $('#Procedurë').prop("checked", true);
                  } else {
                    $('#Procedurë').prop("checked", false);
                  }
                  if (data[0].pershkrimiproblemit.includes('Tjetër')) {
                    $('#Tjetër').prop("checked", true);
                  } else {
                    $('#Tjetër').prop("checked", false);
                  }
                  if (data[0].pershkrimiproblemit.includes('problem')) {
                    $('#problem').prop("checked", true);
                  } else {
                    $('#problem').prop("checked", false);
                  }
                  if (data[0].llojivizites.includes('Mirëmbajtje')) {
                    $('#Mirëmbajtje').prop("checked", true);
                  } else {
                    $('#Mirëmbajtje').prop("checked", false);
                  }
                  if (data[0].llojivizites.includes('Urgjent')) {
                    $('#Urgjent').prop("checked", true);
                  } else {
                    $('#Urgjent').prop("checked", false);
                  }
                  if (data[0].llojivizites.includes('Trajnim')) {
                    $('#Trajnim').prop("checked", true);
                  } else {
                    $('#Trajnim').prop("checked", false);
                  }
                  if (data[0].llojivizites.includes('Instalim')) {
                    $('#Instalim').prop("checked", true);
                  } else {
                    $('#Instalim').prop("checked", false);
                  }
                  if (data[0].llojivizites.includes('Tjetërvz')) {
                    $('#Tjetërvz').prop("checked", true);
                  } else {
                    $('#Tjetërvz').prop("checked", false);
                  }

                  if (data[0].datasot != null) {
                    $('#datasot').val(data[0].datasot);
                  }
                  if (data[0].kohaardhjes != null) {
                    $('#kohaardhjes').val(data[0].kohaardhjes);
                  }
                  if (data[0].kohalargimit != null) {
                    $('#kohalarg').val(data[0].kohalargimit);
                  }
                  if (data[0].Trajnim != null) {
                    $('#trajnimdata').val(data[0].Trajnim);
                  }
                  if (data[0].Instalim != null) {
                    $('#instalimdata').val(data[0].Instalim);
                  }
                  if (data[0].Tjetërdetaje != null) {
                    $('#tjeterdata').val(data[0].Tjetërdetaje);
                  }
                  if (data[0].Kohakompani != null) {
                    $('#Kohakompani').val(data[0].Kohakompani);
                  }
                  if (data[0].Kohalab != null) {
                    $('#Kohalab').val(data[0].Kohalab);
                  }
                  if (data[0].Kohaudhe != null) {
                    $('#Kohaudhe').val(data[0].Kohaudhe);
                  }
                  if (data[0].Faturime != null) {
                    $('#Faturime').val(data[0].Faturime);
                  }
                  if (data[0].TT != null) {
                    $('#TT').val(data[0].TT);
                  }
                  if (data[0].Numrifatures != null) {
                    $('#Numrifatures').val(data[0].Numrifatures);
                  }
                  if (data[0].pershkrimipunes != null) {
                    $('#pershkrimipunes').val(data[0].pershkrimipunes);
                  }
                  if (data[0].Testetekryera != null) {
                    $('#Testetekryera').val(data[0].Testetekryera);
                  }
                  if (data[0].kohapershkrim != null) {
                    $('#kohapershkrim').val(data[0].kohapershkrim);
                  }
                  if (data[0].Shenimevizit != null) {
                    $('#Shenimevizit').val(data[0].Shenimevizit);
                  }
                  if (data[0].materialekonsumi != null) {
                    $('#materialekonsumi').val(data[0].materialekonsumi);
                  }

                  if (data[0].Aparaticheck === 1) {
                    $('#Aparaticheck').prop("checked", true);
                  } else {
                    $('#Aparaticheck').prop("checked", false);
                  }
                  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">
            <!-- <button type="button" class="btn btn-sm btn-success mbyllplotsisht">Mbyll plotësisht</button>
                        <button type="button" id="ruajperanaliz" class="btn btn-sm btn-warning">Ruaj</button> -->
          </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>
      $('.resizable').each(function() {
        this.setAttribute('style', 'height: 70px !important;overflow-y:hidden;');
      }).on('input', function() {
        this.style.height = 'auto';
        $('.resizable').attr('style', 'height: ' + (this.scrollHeight) + 'px !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");
}
?>