var BrutusinForms = brutusin["json-forms"]; var bf = BrutusinForms.create(schema); function requestTeam(teamCode, eventCode, pinCode) { return $.get("registration.php?action=getTeam&teamCode=" + teamCode + "&eventCode=" + eventCode + "&pinCode=" + pinCode + "&langCode=" + window.ctx.langCode + ""); } function requestEvent(eventCode) { return $.get("registration.php?action=getEvent&eventCode=" + eventCode + "&langCode=" + window.ctx.langCode + ""); } function clearModalPanel() { // modal $("#modalPanel").modal('hide'); $("#modalPanel #alert").empty().removeClass("alert-success alert-danger").addClass("hide"); $("#modalPanel #pinCodeForm").addClass("hide").find("#pinCode").val(""); $("#modalPanel #teamCodeForm").addClass("hide").find("#teamCode").val(""); $("#modalPanel #dismissModal").addClass("hide").unbind().click(function() { $("#modalPanel").modal('hide'); }); $("#modalPanel #login").addClass("hide").unbind().click(function() { $("#modalPanel").modal('hide'); }); $("#modalPanel #registerNewTeam").addClass("hide").unbind().click(function() { $("#modalPanel").modal('hide'); }); } function clearDashboard() { // reset $("#form-container").empty(); $("#showTeamForm").addClass("hide"); $("#saveTeam").addClass("hide"); $("#cancelEdit").addClass("hide"); $("#validate").addClass("hide"); $("#teamCode").empty(); $("#membersCount").empty(); $("#isApproved").empty().removeClass("label-success label-warning"); $("#isPaid").empty().removeClass("label-success label-danger"); $("#notApprovedComment").empty(); $("#notPaidComment").empty(); $("#paymentList").empty(); $("#paymentPanel").addClass("hide"); $("#ticketsPanel").addClass("hide"); $("#ticketPanelIndividual #makePaymentsHolder").empty(); $("#ticketPanelTeam #makePaymentsHolder").empty(); $("#statusPanel").addClass("hide"); $("#teamCodePanel").addClass("hide"); $("#eventPanel").addClass("hide"); $("#paymentStatusPanel").addClass("hide"); $("#showTeamForm").unbind(); $("#saveTeam").unbind(); $("#cancelEdit").unbind(); $("#validate").unbind(); $("#dashboardPanel").addClass("hide"); $("#partners").addClass("hide"); $("#partners #repPanel").empty(); $("#locations").addClass("hide"); $("#locations #repPanel").empty(); $("#repModal #title").text(""); $("#repModal #iframe").attr('src', ""); $("#repModal #requestRep").unbind(); $("#start").unbind().click(function() { init(); }); $("#exit").unbind().click(function() { logout(); clear(); }); } function clear() { clearDashboard(); } function logout() { delete (window.ctx.pinCode); window.sessionStorage.removeItem('pinCode'); } function alertMessage(success, text, callback) { clearModalPanel(); $("#modalPanel #alert").removeClass("hide"); $("#modalPanel #alert").addClass(success ? "alert-success" : "alert-danger"); $("#modalPanel #dismissModal").removeClass("hide"); $("#modalPanel #alert").text(text); if (callback) $("#modalPanel #dismissModal").click(callback); $("#modalPanel").modal('show'); } function initDashboard(action, team, event, pinCode, error) { var langCode = window.ctx.langCode; clearDashboard(); // message var message = window.ctx.message; if(message){ alertMessage(true, message); delete(window.ctx.message); } console.log(team); var eventCode = event ? event.eventCode : null; var teamCode = team ? team.teamCode : null; $("#eventName").text(event.eventName); $("#eventPanel").removeClass("hide"); if (action == "error") { alertMessage(false, error, function() { init(); }); } else if (action == "newTeam") { $("#isApproved").addClass("label-primary").text(S("NEW TEAM")); $("#statusPanel").removeClass("hide"); $("#dashboardPanel").removeClass("hide"); bf.render($("#form-container").get(0), team); $("#saveTeam").removeClass("hide"); $("#cancelEdit").removeClass("hide"); $("#validate").removeClass("hide"); } else if (action == "pinCode") { clearModalPanel(); $("#modalPanel #pinCodeForm").removeClass("hide"); $("#modalPanel #teamCodeForm").removeClass("hide"); $("#modalPanel #teamCodeForm").find("#teamCodeInput").val(window.ctx.teamCode); $("#modalPanel #login").removeClass("hide"); $("#modalPanel #registerNewTeam").removeClass("hide"); $("#modalPanel #login").click(function() { pinCode = $("#modalPanel #pinCodeForm").find("#pinCode").val(); teamCode = $("#modalPanel #teamCodeForm").find("#teamCodeInput").val(); window.ctx.pinCode = pinCode; window.ctx.teamCode = teamCode; init(); }); $("#modalPanel #registerNewTeam").click(function() { window.ctx.teamCode = "NEW"; init(); }); $("#modalPanel").modal('show'); } else { $("#teamCode").text(team.teamCode); $("#membersCount").text(team.teamMembers.length); $("#teamCodePanel").removeClass("hide"); // isApproved if (team.isApproved) { $("#isApproved").addClass("label-success").text(S("APPROVED")); } else { $("#isApproved").addClass("label-warning").text(S("NOT APPROVED")); $("#notApprovedComment").text(team.notApprovedComment); } $("#statusPanel").removeClass("hide"); // isPaid if (team.isPaid) { $("#isPaid").addClass("label-success").text(S("PAID")); } else { $("#isPaid").addClass("label-danger").text(S("NOT PAID")); $("#notPaidComment").text(team.notPaidComment); $("#ticketsPanel").removeClass("hide"); } $("#paymentStatusPanel").removeClass("hide"); // payments var fillTicket = function(ticket, makePaymentHolderJq) { makePaymentHolderJq.find("#ticketName").text(ticket.name[langCode]); makePaymentHolderJq.find("#ticketAmount").text(ticket.amount / 100 + " " + ticket.currency + ""); makePaymentHolderJq.find("#ticketDescription").text(ticket.description[langCode]); makePaymentHolderJq.find("#makePayment").click(function() { location.href = "payments.php?action=payment&event_code=" + eventCode + "&team_code=" + teamCode + "&ticket_code=" + ticket.ticketCode + "&lang_code=" + window.ctx.langCode; }); makePaymentHolderJq.find("#makePayment").addClass(ticket.type == "team" ? "btn-success" : "btn-primary"); } var tickets = event.tickets; Object.keys(tickets).forEach(function(ticketCode) { var ticket = tickets[ticketCode]; if(!ticket.valid) return; var makePaymentsHolderJq = ticket.type == "team" ? $("#ticketPanelTeam #makePaymentsHolder") : $("#ticketPanelIndividual #makePaymentsHolder"); var makePaymentHolderJq = $("#makePaymentHolderTemplate").clone().removeAttr("id"); fillTicket(ticket, makePaymentHolderJq); makePaymentsHolderJq.append(makePaymentHolderJq); makePaymentHolderJq.removeClass("hide"); }); // payment history var payments = team.payments; if (!payments) payments = []; if (Object.keys(payments).length > 0) { Object.keys(payments).forEach(function(orderId) { var payment = payments[orderId]; var newPaymentJq = $("#paymentTemplate").clone().removeAttr("id"); newPaymentJq.removeClass("hide").text("" + payment.timestamp + " | " + (payment.amount / 100) + " " + payment.currency + " | '" + tickets[payment.ticketCode].name[langCode] + "' " + S("starting fee") + " | " + payment.orderId + ""); $("#paymentList").append(newPaymentJq); }); } else { var newPaymentJq = $("#paymentTemplate").clone().removeAttr("id"); newPaymentJq.removeClass("hide").text(S("No payments yet")); $("#paymentList").append(newPaymentJq); } $("#paymentPanel").removeClass("hide"); $("#showTeamForm").removeClass('hide'); // representing fillReps(event.reps, "partner", $("#partners #repPanel"), team); $("#partners").removeClass('hide'); fillReps(event.reps, "location", $("#locations #repPanel"), team); $("#locations").removeClass('hide'); $("#dashboardPanel").removeClass("hide"); console.log(event); } $("#showTeamForm").click(function() { bf.render($("#form-container").get(0), team); $("#showTeamForm").addClass("hide"); $("#saveTeam").removeClass("hide"); $("#cancelEdit").removeClass("hide"); $("#validate").removeClass("hide"); // brutusin hack $("td.prop-value > div > button.btn").html("+ " + S("Add item")); }); $("#validate").click(function() { bf.validate() }); $("#saveTeam").click(function(evnt) { $(evnt.target).attr('disabled',true); var newTeam = bf.getData(); if (teamCode) { newTeam.teamCode = team.teamCode; newTeam.pinCode = pinCode; } newTeam.eventCode = event.eventCode; newTeam.langCode = langCode; console.log(newTeam); postTeam(newTeam, function(resultString) { var result = JSON.parse(resultString); if (result.success) { var team = result.data; window.ctx.teamCode = team.teamCode; window.ctx.pinCode = team.pinCode; window.ctx.message = S("Data is successfully saved"); init(); } else { var error = result.error; alertMessage(false, error); } $(evnt.target).attr('disabled', false); }); }); $("#cancelEdit").click(function() { init(); }); // brutusin hack $("td.prop-value > div > button.btn").html("+ " + S("Add item")); } function fillReps(reps, type, containerJq, team) { shuffle(Object.keys(reps)).forEach(function(key) { var rep = reps[key]; if (rep.type != type) return; var showRepJq = $("#showRepTmpl").clone().removeAttr("id"); showRepJq.find("#title").text(rep.title); showRepJq.find("#logo").attr('src', rep.logoUrl); if(team.reps && team.reps.length>0 && team.reps.includes(key)) showRepJq.addClass("btn-success"); if(rep.approval == "approved") { showRepJq.find("#approvalApproved").removeClass("hide"); showRepJq.click(function() { $("#repModal #title").text(rep.title); $("#repModal #iframe").attr('src', rep.offerUrl); $("#repModal #requestRep").unbind().click(function() { if (!confirm("Ar pasiruošę atstovauti? Nepamirškite susisiekti su Partneriu tiesiogiai bet kuriuo atveju!")) return; requestRep(window.ctx.teamCode, window.ctx.eventCode, window.ctx.pinCode, key).done(function(result) { if (result.success) { alertMessage(true, "Jūsų prašymas ir duomenys išsiųsti! Dabar nelaukite ir susisiekite tiesiogiai."); } else { alertMessage(false, "Nepavyko išsiųsti prašymo: \nPriežastis: " + result.error) } }).fail(function() { alertMessage(false, "Nepavyko išsiųsti prašymo, kontaktuokite tiesiogiai :(. Klaida: "); }).always(function() { $("#repModal").modal("hide"); }); }); $("#repModal").modal('show'); }); }else{ showRepJq.find("#approvalComing").removeClass("hide"); } containerJq.append(showRepJq); showRepJq.removeClass('hide'); }); } function init() { var ctx = window.ctx; if (!ctx) { ctx = {}; window.ctx = ctx; } var isNew = false; if (ctx.teamCode == 'NEW') { delete (ctx.teamCode); window.sessionStorage.removeItem('teamCode'); logout(); isNew = true; } // ctx has precedence over session if (!ctx.teamCode) ctx.teamCode = window.sessionStorage.getItem('teamCode'); if (!ctx.eventCode) ctx.eventCode = window.sessionStorage.getItem('eventCode'); if (!ctx.langCode) ctx.langCode = window.sessionStorage.getItem('langCode'); if (!ctx.pinCode) ctx.pinCode = window.sessionStorage.getItem('pinCode'); if (ctx.teamCode) window.sessionStorage.setItem('teamCode', ctx.teamCode); if (ctx.eventCode) window.sessionStorage.setItem('eventCode', ctx.eventCode); if (ctx.langCode) window.sessionStorage.setItem('langCode', ctx.langCode); if (ctx.pinCode) window.sessionStorage.setItem('pinCode', ctx.pinCode); var teamCode = window.ctx.teamCode; var eventCode = window.ctx.eventCode; var langCode = window.ctx.langCode; if (!langCode) langCode = "lt"; var pinCode = window.ctx.pinCode; var event = null; var team = null; var action; var error; if (!eventCode) return; if (isNew) action = "newTeam"; else if (!pinCode) action = "pinCode"; else action = "currentTeam"; $.when(requestEvent(eventCode), (function() { if (teamCode && pinCode) { return requestTeam(teamCode, eventCode, pinCode); } else { return $.Deferred().resolve(null).promise(); } }).call()).done(function(eventResultArr, teamResultArr) { var eventResult = eventResultArr[0]; if (eventResult.success) { event = eventResult.data; } else { error = eventResult.error; } if (teamCode && pinCode) { var teamResult = teamResultArr[0]; if (teamResult.success) { team = teamResult.data; } else { error = teamResult.error; } } if (error) { logout(); action = "error"; } initDashboard(action, team, event, pinCode, error); }); } function shuffle(array) { var currentIndex = array.length, temporaryValue, randomIndex; // While there remain elements to shuffle... while (0 !== currentIndex) { // Pick a remaining element... randomIndex = Math.floor(Math.random() * currentIndex); currentIndex -= 1; // And swap it with the current element. temporaryValue = array[currentIndex]; array[currentIndex] = array[randomIndex]; array[randomIndex] = temporaryValue; } return array; } function postTeam(team, callback) { $.post("registration.php?action=putTeam&langCode=" + window.ctx.langCode + "", JSON.stringify({ "data" : team }), callback, "text"); } function requestRep(teamCode, eventCode, pinCode, repCode) { return $.get("registration.php?action=requestRep&teamCode=" + teamCode + "&eventCode=" + eventCode + "&pinCode=" + pinCode + "&repCode=" + repCode + ""); } $(document).ready(function() { init(); // requestTeam("503", "123456"); })