Your IP : 3.142.12.198


Current Path : /data/web/virtuals/51568/virtual/www/www/ucebnice/
Upload File :
Current File : /data/web/virtuals/51568/virtual/www/www/ucebnice/ucebnice.js

class Ucebnice {
   setUcebnice (id, idnaklad, titul, idpredmet, rokvydani, rokstudia, jeaktivni){
     this.id = id; 
     this.idnaklad = idnaklad; 
     this.titul = titul; 
     this.idpredmet = idpredmet; 
     this.rokvydani = rokvydani; 
     this.rokstudia = rokstudia; 
     this.jeaktivni = jeaktivni;
   }
}
  
let ucUcebnice = new Ucebnice();
  

/*  MODAL */

class Predmet {
  constructor(id, predmet, zkratka, stupen) {
    this.id = id;
    this.predmet = predmet;
    this.zkratka = zkratka;
    this.stupen = stupen;
  }
  setPredmet (id, predmet, zkratka, stupen){
    this.id = id;
    this.predmet = predmet;
    this.zkratka = zkratka;
    this.stupen = stupen;
  };
}
  
let ucebnicePredmet = new Predmet();


class Naklad {
   setNaklad (id, naklad){
     this.id = id; 
     this.naklad = naklad; 
   }
}

let ucebniceNaklad = new Naklad();
  
function nastavViditelnost(element) {
    setTimeout(function () {
        $(element).addClass("d-none");
    }, 4000)
}

function schovejTlacitka(schovej){
  if (schovej){
     $("#btnNovyPredmet").css("visibility", "hidden");  
  } else {
     $("#btnNovyPredmet").css("visibility", "visible");  
  }
    
}
function editujPredmet(id){
  stornoSmaz();
  stornoEditace();
  stornoNovy();
  ucebnicePredmet.setPredmet(id, $("#prPredmet"+id).text(), $("#prZkratka"+id).text(), $("#prStupen"+id).text());
  schovejTlacitka(true);
  $("#prPredmet"+id).html(`<input id="newPrPredmet" value="${ucebnicePredmet.predmet}" placeholder="název předmětu" type="text" class="form-control rounded-0" required>`);
  $("#prStupen"+id).html(`<input id="newPrStupen" value="${ucebnicePredmet.stupen}" placeholder="stupeň" type="text" class="form-control rounded-0" required>`);
  $("#prZkratka"+id).html(`<input id="newPrZkratka" value="${ucebnicePredmet.zkratka}" placeholder="zkratka" type="text" class="form-control rounded-0" required>`);  
  $("#prDe"+id).html(`<span class="text-warning"><i class="fas fa-undo"></i></span>`); 
  $("#prEd"+id).html(`<span  class="text-success"><i class="far fa-save"></i></span>`); 
  document.getElementById("prDe"+id).onclick = new Function(`stornoEditace()`);
  document.getElementById("prEd"+id).onclick = new Function(`ulozEditace()`);
}

function OKString(str){
  if (str && str.trim().length) {  
    return true
  } else {return false}
}
function kontrolaEditace(id){
  let predmet =  $("#newPrPredmet").val(),
      zkratka =  $("#newPrZkratka").val(),
      stupen =  $("#newPrStupen").val();
  if (OKString(predmet) && OKString(zkratka)  && OKString(stupen)) {
    ucebnicePredmet.setPredmet(id, predmet, zkratka, stupen);
    return true;
  } else {
    let errMsg = `<tr id="trErrMsg"><td colspan="5"><div class="alert alert-danger">Vyplň všechny údaje.</div></td></tr>`;
    $("#prTr"+id).after(errMsg);
    return false;  
  } 
}
function ulozEditace(){
  $("#trErrMsg").remove();
  if (kontrolaEditace(ucebnicePredmet.id)) {
    $.ajax({
        url: "www/ucebnice/predmety-uloz.php",
        type: 'POST',
        dataType: 'HTML',
        data: {
          'predmet': JSON.stringify(ucebnicePredmet),
        },
        success: function (result) {
           stornoEditace();
           }
     });   
  }
}
function stornoEditace(){
  $("#trErrMsg").remove();
  schovejTlacitka(false);
  if (ucebnicePredmet.id !== undefined) {
    let id = ucebnicePredmet.id;
    $("#prPredmet"+id).html(`${ucebnicePredmet.predmet}`);
    $("#prStupen"+id).html(`${ucebnicePredmet.stupen}`);
    $("#prZkratka"+id).html(`${ucebnicePredmet.zkratka}`); 
    $("#prDe"+id).html(`<span class="ikonaForm text-danger"><i class="far fa-trash-alt"></i></span>`); 
    $("#prEd"+id).html(`<span  class="text-info"><i class="far fa-edit"></i></span>`);
    document.getElementById("prDe"+id).onclick = new Function(`smazPredmet(${id})`);
    document.getElementById("prEd"+id).onclick = new Function(`editujPredmet(${id})`);        
    ucebnicePredmet.setPredmet(undefined, undefined, undefined, undefined);
    delete (ucebnicePredmet);   
  } 
}

