Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
126 kB
0
Indexable
Never
var 
    active = "main",
    ticketcount = 0,
    total = 0;
//var
const 
    active_select = "rgb(47,191,249)",
    inactive_select = "rgb(41, 121, 150)",
    judgement_elements = document.querySelectorAll(".judgement-item"),
    judgement_summary = document.querySelector("#judgement-summary");
//const
var activeTicketList = '';
window.onload = ()=>{
    document.querySelector("#"+active).style.display = "block";
    $('html').css('display', 'none');
    $('body').css('display', 'none');

    let judgementTickets = '<p>Kategorie</p>';
    let judgementData = '';
    for(let i in judgementTicketList) {
        let data = judgementTicketList[i];
        let data2 = data.data;
        if (i == 0) {
            judgementTickets = judgementTickets + `<p class="category-button-selected" onclick="changeTicketCategory(this, '`+i+`')" id="`+data.categoryName+`">`+data.categoryName+`</p>`;
            for(let j in data2) {
                let data3 = data2[j];
                let name = data3.name;
                let time = data3.time;
                let bill = data3.bill;
                let count = data3.count;
                judgementData = judgementData + `<div class="judgement-ticket-box"><p class="ticket-count"><span>`+count+`</span><br/><span><i class="fa-light fa-plus" onclick="addAmount(this, '`+i+`', '`+j+`')"></i><i class="fa-light fa-minus" onclick="decreaseAmount(this, '`+i+`', '`+j+`')"></i></span></p><div id="icon"><i class="fa-solid fa-circle"></i></div><p class="content"><br/>`+name+`<br/><p class="judgement-ticket-amount">`+time+` msc.<span>$`+bill+`</span></p></p></div>`;
            }
        } else {
            judgementTickets = judgementTickets + `<p class="category-button" onclick="changeTicketCategory(this, '`+i+`')" id="`+data.categoryName+`">`+data.categoryName+`</p>`;
        }
    }
    this.document.getElementById("category-nav").innerHTML = judgementTickets;
    this.document.getElementById("main-ticket-list").innerHTML = judgementData;
    if (judgementTicketList.length > 0) {
        activeTicketList = document.querySelector("#"+judgementTicketList[0].categoryName);
    }
}
const
    patrolSelect = document.querySelector(".patrol-select"),
    patrolBox = document.querySelector("#popup-patrol-background"),
    citizenBg = document.querySelector("#popup-citizen-background"),
    citizenPictureEdit = document.querySelector("#popup-citizen-picture-edit"),
    citizenWanted = document.querySelector("#popup-citizen-wanted"),
    citizenNote = document.querySelector("#popup-citizen-note"),
    citizenDescription = document.querySelector("#popup-citizen-description"),
    vehicleBg = document.querySelector("#popup-vehicle-background"),
    vehicleWanted = document.querySelector("#popup-vehicle-wanted"),
    vehicleDescription = document.querySelector("#popup-vehicle-description"),
    vehicleNote = document.querySelector("#popup-vehicle-note"),
    workerAddPopup = document.querySelector("#popup-worker-background"),
    discourseBg = document.querySelector("#popup-discourse-background"),
    discoursePictureEdit = document.querySelector("#popup-discourse-picture-edit"),
    discoursePhoto = document.querySelector("#discourse-picture-edit"),
    announceBg = document.querySelector("#popup-announce-background"),
    announceDesc = document.querySelector("#popup-announce-description"),
    judgementBg = document.querySelector("#popup-judgement-background"),
    judgementImport = document.querySelector("#popup-judgement-import");
//const
let headerNameAndTime = '<span class="tirex-name">Witaj, <b>NAME, SURNAME - TIME</b></span>';

var
    ownJobGrade = 0,
    discourseCitizensList = [],
    discourseOfficersList = [],
    discourseVehiclesList = [],
    discourseImagesList = [],
    judgementsCitizensList = [],
    judgementsTicketList = [];
//var

// UPDATE GLOBAL DATA TIREX
function mainAddFpList(officers) {
    let fpList = '<tr><th>Imię i nazwisko</th><th>Jednostka</th><th>Kanał radiowy</th><th>Status</th><th>Numer odznaki</th></tr>';
    let maxFp = 0;
    
    for (let i in officers) {
        let data = officers[i];
        let name = data.firstname + ' ' + data.lastname;
        let crypto = data.crypto;
        let radio = data.radio;
        let status = data.status;
        let badge = data.badge; 

        if (status == "yes") {
            fpList = fpList + '<tr><td>'+name+'</td><td>'+crypto+'</td><td>'+radio+'</td><td><span class="status available">Dostępny</span></td><td>'+badge+'</td></tr>';
            maxFp = maxFp + 1;
        } else {
            fpList = fpList + '<tr><td>'+name+'</td><td>'+crypto+'</td><td>'+radio+'</td><td><span class="status unavailable">Niedostępny</span></td><td>'+badge+'</td></tr>';
        }
    }
    this.document.getElementById("cop-count").innerHTML = maxFp;
    this.document.getElementById("tirex-fpList").innerHTML = fpList;
    return;
}
function patrolsAddPatrolList(patrols, yourSsn) {
    var isInPatrolList = [];
    var joinPatrolList = [];

    for (let i in patrols) {
        let data = patrols[i];
        let officers = data.officers;
        let patrolLength = isInPatrolList.length;
        for (let j in officers) {
            let data1 = officers[j];
            let officerSsn = data1.ssn;
            if (officerSsn == yourSsn) {
                isInPatrolList.push(data);
                break;
            }
        }
        if (patrolLength == isInPatrolList.length) {
            joinPatrolList.push(data);
        }
    }

    let patrolList = '';

    for (let i in isInPatrolList) {
        let data = isInPatrolList[i];
        let ssn = data.yourSsn;
        let creator = data.creator;
        let patrolId = data.patrolId;
        let patrolType = data.patrolType;
        let officers = data.officers;
        let blocked = data.blocked;
        if (creator == yourSsn) {
            if (blocked == true) {
                patrolList = `<div class="item patrol" id="patrol-num1" style="--width:calc(100% - 30px); --height: 225px; background-color: var(--itemcolor); --margin: 5px 15px;"><div id="header"><p>Patrol `+patrolType+` #`+patrolId+`</p><div id="patrol-buttons"><span class="patrol-button patrol-invalid onlyCursorHover" onclick="menagePatrol('block', '`+patrolId+`')">Zajęty</span><span id="patrol-party" onclick="menagePatrol('leave','`+patrolId+`')" class="patrol-button patrol-invalid">Opuść</span></div><span>Aktywni funkcjonariusze</span></div><table class="patrol-members">`;
            } else {
                patrolList = `<div class="item patrol" id="patrol-num1" style="--width:calc(100% - 30px); --height: 225px; background-color: var(--itemcolor); --margin: 5px 15px;"><div id="header"><p>Patrol `+patrolType+` #`+patrolId+`</p><div id="patrol-buttons"><span class="patrol-button patrol-valid onlyCursorHover" onclick="menagePatrol('block', '`+patrolId+`')">Dostępny</span><span id="patrol-party" onclick="menagePatrol('leave', '`+patrolId+`')" class="patrol-button patrol-invalid">Opuść</span></div><span>Aktywni funkcjonariusze</span></div><table class="patrol-members">`;
            }
        } else {
            if (blocked == true) {
                patrolList = `<div class="item patrol" id="patrol-num1" style="--width:calc(100% - 30px); --height: 225px; background-color: var(--itemcolor); --margin: 5px 15px;"><div id="header"><p>Patrol `+patrolType+` #`+patrolId+`</p><div id="patrol-buttons"><span class="patrol-button patrol-invalid">Zajęty</span><span id="patrol-party" onclick="menagePatrol('leave','`+patrolId+`')" class="patrol-button patrol-invalid">Opuśc</span></div><span>Aktywni funkcjonariusze</span></div><table class="patrol-members">`;
            } else {
                patrolList = `<div class="item patrol" id="patrol-num1" style="--width:calc(100% - 30px); --height: 225px; background-color: var(--itemcolor); --margin: 5px 15px;"><div id="header"><p>Patrol `+patrolType+` #`+patrolId+`</p><div id="patrol-buttons"><span class="patrol-button patrol-valid">Dostępny</span><span id="patrol-party" onclick="menagePatrol('leave', '`+patrolId+`')" class="patrol-button patrol-invalid">Opuśc</span></div><span>Aktywni funkcjonariusze</span></div><table class="patrol-members">`;
            }
        }

        for (let j in officers) {
            let data1 = officers[j];
            let officerSsn = data1.ssn;
            let badge = data1.badge;
            let crypto = data1.crypto;
            let job = data1.job;
            let name = data1.name;
            if (officerSsn == ssn) {
                patrolList = patrolList + '<tr><td>'+name+' (Ty)</td><td>'+crypto+'</td><td>'+badge+'</td></tr>';
            } else {
                if (ssn == creator) {
                    patrolList = patrolList + `<tr><td><i class="fa-regular fa-trash party-kick" onclick="menagePatrol('kick', '`+officerSsn+`')"></i>`+name+`</td><td>`+crypto+`</td><td>`+badge+`</td></tr>`;
                } else {
                    patrolList = patrolList + '<tr><td>'+name+'</td><td>'+crypto+'</td><td>'+badge+'</td></tr>';
                }
            }
        }
        patrolList = patrolList + '</table></div>';
    }

    for (let i in joinPatrolList) {
        let data = joinPatrolList[i];
        let patrolId = data.patrolId;
        let patrolType = data.patrolType;
        let officers = data.officers;
        let blocked = data.blocked;
        if (blocked == true) {
            patrolList = `<div class="item patrol" id="patrol-num1" style="--width:calc(100% - 30px); --height: 225px; background-color: var(--itemcolor); --margin: 5px 15px;"><div id="header"><p>Patrol `+patrolType+` #`+patrolId+`</p><div id="patrol-buttons"><span class="patrol-button patrol-invalid">Zajęty</span><span id="patrol-party" class="patrol-button patrol-valid">Dołącz</span></div><span>Aktywni funkcjonariusze</span></div><table class="patrol-members">`;
        } else {
            patrolList = `<div class="item patrol" id="patrol-num1" style="--width:calc(100% - 30px); --height: 225px; background-color: var(--itemcolor); --margin: 5px 15px;"><div id="header"><p>Patrol `+patrolType+` #`+patrolId+`</p><div id="patrol-buttons"><span class="patrol-button patrol-valid">Dostępny</span><span id="patrol-party" onclick="menagePatrol('join', '`+patrolId+`')" class="patrol-button patrol-valid">Dołącz</span></div><span>Aktywni funkcjonariusze</span></div><table class="patrol-members">`;
        }

        for (let j in officers) {
            let data1 = officers[j];
            let badge = data1.badge;
            let job = data1.job;
            let name = data1.name;
            patrolList = patrolList + '<tr><td>'+name+'</td><td>'+job+'</td><td>'+badge+'</td></tr>';
        }
        patrolList = patrolList + '</table></div>';
    }

    this.document.getElementById("tirex-patrols").innerHTML = patrolList;
    return;
}
function patrolAddDispatchList(dispatch) {
    let dispatchList = ``;
    if (dispatch.length > 0) {
        for (let i in dispatch) {
            let data = dispatch[i];
            let id = data.id;
            let content = data.content;
            let officers = data.officers;
            let maxOfficers = data.maxOfficers;
            dispatchList = dispatchList + `<tr><td style="width:40%">`+content+`</td><td>#`+id+`</td><td><span style="font-size:14px;">FP: `+officers.length+`/`+maxOfficers+`</span></td><td><span onclick="dispatchDataAction('deleteFromList', '`+id+`')"><i class="fa-regular fa-trash"></i></span><span onclick="dispatchDataAction('addToList', '`+id+`')"><i class="fa-regular fa-plus"></i></span></td></tr>`;
        }
    }
    this.document.getElementById("patrol-announcements").innerHTML = dispatchList;
}
function citizensAddCitizenList(citizens) {
    let citizenList = '<tr><th style="width:50%;">Obywatel</th><th style="width:20%;">Poszukiwany</th><th style="width:20%;">Social Security Number</th><th style="width:10%;"></th></tr>';
    if (citizens.length > 0) {
        for (let i in citizens) {
            let data = citizens[i];
            let ssn = data.ssn;
            let name = data.firstname+' '+data.lastname;
            let wanted = data.wanted;
            let image = 'public/img/citizen-picture-none.png';
            if (data.image && data.image.length > 0) {
                image = data.image;
            }

            if (wanted.toUpperCase() == "TAK") {
                citizenList = citizenList + `<tr onclick="showCitizen('`+ssn+`')"><td><img src="`+image+`" id="table-citizen-picture"><p class="citizen-name">`+name+`</p></td><td><span style="background-color: rgb(150, 0, 0) !important;">`+wanted+`</span></td><td>`+ssn+`</td><td><span><i class="fa-solid fa-angle-right"></i></span></td></tr>`;
        
            } else {
                citizenList = citizenList + `<tr onclick="showCitizen('`+ssn+`')"><td><img src="`+image+`" id="table-citizen-picture"><p class="citizen-name">`+name+`</p></td><td><span>`+wanted+`</span></td><td>`+ssn+`</td><td><span><i class="fa-solid fa-angle-right"></i></span></td></tr>`;
            }
        }
    }
    this.document.getElementById("citizen-list-table").innerHTML = citizenList;
    return;
}
function citizensAddCitizenWanted(citizens) {
    let wantedCitizenList = '<p>Najnowsze listy gończe</p>';
    if (citizens.length > 0) {
        for (let i in citizens) {
            let data = citizens[i];
            let name = data.name;
            let ssn = data.ssn;
            wantedCitizenList = wantedCitizenList + `<div class="wanted-box onlyCursorHover" onclick="showCitizen('`+ssn+`', 'citizens')"><div id="icon"><i class="fa-solid fa-circle"></i></div><div id="content">`+name+`</div></div>`;
        }
    }
    this.document.getElementById("wanted").innerHTML = wantedCitizenList;
    return;
}
function citizensAddVehicleWanted(vehicles) {
    let wantedVehiclesList = '<p>Najnowsze BOLO pojazdów</p>';
    if (vehicles.length > 0) {
        for (let i in vehicles) {
            let data = vehicles[i];
            let model = data.model;
            let plates = data.plates;
            let vin = data.vin;
            wantedVehiclesList = wantedVehiclesList + `<div class="bolo-box onlyCursorHover" onclick="showVehicle('`+vin+`', 'vehicles')"><div id="name">`+model+`</div><div id="register">`+plates+`</div></div>`;
        }
    }
    this.document.getElementById("bolo").innerHTML = wantedVehiclesList;
    return;
}
function vehiclesAddVehicleList(vehicles) {
    let vehicleList = '<table id="discourse-list"><tr><th style="width:30%">Model</th><th style="width:10%">Poszukiwany</th><th style="width:20%">Właściciel</th><th style="width:15%">VIN</th><th style="width:20%">Tablica Rej</th><th style="width:5%"></th></tr>';
    if (vehicles.length > 0) {
        for (let i in vehicles) {
            let data = vehicles[i];
            let model = data.model;
            let wanted = data.wanted;
            let owner = data.owner;
            let vin = data.vin;
            let plates = data.plates;
            if (wanted.toUpperCase() == "TAK") {
                vehicleList = vehicleList + `<tr onclick="showVehicle('`+vin+`')"><td>`+model+`</td><td><span class="table-data-background" style="background-color: rgb(150, 0, 0) !important;">`+wanted+`</span></td><td><span class="table-data-background">`+owner+`</span></td><td>`+vin+`</td><td>`+plates+`</td><td><span><i class="fa-solid fa-angle-right"></i></span></td></tr>`;

            } else {
                vehicleList = vehicleList + `<tr onclick="showVehicle('`+vin+`')"><td>`+model+`</td><td><span class="table-data-background">`+wanted+`</span></td><td><span class="table-data-background">`+owner+`</span></td><td>`+vin+`</td><td>`+plates+`</td><td><span><i class="fa-solid fa-angle-right"></i></span></td></tr>`;
            }
        }
    }
    vehicleList = vehicleList + '</table>';
    this.document.getElementById("tirex-car-list").innerHTML = vehicleList;
    return;
}
function weaponsAddWeaponList(weapons) {
    let weaponList = '<tr><th>Model</th><th>Właściciel</th><th>Numer seryjny</th><th>Data zakupu</th></tr>';

    if (weapons.length > 0) {
        for (let i in weapons) {
            let data = weapons[i];
            let model = data.model;
            let owner = data.name;
            let serialNumber = data.serial_number;
            let date = data.purchase;

            weaponList = weaponList + '<tr><td>'+model+'</td><td><span class="table-data-background">'+owner+'</span></td><td><span class="table-data-background">'+serialNumber+'</span></td><td>'+date+'</td></tr>';
        }
    }
    this.document.getElementById("weapon-evi-table").innerHTML = weaponList;
    return;
}
function discourseAddDiscourseList(discourses) {
    let discourseList = '<table id="discourse-list"><tr><th style="width:30%">Tytuł sprawy</th><th style="width:10%">Numer sprawy</th><th style="width:20%">Data Edycji</th><th style="width:15%">Autor</th><th style="width:20%">Status</th><th style="width:5%"></th></tr>';

    if (discourses.length > 0) {
        for (let i in discourses) {
            let data = discourses[i];
            let name = data.name;
            let id = data.id;
            let edit_date = data.edit_date;
            let author = data.author;
            let status = data.status;

            if (status == "true") {
                discourseList = discourseList + `<tr onclick="showDiscourse('`+id+`')"><td>`+name+`</td><td>#`+id+`</td><td>`+edit_date+`</td><td>`+author.name+`</td><td><span class="patrol-button patrol-valid">Rozwiązana</span></td><td><span><i class="fa-solid fa-angle-right"></i></span></td></tr>`;
            } else {
                discourseList = discourseList + `<tr onclick="showDiscourse('`+id+`')"><td>`+name+`</td><td>#`+id+`</td><td>`+edit_date+`</td><td>`+author.name+`</td><td><span class="patrol-button patrol-invalid">Nierozwiązana</span></td><td><span><i class="fa-solid fa-angle-right"></i></span></td></tr>`;
            }
        }
    }

    discourseList = discourseList + '</table>';
    this.document.getElementById("tirex-discourse-list").innerHTML = discourseList;
    return;
}
function annoucementsAddList(annoucements) {
    let annoucemList = '<p>Nowe komunikaty</p>';
        if (annoucements.length > 0) {
            for (let i in annoucements) {
                let data = annoucements[i];
                let author = data.author;
                let id = data.id;
                let status = data.status;
                let date = data.expiry_date;
                let content = data.content;

                annoucemList = annoucemList + '<div class="item announcement-box" style="--width: calc(100% - 100px); --height: auto; background-color:var(--itemcolor); --margin: 5px 25px; --padding: 0px 25px 15px 25px;"><span>#'+id+'<span style="float:right;">'+author+'</span></span><p><span style="margin-top:10px;display:inline-block;"><i style="margin:0px 10px 0px 0px;" class="fa-light fa-calendar"></i> Wygasa: <span>00:00 '+date+'</span></span><span style="float:right;background-color:var(--buttoncolor);color:black;margin:0;" class="judgement-button">'+status+'</span></p><br/>'+content+'</div>';
            }
        }
    this.document.getElementById("annoucements-panel").innerHTML = annoucemList;
    return;
}
function workersAddWorkerList(workers) {
    let workerList = '<table id="discourse-list"><tr><th style="width:30%">Imię i nazwisko</th><th style="width:10%">Stopień</th><th style="width:20%">Jednostka</th><th style="width:15%">Ostatnio Aktywny</th><th style="width:20%">Czas na służbie</th><th style="width:5%"></th></tr>';
    if (workers.length > 0) {
        for (let i in workers) {
            let data = workers[i];
            let ssn = data.ssn;
            let name = data.firstname+' '+data.lastname
            let grade = data.job_grade;
            let crypto = data.crypto;
            let lastOnDuty = data.lastOnDuty;
            let dutyTime = data.dutyTime;
            workerList = workerList + `<tr onclick="showWorker('`+ssn+`')"><td>`+name+`</td><td>`+grade+`</td><td>`+crypto+`</td><td>`+lastOnDuty+`</td><td>`+dutyTime+`</td><td><span><i class="fa-solid fa-angle-right"></i></span></td></tr>`;
        }
    }
    workerList = workerList + '</table>';
    this.document.getElementById("tirex-worker-list").innerHTML = workerList;
    return;
}

