import { Component, OnInit } from '@angular/core'; import { Router } from '@angular/router'; import { BingoCartelasModel, BingoSorteiosModel } from '../models/bingoModel'; import { BingoService } from '../services/bingoService'; @Component({ selector: 'app-bingo-dashboard', templateUrl: './bingo-dashboard.component.html', styleUrls: ['./bingo-dashboard.component.scss', './../app.component.scss'] }) export class BingoDashboardComponent implements OnInit { FormSorteio: BingoSorteiosModel = new BingoSorteiosModel(); ListaSorteios: BingoSorteiosModel[] = new Array(); CartelaNumeros: number = 25; AbreCartelas: boolean = false; Novo: boolean = false; Editando: boolean = false; constructor( private bingoService: BingoService, private router: Router ) { } ngOnInit() { this.AtualizarDados(); } AtualizarDados() { localStorage.setItem("Cartelas", ""); localStorage.setItem("NMax", "0"); localStorage.setItem("NParticipantes", "0"); this.CancelarBingo(); this.bingoService.getAllSorteios().then(resSorteios => { this.ListaSorteios = resSorteios; this.GetCartelasSorteio(0); }); } GetCartelasSorteio(Index) { if (Index < this.ListaSorteios.length) { this.bingoService.getCartelasSorteio(this.ListaSorteios[Index].idbingosorteios).then(resCartelas => { this.ListaSorteios[Index].Cartelas = resCartelas; Index++; this.GetCartelasSorteio(Index); }); } else { //console.log(this.ListaSorteios); } } NovoBingo() { this.FormSorteio = new BingoSorteiosModel(); this.Novo = true; } SalvarBingo() { if (this.FormSorteio.idbingosorteios > 0) { this.SalvarSorteio(); } else { if (localStorage.getItem("Cartelas") == "") { alert("Gere as cartelas para poder salvar"); return; } if (localStorage.getItem("NMax") == "0" || localStorage.getItem("NParticipantes") == "0") { alert("Preencha corretamente os numeros de participantes e cartelas"); return; } this.FormSorteio.n_max = parseInt(localStorage.getItem("NMax")); this.FormSorteio.participantes_max = parseInt(localStorage.getItem("NParticipantes")); let CartelasGeradas: CasaCartela[] = JSON.parse(localStorage.getItem("Cartelas")); this.FormSorteio.Cartelas = new Array(); for (let i = 0; i < CartelasGeradas.length; i++) { let Cartela: BingoCartelasModel = new BingoCartelasModel(); Cartela.canhoto = this.FormSorteio.Cartelas.length + 1; for (let o = 0; o < this.CartelaNumeros; o++) { if (Cartela.numeros != "") Cartela.numeros += ","; Cartela.numeros += CartelasGeradas[i][o].display; } this.FormSorteio.Cartelas.push(Cartela); } console.log(this.FormSorteio); this.SalvarSorteio(); } } SalvarSorteio() { this.bingoService.SalvarBingoSorteio(this.FormSorteio).then(resSalvar => { if (resSalvar['status'] == 200) { if (this.FormSorteio.idbingosorteios > 0) { alert("Sorteio inserido com sucesso"); this.AtualizarDados(); } else { // SALVAR AS CARTELAS SE ESTIVER EM MODO DE INSERT this.FormSorteio.idbingosorteios = resSalvar['response']['insertId']; this.bingoService.getAllCartelas().then(resAllCartelas => { for (let i = 0; i < this.FormSorteio.Cartelas.length; i++) { this.FormSorteio.Cartelas[i].canhoto += resAllCartelas.length; } this.bingoService.insertBingoCartela(this.FormSorteio).then(resCartelas => { alert("Sorteio inserido com sucesso"); this.AtualizarDados(); }); }); } } else { alert("Erro ao inserir Sorteio"); console.log(resSalvar['error']); } }); } CancelarBingo() { this.FormSorteio = new BingoSorteiosModel(); this.Novo = false; this.Editando = false; } ExportarCartelas(Sorteio: BingoSorteiosModel) { this.bingoService.exportarExcel(Sorteio).then(resExport => { window.open('http://zendioninc.mooo.com/Jogos/Bingo/Gerador/Cartelas/' + Sorteio.idbingosorteios + '.csv', '_blank'); }); } EditarSorteio(Sorteio: BingoSorteiosModel) { this.FormSorteio = new BingoSorteiosModel(); this.FormSorteio.data_sorteio = Sorteio.data_sorteio.split("/")[2] + "-" + Sorteio.data_sorteio.split("/")[1] + "-" + Sorteio.data_sorteio.split("/")[0]; this.FormSorteio.descricao = Sorteio.descricao; this.FormSorteio.idbingosorteios = Sorteio.idbingosorteios; this.FormSorteio.n_max = Sorteio.n_max; this.FormSorteio.participantes_max = Sorteio.participantes_max; this.FormSorteio.rodada = Sorteio.rodada; this.FormSorteio.Cartelas = Sorteio.Cartelas; this.FormSorteio.VerCartelas = false; this.Editando = true; } RemoverSorteio(Sorteio: BingoSorteiosModel) { if (!confirm("Tem certeza que deseja remover esse sorteio?")) return; this.bingoService.deleteBingoSorteio(Sorteio.idbingosorteios).then(resDelete => { alert("Removido com sucesso"); this.AtualizarDados(); }); } SalvarCartela(Cartela: BingoCartelasModel) { if (Cartela.Editando) { this.bingoService.updateBingoCartela(Cartela).then(resUpdate => { Cartela.Editando = !Cartela.Editando; }); } else { Cartela.Editando = !Cartela.Editando; } } Voltar() { this.router.navigate(['dashboard/cliente']); } } class CasaCartela { display: string; tipo: number; }