function smazPredmet(id){
  $("#trErrMsg").remove();
  stornoEditace();
  stornoNovy();
  schovejTlacitka(true);
  $("#smazPredmetDotaz").remove();
  let  predmet = $("#prPredmet" + id).text(),
       dotaz=`
<tr id="smazPredmetDotaz">
  <td colspan="5"  class="alert alert-danger mb-0 py-1 rounded-0" >
    <div class="d-flex">
      <div class="p-2"> Chceš smazat "${predmet}" ?</div>  
      <button class="ml-auto btn btn-info rounded-0" onclick="stornoSmaz()">Storno</button>
      <button class="btn btn-danger ml-2 rounded-0" onclick="smazatSmaz(${id})">Smazat</button>  
    </div>
  </td>
</tr>  
  `;
  $("#prTr"+id).after(dotaz);
}
function stornoSmaz(){
   schovejTlacitka(false);
   $("#smazPredmetDotaz").remove();
}
function smazatSmaz(id){
  $.ajax({
      url: "www/ucebnice/predmety-smaz.php",
      type: 'POST',
      dataType: 'HTML',
      data: {
        'id': id,
      },
      success: function (result) {
         $('#smazPredmetDotaz').remove();
         $("#prTr"+id).remove();
         schovejTlacitka(false);
         if ($("#detailUcebniceForma").attr("id-ucebnice") == id) {
           $("#ucebniceDetail").html("");
         }       
         }
   }); 
}
function novyPredmet(){
  $("#trErrMsg").remove();
  stornoSmaz();
  stornoEditace();
  $("#btnNovyPredmet").addClass("d-none");
  $("#modalPredmetyTop").prepend(`<div id="headNovyPredmet" class="h4">Nový předmět</div>`);
  
  let newTr = 
  `<tr id="prTr0" >
      <td id="prPredmet0" ><input id="newPrPredmet" value="" placeholder="název předmětu" type="text" class="form-control rounded-0" required></td>
      <td id="prStupen0"><input id="newPrStupen" value="" placeholder="stupeň" type="text" class="form-control rounded-0" required></td>
      <td id="prZkratka0"><input id="newPrZkratka" value="" placeholder="zkratka" type="text" class="form-control rounded-0" required></td>
      <td id="prDe0" class="ikonaForm" onclick="stornoNovy()"><span class="text-warning "><i class="fas fa-undo pt-2"></i></span></td> 
      <td id="prEd0" class="ikonaForm" onclick="zalozNovy()"><span  class="text-success"><i class="far fa-save pt-2"></i></span></td>                                                          
  </tr>`;
  $("#predmety_head").after(newTr);
}

function stornoNovy(){
   $("#headNovyPredmet").remove();
   $("#btnNovyPredmet").removeClass("d-none"); 
   $("#prTr0").remove();
}

function zalozNovy(){
  $("#trErrMsg").remove();
  if (kontrolaEditace(0)) {
    $.ajax({
        url: "www/ucebnice/predmety-uloz.php",
        type: 'POST',
        dataType: 'HTML',
        data: {
          'predmet': JSON.stringify(ucebnicePredmet),
        },
        success: function (result) {
            stornoEditace();
            $.ajax({
                url: "www/ucebnice/predmety-modal-napln.php",
                type: 'POST',
                dataType: 'HTML',
                data: {
                },
                success: function (result) {
                   $("#modalBodyTable").html(result);
                   $("#headNovyPredmet").remove();
                   $("#btnNovyPredmet").removeClass("d-none")
                }
             });
           
           }
     });   
  }
}
 /**************************************/
 

function schovejTlacitkaNaklad(schovej){
  if (schovej){
     $("#btnNoveNaklad").css("visibility", "hidden");  
  } else {
     $("#btnNoveNaklad").css("visibility", "visible");  
  }
    
}
function editujNaklad(id){
  stornoEditaceNaklad();
  stornoSmazNaklad();
  stornoNoveNaklad();
  ucebniceNaklad.setNaklad(id, $("#naNaklad"+id).text());
  schovejTlacitkaNaklad(true);
  $("#naNaklad"+id).html(`<input id="newNaNaklad" value="${ucebniceNaklad.naklad}" placeholder="jméno nakladatelství" type="text" class="form-control rounded-0" required>`);
  $("#naDe"+id).html(`<span class="text-warning"><i class="fas fa-undo"></i></span>`); 
  $("#naEd"+id).html(`<span  class="text-success"><i class="far fa-save"></i></span>`); 
  document.getElementById("naDe"+id).onclick = new Function(`stornoEditaceNaklad()`);
  document.getElementById("naEd"+id).onclick = new Function(`ulozEditaceNaklad()`);
} 