function workersAddWorkerCarList(workers) {
    let carList = '<table id="discoursecar-list"><tr><th style="width:10%">Praca</th><th style="width:20%">Rejestracja</th><th style="width:15%">VIN<th style="width:20%">Właścicel</th><th style="width:20%">WspółWłaścicel</th></th><th style="width:5%"></th></tr>';
    if (workers.length > 0) {
        for (let i in workers) {
            let data = cars[i];

            let ssn = data.ssn;
            let name = data.firstname+' '+data.lastname
            let grade = data.job_grade;
            let crypto = data.crypto;
            let lastOnDuty = data.lastOnDuty;
            let dutyTime = data.dutyTime;
            carList = carList + `<tr onclick="showWorker('`+ssn+`')"><td>`+name+`</td><td>`+grade+`</td><td>`+crypto+`</td><td>`+lastOnDuty+`</td><td>`+dutyTime+`</td><td><span><i class="fa-solid fa-angle-right"></i></span></td></tr>`;
        }
    }
    carList = carList + '</table>';
    this.document.getElementById("tirex-workercar-list").innerHTML = workerList;
    return;
}

function updateGptData(data) {
    if (active == "main") {
        headerNameAndTime = '<span class="tirex-name">Witaj, '+data.name+' - '+data.dutyTime+'</span>';
        $('.tirex-name').text("Witaj, "+data.name+" - "+data.dutyTime);
        this.document.getElementById("annoucement").innerHTML = data.annoucement;
        mainAddFpList(data.officers);
        citizensAddCitizenWanted(data.citizens);
        citizensAddVehicleWanted(data.vehicles);
    } else if (active == "patrol") {
        patrolsAddPatrolList(data.patrols, data.ssn);
        patrolAddDispatchList(data.dispatch);
    } else if (active == "citizens") {
        citizensAddCitizenList(data);
    } else if (active == "vehicles") {
        vehiclesAddVehicleList(data);
    } else if (active == "weapon_evi") {
        weaponsAddWeaponList(data.weaponList);
    } else if (active == "discourse") {
        discourseAddDiscourseList(data);
    } else if (active == "judgement") {

    } else if (active == "evidence") {

    } else if (active == "annoucem") {
        annoucementsAddList(data);
    } else if (active == "workers") {
        workersAddWorkerList(data);
    }
    return;
}
// UPDATE GLOBAL DATA TIREX

function openPopup(source, el){
    document.querySelector("#citizen-note-description").innerHTML = "";
    if(el){
        var noteval = el.parentElement.parentElement.children[1].innerHTML.trim();
        document.querySelector("#citizen-note-description").innerHTML = noteval;
    }
    if(source == "patrol"){
        patrolBox.style.display = "block";
        document.querySelector("#patrol-selected-vehicle").value = "";
        document.querySelector("#patrol-display-number").value = "";
    }
    else if(source == "citizen-picture-edit"){
        citizenBg.style.display = "block";
        citizenPictureEdit.style.display = "block";
    }
    else if(source == "popup-citizen-wanted"){
        citizenBg.style.display = "block";
        citizenWanted.style.display = "block";
        return;
    }else if(source == "popup-citizen-description"){
        citizenBg.style.display = "block";
        citizenDescription.style.display = "block";
        return;
    }
    else if(source == "popup-citizen-note"){
        citizenBg.style.display = "block";
        citizenNote.style.display = "block";
        return;
    }else if(source == "popup-vehicle-wanted"){
        vehicleBg.style.display = "block";
        vehicleWanted.style.display = "block";
        return;
    }else if(source == "popup-vehicle-note"){
        vehicleBg.style.display = "block";
        vehicleNote.style.display = "block";
        return;
    }else if(source == "popup-vehicle-description"){
        vehicleBg.style.display = "block";
        vehicleDescription.style.display = "block";
        return;
    }else if(source == "popup-add-worker"){
        workerAddPopup.style.display = "block";
        document.querySelector("#popup-add-worker").style.display = "block";
        return;
    }else if(source == 'popup-worker-description'){
        workerAddPopup.style.display = "block";
        document.querySelector("#popup-worker-description").style.display = "block";
        return;
    }else if(source == 'popup-worker-level'){
        workerAddPopup.style.display = "block";
        document.querySelector("#popup-worker-level").style.display = "block";
        return;
    }
    else if(source == 'popup-worker-license'){
        workerAddPopup.style.display = "block";
        document.querySelector("#popup-worker-license").style.display = "block";
        return;
    }
    else if(source == 'popup-worker-crypto'){
        workerAddPopup.style.display = "block";
        document.querySelector("#popup-worker-crypto").style.display = "block";
        return;
    }
    else if(source == 'popup-worker-badge'){
        workerAddPopup.style.display = "block";
        document.querySelector("#popup-worker-badge").style.display = "block";
        return;
    }
    else if(source == 'discourse-picture-edit'){
        discourseBg.style.display = "block";
        discoursePictureEdit.style.display = "block";
    }
    else if(source == 'popup-announce-description'){
        this.document.getElementById("announcement-description").value = '';
        this.document.getElementById("tirex-expiry-days").value = '';
        announceBg.style.display = "block";
        announceDesc.style.display = "block";
    }
    else if(source == 'popup-judgement-import'){
        judgementBg.style.display = "block";
        judgementImport.style.display = "block";
    }
}
function closePopup(source){
    if(source == "patrol") { 
        return patrolBox.style.display = "none"; 
    }
    else if(source == "citizen-picture-edit") {
        citizenBg.style.display = "none";
        citizenPictureEdit.style.display = "none";
        return;
    }
    else if(source == "discourse-picture-edit") {
        discourseBg.style.display = "none";
        discoursePictureEdit.style.display = "none";
        return;
    }
    else if(source == "popup-citizen-wanted"){
        citizenBg.style.display = "none";
        citizenWanted.style.display = "none";
        return;
    }
    else if(source == "popup-citizen-description"){
        citizenBg.style.display = "none";
        citizenDescription.style.display = "none";
        return;
    }
    else if(source == "popup-citizen-note"){
        citizenBg.style.display = "none";
        citizenNote.style.display = "none";
        return;
    }else if(source == "popup-vehicle-wanted"){
        vehicleBg.style.display = "none";
        vehicleWanted.style.display = "none";
        return;
    }else if(source == "popup-vehicle-note"){
        vehicleBg.style.display = "none";
        vehicleNote.style.display = "none";
        return;
    }else if(source == "popup-vehicle-description"){
        vehicleBg.style.display = "none";
        vehicleDescription.style.display = "none";
        return;
    }else if(source == "popup-add-worker"){
        workerAddPopup.style.display = "none";
        document.querySelector("#popup-add-worker").style.display = "none";
        return;
    }else if(source == 'popup-worker-description'){
        workerAddPopup.style.display = "none";
        document.querySelector("#popup-worker-description").style.display = "none";
        return;
    }
    else if(source == 'popup-worker-level'){
        workerAddPopup.style.display = "none";
        document.querySelector("#popup-worker-level").style.display = "none";
        return;
    }
    else if(source == 'popup-worker-license'){
        workerAddPopup.style.display = "none";
        document.querySelector("#popup-worker-license").style.display = "none";
        return;
    }
    else if(source == 'popup-worker-crypto'){
        workerAddPopup.style.display = "none";
        document.querySelector("#popup-worker-crypto").style.display = "none";
        return;
    }
    else if(source == 'popup-worker-badge'){
        workerAddPopup.style.display = "none";
        document.querySelector("#popup-worker-badge").style.display = "none";
        return;
    }
    else if(source == 'popup-announce-description'){
        announceBg.style.display = "none";
        announceDesc.style.display = "none";
        return;
    }
    else if(source == 'popup-judgement-import'){
        judgementBg.style.display = "none";
        judgementImport.style.display = "none";
        return;
    }
}
function addFromPopup(source, data){
    if(source == "patrol"){
        patrolVeh = document.querySelector("#patrol-selected-vehicle").value;
        patrolNumber = document.querySelector("#patrol-display-number").value;
        let typedName = $('#tirex-search-patrol').val();
        let readyTypeName = typedName.replace(/\s+/g, '');
        let typedName2 = $('#tirex-gpt-dispatch-search').val();
        let readyTypeName2 = typedName2.replace(/\s+/g, '');
        $.post('http://ES-Gpt/GptAddData', JSON.stringify({active: active, type: 'create', patrolType: patrolVeh, patrolNumber: patrolNumber})).then(data => $.post('http://ES-Gpt/GptGetData', JSON.stringify({active: active, type: "search", type2: "search", name: readyTypeName, name2: readyTypeName2})).then(data => updateGptData(data) ));
    }
    else if(source == "citizen-picture-edit"){
        let newImage = document.querySelector("#citizen-picture-url").value;
        $.post('http://ES-Gpt/GptAddData', JSON.stringify({active: active, type: 'newImage', user:data, image: newImage})).then(data => showCitizen(data));
    }
    else if(source == "popup-citizen-wanted"){
        let ssn = data;
        let caseId = $('#citizen-wanted-number').val();
        let content = $('#citizen-wanted-description').val();
        let days = $('#citizen-wanted-duration').val();
        if (!caseId) {
            caseId = 'Brak';
        } 
        if (!content) {
            content = 'Brak';
        }
        if (!days) {
            days = 0;
        }
        $.post('http://ES-Gpt/GptAddData', JSON.stringify({active: active, type: 'addWanted', user: ssn, content: content, case: caseId, expiry: days})).then(data => $.post('http://ES-Gpt/GptGetData', JSON.stringify({active: active, type:"citizen", user: ssn})).then(data => setupCitizenInfo(data) ) );
    }
    else if(source == "popup-citizen-note"){
        citizenNoteAction('add', data, 'none');
    }else if (source == "discourse-picture-edit") {
        let newImage = document.querySelector("#discourse-picture-url").value;
        discourseAddElement(newImage, 'none', data);
    }else if(source == "popup-add-worker"){
        let playerId = $('#tirex-workers-add-worker-id').val();
        let newJobGrade = this.document.getElementById("tirex-workers-add-worker-grade-list").value;
        let jjbbasd = newJobGrade.split("-");
        $.post('http://ES-Gpt/GptAddData', JSON.stringify({active: active, type: 'addWorker', player: playerId, newJob: jjbbasd[0], newGrade: jjbbasd[1]})).then(data => $.post('http://ES-Gpt/GptGetData', JSON.stringify({active: active})).then(data => updateGptData(data)) );
    }else if(source == 'popup-worker-description'){

    }else if(source == "popup-vehicle-wanted"){
        let vin = data;
        let caseId = $('#vehicle-wanted-number').val();
        let content = $('#tirex-vehicle-wanted-description').val();
        let days = $('#vehicle-wanted-duration').val();
        if (!caseId) {
            caseId = 'Brak';
        } 
        if (!content) {
            content = 'Brak';
        }
        if (!days) {
            days = 0;
        }
        $.post('http://ES-Gpt/GptAddData', JSON.stringify({active: active, type: 'addWanted', user: vin, content: content, case: caseId, expiry: days})).then(data => $.post('http://ES-Gpt/GptGetData', JSON.stringify({active: active, type:"vehicle", vehicle: vin})).then(data => setupVehicleInfo(data) ) );
    }else if(source == "popup-vehicle-note"){
        vehicleNoteAction('add', data, 'none');
    }else if(source == "popup-vehicle-description"){

    }

    else if (source == "popup-judgement-import") {
        let caseId = this.document.getElementById("judgement-imported-id").value;
        let time = 0;
        let bill = 0;
        
        this.document.getElementById("tirex-judgement-menu-time").value = time;
        this.document.getElementById("tirex-judgement-menu-bill").value = bill;
        this.document.getElementById("tirex-judgement-menu-case").value = '';
        this.document.getElementById("tirex-tickets-block-list").innerHTML = `<span class="discourse-person-button">Brak</span>`;

        $.post('http://ES-Gpt/GptGetData', JSON.stringify({active: active, type:"getCaseJudgement", case: caseId})).then(newData => {
            if (newData.length > 0) {
                let ticketsList = '';
                judgementsTicketList = [];
                for(let i in newData) {
                    let data = newData[i];

                    for(let j in judgementTicketList) {
                        let data2 = judgementTicketList[j];
                        if (data2.categoryName == data.category) {
                            for(let k in data2.data) {
                                let data3 = data2.data[k];
                                if (data3.name == data.name) {
                                    for(let l = 0; l < data.count; l++) {
                                        time = time + data3.time;
                                        bill = bill + data3.bill;
                                    }
                                }
                            }
                        }
                    }

                    ticketsList = ticketsList + `<span class="discourse-person-button">[ `+data.category+` ] `+data.name+` x`+data.count+`</span>`;
                    judgementsTicketList.push({category: data.category, name: data.name, count: data.count});
                }
                this.document.getElementById("tirex-tickets-block-list").innerHTML = ticketsList;
                this.document.getElementById("tirex-judgement-menu-case").value = caseId;
                this.document.getElementById("tirex-judgement-menu-time").value = time;
                this.document.getElementById("tirex-judgement-menu-bill").value = bill;
            }
        });
    }

    else if (source == "popup-announce-description") {
        let content = this.document.getElementById("announcement-description").value;
        let days = this.document.getElementById("tirex-expiry-days").value;
        if (!days) {
            days = 0;
        }
        $.post('http://ES-Gpt/GptAddData', JSON.stringify({active: active, type: 'addAnnounce', content: content, days: days})).then(data => $.post('http://ES-Gpt/GptGetData', JSON.stringify({active: active})).then(data => annoucementsAddList(data) ) );
    }

    else if (source == "popup-worker-level") {
        let newJobGrade = this.document.getElementById("tirex-workers-change-worker-grade").value;
        let jjbbasd = newJobGrade.split("-");
        let workerSsn = data;
        $.post('http://ES-Gpt/GptAddData', JSON.stringify({active: active, type: 'changeGrade', player: workerSsn, newGrade: jjbbasd[1]})).then(data => $.post('http://ES-Gpt/GptGetData', JSON.stringify({active: active, type:"worker", user: workerSsn})).then(data => setupWorkerInfo(data) ) );
    }
    else if (source == "popup-worker-license") {
        let license = this.document.getElementById("tirex-workers-change-worker-license").value;
        let workerSsn = data;
        $.post('http://ES-Gpt/GptAddData', JSON.stringify({active: active, type: 'changeLicense', player: workerSsn, license: license})).then(data => $.post('http://ES-Gpt/GptGetData', JSON.stringify({active: active, type:"worker", user: workerSsn})).then(data => setupWorkerInfo(data) ) );
    }
    else if (source == "popup-worker-crypto") {
        let crypto = this.document.getElementById("tirex-workers-change-worker-crypto").value;
        let workerSsn = data;
        print(crypto + '' + workerSsn)
        $.post('http://ES-Gpt/GptAddData', JSON.stringify({active: active, type: 'changeCrypto', player: workerSsn, crypto: crypto})).then(data => $.post('http://ES-Gpt/GptGetData', JSON.stringify({active: active, type:"worker", user: workerSsn})).then(data => setupWorkerInfo(data) ) );
    }else if (source == "popup-worker-badge") {
        let newBadge = $('#tirex-workers-new-badge').val();
        let workerSsn = data;
        $.post('http://ES-Gpt/GptAddData', JSON.stringify({active: active, type: 'changeBadge', player: workerSsn, newBadge: newBadge})).then(data => $.post('http://ES-Gpt/GptGetData', JSON.stringify({active: active, type:"worker", user: workerSsn})).then(data => setupWorkerInfo(data) ) );
    }else if(source == "popup-add-car"){
        let playerId = $('#tirex-workers-add-car-ssn').val();
        let newJobGrade = this.document.getElementById("tirex-workers-add-worker-grade-list").value;
        let jjbbasd = newJobGrade.split("-");
        $.post('http://ES-Gpt/GptAddData', JSON.stringify({active: active, type: 'addWorker', player: playerId, newJob: jjbbasd[0], newGrade: jjbbasd[1]})).then(data => $.post('http://ES-Gpt/GptGetData', JSON.stringify({active: active})).then(data => updateGptData(data)) );
    }
    return closePopup(source);
}

