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

  if ($_SESSION['_session']['submit_service_request'] == 1) {
    require 'header.php';
?>
    <style>
      .btn-clipboard {
        position: absolute;
        top: 0.65rem;
        right: 0.65rem;
        padding: 0.25rem 0.5rem;
        color: #0d6efd;
        background-color: #fff;
        border: 1px solid;
        border-radius: 0.25rem;
      }

      .inp>*:not(.exp) {
        margin-left: 30px;
      }
    </style>
    <div class="container" style="max-width: 1500px">
      <div class="card o-hidden border-0 shadow-lg my-5">
        <div class="card-body p-0">
          <!-- Nested Row within Card Body -->
          <div class="row">
            <div class="col-lg-12">
              <div class="p-5">
                <div class="card-header card-header-success" style="padding-left: 12px; padding-top: 0; padding-bottom: 0;">
                  <h4 class="card-title">APLIKACION SERVISI </h4>
                  <p class="card-category">Plotësoni fushat e më poshtme për të bërë kërkesë për servisim të paisjes.</p>
                </div>
                <div class="card-body" id="divvv">
                  <div class="row">
                    <div class="col">
                      <div class="row" style="    justify-content: center;">
                        <div class="col-sm-12 col-md-12" id="getBrandP">
                          <script>
                            $.ajax({
                              url: url + "/perkatsipaisja/" + <?php echo $_SESSION['_session']['institucioni']; ?>,
                              headers: {
                                'api_token': token
                              },
                              type: 'GET',
                              success: function(data, status) {
                                y = "<select  id=\"getbrandPr0\" class=\"form-control filtercl  select2\" autocomplete=\"off\" tabindex=\"-1\">";
                                y += "<option value=\"\">-Zgjedh Pajisjen-</option>";
                                if (data == "[300]") {}
                                else {
                                  for (i = 0; i < data.length; i++) {
                                    y += '<option value="' + data[i].pajisja_id + '" name="' + data[i].firma_prodhuese + '">' + data[i].emri_pajisjes + '</option> ';
                                  }
                                }
                                document.getElementById("getBrandP").innerHTML = y;

                                $("#getbrandPr0").change(function() {
                                  $("#getbrandPr0 option").removeClass("drr");
                                  $("#getbrandPr0 option:selected").addClass("drr");
                                  hideselect();
                                  $.ajax({
                                    url: url + "/servpaisje/" + $("#getbrandPr0").val(),
                                    headers: {
                                      'api_token': token
                                    },
                                    type: 'GET',
                                    success: function(data, status) {
                                      $("#get_services").empty();
                                      $("#get_services").append('<label style="font-weight:700;" class="inp"><input id="selall0" value="" type="checkbox">TE GJITHA</label>');
                                      for (i = 0; i < data.length; i++) {

                                        $("#get_services").append('<label class="inp"><input name="serv" class="selall0" type="checkbox" value="' + data[i].service_name + '" id="' + data[i].services_id + '">' + data[i].service_name + '</label>');
                                      }
                                      $("#get_services").show();

                                      $('#selall0').click(function(event) {
                                        if (this.checked) {
                                          // Iterate each checkbox
                                          $('.selall0').each(function() {
                                            this.checked = true;
                                          });
                                        } else {
                                          $('.selall0').each(function() {
                                            this.checked = false;
                                          });
                                        }
                                      });
                                    }
                                  });

                                });
                              }
                            });
                          </script>
                        </div> <!-- end end end end   -->

                        <!-- ON SELECT OF BRAND OPEN THIS DIV, ne perzgjedhjen e brandit ose firmes hapet nje div me checkboxa me pajisje te firmes perzgjedhese, dhe nje div checkbox me gjith serviset -->

                        <div class="col-mb-12">
                          <div class="row">

                            <div class="col-mb-6">
                              <div>
                                <div class="col" id="get_services" style="display:none;">

                                </div>
                              </div> <!-- end row  -->
                            </div> <!-- end col-mb-6  -->
                          </div>
                        </div>

                      </div><!-- end row  -->
                    </div> <!-- end col  -->

                    <button onClick="shto()" id="btnshto" class="btn btn-clipboard" title="" data-bs-original-title="Shto rubrikë për detaje" style="margin-right: 36px;"><i class="material-icons">add</i></button>
                    <button onClick="heq()" id="btnhiq" class="btn btn-clipboard" title="" data-bs-original-title="Shto rubrikë për detaje"><i class="material-icons">remove</i></button>

                  </div><!-- end row  -->
                  </form> <!-- end form  -->
                </div>

                <div class="row" style="margin:0px;">
                  <div class="col-md-12">
                    <p> Prioriteti i kërkesës</p>
                    <div id="prioriteti" class="row">
                      <script>
                        $.ajax({
                          url: url + "/prioritete",
                          headers: {
                            'api_token': token
                          },
                          data: {
                            veprimi: 'select'
                          },
                          success: function(data, status) {
                            $("#prioriteti").empty();
                            for (i = 0; i < data.length; i++) {

                              $("#prioriteti").append('<div class="col-md-3 col-12" style="word-break: break-word;"><input class="selpr" value="' + data[i].emri + '" type="checkbox" name="prio" id="pr' + data[i].id + '" '+
                              'onchange="$(\'[id^=label]\').css(\'color\',\'\');$(\'#label' + data[i].id + '\').css(\'color\',\'red\');" > <label for="pr' + data[i].id + '" class="inp" style="font-size: 14px;line-height: 0;color: #6c6c6c;font-weight: 700;font-weight: 500;">' + data[i].emri + '</label>'+
                              '<br><label id="label' + data[i].id + '" >'+data[i].pershkrimi+' </label></div>');
                            }
                            // $("#prioriteti").show();
                            $('.selpr').click(function(event) {
                              if (this.checked) {
                                // Iterate each checkbox
                                $('.selpr').each(function() {
                                  this.checked = false;
                                });
                                this.checked = true;
                              }
                            });
                          }
                        });
                      </script>

                    </div>
                  </div>
                </div>

                <div class="row">
                  <div class="col-md-12">
                    <div class="form-group">
                      <label style="color: black;">Më shumë detaje</label>
                      <div class="form-group bmd-form-group">
                        <label class="form-floating" style="background-color: white;"> Ne rast te ndonje problemi tjeter, pershkruani ne vazhdim: </label>
                        <textarea id="pershkrimii" class="form-control" rows="5" name="koment" style="border: 1px solid green; border-radius: 6px;"></textarea>
                      </div>
                    </div>
                  </div>
                </div>
                <button name="button" type="button" id="dergobtn" class="btn btn-success pull-right">Dërgo</button>
                <div class="clearfix">Vërejtje: Kërkesat për servisim do të merren parasysh vetëm me aplikacion të plotësuar!</div>
                <div class="shfaqmszh" style="display: none;"></div>

              </div>
            </div>
          </div>
        </div>
      </div>
    </div>


    <script>
      function heq() {
        if (u > 1) {
          $('#slectnr' + (u - 1)).remove();
          u -= 1;
          hideselect();
          $('#btnshto').prop('disabled', false);
        }
      }
      u = 1;

      function myFunction(u) {
        $("#getbrandPr" + u + "option").removeClass("drr");
        $("#getbrandPr" + u + " option:selected").addClass("drr");
        hideselect();
        // showselected();
        $.ajax({
          url: url + "/servpaisje/" + $("#getbrandPr" + u).val(),
          headers: {
            'api_token': token
          },
          type: 'GET',
          success: function(data, status) {
            $("#get_services" + u).empty();
            $("#get_services" + u).append('<label style="font-weight:700;" class="inp"><input id="selall' + u + '" type="checkbox">TE GJITHA</label>');
            for (i = 0; i < data.length; i++) {

              $("#get_services" + u).append('<label class="inp"><input name="serv" class="selall' + u + '"type="checkbox"  id="' + data[i].services_id + '">' + data[i].service_name + '</label>');
            }
            $("#get_services" + u).show();

            $('#selall' + u).click(function(event) {
              if (this.checked) {
                // Iterate each checkbox
                $('.selall' + u).each(function() {
                  this.checked = true;
                });
              } else {
                $('.selall' + u).each(function() {
                  this.checked = false;
                });
              }
            });
          }
        });
      }

      function hideselect() {
        for (let i = 0; i < u; i++) {
          // $('#getbrandPr'+i+' option').show();
          val = $('#getbrandPr' + i + ' option:selected').val();
          jQuery(".filtercl option[value='" + val + "']").hide();
        }
        $('.drr').show();
      }

      function shto() {

        $.ajax({
          url: url + "/perkatsipaisja/" + <?php echo $_SESSION['_session']['institucioni']; ?>,
          headers: {
            'api_token': token
          },
          type: 'GET',
          success: function(data, status) {
            x = ' <div id="slectnr' + u + '" class="row"> <div class="col">' +
              '<div class="row" style="    justify-content: center;">' +
              '<div class="col-sm-12 col-md-12" id="sgetBrandP' + u + '">';


            x += '<select  onchange="myFunction(' + u + ')"   id="getbrandPr' + u + '"   class="form-control filtercl selectized select2" autocomplete="off" tabindex="-1">';
            x += '<option value="" >-Zgjedh Pajisjen-</option>';
            if ((u + 1) == data.length) {
              $('#btnshto').prop('disabled', true);
            }
            for (i = 0; i < data.length; i++) {

              x += '<option value="' + data[i].pajisja_id + '" name="' + data[i].firma_prodhuese + '">' + data[i].emri_pajisjes + '</option> ';
            }



            y = '</div>' +
              '<div class="col-mb-12">' +
              ' <div class="row">' +
              '<div class="col-mb-6">'

              +
              ' </div>' +
              '<div class="col-mb-6">' +
              ' <div >' +
              '  <div class="col " id="get_services' + u + '" style="display:none;">'

              +
              ' </div>' +
              ' </div>' +
              ' </div> ' +
              '</div>' +
              ' </div>'

              +
              ' </div>' +
              '</div></div>';
            $('#divvv').append(x);
            $('#sgetBrandP' + u).after(y);
            hideselect();

            u += 1
          },
        });


      }
      visitid = null;
      $.ajax({
        type: 'GET',
        url: url + "/getkerkesaid",
        headers: {
          'api_token': '<?php echo $token ?>'
        },
        success: function(data, textStatus, jqXHR) {

          visitid = data[0].id + 1;


        },
      });
      $('#dergobtn').click(function() {


        if ($('input[type=checkbox][name=prio]').is(':checked') && $('input[type=checkbox][name=serv]').is(':checked')) {

          for (let i = 0; i < u; i++) {
            eval('var arrayselected' + i + '= [];');

            $(".selall" + i + ":checked").each(function() {
              eval('arrayselected' + i).push($(this).attr('id'));
            });

          }

          for (let i = 0; i < u; i++) {



            $.each(eval('arrayselected' + i), function(indexser, valueser) {
              $.ajax({
                type: 'POST',
                url: url + "/shtokerkes",
                data: {
                  brand: $('#getbrandPr' + i + ' option:selected').attr('name'),
                  paisje: $('#getbrandPr' + i + ' option:selected').val(),
                  service: valueser,
                  prioriteti: $('input[type=checkbox][name=prio]:checked').attr('id'),
                  pershkrimi: $("#pershkrimii").val(),
                  visitid: visitid,
                  emriperdorues: '<?php echo $_SESSION['_session']['id']; ?>',
                  emriinstitutit: <?php echo $_SESSION['_session']['institucioni']; ?>,


                },
                headers: {
                  'api_token': '<?php echo $token ?>'
                },
                // success: function(data, textStatus, jqXHR) {    toastr.success('Kërkesa u dërgua me sukses');},
                success: function(data, status) {},

                error: function(request, textStatus, errorThrown) {
                  $(".shfaqmszh").show().addClass('alert-danger').html(request.responseJSON.message);

                }
              });
            });

          }
          $.ajax({
            type: "POST",
            url: url + "/shtokomentimin",
            data: {
              pershkrimi: $("#pershkrimii").val(),
              visitid: visitid,
              emriperdorues: '<?php echo $_SESSION['_session']['id']; ?>',
            },
            headers: {
              'api_token': '<?php echo $token ?>'
            },
            success: function(response) {
              toastr.success('Kërkesa u dërgua me sukses');
              setTimeout(function() {
                window.location.replace("./Kerkesat.php");
              }, 1000);
            }
          });
        } else {
          $(".shfaqmszh").show().addClass('alert-danger').html("Ju lutem plotsoni te gjitha fushat");
        }
      });
    </script>
<?php require 'footer.php';
  } else {
    header("location:./dashboard.php");
  }
} else {
  header("location:../logout.php");
}
?>