function stornoEditaceNaklad(){
  $("#trErrMsgNaklad").remove();
  schovejTlacitkaNaklad(false);
  if (ucebniceNaklad.id !== undefined) {
    let id = ucebniceNaklad.id;
    $("#naNaklad"+id).html(`${ucebniceNaklad.naklad}`);
    $("#naDe"+id).html(`<span class="ikonaForm text-danger"><i class="far fa-trash-alt"></i></span>`); 
    $("#naEd"+id).html(`<span  class="text-info"><i class="far fa-edit"></i></span>`);
    document.getElementById("naDe"+id).onclick = new Function(`smazNaklad(${id})`);
    document.getElementById("naEd"+id).onclick = new Function(`editujNaklad(${id})`);        
    ucebniceNaklad.setNaklad(undefined, undefined);
  } 
}

function kontrolaEditaceNaklad(id){
  let naklad =  $("#newNaNaklad").val();
  if (OKString(naklad)) {
    ucebniceNaklad.setNaklad(id, naklad);
    return true;
  } else {
    let errMsg = `<tr id="trErrMsgNaklad"><td colspan="5"><div class="alert alert-danger">Nakladatelství musí mít jméno.</div></td></tr>`;
    $("#naTr"+id).after(errMsg);
    return false;  
  } 
}

function ulozEditaceNaklad(){
  $("#trErrMsg").remove();
  if (kontrolaEditaceNaklad(ucebniceNaklad.id)) {
    $.ajax({
        url: "www/ucebnice/nakladatelstvi-uloz.php",
        type: 'POST',
        dataType: 'HTML',
        data: {
          'naklad': JSON.stringify(ucebniceNaklad),
        },
        success: function (result) {
           stornoEditaceNaklad();
           }
     });   
  }
}

function smazNaklad(id){
  $("#trErrMsgNaklad").remove();
  stornoEditaceNaklad();
  stornoNoveNaklad();
  schovejTlacitkaNaklad(true);
  $("#smazNakladDotaz").remove();
  let  naklad = $("#naNaklad" + id).text(),
       dotaz=`
<tr id="smazNakladDotaz">
  <td colspan="5"  class="alert alert-danger mb-0 py-1 rounded-0" >
    <div class="d-flex">
      <div class="p-2"> Chceš smazat "${naklad}" ?</div>  
      <button class="ml-auto btn btn-info rounded-0" onclick="stornoSmazNaklad()">Storno</button>
      <button class="btn btn-danger ml-2 rounded-0" onclick="smazatSmazNaklad(${id})">Smazat</button>  
    </div>
  </td>
</tr>  
  `;
  $("#naTr"+id).after(dotaz);
}
function stornoSmazNaklad(){
   schovejTlacitkaNaklad(false);
   $("#smazNakladDotaz").remove();
}
function smazatSmazNaklad(id){
  $.ajax({
      url: "www/ucebnice/nakladatelstvi-smaz.php",
      type: 'POST',
      dataType: 'HTML',
      data: {
        'id': id,
      },
      success: function (result) {
         $('#smazNakladDotaz').remove();
         $("#naTr"+id).remove();
         schovejTlacitkaNaklad(false);       
         }
   }); 
}

function noveNaklad(){
  $("#trErrMsgNaklad").remove();
  stornoSmazNaklad();
  stornoEditaceNaklad();
  $("#btnNoveNaklad").addClass("d-none");
  $("#modalNakladTop").prepend(`<div id="headNoveNaklad" class="h4">Nové nakladatelství</div>`);
  
  let newTr = 
  `<tr id="naTr0" >
      <td id="naNaklad0" ><input id="newNaNaklad" value="" placeholder="název nakladatelství" type="text" class="form-control rounded-0" required></td>
      <td id="naDe0" class="ikonaForm" onclick="stornoNoveNaklad()"><span class="text-warning "><i class="fas fa-undo pt-2"></i></span></td> 
      <td id="naEd0" class="ikonaForm" onclick="zalozNoveNaklad()"><span  class="text-success"><i class="far fa-save pt-2"></i></span></td>                                                          
  </tr>`;
  $("#naklad_head").after(newTr);
}

function stornoNoveNaklad(){
   $("#headNoveNaklad").remove();
   $("#btnNoveNaklad").removeClass("d-none"); 
   $("#trErrMsgNaklad").remove();
   $("#naTr0").remove();
}

function zalozNoveNaklad(){
  $("#trErrMsgNaklad").remove();
  if (kontrolaEditaceNaklad(0)) {
    $.ajax({
        url: "www/ucebnice/nakladatelstvi-uloz.php",
        type: 'POST',
        dataType: 'HTML',
        data: {
          'naklad': JSON.stringify(ucebniceNaklad),
        },
        success: function (result) {
            stornoEditaceNaklad();
            $.ajax({
                url: "www/nakladatelstvi-modal-napln.php",
                type: 'POST',
                dataType: 'HTML',
                data: {
                },
                success: function (result) {
                   $("#modalBodyTable").html(result);
                   $("#headNoveNaklad").remove();
                   $("#btnNoveNaklad").removeClass("d-none")
                }
             });
           
           }
     });   
  }
}

/* konec MODAL */    

/* volani modalniho okna */