function menagePatrol(actionType, patrolId){
    let typedName = $('#tirex-search-patrol').val();
    let readyTypeName = typedName.replace(/\s+/g, '');
    let typedName2 = $('#tirex-gpt-dispatch-search').val();
    let readyTypeName2 = typedName2.replace(/\s+/g, '');
    if (actionType == "kick") {
        $.post('http://ES-Gpt/GptAddData', JSON.stringify({active: active, type: actionType, player: ssn})).then(data => $.post('http://ES-Gpt/GptGetData', JSON.stringify({active: active, type: "search", type2: "search", name: readyTypeName, name2: readyTypeName2})).then(data => updateGptData(data) ));
    } else {
        $.post('http://ES-Gpt/GptAddData', JSON.stringify({active: active, type: actionType, id: patrolId})).then(data => $.post('http://ES-Gpt/GptGetData', JSON.stringify({active: active, type: "search", type2: "search", name: readyTypeName, name2: readyTypeName2})).then(data => updateGptData(data) ));
    }
}

function annoucementDelete(el){
    el.parentElement.remove();
}

function turnSection(source){
    document.querySelector("#"+active).style.display = "none";
    document.querySelector("#"+source).style.display = "block";
    active = source;
    $.post('http://ES-Gpt/GptGetData', JSON.stringify({active: active})).then(data => updateGptData(data));
    return;
}

function showCase(caseId) {
    turnSection('discourse');
    showDiscourse(caseId);
}
function deleteCitizenWanted(id, ssn) {
    $.post('http://ES-Gpt/GptAddData', JSON.stringify({active: active, type: 'deleteWanted', id: id})).then(data => $.post('http://ES-Gpt/GptGetData', JSON.stringify({active: active, type:"citizen", user: ssn})).then(data => setupCitizenInfo(data) ) );
}
function citizenNoteAction(action, ssn, id) {
    if (action == "add") {
        let content = $('#citizen-note-description').val();
        const important = document.querySelector("#pill1-citizen");
        $.post('http://ES-Gpt/GptAddData', JSON.stringify({active: active, type: 'addNote', user: ssn, content: content, important: important.checked})).then(data => $.post('http://ES-Gpt/GptGetData', JSON.stringify({active: active, type:"citizen", user: ssn})).then(data => setupCitizenInfo(data) ) );
    } else if (action == "openAdd") {
        this.document.getElementById("popup-citizen-note").innerHTML = `<p>Dodaj notatkę</p><div class="toggle-pill"><input type="checkbox" id="pill1-citizen" name="check"><label for="pill1-citizen"></label></div><span class="toggle-pill-label">Ważne</span><textarea id="citizen-note-description" placeholder="Wpisz treść notatki"></textarea><input type="button" onclick="closePopup('popup-citizen-note')" class="popup-button popup-cancel" style="--width:calc(50% - 5px);" value="Anuluj"><input type="button" onclick="addFromPopup('popup-citizen-note', '`+ssn+`')" class="popup-button popup-confirm" style="--width:calc(50% - 5px);--margin:0px 0px 0px 10px;"  value="Potwierdź">`;
        openPopup('popup-citizen-note');
    } else if (action == "edit") {
        closePopup('popup-citizen-note');
        let content = $('#citizen-note-description').val();
        $.post('http://ES-Gpt/GptAddData', JSON.stringify({active: active, type: 'editNote', id: id, content: content})).then(data => $.post('http://ES-Gpt/GptGetData', JSON.stringify({active: active, type:"citizen", user: ssn})).then(data => setupCitizenInfo(data) ) );
    } else if (action == "openEdit") {
        $.post('http://ES-Gpt/GptGetData', JSON.stringify({active: active, type:"getNote", id: id})).then(data => {
            this.document.getElementById("popup-citizen-note").innerHTML = `<p>Edytuj notatkę</p><textarea id="citizen-note-description" placeholder="Wpisz treść notatki"></textarea><input type="button" onclick="closePopup('popup-citizen-note')" class="popup-button popup-cancel" style="--width:calc(50% - 5px);" value="Anuluj"><input type="button" onclick="citizenNoteAction('edit', '`+data.ssn+`', '`+data.id+`')" class="popup-button popup-confirm" style="--width:calc(50% - 5px);--margin:0px 0px 0px 10px;"  value="Potwierdź">`;
            this.document.getElementById("citizen-note-description").value = data.content;
            openPopup('popup-citizen-note');
        });
    } else if (action == "delete") {
        $.post('http://ES-Gpt/GptAddData', JSON.stringify({active: active, type: 'deleteNote', id: id})).then(data => $.post('http://ES-Gpt/GptGetData', JSON.stringify({active: active, type:"citizen", user: ssn})).then(data => setupCitizenInfo(data) ) );
    }
}
function setupCitizenInfo(newData) {
    let data = newData;
    let name = data.firstname+' '+data.lastname;
    let birth = data.dateofbirth;
    let ssn = data.ssn;
    let phoneNumber = data.phoneNumber
    let image = 'public/img/citizen-picture-none.png';
    if (data.image && data.image.length > 0) {
        image = data.image;
    }

    let headerInfo = `<div class="item" style="--width:256px; --height:256px; background-color:var(--itemcolor); background: url(`+image+`); background-size: 100% 100%; --margin: 25px;">
                  <p class="citizen-picture-edit" onclick="openPopup('citizen-picture-edit')"><i class="fa-light fa-pen"></i></p>
                </div>
                <div id="citizen-personal-info" class="item" style="background-color:transparent; --width: calc(100% - 256px - 125px - 10px); --height: 246px; --margin: 25px 0px 0px 50px; --padding:10px;">
                  <span>Imię i nazwisko</span>
                  <p>`+name+`</p>
                  <hr/>
                  <span>Data urodzenia</span>
                  <p>`+birth+`</p>
                  <hr/>
                  <span>Numer telefonu</span>
                  <p>`+phoneNumber+`</p>
                  <hr/>
                  <span>Social Security Number</span>
                  <p>`+ssn+`</p> 
                  <hr/>
                </div>`;

    let wantedInfo = '';
    let historyInfo = '';
    let notesInfo = '';
    let vehicleInfo = '';

    if (data.wanted.length > 0) {
        for (let i in data.wanted) {
            let data2 = data.wanted[i];
            let wantedId = data2.wantedId;
            let content = data2.content;
            let caseId = data2.case;
            let author = data2.author;
            let expiry_date = data2.expiry_date;
            
            if (caseId == "Brak") {
                wantedInfo = wantedInfo + `<div class="item citizen-wanted-box"  style="--width: calc(100% - 100px); --height: auto; background-color:var(--veryred); --margin: 25px 25px; --padding: 0px 25px 15px 25px;"><div id="header"><i style="margin:0px 10px 0px 0px;" class="fa-light fa-calendar"></i> Wygasa: <span>00:00 `+expiry_date+`</span><span style="float:right;">`+author+`</span></div><div id="note">`+content+`</div><div id="footer"><span id="info">`+caseId+`</span><span id="button" onclick="deleteCitizenWanted('`+wantedId+`', '`+ssn+`')"><i class="fa-light fa-trash"></i></span></div></div>`;
            } else {
                wantedInfo = wantedInfo + `<div class="item citizen-wanted-box"  style="--width: calc(100% - 100px); --height: auto; background-color:var(--veryred); --margin: 25px 25px; --padding: 0px 25px 15px 25px;"><div id="header"><i style="margin:0px 10px 0px 0px;" class="fa-light fa-calendar"></i> Wygasa: <span>00:00 `+expiry_date+`</span><span style="float:right;">`+author+`</span></div><div id="note">`+content+`</div><div id="footer"><span class="onlyCursorHover" id="info" onclick="showCase('`+caseId+`')">#`+caseId+`</span><span id="button" onclick="deleteCitizenWanted('`+wantedId+`', '`+ssn+`')"><i class="fa-light fa-trash"></i></span></div></div>`;
            }
        }
        $('#tirex-citizen-status-nowanted').css('display', 'none');
        $('#tirex-citizen-status-wanted').css('display', 'block');
    } else {
        $('#tirex-citizen-status-wanted').css('display', 'none');
        $('#tirex-citizen-status-nowanted').css('display', 'block');
    }

    if (data.vehicles.length > 0) {
        for (let i in data.vehicles) {
            let data2 = data.vehicles[i];
            let model = data2.model;
            let vin = data2.vin;
            let plates = data2.plates;
            let wantedArray = data2.wanted;

            if (wantedArray.length > 0) {
                vehicleInfo = vehicleInfo + `<div class="item person-car" style="--width: calc(100% - 100px); --height: 200px; background-color: var(--itemcolor); --margin: 0px 25px; --padding: 0px 25px;"><p>`+model+`</p><hr/><span>Numer VIN: <strong>`+vin+`</strong></span><hr/><span>Tablica rejestracyjna: <strong> `+plates+`</strong></span><hr/><span>Status:`;
                for(let j in wantedArray) {
                    let wantedId = wantedArray[j];
                    vehicleInfo = vehicleInfo + `<span class="citizen-button citizen-button-status" onclick="showVehicle('`+vin+`', 'vehicles')" style="background-color: rgb(150,0,0) !important">Poszukiwanie #`+wantedId+`</span>`;
                }
                vehicleInfo = vehicleInfo + `</span></div>`;
            } else {
                vehicleInfo = vehicleInfo + `<div class="item person-car" style="--width: calc(100% - 100px); --height: 200px; background-color: var(--itemcolor); --margin: 0px 25px; --padding: 0px 25px;"><p>`+model+`</p><hr/><span>Numer VIN: <strong>`+vin+`</strong></span><hr/><span>Tablica rejestracyjna: <strong> `+plates+`</strong></span><hr/><span>Status: <span class="citizen-button citizen-button-status">Brak Poszukiwań</span></span></div>`;
            }
        }
        $('.tirex-citizen-cars-hide').css('display', 'none');
        $('.tirex-citizen-cars-block').css('display', 'block');
    } else {
        $('.tirex-citizen-cars-block').css('display', 'none');
        $('.tirex-citizen-cars-hide').css('display', 'block');
    }

    if (data.history.length > 0) {
        for (let i in data.history) {
            let data2 = data.history[i];
            let id = data2.id
            let bill = data2.bill;
            let time = data2.time;
            let createTime = data2.create_time;
            let createDate = data2.create_date
            let author = data2.author;
            let reason = data2.reason;
            historyInfo = historyInfo + `<div class="citizen-ticket-box"><div><span style="float:left;">#`+id+`</span><span style="float:right;">`+author+`</span></div><br/><div style="margin: 5px 0px;"><i style="margin:0px 10px 0px 0px;" class="fa-light fa-calendar"></i>`+createTime+` `+createDate+`&nbsp;&nbsp;$`+bill+` | `+time+` msc.</div>`;
            for (let j in reason) {
                let data3 = reason[j];
                historyInfo = historyInfo + `<div class="citizen-button citizen-button-status" style="margin:0; cursor: default !important;">[ `+data3.category+` ] `+data3.name+` x`+data3.count+`</div>`;
            }
            historyInfo = historyInfo + `</div>`;
        }
        if (data.history.length > 3) {
            $('#tirex-citizen-history-button').css('display', 'block');
        } else {
            $('#tirex-citizen-history-button').css('display', 'none');
        }
    } else {
        $('#tirex-citizen-history-button').css('display', 'none');
    }

    if (data.notes.length > 0) {
        for (let i in data.notes) {
            let data2 = data.notes[i];
            let id = data2.id;
            let important = data2.important;
            let content = data2.content;
            let createTime = data2.create_time;
            let createDate = data2.create_date;
            let author = data2.author;
            if (important == "true") {
                notesInfo = notesInfo + `<div class="item citizen-wanted-box"  style="--width: calc(100% - 100px); --height: auto; background-color:var(--veryred); --margin: 10px 25px; --padding: 0px 25px 15px 25px;"><div id="header"><i style="margin:0px 10px 0px 0px;" class="fa-light fa-calendar"></i><span>`+createTime+` `+createDate+`</span><span style="float:right;">`+author+`</span></div><div id="note">`+content+`</div><div id="footer"><span id="del-note" class="citizen-button citizen-button-status" style="margin:0;" onclick="citizenNoteAction('delete', '`+ssn+`', '`+id+`')">Usuń</span><span id="edit-note" class="citizen-button citizen-button-status" onclick="citizenNoteAction('openEdit', '`+ssn+`', '`+id+`')">Edytuj notatkę</span></div></div>`;
            } else {
                notesInfo = notesInfo + `<div class="item citizen-wanted-box"  style="--width: calc(100% - 100px); --height: auto; background-color:var(--itemcolor); --margin: 10px 25px; --padding: 0px 25px 15px 25px;"><div id="header"><i style="margin:0px 10px 0px 0px;" class="fa-light fa-calendar"></i><span>`+createTime+` `+createDate+`</span><span style="float:right;">`+author+`</span></div><div id="note">`+content+`</div><div id="footer"><span id="del-note" class="citizen-button citizen-button-status" style="margin:0;" onclick="citizenNoteAction('delete', '`+ssn+`', '`+id+`')">Usuń</span><span id="edit-note" class="citizen-button citizen-button-status" onclick="citizenNoteAction('openEdit', '`+ssn+`', '`+id+`')">Edytuj notatkę</span></div></div>`;
            }
        }
        if (data.notes.length > 1) {
            $('#tirex-citizen-notes-button').css('display', 'block');
        } else {
            $('#tirex-citizen-notes-button').css('display', 'none');
        }
    } else {
        $('#tirex-citizen-notes-button').css('display', 'none');
    }
    

    
    this.document.getElementById("tirex-citizen-info-header").innerHTML = headerInfo; 
    this.document.getElementById("tirex-citizen-status-wanted").innerHTML = wantedInfo;
    this.document.getElementById("vehicle-list").innerHTML = vehicleInfo;
    this.document.getElementById("citizen-ticket-list").innerHTML = historyInfo;
    this.document.getElementById("citizen-note-list").innerHTML = notesInfo;
    this.document.querySelector(".citizen-header").innerHTML = "<span id='citizen-previous' onclick='hideCitizen(this)'><i class='fa-solid fa-angle-left'></i></span>Obywatel: <span>"+name+"</span><button onclick=\"addUserToJudgements('"+ssn+"', '"+name+"')\"><i class='fa-regular fa-circle-plus'></i>&nbsp;&nbsp;&nbsp;Dodaj wyrok</button>"
    this.document.getElementById("citizen-settings").innerHTML = `<br/><span class="citizen-button citizen-button-status" onclick="openPopup('popup-citizen-wanted')">Dodaj poszukiwanie</span><!-- <span class="citizen-button citizen-button-status" onclick="openPopup('popup-citizen-description')" style="margin-left:0;">Edytuj komentarz</span> --><span class="citizen-button citizen-button-status" onclick="citizenNoteAction('openAdd', '`+ssn+`')" style="margin-left:0;">Dodaj notatkę</span><!-- <span class="citizen-button citizen-button-status" onclick="turnSection('judgement')" style="margin-left:0;">Dodaj wyrok</span> --><br/><br/>`;

    this.document.getElementById("popup-citizen-wanted").innerHTML = `<p>Dodaj nowe poszukiwanie</p><input type="number" id="citizen-wanted-number" placeholder="Podaj numer sprawy (nieobowiązkowe)"><textarea id="citizen-wanted-description" placeholder="Treść"></textarea><input type="number" id="citizen-wanted-duration" placeholder="Ilość dni poszukiwania"><input type="button" onclick="closePopup('popup-citizen-wanted')" class="popup-button popup-cancel" style="--width:calc(50% - 5px);" value="Anuluj"><input type="button" onclick="addFromPopup('popup-citizen-wanted', '`+ssn+`')" class="popup-button popup-confirm" style="--width:calc(50% - 5px);--margin:0px 0px 0px 10px;"  value="Potwierdź">`;
    this.document.getElementById("popup-citizen-note").innerHTML = `<p>Dodaj notatkę</p><div class="toggle-pill"><input type="checkbox" id="pill1-citizen" name="check"><label for="pill1-citizen"></label></div><span class="toggle-pill-label">Ważne</span><textarea id="citizen-note-description" placeholder="Wpisz treść notatki"></textarea><input type="button" onclick="closePopup('popup-citizen-note')" class="popup-button popup-cancel" style="--width:calc(50% - 5px);" value="Anuluj"><input type="button" onclick="addFromPopup('popup-citizen-note', '`+ssn+`')" class="popup-button popup-confirm" style="--width:calc(50% - 5px);--margin:0px 0px 0px 10px;"  value="Potwierdź">`;
    
    return;
}

function deleteVehicleWanted(id, vin) {
    $.post('http://ES-Gpt/GptAddData', JSON.stringify({active: active, type: 'deleteWanted', id: id})).then(data => $.post('http://ES-Gpt/GptGetData', JSON.stringify({active: active, type:"vehicle", vehicle: vin})).then(data => setupVehicleInfo(data) ));
}
function vehicleNoteAction(action, vin, id) {
    if (action == "add") {
        let content = $('#vehicle-note-description').val();
        const important = document.querySelector("#pill1-vehicle");
        $.post('http://ES-Gpt/GptAddData', JSON.stringify({active: active, type: 'addNote', vin: vin, important: important.checked, content: content})).then(data => $.post('http://ES-Gpt/GptGetData', JSON.stringify({active: active, type:"vehicle", vehicle: vin})).then(data => setupVehicleInfo(data) ));
    } else if (action == "openAdd") {
        this.document.getElementById("popup-vehicle-note").innerHTML = `<p>Dodaj notatkę</p><div class="toggle-pill"><input type="checkbox" id="pill1-vehicle" name="check"><label for="pill1-vehicle"></label></div><span class="toggle-pill-label">Ważne</span><textarea id="vehicle-note-description" placeholder="Wpisz treść notatki"></textarea><input type="button" onclick="closePopup('popup-vehicle-note')" class="popup-button popup-cancel" style="--width:calc(50% - 5px);" value="Anuluj"><input type="button" onclick="addFromPopup('popup-vehicle-note', '`+vin+`')" class="popup-button popup-confirm" style="--width:calc(50% - 5px);--margin:0px 0px 0px 10px;"  value="Potwierdź">`;
        openPopup('popup-vehicle-note');
    } else if (action == "edit") {
        closePopup('popup-vehicle-note');
        let content = $('#vehicle-note-description').val();
        $.post('http://ES-Gpt/GptAddData', JSON.stringify({active: active, type: 'editNote', id: id, content: content})).then(data => $.post('http://ES-Gpt/GptGetData', JSON.stringify({active: active, type:"vehicle", vehicle: vin})).then(data => setupVehicleInfo(data) ) );
    } else if (action == "openEdit") {
        $.post('http://ES-Gpt/GptGetData', JSON.stringify({active: active, type:"getNote", id: id})).then(data => {
            this.document.getElementById("popup-vehicle-note").innerHTML = `<p>Edytuj Notatkę</p><textarea id="vehicle-note-description" placeholder="Wpisz treść notatki"></textarea><input type="button" onclick="closePopup('popup-vehicle-note')" class="popup-button popup-cancel" style="--width:calc(50% - 5px);" value="Anuluj"><input type="button" onclick="vehicleNoteAction('edit', '`+data.vin+`', '`+data.id+`')" class="popup-button popup-confirm" style="--width:calc(50% - 5px);--margin:0px 0px 0px 10px;"  value="Potwierdź">`;
            this.document.getElementById("vehicle-note-description").value = data.content;
            openPopup('popup-vehicle-note');
        });
    } else if (action == "delete") {
        $.post('http://ES-Gpt/GptAddData', JSON.stringify({active: active, type: 'deleteNote', id: id})).then(data => $.post('http://ES-Gpt/GptGetData', JSON.stringify({active: active, type:"vehicle", vehicle: vin})).then(data => setupVehicleInfo(data) ));
    }
}
function setupVehicleInfo(newData) {
    let data = newData[0];
    let model = data.model;
    let vin = data.vin;
    let plates = data.plate;
    let owner = data.ssn;
    let coowner = data.cossn;
    let ownerName = data.ownerName;
    let coownerName = data.coownerName;

    let headerInfo = '';
    let wantedInfo = '';
    let notesInfo = '';

    if (coowner && coownerName) {
        headerInfo = headerInfo + `<div id="left"><span>Model</span><p>`+model+`</p><hr/><span>Właściciel</span><p><span class="citizen-button citizen-button-status" onclick="showCitizen('`+owner+`', 'citizens')" style="margin-left:0;">`+ownerName+`</span></p><hr/><span>Współwłaściciel</span><p><span class="citizen-button citizen-button-status" onclick="showCitizen('`+coowner+`', 'citizens')" style="margin-left:0;">`+coownerName+`</span></p><hr/></div>`;
    } else {
        headerInfo = headerInfo + `<div id="left"><span>Model</span><p>`+model+`</p><hr/><span>Właściciel</span><p><span class="citizen-button citizen-button-status" onclick="showCitizen('`+owner+`', 'citizens')" style="margin-left:0;">`+ownerName+`</span></p><hr/><span>Współwłaściciel</span><p><span class="citizen-button citizen-button-status" style="margin-left:0; cursor:default !important;">Brak</span></p><hr/></div>`;
    }
    headerInfo = headerInfo + `<div id="right"><span>Numer VIN</span><p>`+vin+`</p><hr/><span>Rejestracja</span><p>`+plates+`</p><hr/></div>`;

    if (data.wanted.length > 0) {
        for (let i in data.wanted) {
            let data2 = data.wanted[i];
            let wantedId = data2.wantedId;
            let content = data2.content;
            let caseId = data2.case;
            let author = data2.author;
            let expiry_date = data2.expiry_date;

            if (caseId == "Brak") {
                wantedInfo = wantedInfo + `<div class="item citizen-wanted-box"  style="--width: calc(100% - 100px); --height: auto; background-color:var(--veryred); --margin: 25px 25px; --padding: 0px 25px 15px 25px;"><div id="header"><i style="margin:0px 10px 0px 0px;" class="fa-light fa-calendar"></i> Wygasa: <span>00:00 `+expiry_date+`</span><span style="float:right;">`+author+`</span></div><div id="note">`+content+`</div><div id="footer"><span id="info">`+caseId+`</span><span id="button" onclick="deleteVehicleWanted('`+wantedId+`', '`+vin+`')"><i class="fa-light fa-trash"></i></span></div></div>`;
            } else {
                wantedInfo = wantedInfo + `<div class="item citizen-wanted-box"  style="--width: calc(100% - 100px); --height: auto; background-color:var(--veryred); --margin: 25px 25px; --padding: 0px 25px 15px 25px;"><div id="header"><i style="margin:0px 10px 0px 0px;" class="fa-light fa-calendar"></i> Wygasa: <span>00:00 `+expiry_date+`</span><span style="float:right;">`+author+`</span></div><div id="note">`+content+`</div><div class="onlyCursorHover" id="footer" onclick="showCase('`+caseId+`')"><span id="info">#`+caseId+`</span><span id="button" onclick="deleteVehicleWanted('`+wantedId+`', '`+vin+`')"><i class="fa-light fa-trash"></i></span></div></div>`;
            }
        }
        $('#tirex-vehicle-status-nowanted').css('display', 'none');
        $('#tirex-vehicle-status-wanted').css('display', 'block');
    } else {
        $('#tirex-vehicle-status-wanted').css('display', 'none');
        $('#tirex-vehicle-status-nowanted').css('display', 'block');
    }

    if (data.notes.length > 0) {
        for (let i in data.notes) {
            let data2 = data.notes[i];
            let id = data2.id;
            let important = data2.important;
            let content = data2.content;
            let createTime = data2.create_time;
            let createDate = data2.create_date;
            let author = data2.author;
            if (important == "true") {
                notesInfo = notesInfo + `<div class="item citizen-wanted-box"  style="--width: calc(100% - 100px); --height: auto; background-color:var(--veryred); --margin: 10px 25px; --padding: 0px 25px 15px 25px;"><div id="header"><i style="margin:0px 10px 0px 0px;" class="fa-light fa-calendar"></i><span>`+createTime+` `+createDate+`</span><span style="float:right;">`+author+`</span></div><div id="note">`+content+`</div><div id="footer"><span id="del-note" class="citizen-button citizen-button-status" style="margin:0;" onclick="vehicleNoteAction('delete', '`+vin+`', '`+id+`')">Usuń</span><span id="edit-note" class="citizen-button citizen-button-status" onclick="vehicleNoteAction('openEdit', '`+vin+`', '`+id+`')">Edytuj notatkę</span></div></div>`;
            } else {
                notesInfo = notesInfo + `<div class="item citizen-wanted-box"  style="--width: calc(100% - 100px); --height: auto; background-color:var(--itemcolor); --margin: 10px 25px; --padding: 0px 25px 15px 25px;"><div id="header"><i style="margin:0px 10px 0px 0px;" class="fa-light fa-calendar"></i><span>`+createTime+` `+createDate+`</span><span style="float:right;">`+author+`</span></div><div id="note">`+content+`</div><div id="footer"><span id="del-note" class="citizen-button citizen-button-status" style="margin:0;" onclick="vehicleNoteAction('delete', '`+vin+`', '`+id+`')">Usuń</span><span id="edit-note" class="citizen-button citizen-button-status" onclick="vehicleNoteAction('openEdit', '`+vin+`', '`+id+`')">Edytuj notatkę</span></div></div>`;
            }
        }
        if (data.notes.length > 1) {
            $('#tirex-vehicle-notes-button').css('display', 'block');
        } else {
            $('#tirex-vehicle-notes-button').css('display', 'none');
        }
    } else {
        $('#tirex-vehicle-notes-button').css('display', 'none');
    }

    this.document.getElementById("tirex-vehicle-status-wanted").innerHTML = wantedInfo;
    this.document.getElementById("vehicle-note-list").innerHTML = notesInfo;

    this.document.getElementById("tirex-vehicle-header").innerHTML = headerInfo;

    this.document.getElementById("vehicle-settings").innerHTML = `<br/><span class="citizen-button citizen-button-status" style="margin-left:0;" onclick="openPopup('popup-vehicle-wanted')">Dodaj poszukiwanie</span><span class="citizen-button citizen-button-status" onclick="vehicleNoteAction('openAdd', '`+vin+`')" style="margin-left:0;">Dodaj notatkę</span><br/><br/>`;
    this.document.getElementById("popup-vehicle-wanted").innerHTML = `<p>Dodaj nowe poszukiwanie</p><input type="text" id="vehicle-wanted-number" placeholder="Podaj numer sprawy (nieobowiązkowe)"><textarea id="tirex-vehicle-wanted-description" placeholder="Treść"></textarea><input type="number" id="vehicle-wanted-duration" placeholder="Ilość dni poszukiwania"><input type="button" onclick="closePopup('popup-vehicle-wanted')" class="popup-button popup-cancel" style="--width:calc(50% - 5px);" value="Anuluj"><input type="button" onclick="addFromPopup('popup-vehicle-wanted', '`+vin+`')" class="popup-button popup-confirm" style="--width:calc(50% - 5px);--margin:0px 0px 0px 10px;"  value="Potwierdź">`;
    return;
}
function showCitizen(data, data2){
    this.document.getElementById("tirex-search-citizen").value = '';
    if (data2) {
        turnSection(data2);
        $.post('http://ES-Gpt/GptGetData', JSON.stringify({active: active, type:"citizen", user: data})).then(data => setupCitizenInfo(data) );
        this.document.getElementById("popup-citizen-picture-edit").innerHTML = `<p>Podaj link URL do zdjęcia</p><input type="text" id="citizen-picture-url"><input type="button" onclick="closePopup('citizen-picture-edit')" class="popup-button popup-cancel" style="--width:calc(50% - 5px);" value="Anuluj"><input type="button" onclick="addFromPopup('citizen-picture-edit', '`+data+`')" class="popup-button popup-confirm" style="--width:calc(50% - 5px);--margin:0px 0px 0px 10px;"  value="Potwierdź">`;
        document.querySelector("#citizen-search-content").style.display = "none";
        document.querySelector("#citizen-more-info").style.display = "block";
    } else {
        $.post('http://ES-Gpt/GptGetData', JSON.stringify({active: active, type:"citizen", user: data})).then(data => setupCitizenInfo(data) );
        this.document.getElementById("popup-citizen-picture-edit").innerHTML = `<p>Podaj link URL do zdjęcia</p><input type="text" id="citizen-picture-url"><input type="button" onclick="closePopup('citizen-picture-edit')" class="popup-button popup-cancel" style="--width:calc(50% - 5px);" value="Anuluj"><input type="button" onclick="addFromPopup('citizen-picture-edit', '`+data+`')" class="popup-button popup-confirm" style="--width:calc(50% - 5px);--margin:0px 0px 0px 10px;"  value="Potwierdź">`;
        document.querySelector("#citizen-search-content").style.display = "none";
        document.querySelector("#citizen-more-info").style.display = "block";
    }
}