function predmetyEdit() {
    let nc = document.getElementById('predmetyModal');
    if ((nc === undefined) || (nc === null)) {
        $.ajax({
            url: "www/ucebnice/predmety-modal.php",
            type: 'POST',
            dataType: 'HTML',
            success: function (result) {
                $('#proPredmetyModal').html(result);                    
                $.ajax({
                    url: "www/ucebnice/predmety-modal-napln.php",
                    type: 'POST',
                    dataType: 'HTML',
                    data: {
                    },
                    success: function (result) {
                       $("#modalBodyTable").html(result);
                       $("#predmetyModal").modal("show");
                       }
                 });     
            }
        });
    } else {
        $.ajax({
            url: "www/ucebnice/predmety-modal-napln.php",
            type: 'POST',
            dataType: 'HTML',
            data: {
            },
            success: function (result) {
               $("#modalBodyTable").html(result);
               $("#predmetyModal").modal("show");
               }
         });
    }
}


function nakladatEdit(){
    let nc = document.getElementById('nakladatModal');
    if ((nc === undefined) || (nc === null)) {
        $.ajax({
            url: "www/ucebnice/nakladatelstvi-modal.php",
            type: 'POST',
            dataType: 'HTML',
            success: function (result) {
                $('#proPredmetyModal').html(result);                    
                $.ajax({
                    url: "www/ucebnice/nakladatelstvi-modal-napln.php",
                    type: 'POST',
                    dataType: 'HTML',
                    data: {
                    },
                    success: function (result) {
                       $("#modalNakladBodyTable").html(result);
                       $("#nakladModal").modal("show");
                       }
                 });     
            }
        });
    } else {
        $.ajax({
            url: "www/ucebnice/nakladatelstvi-modal-napln.php",
            type: 'POST',
            dataType: 'HTML',
            data: {
            },
            success: function (result) {
               $("#modalNakladBodyTable").html(result);
               $("#nakladModal").modal("show");
            }
         });
    }
}    


function konecPredmetyEdit() {
  $("#predmetyModal").modal("hide"); 
}

function konecNakladEdit() {
  $("#nakladModal").modal("hide");
}

/******************************************************************************/

function skladPohyb(){
  let id = $("#detailUcebniceForma").attr("id-ucebnice"),
      ucebnice = $("#ucTitul" + id).text();
  $.ajax({
      url: "www/ucebnice/pohyby-modal.php",
      type: 'POST',
      dataType: 'HTML',
      data: {
        'operace': 'doplneniSkladu', 
        'ucebnice': ucebnice,
        'idkniha': id,
      },
      success: function (result) {
        $('#proPredmetyModal').html(result);
        $("#pohDatum").val(new Date().toISOString().substring(0, 10));         
        $("#doplneniSkladu").modal("show");     
      }
  });
} 

class PohybUcebnice {
   setPohybUcebnice (id, idkniha, kusu, datum, typ, poznamka){
     this.id = id;
     this.idkniha = idkniha; 
     this.kusu = kusu; 
     this.datum = datum; 
     this.typ = typ; 
     this.poznamka = poznamka;
   }
}
  
let pohybUcebnice = new PohybUcebnice();

function zapisDoplneniSkladu(){
    $("errPohyb").html("").addClass('d-none');
    let datum = $("#pohDatum").val(),
        id = $("#detailUcebniceForma").attr("id-ucebnice"),
        pocet = parseInt($("#pohKs").val(), 10),
        poznamka = $("#pohPoznamka").val(),
        typ = $('input[name=pohTyp]:checked').val(),
        aktualne = $("#doplneniSkladu").attr("pocet-aktualne"),
        zapujceno = $("#doplneniSkladu").attr("zapujcka-aktualne"),
        nejvyse = aktualne - zapujceno;

        errMsg = [],
        dnes = new Date().toISOString().substring(0,10);
    if ((pocet<1)||(isNaN(pocet))) {
      errMsg.push('počet kusů musí být alespoň 1');
    }
    if (datum > dnes){
      errMsg.push('datum v budoucnosti je divné?');
    }
    if (typ === undefined){
      errMsg.push('jeden typ pohybu musíš zvolit');
    }     
    if ((typ == "1") && (pocet > nejvyse)){
      errMsg.push(`nejvýše lze odepsat ${nejvyse} ks.`)
    }
    if (errMsg.length > 0){
      let errHtml = '<ul class="mb-1">';
      errMsg.forEach(function(item){
         errHtml += `<li>${item}</li>`;
      });
      errHtml += '</ul>';
      $("#errPohyb").html(errHtml).removeClass('d-none');
    } else {
      typ = parseInt(typ, 10)
      typ -= 2;
      if ((poznamka === undefined) || (poznamka === null)) {  poznamka = '';}
      pohybUcebnice.setPohybUcebnice (0, id, pocet, datum, typ, poznamka); 
      $("#doplneniSkladu").modal("hide");     
       $.ajax({
          url: "www/ucebnice/pohyb-uloz.php",
          type: 'POST',
          dataType: 'HTML',
          data: {
            'pohyb': JSON.stringify(pohybUcebnice),
          },
          success: function (result) {
            $("#ucCelkem"+id).html(result);
            ucebniceSklad(id);
          }
       });         
    }
}   

/***************************************************************************/

function ucebniceZapujcka(){
  let id = $("#detailUcebniceForma").attr("id-ucebnice"),
      ucebnice = $("#ucTitul" + id).text();
  $.ajax({
      url: "www/ucebnice/pohyby-modal.php",
      type: 'POST',
      dataType: 'HTML',
      data: {
        'operace': 'ucebniceZapujcka', 
        'ucebnice': ucebnice,
        'idkniha': id,
      },
      success: function (result) {
        $('#proPredmetyModal').html(result);
        $("#zapDatum").val(new Date().toISOString().substring(0, 10));
        $("#ucebniceZapujcka").modal("show");        
      }
  });
}

function zapujckaVratit(idzapujcky, kdo){
  let id = $("#detailUcebniceForma").attr("id-ucebnice"),
      ucebnice = $("#ucTitul" + id).text()+"<br>" + kdo;
  $.ajax({
      url: "www/ucebnice/pohyby-modal.php",
      type: 'POST',
      dataType: 'HTML',
      data: {
        'operace': 'zapujckaVratit', 
        'ucebnice': ucebnice,
        'idzapujcky': idzapujcky,
      },
      success: function (result) {
        $('#proPredmetyModal').html(result);
        $("#vracDatum").val(new Date().toISOString().substring(0, 10));
        $("#zapujckaVratit").modal("show");        
      }
  });
}

function zapisUcebniceVraceni(idzapujcky){
    $("errZapujcka").html("").addClass('d-none');
    let datum = $("#vracDatum").val(),
        idkniha = $("#detailUcebniceForma").attr("id-ucebnice"),
        pocet = parseInt($("#vracKs").val(), 10),
        errMsg = [],
        nejvyse = $("#zapujckaVratit").attr("pocet-zapujceno") - $("#zapujckaVratit").attr("pocet-vraceno");
        
    if ((pocet<1)||(isNaN(pocet))) {
      errMsg.push('počet kusů musí být alespoň 1');
    } else if (pocet > nejvyse){
      errMsg.push(`nejvýše lze vrátit ${nejvyse} ks`);
    }
    
    if (errMsg.length > 0){
      let errHtml = '<ul class="mb-1">';
      errMsg.forEach(function(item){
         errHtml += `<li>${item}</li>`;
      });
      errHtml += '</ul>';
      $("#errZapujcka").html(errHtml).removeClass('d-none');
    }  else {      
      $("#zapujckaVratit").modal("hide");     
       $.ajax({
          url: "www/ucebnice/zapujcka-vratit.php",
          type: 'POST',
          dataType: 'HTML',
          data: {
            'idzapujcky': idzapujcky,
            'pocet': pocet,
            'datum': datum,
            'idkniha' :  idkniha, 
          },
          success: function (result) {
            ucebniceZapujcky(idkniha);
            $("#ucZapujceno"+idkniha).html(result);
          }
       });         
    }
  
}

function zapujckaHistorie(idzapujcky, kdo){
  let id = $("#detailUcebniceForma").attr("id-ucebnice"),
      ucebnice = $("#ucTitul" + id).text()+"<br>" + kdo;
  $.ajax({
      url: "www/ucebnice/pohyby-modal.php",
      type: 'POST',
      dataType: 'HTML',
      data: {
        'operace': 'zapujckaHistorie', 
        'ucebnice': ucebnice,
        'idzapujcky': idzapujcky,
      },
      success: function (result) {
        $('#proPredmetyModal').html(result);
        $("#zapujckaHistorie").modal("show");        
      }
  });
}

function zavritHistorieVraceni(){
  $("#zapujckaHistorie").modal("hide");    
}
    
/* konec volani modalniho okna */    
    
    
/* forma na novou ucebnici */    



/* ulozeni ucebnice */

function kontrolaKnihy(id){
  $("#errUcebnice").addClass("d-none");
  let idnaklad = $("#nakladSeznam").val(),
      titul = $("#nazevuc").val(),
      idpredmet =  $("#predmetSeznam").val(),
      rokvydani =  $("#rokVydani").val(),
      rokstudia =  $("#rocnik").val(), 
      err = [], 
      errMsg = `<div class="alert alert-danger py-1 mb-1"><ul class=mb-0 pb-1">\n`;
  if (!OKString(titul)) err.push("chybí jméno učebnice");
//  if (idnaklad < 1) err.push("není vybráno nakladatelství");
//  if (!OKString(rokvydani)) err.push("chybí rok vydání");
  if (idpredmet < 1) err.push("není vybrán předmět");
  if (rokstudia < 1) err.push("není vybrán rok studia");
  if (err.length > 0){
     for(let i in err){ errMsg += `<li>${err[i]}</li>\n`;}
     errMsg += `</ul></div>`;
     $("#errUcebnice").html(errMsg);
     $("#errUcebnice").removeClass("d-none");
     return false;
  } else {
     ucUcebnice.setUcebnice (id, idnaklad, titul, idpredmet, rokvydani, rokstudia);
     return true;
  }
}
    