function hideCitizen(source){
    document.querySelector("#citizen-search-content").style.display = "block";
    document.querySelector("#citizen-more-info").style.display = "none";
    document.querySelector(".citizen-header").innerHTML = headerNameAndTime;
}



function setupWorkerInfo(newData) {
    let allWorkerInfo = '';
    let data = newData;
    let name = data.firstname+' '+data.lastname;
    let birth = data.dateofbirth;
    let socialNumber = data.ssn;
    let crypto = data.crypto;
    let grade = data.job_grade;
    let badge = data.badge;
    let dutyTime = data.dutyTime;
    let licensList = data.licens;
    let licens = "";
    if (licensList.length > 0) {
        for (let i in licensList) {
            let dataLic = licensList[i];
            let name = dataLic.name;

            if (i == 0) {
                licens = licens + name;
            } else {
                licens = licens + ', ' + name;
            }
        }
    } else {
        licens = "Brak";
    }

    // informacje o typie
    allWorkerInfo = allWorkerInfo + '<div id="left"><span>Imię i nazwisko</span><p>'+name+'</p><hr/><span>Jednostka</span><p>'+crypto+'</p><hr/><span>Stopień</span><p>'+grade+'</p><hr/><span>Czas na służbie</span><p>'+dutyTime+'</p><hr/><span>Licencje</span><p>'+licens+'</p></div>';
    allWorkerInfo = allWorkerInfo + '<div id="right"><span>Data urodzenia</span><p>'+birth+'</p><hr/><span>Numer SSN</span><p>'+socialNumber+'</p><hr/><span>Numer Odznaki</span><p>'+badge+'</p><hr/></div>';

    // komentarz
    allWorkerInfo = allWorkerInfo + '<hr/><br/>';

    // przyciski
    allWorkerInfo = allWorkerInfo + `<span class="citizen-button citizen-button-status" style="margin-left:0;" onclick="openPopup('popup-worker-level')">Zmień stopień</span><span class="citizen-button citizen-button-status" style="margin-left:0;" onclick="openPopup('popup-worker-license')">Zarządzaj licencją</span><span class="citizen-button citizen-button-status" style="margin-left:0;" onclick="openPopup('popup-worker-badge')">Zarządzaj odznaką</span>`;

    this.document.getElementById("worker-details").innerHTML = allWorkerInfo;
    this.document.getElementById("tirex-workers-level-button").innerHTML = `<input type="button" onclick="addFromPopup('popup-worker-level', '`+socialNumber+`')" class="popup-button popup-confirm" style="--width:calc(50% - 5px);--margin:-35px 0px 0px 130px;"  value="Potwierdź">`;
    this.document.getElementById("tirex-workers-license-button").innerHTML = `<input type="button" onclick="addFromPopup('popup-worker-license', '`+socialNumber+`')" class="popup-button popup-confirm" style="--width:calc(50% - 5px);--margin:-35px 0px 0px 130px;"  value="Potwierdź">`;
    this.document.getElementById("tirex-workers-badge-button").innerHTML = `<input type="button" onclick="addFromPopup('popup-worker-badge', '`+socialNumber+`')" class="popup-button popup-confirm" style="--width:calc(50% - 5px);--margin:-35px 0px 0px 130px;"  value="Potwierdź">`;
    return;
}


function showWorker(data){
    this.document.getElementById("tirex-search-worker").value = '';
    $.post('http://ES-Gpt/GptGetData', JSON.stringify({active: active, type:"worker", user: data})).then(data => setupWorkerInfo(data) );
    document.querySelector("#workers-search-box").style.display = "none";
    document.querySelector("#worker-details").style.display = "block";
    document.querySelector(".worker-header").innerHTML = "<span id='citizen-previous' onclick='hideWorker(this)'><i class='fa-solid fa-angle-left'></i></span>"+headerNameAndTime;
}


function hideWorker(source){
    document.querySelector("#workers-search-box").style.display = "block";
    document.querySelector("#worker-details").style.display = "none";
    document.querySelector(".worker-header").innerHTML = headerNameAndTime+"<button onclick=\"openPopup('popup-add-worker')\"><i class=\"fa-regular fa-circle-plus\"></i>&nbsp;&nbsp;&nbsp;Dodaj pracownika</button>";
}

function setupDiscourseInfo(newData) {
    discourseCitizensList = [];
    discourseOfficersList = [];
    discourseVehiclesList = [];
    discourseImagesList = [];
    let id = newData.id;
    let name = newData.name;
    let status = newData.status;
    var citizens = newData.citizens;
    var officers = newData.officers;
    var vehicles = newData.vehicles;
    var judgments = newData.judgments;
    let content = newData.content;
    var images = newData.images;
    var author = newData.author;

    this.document.getElementById("tirex-discourse-setup-content").value = content;
    this.document.getElementById("tirex-discourse-citizens-buttons").innerHTML = '';
        this.document.getElementById("tirex-discourse-officers-buttons").innerHTML = '';
        this.document.getElementById("tirex-discourse-vehicles-buttons").innerHTML = '';
    this.document.getElementById("tirex-discurse-load-images-list").innerHTML = '';
    for(let i in citizens) {
        let data = citizens[i];
        discourseAddElement(data.ssn, data.name, 'citizen1');
    }
    for(let i in officers) {
        let data = officers[i];
        discourseAddElement(data.ssn, data.name, 'officer1');
    }
    for(let i in vehicles) {
        let data = vehicles[i];
        discourseAddElement(data.vin, data.plate, 'vehicle1');
    }
    for(let i in images) {
        let data = images[i];
        discourseAddElement(data.image, 'none', 'image1');
    }
    
    let judgementString = '';
    if (judgments.length > 0) {
        for (let i in judgments) {
            let data = judgments[i];
            judgementString = judgementString + `<span class="judgement-button">[ `+data.category+` ] `+data.name+` x`+data.count+`</span>`;
        }
    } else {
        judgementString = judgementString + `<span class="judgement-button">Brak...</span>`;
    }
    this.document.getElementById("tirex-judgments-blocks").innerHTML = judgementString;
    
    this.document.getElementById("tirex-discourse-title").innerHTML = '<p>'+name+'</p>';
    this.document.getElementById("tirex-discourse-author-name").innerHTML = '<p>'+author.name+'</p>';
    this.document.getElementById("tirex-discourse-case-id").innerHTML = '<p>#'+id+'</p>';
    if (status == 'true') {
        this.document.getElementById("tirex-discourse-case-status").innerHTML = `<p><span class="patrol-button patrol-valid onlyCursorHover" id="tirex-button-discourse-status" onclick="changeDiscourseStatus('dos')">Rozwiązana</span></p>`;
    } else {
        this.document.getElementById("tirex-discourse-case-status").innerHTML = `<p><span class="patrol-button patrol-invalid onlyCursorHover" id="tirex-button-discourse-status" onclick="changeDiscourseStatus('dos')">Nierozwiązana</span></p>`;
    }

    this.document.getElementById("tirex-case-buttons-add").innerHTML = `<span class="judgement-button" style="background-color:var(--buttoncolor);color:black;cursor:pointer;" onclick="caseJudgements('open', '`+id+`')">Dodaj wyrok</span>`;
}
function showDiscourse(data){
    let caseId = data;function showWorker(data){
    this.document.getElementById("tirex-search-worker").value = '';
    $.post('http://ES-Gpt/GptGetData', JSON.stringify({active: active, type:"worker", user: data})).then(data => setupWorkerInfo(data) );
    document.querySelector("#workers-search-box").style.display = "none";
    document.querySelector("#worker-details").style.display = "block";
    document.querySelector(".worker-header").innerHTML = "<span id='citizen-previous' onclick='hideWorker(this)'><i class='fa-solid fa-angle-left'></i></span>"+headerNameAndTime;
}

function hideWorker(source){
    document.querySelector("#workers-search-box").style.display = "block";
    document.querySelector("#worker-details").style.display = "none";
    document.querySelector(".worker-header").innerHTML = headerNameAndTime+"<button onclick=\"openPopup('popup-add-worker')\"><i class=\"fa-regular fa-circle-plus\"></i>&nbsp;&nbsp;&nbsp;Dodaj pracownika</button>";
}
    this.document.getElementById("tirex-search-discourses").value = '';
    $.post('http://ES-Gpt/GptGetData', JSON.stringify({active: active, type:"discourse", id: caseId})).then(data => setupDiscourseInfo(data) );
    document.querySelector("#discourse-search-box").style.display = "none";
    document.querySelector("#discourse-details").style.display = "block";
    document.querySelector(".discourse-header").innerHTML = "<span id='citizen-previous' onclick='hideDiscourse(this)'><i class='fa-solid fa-angle-left'></i></span>"+headerNameAndTime+`<button onclick="saveDataInDiscourse('`+caseId+`')"><i class="fa-regular fa-floppy-disk"></i>&nbsp;&nbsp;&nbsp;Zapisz Zmiany</button>`;

    $('#tirex-discourse-button2').css('display', 'block');
    $('#tirex-discourse-button1').css('display', 'none');
}

function hideDiscourse(source){
    document.querySelector("#discourse-search-box").style.display = "block";
    document.querySelector("#discourse-details").style.display = "none";
    document.querySelector(".discourse-header").innerHTML = headerNameAndTime;
}

function showVehicle(data, data2){
    this.document.getElementById("tirex-search-cars").value = '';
    if (data2) {
        turnSection(data2);
        $.post('http://ES-Gpt/GptGetData', JSON.stringify({active: active, type:"vehicle", vehicle: data})).then(data => setupVehicleInfo(data) );
        document.querySelector("#vehicle-search-box").style.display = "none";
        document.querySelector("#vehicle-details").style.display = "block";
        document.querySelector(".vehicle-header").innerHTML = "<span id='citizen-previous' onclick='hideVehicle(this)'><i class='fa-solid fa-angle-left'></i></span>"+headerNameAndTime;
    } else {
        $.post('http://ES-Gpt/GptGetData', JSON.stringify({active: active, type:"vehicle", vehicle: data})).then(data => setupVehicleInfo(data) );
        document.querySelector("#vehicle-search-box").style.display = "none";
        document.querySelector("#vehicle-details").style.display = "block";
        document.querySelector(".vehicle-header").innerHTML = "<span id='citizen-previous' onclick='hideVehicle(this)'><i class='fa-solid fa-angle-left'></i></span>"+headerNameAndTime;
    }
}

function hideVehicle(source){
    document.querySelector("#vehicle-search-box").style.display = "block";
    document.querySelector("#vehicle-details").style.display = "none";
    document.querySelector(".vehicle-header").innerHTML = headerNameAndTime;
}

function showEvidence(source){
    document.querySelector("#evidence-search-box").style.display = "none";
    document.querySelector("#evidence-details").style.display = "block";
    document.querySelector(".evidence-header").innerHTML = "<span id='citizen-previous' onclick='hideEvidence(this)'><i class='fa-solid fa-angle-left'></i></span><span>Dowód nr #00</b></span>";
}