function ulozKnihu(){
  let id = $("#detailUcebniceForma").attr("id-ucebnice");
  if (kontrolaKnihy(id)) {
    $("#msgKopirujes").addClass("d-none");
    $.ajax({
      url: "www/ucebnice/ucebnice-uloz.php",
      type: 'POST',
      dataType: 'HTML',
      data: {
        'ucebnice': JSON.stringify(ucUcebnice),
      },
      success: function (result) {
        if (id == 0){
          let newid = result, 
          htx = 
` <tr id="radekUc${newid}" >
    <td id="ucTitul${newid}" style="cursor: pointer;"  onclick="editujUcebnici(${newid})">${ucUcebnice.titul} (${ucUcebnice.rokvydani})</td>
    <td id="ucCelkem${newid}" class="text-right pr-3" width="80px">0</td>
    <td id="ucZapujceno${newid}" class="text-right pr-3" width="80px">0</td>
    <td id="ucDel${newid}" class="ikonaForm text-danger" onclick="smazUcebnici(${newid})"><i class="far fa-trash-alt"></td> 
    <td id="ucEd${newid}" class="ikonaForm text-info" onclick="editujUcebnici(${newid})"><i class="far fa-edit"></i></td>                                                          
  </tr> `;
      $("#tblSezUcBody").prepend(htx);
      editujUcebnici(newid);  
        } else {
           $("#ucTitul" + id).html(`${ucUcebnice.titul} (${ucUcebnice.rokvydani})`)
        }
       let msgUlozeno = `<div class="alert alert-success alert-dismissible mb-0 py-1 w-100">
                              <a href="#" class="close" style="padding: 5px" data-dismiss="alert" aria-label="close">&times;</a>
                              ULOŽENO</div>`;
       $("#errUcebnice").html(msgUlozeno).removeClass("d-none");
       nastavViditelnost("#errUcebnice");         
      }
   });  
  }  
}              

function smazUcebnici(id){
  $("#smazUcebniciDotaz").remove();
  let  ucebnice = $("#ucTitul" + id).text(),
       dotaz=`
<tr id="smazUcebniciDotaz">
  <td colspan="5"  class="alert alert-danger mb-0 py-1 rounded-0" >
    <div class="d-flex">
      <div id="smazDotazText" class="p-2"> Chceš smazat "${ucebnice}" ?</div>  
      <button class="ml-auto btn btn-info rounded-0" onclick="stornoSmazUcebnici()">Storno</button>
      <button id='btnSmazatSmazat' class="btn btn-danger ml-2 rounded-0" onclick="smazUcebniciSmazat(${id})">Smazat</button>  
    </div>
  </td>
</tr>  
  `;
  $("#radekUc"+id).after(dotaz);

}    

function stornoSmazUcebnici(){
   $("#smazUcebniciDotaz").remove();
}

function smazUcebniciSmazat(id){
  let celkem = $("#ucCelkem"+id).text(),
      zapujceno = $("#ucZapujceno"+id).text();
  if ((celkem > 0) || (zapujceno > 0)) {
    $("#smazDotazText").html("Jsou nějaké kusy skladem resp. zapůjčené. Nelze mazat!");
    $("#btnSmazatSmazat").addClass('d-none');
  } else {
      $.ajax({
          url: "www/ucebnice/ucebnice-smaz.php",
          type: 'POST',
          dataType: 'HTML',
          data: {
            'id': id,
          },
          success: function (result) {
             $('#smazUcebniciDotaz').remove();
             $("#radekUc"+id).remove();
             if (id == $("#detailUcebniceForma").attr("id-ucebnice")){
                $("#btnBar").addClass("d-none");
                $("#ucebniceDetail").html("");
             }
          }
       });   
  } 
}

function pisNazev(){
  let id = $("#detailUcebniceForma").attr("id-ucebnice");
  if (id == 0){
      $("#ucebniceTitulek").html('Nová učebnice - ' + $("#nazevuc").val());
  } else {
    $("#ucebniceTitulek").html($("#nazevuc").val());
    $("#ucTitul"+id).html($("#nazevuc").val() + ' (' + $("#rokVydani").val() + ')');  
  }
}

class Filtr {
  constructor (){
    $("#btnFiltrujUcebnice").addClass("d-none");
    $("#btnZrusFiltrUcebnice").addClass("d-none"); 
    this.ukazZrusFiltr = false;
    
  }
  
  jeFiltr (){
    this.stav = true;
    $("#btnZrusFiltrUcebnice").removeClass("d-none");
    $("#btnFiltrujUcebnice").addClass("d-none");
  }
  
  zrusFiltr(){
    this.stav = false;
    $("#btnZrusFiltrUcebnice").addClass("d-none"); 
    $("#predFiltr").val(0);
    $("#rocFiltr").val(0);
    $('[id^="filtrKdo"]').prop("checked", false);
    $("#idComboselectKdoFiltr").html("");
  }