function hideEvidence(source){
    document.querySelector("#evidence-search-box").style.display = "block";
    document.querySelector("#evidence-details").style.display = "none";
    document.querySelector(".evidence-header").innerHTML = headerNameAndTime;
}

function toggleVehicleInfo(source, type, n){
    if(type){
        if(n == 1){
            document.querySelector("#vehicle-note-list").style.height = "auto";
            source.setAttribute("value", "Zwiń");
            source.setAttribute("onclick", "toggleVehicleInfo(this, false, 1)");
        }else if(n == 2){
            document.querySelector("#vehicle-discourse-list").style.height = "auto";
            source.setAttribute("value", "Zwiń");
            source.setAttribute("onclick", "toggleVehicleInfo(this, false, 2)");
        }
    }else{
        if(n == 1){
            document.querySelector("#vehicle-note-list").style.height = "150px";
            source.setAttribute("value", "Rozwiń");
            source.setAttribute("onclick", "toggleVehicleInfo(this, true, 1)");
        }else if(n == 2){
            document.querySelector("#vehicle-discourse-list").style.height = "150px";
            source.setAttribute("value", "Rozwiń");
            source.setAttribute("onclick", "toggleVehicleInfo(this, true, 2)");
        }
    }
}

function togglePlayerVehicles(source, type, n){
    if(type){
        if(n == 1){
            document.querySelector("#citizen-ticket-list").style.height = "auto";
            source.setAttribute("value", "Zwiń");
            source.setAttribute("onclick", "togglePlayerVehicles(this, false, 1)");
        }
        else if(n == 2){
            document.querySelector("#citizen-note-list").style.height = "auto";
            source.setAttribute("value", "Zwiń");
            source.setAttribute("onclick", "togglePlayerVehicles(this, false, 2)");
        }
        else if(n == 3){
            document.querySelector("#vehicle-list").style.height = "auto";
            source.setAttribute("value", "Zwiń");
            source.setAttribute("onclick", "togglePlayerVehicles(this, false, 3)");
        }
    }else{
        if(n== 1){
            document.querySelector("#citizen-ticket-list").style.height = "150px";
            source.setAttribute("value", "Rozwiń");
            source.setAttribute("onclick", "togglePlayerVehicles(this, true, 1)");
        }
        else if(n== 2){
            document.querySelector("#citizen-note-list").style.height = "150px";
            source.setAttribute("value", "Rozwiń");
            source.setAttribute("onclick", "togglePlayerVehicles(this, true, 2)");
        }
        else if(n == 3){
            document.querySelector("#vehicle-list").style.height = "150px";
            source.setAttribute("value", "Rozwiń");
            source.setAttribute("onclick", "togglePlayerVehicles(this, true, 3)");
        }
    }
}

function changeTicketCategory(source, id){
    activeTicketList.setAttribute("class", "category-button");
    source.setAttribute("class", "category-button-selected");
    activeTicketList = source;

    let judgementData = '';
    for(let i in judgementTicketList[id].data) {
        let data = judgementTicketList[id].data[i];
        let name = data.name;
        let time = data.time;
        let bill = data.bill;
        let count = data.count;
        judgementData = judgementData + `<div class="judgement-ticket-box"><p class="ticket-count"><span>`+count+`</span><br/><span><i class="fa-light fa-plus" onclick="addAmount(this, '`+id+`', '`+i+`')"></i><i class="fa-light fa-minus" onclick="decreaseAmount(this, '`+id+`', '`+i+`')"></i></span></p><div id="icon"><i class="fa-solid fa-circle"></i></div><p class="content"><br/>`+name+`<br/><p class="judgement-ticket-amount">`+time+` msc.<span>$`+bill+`</span></p></p></div>`;
    }
    this.document.getElementById("main-ticket-list").innerHTML = judgementData;
}
var moneyAmount = 0;
    jailAmount = 0;

function addAmount(source, id, row){
    el = source.parentElement.parentElement.children[0];
    el.innerHTML = parseInt(el.innerHTML)+1;
    judgementTicketList[id].data[row].count = judgementTicketList[id].data[row].count + 1;
    moneyAmount = moneyAmount + judgementTicketList[id].data[row].bill;
    jailAmount = jailAmount + judgementTicketList[id].data[row].time;
    this.document.getElementById("moneyamount").value = '$ '+moneyAmount;
    this.document.getElementById("jailamount").value = jailAmount+' msc.';
}

function decreaseAmount(source, id, row){
    el = source.parentElement.parentElement.children[0];
    if(el.innerHTML > 0){
        el.innerHTML = parseInt(el.innerHTML)-1;
        judgementTicketList[id].data[row].count = judgementTicketList[id].data[row].count - 1;
        moneyAmount = moneyAmount - judgementTicketList[id].data[row].bill;
        jailAmount = jailAmount - judgementTicketList[id].data[row].time;
    }
    this.document.getElementById("moneyamount").value = '$ '+moneyAmount;
    this.document.getElementById("jailamount").value = jailAmount+' msc.';
}

function openJudgementList(source){
    for(let i in judgementTicketList) {
        for(let j in judgementTicketList[i].data) {
            judgementTicketList[i].data[j].count = 0;
        }
    }
    changeTicketCategory(activeTicketList, 0)
    moneyAmount = 0;
    jailAmount = 0;
    this.document.getElementById("moneyamount").value = '$ '+moneyAmount;
    this.document.getElementById("jailamount").value = jailAmount+' msc.';

    this.document.getElementById("tirex-judgement-close-button").innerHTML = `<button onclick="closeJudgementList(this)" class="discourse-refresh" style="width:15%;">Powrót</button>`;
    this.document.getElementById("tirex-judgement-add-button").innerHTML = `<button onclick="saveJudgementList(this)" class="discourse-refresh" style="width:25%;float:right;margin-right:15px;padding:10px 25px;border-radius:5px;margin-top:0px;">Zapisz</button>`;

    document.querySelector("#judgement-firstpage").style.display = "none";
    document.querySelector("#judgement-ticket-list").style.display = "block";
}

function closeJudgementList(source){
    document.querySelector("#judgement-firstpage").style.display = "block";
    document.querySelector("#judgement-ticket-list").style.display = "none";
}

function saveJudgementList(source){
    this.document.getElementById("tirex-judgement-menu-time").value = jailAmount;
    this.document.getElementById("tirex-judgement-menu-bill").value = moneyAmount;
    this.document.getElementById("tirex-judgement-menu-case").value = '';
    let ticketsList = '';
    judgementsTicketList = [];
  
    for(let i in judgementTicketList) {
        for(let j in judgementTicketList[i].data) {
            if (judgementTicketList[i].data[j].count > 0) {
                ticketsList = ticketsList + `<span class="discourse-person-button">[ `+judgementTicketList[i].categoryName+` ] `+judgementTicketList[i].data[j].name+` x`+judgementTicketList[i].data[j].count+`</span>`;
                judgementsTicketList.push({category: judgementTicketList[i].categoryName, name: judgementTicketList[i].data[j].name, count: judgementTicketList[i].data[j].count});
            }
        }
    }
    if (ticketsList.length === 0) {
        ticketsList = ticketsList + `<span class="discourse-person-button">Brak</span>`;
    }

    this.document.getElementById("tirex-tickets-block-list").innerHTML = ticketsList;
    document.querySelector("#judgement-firstpage").style.display = "block";
    document.querySelector("#judgement-ticket-list").style.display = "none";
}
function sendToJailAndGiveBill() {
    let users = judgementsCitizensList;
    if (users.length > 0) {
        let time = this.document.getElementById("tirex-judgement-menu-time").value;
        let bill = this.document.getElementById("tirex-judgement-menu-bill").value;
        $.post('http://ES-Gpt/GptAddData', JSON.stringify({active: active, type: "sendJailAndBill", players: users, reason: judgementsTicketList, bill: bill, time: time}));
    }
}

function createNewDiscourse(){
    discourseCitizensList = [];
    discourseOfficersList = [];
    discourseVehiclesList = [];
    discourseImagesList = [];

    turnSection("discourse");
    document.querySelector("#discourse-search-box").style.display = "none";
    document.querySelector("#discourse-details").style.display = "none";
    document.querySelector("#new-discourse").style.display = "block";
    document.querySelector(".discourse-header").innerHTML = "<span id='citizen-previous' onclick='hideNewDiscourse()'><i class='fa-solid fa-angle-left'></i></span>"+headerNameAndTime;

    $('#tirex-discourse-button1').css('display', 'block');
    $('#tirex-discourse-button2').css('display', 'none');
}

function hideNewDiscourse(){
    document.querySelector("#new-discourse").style.display = "none";
    document.querySelector("#discourse-search-box").style.display = "block";
    document.querySelector(".discourse-header").innerHTML = headerNameAndTime;
}

function refreshWorkersList() {
    $.post('http://ES-Gpt/GptGetData', JSON.stringify({active: active, type:"refresh"})).then(data => updateGptData(data));
}
function refreshCarWList() {
    $.post('http://ES-Gpt/GptGetData', JSON.stringify({active: active, type:"refresh"})).then(data => updateGptData(data));
}

function openDiscoursePanel() {
    hideNewDiscourse();
    hideDiscourse(this);
    $.post('http://ES-Gpt/GptGetData', JSON.stringify({active: active, type:""})).then(data => updateGptData(data));
    return;
}

function addUserToJudgements(ssn, name) {
    turnSection('judgement');
    discourseAddElement(ssn, name, 'judgements-citizen')
}

function changeDiscourseStatus(data) {
    if (data == "uno") {
        const DiscourseStatusButton = document.querySelector("#tirex-discourse-status-button");

        if (DiscourseStatusButton.innerText == "Nierozwiązana") {
            DiscourseStatusButton.classList.remove("patrol-invalid");
            DiscourseStatusButton.classList.add("patrol-valid");
            DiscourseStatusButton.innerHTML = 'Rozwiązana';
        } else {
            DiscourseStatusButton.classList.remove("patrol-valid");
            DiscourseStatusButton.classList.add("patrol-invalid");
            DiscourseStatusButton.innerHTML = 'Nierozwiązana';
        }
    } else if (data == "dos") {
        const DiscourseStatusButton = document.querySelector("#tirex-button-discourse-status");
        if (DiscourseStatusButton.innerText == "Nierozwiązana") {
            DiscourseStatusButton.classList.remove("patrol-invalid");
            DiscourseStatusButton.classList.add("patrol-valid");
            DiscourseStatusButton.innerHTML = 'Rozwiązana';
        } else {
            DiscourseStatusButton.classList.remove("patrol-valid");
            DiscourseStatusButton.classList.add("patrol-invalid");
            DiscourseStatusButton.innerHTML = 'Nierozwiązana';
        }
    }
    return;
}

function discourseAddElement(data, data1, type) {
    if (type == "citizen") {
        discourseCitizensList.push({ssn: data, name: data1});
        let discourseCitizensListString = '';
        for(let i in discourseCitizensList) {
            let nowData = discourseCitizensList[i];
            let name = nowData.name;
            let ssn = nowData.ssn;
            discourseCitizensListString = discourseCitizensListString + `<span class="judgement-button"><i class="fa-regular fa-trash party-kick" onclick="discourseAddElement('`+ssn+`', 'none', 'deleteCitizen')"></i>`+name+` [`+ssn+`]</span>`;
        }
        this.document.getElementById("tirex-citizens-buttons").innerHTML = discourseCitizensListString;
        discourseCitizens('');
        this.document.getElementById("tirex-search-discourse-citizen").value = '';
    } else if (type == "officer") {
        discourseOfficersList.push({ssn: data, name: data1});
        let discourseOfficersListString = '';
        for(let i in discourseOfficersList) {
            let nowData = discourseOfficersList[i];
            let name = nowData.name;
            let ssn = nowData.ssn;
            discourseOfficersListString = discourseOfficersListString + `<span class="judgement-button"><i class="fa-regular fa-trash party-kick" onclick="discourseAddElement('`+ssn+`', 'none', 'deleteOfficer')"></i>`+name+` [`+ssn+`]</span>`;
        }
        this.document.getElementById("tirex-officers-buttons").innerHTML = discourseOfficersListString;
        discourseOfficers('');
        this.document.getElementById("tirex-search-discourse-officer").value = '';
    } else if (type == "vehicle") {
        discourseVehiclesList.push({vin: data, plate: data1});
        let discourseVehiclesListString = '';
        for(let i in discourseVehiclesList) {
            let nowData = discourseVehiclesList[i];
            let vin = nowData.vin;
            let plates = nowData.plate;
            discourseVehiclesListString = discourseVehiclesListString + `<span class="judgement-button"><i class="fa-regular fa-trash party-kick" onclick="discourseAddElement('`+vin+`', 'none', 'deleteVehicle')"></i>`+plates+` [`+vin+`]</span>`;
        }
        this.document.getElementById("tirex-vehicles-buttons").innerHTML = discourseVehiclesListString;
        discourseVehicles('');
        this.document.getElementById("tirex-search-discourse-vehicle").value = '';
    } else if (type == "image") {
        discourseImagesList.push({image: data});
        let discourseImagesListString = '';
        for(let i in discourseImagesList) {
            let nowData = discourseImagesList[i];
            let image = nowData.image;
            discourseImagesListString = discourseImagesListString + `<div class="discourse-photo" style="position: relative;"><img style="margin:0px;width:350px;height:200px;" src="`+image+`"></img><span class="onlyCursorHover" id="tirex-trash-block-image" onclick="discourseAddElement('`+i+`', 'none', 'deleteImage')"><i class="fa-regular fa-trash" id="tirex-trash-inblock-image"></i></span></div>`;
        }
        this.document.getElementById("tirex-discurse-images-list").innerHTML = discourseImagesListString;
        this.document.getElementById("discourse-picture-url").value = '';
    } else if (type == "deleteCitizen") {
        for(let i in discourseCitizensList) {
            let nowData = discourseCitizensList[i];
            let ssn = nowData.ssn;
            if (ssn == data) {
                discourseCitizensList.splice(i, 1);
                break;
            }
        }
        let discourseCitizensListString = '';
        for(let i in discourseCitizensList) {
            let nowData = discourseCitizensList[i];
            let name = nowData.name;
            let ssn = nowData.ssn;
            discourseCitizensListString = discourseCitizensListString + `<span class="judgement-button"><i class="fa-regular fa-trash party-kick" onclick="discourseAddElement('`+ssn+`', 'none', 'deleteCitizen')"></i>`+name+` [`+ssn+`]</span>`;
        }
        this.document.getElementById("tirex-citizens-buttons").innerHTML = discourseCitizensListString;
    } else if (type == "deleteOfficer") {
        for(let i in discourseOfficersList) {
            let nowData = discourseOfficersList[i];
            let ssn = nowData.ssn;
            if (ssn == data) {
                discourseOfficersList.splice(i, 1);
                break;
            }
        }
        let discourseOfficersListString = '';
        for(let i in discourseOfficersList) {
            let nowData = discourseOfficersList[i];
            let name = nowData.name;
            let ssn = nowData.ssn;
            discourseOfficersListString = discourseOfficersListString + `<span class="judgement-button"><i class="fa-regular fa-trash party-kick" onclick="discourseAddElement('`+ssn+`', 'none', 'deleteOfficer')"></i>`+name+` [`+ssn+`]</span>`;
        }
        this.document.getElementById("tirex-officers-buttons").innerHTML = discourseOfficersListString;
    } else if (type == "deleteVehicle") {
        for(let i in discourseVehiclesList) {
            let nowData = discourseVehiclesList[i];
            let vin = nowData.vin;
            if (vin == data) {
                discourseVehiclesList.splice(i, 1);
                break;
            }
        }
        let discourseVehiclesListString = '';
        for(let i in discourseVehiclesList) {
            let nowData = discourseVehiclesList[i];
            let vin = nowData.vin;
            let plates = nowData.plate;
            discourseVehiclesListString = discourseVehiclesListString + `<span class="judgement-button"><i class="fa-regular fa-trash party-kick" onclick="discourseAddElement('`+vin+`', 'none', 'deleteVehicle')"></i>`+plates+` [`+vin+`]</span>`;
        }
        this.document.getElementById("tirex-vehicles-buttons").innerHTML = discourseVehiclesListString;
    } else if (type == "deleteImage") {
        discourseImagesList.splice(data, 1);
        let discourseImagesListString = '';
        for(let i in discourseImagesList) {
            let nowData = discourseImagesList[i];
            let image = nowData.image;
            discourseImagesListString = discourseImagesListString + `<div class="discourse-photo" style="position: relative;"><img style="margin:0px;width:350px;height:200px;" src="`+image+`"></img><span class="onlyCursorHover" id="tirex-trash-block-image" onclick="discourseAddElement('`+i+`', 'none', 'deleteImage')"><i class="fa-regular fa-trash" id="tirex-trash-inblock-image"></i></span></div>`;
        }
        this.document.getElementById("tirex-discurse-images-list").innerHTML = discourseImagesListString;
    }
    
    else if (type == "citizen1") {
        discourseCitizensList.push({ssn: data, name: data1});
        let discourseCitizensListString = '';
        for(let i in discourseCitizensList) {
            let nowData = discourseCitizensList[i];
            let name = nowData.name;
            let ssn = nowData.ssn;
            discourseCitizensListString = discourseCitizensListString + `<span class="judgement-button"><i class="fa-regular fa-trash party-kick" onclick="discourseAddElement('`+ssn+`', 'none', 'deleteCitizen1')"></i>`+name+` [`+ssn+`]</span>`;
        }
        this.document.getElementById("tirex-discourse-citizens-buttons").innerHTML = discourseCitizensListString;
        discourseCitizens1('');
        this.document.getElementById("tirex-search-discourse-setup-citizen").value = '';
    } else if (type == "officer1") {
        discourseOfficersList.push({ssn: data, name: data1});
        let discourseOfficersListString = '';
        for(let i in discourseOfficersList) {
            let nowData = discourseOfficersList[i];
            let name = nowData.name;
            let ssn = nowData.ssn;
            discourseOfficersListString = discourseOfficersListString + `<span class="judgement-button"><i class="fa-regular fa-trash party-kick" onclick="discourseAddElement('`+ssn+`', 'none', 'deleteOfficer1')"></i>`+name+` [`+ssn+`]</span>`;
        }
        this.document.getElementById("tirex-discourse-officers-buttons").innerHTML = discourseOfficersListString;
        discourseOfficers1('');
        this.document.getElementById("tirex-search-discourse-setup-officer").value = '';
    } else if (type == "vehicle1") {
        discourseVehiclesList.push({vin: data, plate: data1});
        let discourseVehiclesListString = '';
        for(let i in discourseVehiclesList) {
            let nowData = discourseVehiclesList[i];
            let vin = nowData.vin;
            let plates = nowData.plate;
            discourseVehiclesListString = discourseVehiclesListString + `<span class="judgement-button"><i class="fa-regular fa-trash party-kick" onclick="discourseAddElement('`+vin+`', 'none', 'deleteVehicle1')"></i>`+plates+` [`+vin+`]</span>`;
        }
        this.document.getElementById("tirex-discourse-vehicles-buttons").innerHTML = discourseVehiclesListString;
        discourseVehicles1('');
        this.document.getElementById("tirex-search-discourse-setup-vehicle").value = '';
    } else if (type == "image1") {
        discourseImagesList.push({image: data});
        let discourseImagesListString = '';
        for(let i in discourseImagesList) {
            let nowData = discourseImagesList[i];
            let image = nowData.image;
            discourseImagesListString = discourseImagesListString + `<div class="discourse-photo" style="position: relative;"><img style="margin:0px;width:350px;height:200px;" src="`+image+`"></img><span class="onlyCursorHover" id="tirex-trash-block-image" onclick="discourseAddElement('`+i+`', 'none', 'deleteImage1')"><i class="fa-regular fa-trash" id="tirex-trash-inblock-image"></i></span></div>`;
        }
        this.document.getElementById("tirex-discurse-load-images-list").innerHTML = discourseImagesListString;
        this.document.getElementById("discourse-picture-url").value = '';
    } else if (type == "deleteCitizen1") {
        for(let i in discourseCitizensList) {
            let nowData = discourseCitizensList[i];
            let ssn = nowData.ssn;
            if (ssn == data) {
                discourseCitizensList.splice(i, 1);
                break;
            }
        }
        let discourseCitizensListString = '';
        for(let i in discourseCitizensList) {
            let nowData = discourseCitizensList[i];
            let name = nowData.name;
            let ssn = nowData.ssn;
            discourseCitizensListString = discourseCitizensListString + `<span class="judgement-button"><i class="fa-regular fa-trash party-kick" onclick="discourseAddElement('`+ssn+`', 'none', 'deleteCitizen1')"></i>`+name+` [`+ssn+`]</span>`;
        }
        this.document.getElementById("tirex-discourse-citizens-buttons").innerHTML = discourseCitizensListString;
    } else if (type == "deleteOfficer1") {
        for(let i in discourseOfficersList) {
            let nowData = discourseOfficersList[i];
            let ssn = nowData.ssn;
            if (ssn == data) {
                discourseOfficersList.splice(i, 1);
                break;
            }
        }
        let discourseOfficersListString = '';
        for(let i in discourseOfficersList) {
            let nowData = discourseOfficersList[i];
            let name = nowData.name;
            let ssn = nowData.ssn;
            discourseOfficersListString = discourseOfficersListString + `<span class="judgement-button"><i class="fa-regular fa-trash party-kick" onclick="discourseAddElement('`+ssn+`', 'none', 'deleteOfficer1')"></i>`+name+` [`+ssn+`]</span>`;
        }
        this.document.getElementById("tirex-discourse-officers-buttons").innerHTML = discourseOfficersListString;
    } else if (type == "deleteVehicle1") {
        for(let i in discourseVehiclesList) {
            let nowData = discourseVehiclesList[i];
            let vin = nowData.vin;
            if (vin == data) {
                discourseVehiclesList.splice(i, 1);
                break;
            }
        }
        let discourseVehiclesListString = '';
        for(let i in discourseVehiclesList) {
            let nowData = discourseVehiclesList[i];
            let vin = nowData.vin;
            let plates = nowData.plate;
            discourseVehiclesListString = discourseVehiclesListString + `<span class="judgement-button"><i class="fa-regular fa-trash party-kick" onclick="discourseAddElement('`+vin+`', 'none', 'deleteVehicle1')"></i>`+plates+` [`+vin+`]</span>`;
        }
        this.document.getElementById("tirex-discourse-vehicles-buttons").innerHTML = discourseVehiclesListString;
    } else if (type == "deleteImage1") {
        discourseImagesList.splice(data, 1);
        let discourseImagesListString = '';
        for(let i in discourseImagesList) {
            let nowData = discourseImagesList[i];
            let image = nowData.image;
            discourseImagesListString = discourseImagesListString + `<div class="discourse-photo" style="position: relative;"><img style="margin:0px;width:350px;height:200px;" src="`+image+`"></img><span class="onlyCursorHover" id="tirex-trash-block-image" onclick="discourseAddElement('`+i+`', 'none', 'deleteImage')"><i class="fa-regular fa-trash" id="tirex-trash-inblock-image"></i></span></div>`;
        }
        this.document.getElementById("tirex-discurse-load-images-list").innerHTML = discourseImagesListString;
    }

    else if (type == "judgements-citizen") {
        judgementsCitizensList.push({ssn: data, name: data1});
        let discourseCitizensListString = '';
        for(let i in judgementsCitizensList) {
            let nowData = judgementsCitizensList[i];
            let name = nowData.name;
            let ssn = nowData.ssn;
            discourseCitizensListString = discourseCitizensListString + `<span class="judgement-button"><i class="fa-regular fa-trash party-kick" onclick="discourseAddElement('`+ssn+`', 'none', 'delete-judgements-citizen')"></i>`+name+` [`+ssn+`]</span>`;
        }
        this.document.getElementById("tirex-judgements-buttons").innerHTML = discourseCitizensListString;
        judgementsCitizens('');
        this.document.getElementById("tirex-search-judgements-citizen").value = '';
    } else if (type == "delete-judgements-citizen") {
        for(let i in judgementsCitizensList) {
            let nowData = judgementsCitizensList[i];
            let ssn = nowData.ssn;
            if (ssn == data) {
                judgementsCitizensList.splice(i, 1);
                break;
            }
        }
        let discourseCitizensListString = '';
        for(let i in judgementsCitizensList) {
            let nowData = judgementsCitizensList[i];
            let name = nowData.name;
            let ssn = nowData.ssn;
            discourseCitizensListString = discourseCitizensListString + `<span class="judgement-button"><i class="fa-regular fa-trash party-kick" onclick="discourseAddElement('`+ssn+`', 'none', 'delete-judgements-citizen')"></i>`+name+` [`+ssn+`]</span>`;
        }
        this.document.getElementById("tirex-judgements-buttons").innerHTML = discourseCitizensListString;
    }
    return;
}
function discourseCitizens(newData) {
    const searchWrapper = document.querySelector("#tirex-menu1");
    const suggBox = searchWrapper.querySelector("#tirex-box1");

    let listContent = '';

    if (newData.length > 0) {
        for (let i in newData) {
            let data = newData[i];
            let name = data.firstname+' '+data.lastname;
            let ssn = data.ssn;
            listContent = listContent + `<li onclick="discourseAddElement('`+ssn+`', '`+name+`', 'citizen')">`+name+` [`+ssn+`]</li>`;
        }
        suggBox.innerHTML = listContent;
        searchWrapper.classList.add("active");
    } else {
        searchWrapper.classList.remove("active");
    }
    return;
}
function discourseOfficers(newData) {
    const searchWrapper = document.querySelector("#tirex-menu2");
    const suggBox = searchWrapper.querySelector("#tirex-box2");

    let listContent = '';

    if (newData.length > 0) {
        for (let i in newData) {
            let data = newData[i];
            let name = data.firstname+' '+data.lastname;
            let ssn = data.ssn;
            listContent = listContent + `<li onclick="discourseAddElement('`+ssn+`', '`+name+`', 'officer')">`+name+` [`+ssn+`]</li>`;
        }
        suggBox.innerHTML = listContent;
        searchWrapper.classList.add("active");
    } else {
        searchWrapper.classList.remove("active");
    }
    return;
}
function discourseVehicles(newData) {
    const searchWrapper = document.querySelector("#tirex-menu3");
    const suggBox = searchWrapper.querySelector("#tirex-box3");

    let listContent = '';

    if (newData.length > 0) {
        for (let i in newData) {
            let data = newData[i];
            let plates = data.plate;
            let vin = data.vin;
            listContent = listContent + `<li onclick="discourseAddElement('`+vin+`', '`+plates+`', 'vehicle')">`+plates+` [`+vin+`]</li>`;
        }
        suggBox.innerHTML = listContent;
        searchWrapper.classList.add("active");
    } else {
        searchWrapper.classList.remove("active");
    }
    return;
}
function addNewDiscourse() {
    var data = [];
    let discourseName = $('#tirex-new-discourse-name').val();
    let discourseStatus = 'false';
    if (document.querySelector("#tirex-discourse-status-button").innerText == "Rozwiązana") {
        discourseStatus = 'true';
    }
    var citizensList = discourseCitizensList;
    var officersList = discourseOfficersList;
    var vehiclesList = discourseVehiclesList;
    var judgementList = judgementsTicketList;
    let discourseContent = $('#tirex-discourse-content').val();
    var imagesList = discourseImagesList;
    if (discourseName.length > 0 && discourseContent.length > 0) {
        data.push({name: discourseName, status: discourseStatus, citizens: citizensList, officers: officersList, vehicles: vehiclesList, judgements: judgementList, content: discourseContent, images: imagesList});

        $.post('http://ES-Gpt/GptAddData', JSON.stringify({active: active, type: 'addDiscourse', newdata: data})).then(data => openDiscoursePanel() );
    }
}
function discourseCitizens1(newData) {
    const searchWrapper = document.querySelector("#tirex-menu1-1");
    const suggBox = searchWrapper.querySelector("#tirex-box1-1");

    let listContent = '';

    if (newData.length > 0) {
        for (let i in newData) {
            let data = newData[i];
            let name = data.firstname+' '+data.lastname;
            let ssn = data.ssn;
            listContent = listContent + `<li onclick="discourseAddElement('`+ssn+`', '`+name+`', 'citizen1')">`+name+` [`+ssn+`]</li>`;
        }
        suggBox.innerHTML = listContent;
        searchWrapper.classList.add("active");
    } else {
        searchWrapper.classList.remove("active");
    }
    return;
}
function discourseOfficers1(newData) {
    const searchWrapper = document.querySelector("#tirex-menu1-2");
    const suggBox = searchWrapper.querySelector("#tirex-box1-2");

    let listContent = '';

    if (newData.length > 0) {
        for (let i in newData) {
            let data = newData[i];
            let name = data.firstname+' '+data.lastname;
            let ssn = data.ssn;
            listContent = listContent + `<li onclick="discourseAddElement('`+ssn+`', '`+name+`', 'officer1')">`+name+` [`+ssn+`]</li>`;
        }
        suggBox.innerHTML = listContent;
        searchWrapper.classList.add("active");
    } else {
        searchWrapper.classList.remove("active");
    }
    return;
}
function discourseVehicles1(newData) {
    const searchWrapper = document.querySelector("#tirex-menu1-3");
    const suggBox = searchWrapper.querySelector("#tirex-box1-3");

    let listContent = '';

    if (newData.length > 0) {
        for (let i in newData) {
            let data = newData[i];
            let plates = data.plate;
            let vin = data.vin;
            listContent = listContent + `<li onclick="discourseAddElement('`+vin+`', '`+plates+`', 'vehicle1')">`+plates+` [`+vin+`]</li>`;
        }
        suggBox.innerHTML = listContent;
        searchWrapper.classList.add("active");
    } else {
        searchWrapper.classList.remove("active");
    }
    return;
}
function saveDataInDiscourse(caseId) {
    var data = [];
    let discourseStatus = 'false';
    if (document.querySelector("#tirex-button-discourse-status").innerText == "Rozwiązana") {
        discourseStatus = 'true';
    }
    var citizensList = discourseCitizensList;
    var officersList = discourseOfficersList;
    var vehiclesList = discourseVehiclesList;
    var judgementList = judgementsTicketList;
    let discourseContent = $('#tirex-discourse-setup-content').val();
    var imagesList = discourseImagesList;
    if (discourseContent.length > 0) {
        data.push({id: caseId, status: discourseStatus, citizens: citizensList, officers: officersList, vehicles: vehiclesList, judgements: judgementList, content: discourseContent, images: imagesList});

        $.post('http://ES-Gpt/GptAddData', JSON.stringify({active: active, type: 'editDiscourse', newdata: data})).then(data => openDiscoursePanel() );
    }
}
function judgementsCitizens(newData) {
    const searchWrapper = document.querySelector("#tirex-menu-judgements");
    const suggBox = searchWrapper.querySelector("#tirex-box-judgements");

    let listContent = '';

    if (newData.length > 0) {
        for (let i in newData) {
            let data = newData[i];
            let name = data.firstname+' '+data.lastname;
            let ssn = data.ssn;
            listContent = listContent + `<li onclick="discourseAddElement('`+ssn+`', '`+name+`', 'judgements-citizen')">`+name+` [`+ssn+`]</li>`;
        }
        suggBox.innerHTML = listContent;
        searchWrapper.classList.add("active");
    } else {
        searchWrapper.classList.remove("active");
    }
    return;
}