  aktualizujFiltr(){
    $("#btnFiltrujUcebnice").removeClass("d-none");
  }
  
  setFiltr (idpredmet, rokstudia, typzapujcil, idzapujcil){
    this.idpredmet = idpredmet;
    this.rokstudia = rokstudia;
    this.typzapujcil = typzapujcil; 
    this.idzapujcil = idzapujcil; 
  }
}

let filtr = new Filtr();

function stavFiltru(){
  filtr.aktualizujFiltr();
};

function filtrujUcebnice(){
  filtr.jeFiltr();
  $("#detailUcebniceForma").remove();
  $("#pohybSpodek").addClass("d-none");
  $("#btnFiltrujUcebnice").html(filtr.btnText);
  let idzapujcil =  $("#selectKdoFiltr").val(),
      typzapujcil = $('[name="filtrKdo"]:checked').val();
  filtr.setFiltr($("#predFiltr").val(), $("#rocFiltr").val(), typzapujcil, idzapujcil);
  
  $.ajax({
    url: "www/ucebnice/ucebnice-seznam.php",
    type: 'POST',
    dataType: 'HTML',
    data: {
      'filtr': JSON.stringify(filtr),
    },
    success: function (result) {
      $("#ucebniceSeznam").html(result); 
       nastavBtnBar();   
       }
  }); 
}

function zrusFiltrUcebnice(){
  $.ajax({
    url: "www/ucebnice/ucebnice-seznam.php",
    type: 'POST',
    dataType: 'HTML',
    data: {

    },
    success: function (result) {
      $("#ucebniceSeznam").html(result); 
      filtr.zrusFiltr(); 
      let id = $("#detailUcebniceForma").attr("id-ucebnice");
      if (id > 0) {
        $("#radekUc"+id).css("background-color", "#dee2e6");
      } else {
        $("#pohybSpodek").addClass("d-none");
      }
      nastavBtnBar(); 
    }
  });
}

function kopieUcebnice(){
  $("#pohybSpodek").addClass("d-none");
  $("#btnBar").addClass("d-none");
  $("#msgKopirujes").removeClass("d-none");
  $('[id^="radekUc"]').css("background-color", "transparent");
  $("#btnUcebniceKopie").addClass("d-none");
  let f = document.getElementById('detailUcebniceForma');
  ucUcebnice.id = 0;
  if ((f === undefined) || (f === null)) {
    ucebniceForma();
    $.ajax({
        success: function () {
          ucebniceNastav("Založit"); 
          $("#detailUcebniceBody").addClass("show");       
          }
    })     
  } else {
    ucebniceNastav("Založit");
    $("#detailUcebniceBody").addClass("show");
  }   
} 

function ucebniceRefresh(){
   zrusFiltrUcebnice(); 
}

function pohybForma(){
  let f = document.getElementById("pohybVrsek");
  if ((f === undefined) || (f === null)) {
    $.ajax({
      url: "www/ucebnice/pohyb-forma.php",
      type: 'POST',
      dataType: 'HTML',
      data: {
  
      },
      success: function (result) {
         $("#ucebniceDetail").html(result);
      }
    });  
  }
}

function nastavBtnBar(){
  if ( $("#detailUcebniceForma").attr("id-ucebnice") > 0) {
    $("#btnBar").removeClass("d-none");
    $("#pohybSpodek").removeClass("d-none");
  } else {
    $("#btnBar").addClass("d-none");
  }
}

function zapujckaKdo(kdo, selector, jeFiltr = 'false'){
   if (kdo == 'u'){
      $.ajax({
          url: "www/ucebnice/zapujcka-ucitele.php",
          type: 'POST',
          dataType: 'HTML',
          data: {
            'selectName': selector,
            'jeFiltr': jeFiltr,
          },
          success: function (result) {
            $("#idCombo"+selector).html(result);
          }
      });   
   }
   if (kdo == 't') {
      $.ajax({
          url: "www/ucebnice/zapujcka-tridy.php",
          type: 'POST',
          dataType: 'HTML',
          data: {
             'selectName': selector,
             'jeFiltr': jeFiltr,
          },
          success: function (result) {
              $("#idCombo"+selector).html(result);
          }
      });   
  }
}  

class Zapujcka {
  setZapujcka(id, idkniha, datum, pocet, typzapujcil, idzapujcil) {
     this.id = id;
     this.idkniha = idkniha; 
     this.datum = datum; 
     this.pocet = pocet; 
     this.typzapujcil = typzapujcil; 
     this.idzapujcil = idzapujcil;
  }
}

zapujcka = new Zapujcka();

function zapisUcebniceZapujcka(){
    $("errZapujcka").html("").addClass('d-none');
    let datum = $("#zapDatum").val(),
        idkniha = $("#detailUcebniceForma").attr("id-ucebnice"),
        pocet = parseInt($("#zapKs").val(), 10),
        typzapujcil = $('input[name=zapKdo]:checked').val(),
        idzapujcil = $("#selectKdoZapujcka").val(),
        lzePujcit = $("#ucebniceZapujcka").attr("pocet-aktualne") - $("#ucebniceZapujcka").attr("zapujcka-aktualne"),
        errMsg = [],
        dnes = new Date().toISOString().substring(0,10);
    if ((pocet<1)||(isNaN(pocet))) {
      errMsg.push('počet kusů musí být alespoň 1');
    } else if (pocet > lzePujcit) {
      errMsg.push(`k dispozici je pouze ${lzePujcit} ks`);
    }
    if (datum > dnes){
      errMsg.push('datum v budoucnosti je divné?');
    }
    if (typzapujcil === undefined){
      errMsg.push('musíš vybrat komu půjčuješ');
    } else if (idzapujcil<1) {
      errMsg.push('a kdo si půjčuje?');
    }
    if (errMsg.length > 0){
      let errHtml = '<ul class="mb-1">';
      errMsg.forEach(function(item){
         errHtml += `<li>${item}</li>`;
      });
      errHtml += '</ul>';
      $("#errZapujcka").html(errHtml).removeClass('d-none');
    }  else {
      zapujcka.setZapujcka(0, idkniha, datum, pocet, typzapujcil, idzapujcil)
      $("#ucebniceZapujcka").modal("hide");     
       $.ajax({
          url: "www/ucebnice/zapujcka-zapis.php",
          type: 'POST',
          dataType: 'HTML',
          data: {
            'zapujcka': JSON.stringify(zapujcka),
          },
          success: function (result) {
            ucebniceZapujcky(idkniha);
            $("#ucZapujceno"+idkniha).html(result);
          }
       });         
    }
}

function ucebniceTiskDialog(){
  $.ajax({
      url: "www/ucebnice/pohyby-modal.php",
      type: 'POST',
      dataType: 'HTML',
      data: {
        'operace': 'zapujckyTisk', 
      },
      success: function (result) {
        $('#modalTisk').html(result);
        $("#zapujckyTisk").modal("show");     
      }
  });  
} 


function tiskKdo(kdo){
   $("#btnSelAll").removeClass("d-none");
   $("#btnUnselAll").removeClass("d-none");
   if (kdo == 'u'){
      $.ajax({
          url: "www/ucebnice/tisk-ucitele.php",
          type: 'POST',
          dataType: 'HTML',
          data: {
          },
          success: function (result) {
            $("#idDivKdoTisk").html(result);
          }
      });   
   }
   if (kdo == 't') {
      $.ajax({
          url: "www/ucebnice/tisk-tridy.php",
          type: 'POST',
          dataType: 'HTML',
          data: {

          },
          success: function (result) {
              $("#idDivKdoTisk").html(result);
          }
      });   
  }
}  

function btnZapujckyTisk(){
  $("#errTisk").html("").addClass('d-none');
  let errMsg = [], 
      z = '',
      typtisk = $('input[name=tiskKdo]:checked').val();
  if (typtisk === undefined){
    errMsg.push('musíš vybrat koho chceš tisknout');
  } else {
    let chb = document.querySelectorAll("[name=tiskKdoCheck");
    for (let i = 0; i < chb.length; i++){
      if (chb[i].checked) {
        z += chb[i].value + ',';
      }
    }
    if (z.length < 1){
      errMsg.push('zatrhni nejméně jednu položku');
    }
  }

  if (errMsg.length > 0){
    let errHtml = '<ul class="mb-1">';
    errMsg.forEach(function(item){
       errHtml += `<li>${item}</li>`;
    });
    errHtml += '</ul>';
    $("#errTisk").html(errHtml).removeClass('d-none');
  }  else { 
      z = z.substr(0, z.length-1);
      $.ajax({
          url: `www/ucebnice/tisk-tisk.php`,
          success: function (result) {
             $("#zapujckyTisk").modal("hide");
             window.open(`www/ucebnice/tisk-tisk.php?t=${typtisk}&z=${z}`, "skladTisk");            
          }
      });  
  }

}

function tiskSetCheckBoxes(stav){
    let chb = document.querySelectorAll("[name=tiskKdoCheck");
    for (let i = 0; i < chb.length; i++){
      chb[i].checked = stav;
    }
}


/*
function pohybUcebnice(id){
  let f = document.getElementById("pohybVrsek"),
      titul = $("#ucTitul" + id).text();
  $('[id^="radekUc"]').css("background-color", "transparent");    
  $("#radekUc"+id).css("background-color", "#dee2e6");
  $("#pohybDetail").attr("id-pohyb", id);
  nastavBtnBar();    
  if ((f === undefined) || (f === null)) {
    pohybForma();    
    $.ajax({
        success: function () {
          $("#cardPohyb").html(`Pohyby učebnice: <strong>${titul}</strong>`);        
        }
    })     
  } else {
    $("#cardPohyb").html(`Pohyby učebnice: <strong>${titul}</strong>`); 
  }   
  
}
*/