function caseJudgements(type, caseId) {
    if (type == "open") {
        turnSection('judgement');
        openJudgementList(this);
        this.document.getElementById("tirex-judgement-close-button").innerHTML = `<button onclick="caseJudgements('returnToCase', '`+caseId+`')" class="discourse-refresh" style="width:15%;">Powrót</button>`;
        this.document.getElementById("tirex-judgement-add-button").innerHTML = `<button onclick="caseJudgements('setJudgeToCase', '`+caseId+`')" class="discourse-refresh" style="width:25%;float:right;margin-right:15px;padding:10px 25px;border-radius:5px;margin-top:0px;">Zapisz</button>`;
    } else if (type == "returnToCase") {
        closeJudgementList(this);
        showCase(caseId);
    } else if (type == "setJudgeToCase") {
        closeJudgementList(this);
        judgementsTicketList = [];
    
        for(let i in judgementTicketList) {
            for(let j in judgementTicketList[i].data) {
                if (judgementTicketList[i].data[j].count > 0) {
                    judgementsTicketList.push({category: judgementTicketList[i].categoryName, name: judgementTicketList[i].data[j].name, count: judgementTicketList[i].data[j].count});
                }
            }
        }
        $.post('http://ES-Gpt/GptAddData', JSON.stringify({active: active, type: 'setCaseJudgements', case: caseId, judgements: judgementsTicketList})).then(data => showCase(caseId) );
    }
}
function loadGptData(data) {
    let ownJobGrade = data.jobData.ownJobGrade;

    let patrolUnitsList = '<option value="" disabled selected hidden>Wybierz typ patrolu</option>';
    for (let i in data.patrolUnits) {
        let patrol = data.patrolUnits[i];
        patrolUnitsList = patrolUnitsList + `<option value="`+patrol+`">`+patrol+`</option>`;
    }
    this.document.getElementById("patrol-selected-vehicle").innerHTML = patrolUnitsList;

    let jobGradesList = '';
    for (let i in data.jobData.jobGrades) {
        let thisJobData = data.jobData.jobGrades[i];
        let jobLabel = thisJobData.label;
        let jobGrade = thisJobData.grade;
        let jobName = thisJobData.job_name;
        if (jobGrade < ownJobGrade) {
            jobGradesList = jobGradesList + `<option value="`+jobName+`-`+jobGrade+`">`+jobLabel+`</option>`;
        }
    }
    this.document.getElementById("tirex-workers-change-worker-grade").innerHTML = jobGradesList;
    this.document.getElementById("tirex-workers-add-worker-grade-list").innerHTML = jobGradesList;

    let jobLicensList = '';
    for (let i in data.jobData.jobLicense) {
        let thisLicensData = data.jobData.jobLicense[i];
        let licenseType = thisLicensData.type;
        let licenseLabel = thisLicensData.label;
        jobLicensList = jobLicensList + `<option value="`+licenseType+`">`+licenseLabel+`</option>`;
    }
    this.document.getElementById("tirex-workers-change-worker-license").innerHTML = jobLicensList;

    return;
}
function dispatchDataAction(action, id) {
    let newid = id;
    if (action == "addToList") {
        $.post('http://ES-Gpt/DispatchGetData', JSON.stringify({type:"addToList", id: newid}));
    } else if (action == "getCoords") {
        $.post('http://ES-Gpt/DispatchGetData', JSON.stringify({type:"getCoords", id: newid}));
    } else if (action == "deleteFromList") {
        $.post('http://ES-Gpt/DispatchGetData', JSON.stringify({type:"deleteFromList", id: newid}));
    } else if (action == "deleteReport") {
        $.post('http://ES-Gpt/DispatchGetData', JSON.stringify({type:"deleteReport", id: newid}));
    }
}
function loadDispatchData(newData, yourSsn) {
    var nowListArray = [];
    var reportListArray = [];
    let nowReportsList = '';
    let reportsList = '';
    for (let i in newData) {
        let data = newData[i];
        let officers = data.officers;

        let arraysInNowList = nowListArray.length;

        for (let j in officers) {
            let ssn = officers[j].user;
            if (ssn == yourSsn) {
                nowListArray.push(data);
                break;
            }
        }
        if (arraysInNowList == nowListArray.length) {
            reportListArray.push(data);
        }
    }

    for (let i in nowListArray) {
        let data = nowListArray[i];
        let id = data.id;
        let officers = data.officers;
        let createTime = data.createTime;
        let content = data.content;
        let type = data.type;
        let street = data.street;
        let maxOfficers = data.maxOfficers;

        let badgeList = '';
        for (let j in officers) {
            let data2 = officers[j];
            let badge = data2.badge;
            badgeList = badgeList + `<span class="patrol-unit">`+badge+`</span>`;
        }
        nowReportsList = nowReportsList + `<div class="dispatch-box" style="margin-bottom:20;"><div class="header"><span id="close"><i class="fa-light fa-circle-xmark" onclick="dispatchDataAction('deleteFromList', '`+id+`')"></i></span><span id="time">`+createTime+`</span>`+badgeList+`</div><div class="left-content"><p>`+content+`</p><span>`+street+`</span></div><div class="right-content"><span class="button button-warning">`+type+`</span></div><div class="left-footer"><br/><span value="Zakończ zgłoszenie" class="button button-action" onclick="dispatchDataAction('deleteReport', '`+id+`')">Zakończ zgłoszenie</span><span value="Lokalizacja" class="button button-action" onclick="dispatchDataAction('getCoords', '`+id+`')">Lokalizacja</span></div><div class="right-footer"><br/><span class="patrol-units">`+officers.length+`/`+maxOfficers+`</span></div></div>`;
    }
    for (let i in reportListArray) {
        let data = reportListArray[i];
        let id = data.id;
        let officers = data.officers;
        let createTime = data.createTime;
        let content = data.content;
        let type = data.type;
        let street = data.street;
        let maxOfficers = data.maxOfficers;

        let badgeList = '';
        for (let j in officers) {
            let data2 = officers[j];
            let badge = data2.badge;
            badgeList = badgeList + `<span class="patrol-unit">`+badge+`</span>`;
        }
        reportsList = reportsList + `<div class="dispatch-box"><div class="header"><span id="close"><i class="fa-light fa-circle-xmark" onclick="dispatchDataAction('deleteFromList', '`+id+`')"></i></span><span id="time">`+createTime+`</span>`+badgeList+`</div><div class="left-content"><p>`+content+`</p><span>`+street+`</span></div><div class="right-content"><span class="button button-warning">`+type+`</span></div><div class="left-footer"><br/><span value="Reaguj" class="button button-action" onclick="dispatchDataAction('addToList', '`+id+`')">Reaguj</span><span value="Lokalizacja" class="button button-action" onclick="dispatchDataAction('getCoords', '`+id+`')">Lokalizacja</span></div><div class="right-footer"><br/><span class="patrol-units">`+officers.length+`/`+maxOfficers+`</span></div></div>`;
    }
    this.document.getElementById("tirex-dispatch-now-reports").innerHTML = nowReportsList;
    this.document.getElementById("tirex-dispatch-data").innerHTML = reportsList;
    return;
}

window.addEventListener('message', function(event) {
    let data = event.data;
    let type = data.type;
    let acces = data.acces

    data = data[1];
    if (type == "OPEN-GPT") {
        if (acces == true) {
            $('.tirex-worker-panel').css('display', 'block');
            $('.tirex-worker-auto').css('display', 'block')
            $('#tirex-announcements-add-button').css('display', 'block');
        } else if (active == "workers") {
            turnSection("main");
        }
        $('html').css('display', 'block');
        $('body').css('display', 'block');
        $('#tirex-gpt-section').css('display', 'block');
        $('#tirex-dispatch-section').css('display', 'none');
        updateGptData(data);
        loadGptData(event.data);
    } else if (type == "OPEN_DISPATCH") {
        $('html').css('display', 'block');
        $('body').css('display', 'block');
        $('#tirex-dispatch-section').css('display', 'block');
        $('#tirex-gpt-section').css('display', 'none');
        loadDispatchData(data, event.data.ssn);
    } else if (type == "LOAD_DISPATCH_DATA") {
        loadDispatchData(data,event.data.ssn);
        if (active == "patrol") {
            let typedName = $('#tirex-search-patrol').val();
            let readyTypeName = typedName.replace(/\s+/g, '');
            let typedName2 = $('#tirex-gpt-dispatch-search').val();
            let readyTypeName2 = typedName2.replace(/\s+/g, '');
            $.post('http://ES-Gpt/GptGetData', JSON.stringify({active: active, type:"search", type2:"search", name: readyTypeName, name2: readyTypeName2})).then(data => updateGptData(data));
        }
    } else if (type == "CLOSE") {
        $('html').css('display', 'none');
        $('body').css('display', 'none');
        $('#tirex-gpt-section').css('display', 'none');
        $('#tirex-dispatch-section').css('display', 'none');
        $('.tirex-worker-panel').css('display', 'none');
        $('.tirex-worker-auto').css('display', 'none');
        $('#tirex-announcements-add-button').css('display', 'none');
    }
});

$("#tirex-search-discourse-citizen").keyup(function(e) {
    let typedName = $('#tirex-search-discourse-citizen').val();
    let readyTypeName = typedName.replace(/\s+/g, '');
    $.post('http://ES-Gpt/GptGetData', JSON.stringify({active: active, type:"search-citizen", name: readyTypeName})).then(data => discourseCitizens(data));
});
$("#tirex-search-discourse-officer").keyup(function(e) {
    let typedName = $('#tirex-search-discourse-officer').val();
    let readyTypeName = typedName.replace(/\s+/g, '');
    $.post('http://ES-Gpt/GptGetData', JSON.stringify({active: active, type:"search-officer", name: readyTypeName})).then(data => discourseOfficers(data));
});
$("#tirex-search-discourse-vehicle").keyup(function(e) {
    let typedName = $('#tirex-search-discourse-vehicle').val();
    let readyTypeName = typedName.replace(/\s+/g, '');
    $.post('http://ES-Gpt/GptGetData', JSON.stringify({active: active, type:"search-vehicle", name: readyTypeName})).then(data => discourseVehicles(data));
});
$("#tirex-search-discourse-setup-citizen").keyup(function(e) {
    let typedName = $('#tirex-search-discourse-setup-citizen').val();
    let readyTypeName = typedName.replace(/\s+/g, '');
    $.post('http://ES-Gpt/GptGetData', JSON.stringify({active: active, type:"search-citizen", name: readyTypeName})).then(data => discourseCitizens1(data));
});
$("#tirex-search-discourse-setup-officer").keyup(function(e) {
    let typedName = $('#tirex-search-discourse-setup-officer').val();
    let readyTypeName = typedName.replace(/\s+/g, '');
    $.post('http://ES-Gpt/GptGetData', JSON.stringify({active: active, type:"search-officer", name: readyTypeName})).then(data => discourseOfficers1(data));
});
$("#tirex-search-discourse-setup-vehicle").keyup(function(e) {
    let typedName = $('#tirex-search-discourse-setup-vehicle').val();
    let readyTypeName = typedName.replace(/\s+/g, '');
    $.post('http://ES-Gpt/GptGetData', JSON.stringify({active: active, type:"search-vehicle", name: readyTypeName})).then(data => discourseVehicles1(data));
});

$("#tirex-search-judgements-citizen").keyup(function(e) {
    let typedName = $('#tirex-search-judgements-citizen').val();
    let readyTypeName = typedName.replace(/\s+/g, '');
    $.post('http://ES-Gpt/GptGetData', JSON.stringify({active: active, type:"search-citizen", name: readyTypeName})).then(data => judgementsCitizens(data));
});

$("#tirex-search-patrol").keyup(function(e) {
    let typedName = $('#tirex-search-patrol').val();
    let readyTypeName = typedName.replace(/\s+/g, '');
    let typedName2 = $('#tirex-gpt-dispatch-search').val();
    let readyTypeName2 = typedName2.replace(/\s+/g, '');
    $.post('http://ES-Gpt/GptGetData', JSON.stringify({active: active, type:"search", type2:"search", name: readyTypeName, name2: readyTypeName2})).then(data => updateGptData(data));
});
$("#tirex-gpt-dispatch-search").keyup(function(e) {
    let typedName = $('#tirex-search-patrol').val();
    let readyTypeName = typedName.replace(/\s+/g, '');
    let typedName2 = $('#tirex-gpt-dispatch-search').val();
    let readyTypeName2 = typedName2.replace(/\s+/g, '');
    $.post('http://ES-Gpt/GptGetData', JSON.stringify({active: active, type:"search", type2:"search", name: readyTypeName, name2: readyTypeName2})).then(data => updateGptData(data));
});
$("#tirex-search-citizen").keyup(function(e) {
    let typedName = $('#tirex-search-citizen').val();
    let readyTypeName = typedName.replace(/\s+/g, '');
    $.post('http://ES-Gpt/GptGetData', JSON.stringify({active: active, type:"search", name: readyTypeName})).then(data => updateGptData(data));
});
$("#tirex-search-cars").keyup(function(e) {
    let typedName = $('#tirex-search-cars').val();
    let readyTypeName = typedName.replace(/\s+/g, '');
    $.post('http://ES-Gpt/GptGetData', JSON.stringify({active: active, type:"search", name: readyTypeName})).then(data => updateGptData(data));
});
$("#tirex-search-weapon").keyup(function(e) {
    let typedName = $('#tirex-search-weapon').val();
    let readyTypeName = typedName.replace(/\s+/g, '');
    $.post('http://ES-Gpt/GptGetData', JSON.stringify({active: active, type:"search", name: readyTypeName})).then(data => updateGptData(data));
});
$("#tirex-search-discourses").keyup(function(e) {
    let typedName = $('#tirex-search-discourses').val();
    let readyTypeName = typedName.replace(/\s+/g, '');
    $.post('http://ES-Gpt/GptGetData', JSON.stringify({active: active, type:"search", name: readyTypeName})).then(data => updateGptData(data));
});
$("#tirex-search-worker").keyup(function(e) {
    let typedName = $('#tirex-search-worker').val();
    let readyTypeName = typedName.replace(/\s+/g, '');
    $.post('http://ES-Gpt/GptGetData', JSON.stringify({active: active, type:"search", name: readyTypeName})).then(data => updateGptData(data));
});

function GptCloseAll() {
    $.post('http://ES-Gpt/GptClose', JSON.stringify({active: active}));
    return;
}
$(".close").click(function() {
    GptCloseAll();
})
$(document).keyup(function(e) {
    if (e.keyCode == 27) {
        GptCloseAll();
    }
});