﻿var total = 0;
function ControlarExibicao() {
    if (total == 0) {
        IniciarTimer();
        $("#acoes").text("");
        $("#acoes").append("Não existem itens no seu carrinho. Clique <a href=\"Default.aspx\">aqui</a> para voltar a página inicial. ");
        $("#divCarrinho").fadeOut('fast');
        $("#divVIR").fadeOut('fast');
    }
}


function MudarPreco(id, apresentacaoID, tipo) {
    if (tipo == 1) {
        var Combo = $("#precos" + id);
        var precoID = $(Combo).children("option:selected").val();
        var ulOwner = $(Combo).parent('li').parent('ul');

        $.ajax({
            type: "POST",
            url: "carrinho.aspx/MudarPreco",
            contentType: "application/json; charset=utf-8",
            data: "{'CarrinhoID':" + parseInt(id) + ",'PrecoID':" + precoID + "}",
            dataType: "json",
            beforeSend: function () {
                $("#acoes").text("Alterando preço, Por favor aguarde...");
            },
            complete: function () {
                //ControlarExibicoes();
            },
            success: function (result) {
                var retorno = eval('(' + result.d + ')');
                if (retorno.Ret == 1) {
                    $(ulOwner).children("li:eq(6)").text(retorno.PrecoValor);
                    $(ulOwner).children("li:eq(8)").text(retorno.TaxaCV);
                    $(ulOwner).children("li:eq(10)").text(retorno.Total);
                    $("#acoes").text("Preço alterado com sucesso!");
                }
                else if (retorno.Ret == -1) {
                    $("#acoes").text("Não foi possível alterar o preço, Erro: " + retorno.Msg);
                }
                else {
                    alert("Retorno inválido, o preço não será alterado");
                }
            },
            error: function (e) {
                alert(e);
                $("#acoes").text(e);
            }
        });
    }
    else {
        var Combo = $("#precos" + id);

        var precoID = $(Combo).children("option:selected").val();
        var ulOwner = $(Combo).parent('li').parent('ul');

        $.ajax({
            type: "POST",
            url: "carrinho.aspx/MudarPrecoFechada",
            contentType: "application/json; charset=utf-8",
            data: "{'LugarID':" + id + ",'PrecoID':" + precoID + ",'ApresentacaoID':" + apresentacaoID + "}",
            dataType: "json",
            beforeSend: function () {
                $("#acoes").text("Alterando preço, Por favor aguarde...");
            },
            complete: function () {
                //ControlarExibicoes();
            },
            success: function (result) {
                var retorno = eval('(' + result.d + ')');
                if (retorno.Ret == 1) {
                    //Achar as childs corretas de PrecoValor e TaxaConveniencia
                    $(ulOwner).children("li:eq(6)").text(retorno.PrecoValor);
                    $(ulOwner).children("li:eq(8)").text(retorno.TaxaCV);
                    $(ulOwner).children("li:eq(10)").text(retorno.Total);
                    $("#acoes").text("Preço alterado com sucesso!");
                }
                else if (retorno.Ret == -1) {
                    $("#acoes").text("Não foi possível alterar o preço, Erro: " + retorno.Msg);
                }
                else {
                    alert("Retorno inválido, o preço não será alterado");
                }
            },
            error: function (e) {
                alert(e);
                $("#acoes").text(e);
            }
        });
    }
}


$(function () {
    $.ajax({
        type: "POST",
        url: "carrinho.aspx/Carregar",
        contentType: "application/json; charset=utf-8",
        data: "{ }",
        dataType: "json",
        beforeSend: function () {
            $("#acoes").text("Carregando, Por favor aguarde...");
        },
        complete: function () {
            $("#acoes").text("");
            ControlarExibicao();
        },
        success: function (result) {
            var retorno = eval('(' + result.d + ')');


            var html = "";
            var isVir = false;
            $.each(retorno, function () {

                //Cadeira, Pista e Mesa Aberta
                if (this.Ret == 1) {
                    total++;
                    html = "<ul id=\"car" + this.CarrinhoID + "\" style=\"list-style: none; height: 100px; width: 100%\">";
                    html += "<li style=\"width: 16px; float: left; height: 100px;\">";
                    html += "<img src=\"Images/borda_esq_slice.png\" width=\"16\" height=\"100\" />";
                    html += "</li>";
                    html += "<li style=\"width: 76px; float: left; height: 100px;\">";
                    html += "<img src=\"http://www.ingressorapido.com.br/ImagensSistema/Eventos/eI" + this.EventoID + "thumb.jpg\" width=\"76\" height=\"50\" /> <br />";
                    if (this.Status != "Expirado")
                        html += "<span> " + this.Status + " </span></li>";
                    else
                        html += "<span class='ColorVermelha'> " + this.Status + " </span></li>";
                    html += "<li style=\"width: 250px; margin-left: 4px; float: left; height: 100px; text-align: left\">";
                    html += "<span class='ColorAzulEscuro'>Evento:</span> " + this.Evento + " <br />";
                    html += "<span class='ColorAzulEscuro'>Apresentação:</span> " + this.Apresentacao + "<br />";
                    html += "<span class='ColorAzulEscuro'>Local:</span> " + this.Local + "<br />";
                    html += "<span class='ColorAzulEscuro'>Setor:</span> " + this.Setor + "<br />";
                    html += "<span class='ColorAzulEscuro'>Preço:</span> ";
                    var id = this.PrecoID;
                    var status = this.Status;
                    var serieID = this.SerieID;
                    if (status != "Expirado" && serieID == 0) {
                        html += "<select id=\"precos" + this.CarrinhoID + "\" class=\"SelectIngressos ColorCinza\" onchange=\"javascript:MudarPreco(" + this.CarrinhoID + ",0, 1); void(0);\">";
                        $.each(this.Precos, function () {
                            if (this.ID == id)
                                html += "<option selected=\"selected\" value=\"" + this.ID + "\">" + this.Nome + "</option>";
                            else
                                html += "<option value=\"" + this.ID + "\">" + this.Nome + "</option>";
                        });
                        html += "</select>";
                    }
                    else if (this.SerieID > 0) {
                        html += this.Preco;
                    }
                    else {
                        $.each(this.Precos, function () {
                            if (this.ID == id)
                                html += this.Nome;
                        });
                    }
                    html += "</li>";
                    html += "<li class=\"ColorBranco\" style=\"width: 16px; float: left; height: 100px;\">";
                    html += " <img src=\"Images/separador.png\" width=\"18\" height=\"100\" style=\"vertical-align: bottom;\" />";
                    html += "</li>";
                    html += "<li style=\"width: 75px; float: left; height: 100px;\"> " + this.Codigo + "</li>";
                    html += "<li class=\"ColorBranco\" style=\"width: 16px; float: left; height: 100px;\">";
                    html += "<img src=\"Images/separador.png\" width=\"18\" height=\"100\" style=\"vertical-align: bottom;\" />";
                    html += "</li>";
                    html += "<li style=\"width: 100px; float: left; height: 100px;\">" + this.Valor + "</li>";
                    html += "<li class=\"ColorBranco\" style=\"width: 16px; float: left\">";
                    html += "<img src=\"Images/separador.png\" width=\"18\" height=\"100\" style=\"vertical-align: bottom;\" />";
                    html += "</li>";
                    html += "<li style=\"width: 100px; float: left; height: 100px;\">" + this.Conveniencia + "</li>";
                    html += "<li class=\"ColorBranco\" style=\"width: 16px; float: left; height: 100px;\">";
                    html += " <img src=\"Images/separador.png\" width=\"18\" height=\"100\" style=\"vertical-align: bottom;\" />";
                    html += "</li>";
                    html += "<li style=\"width: 100px; float: left; height: 100px;\">" + this.Total + "</li>";
                    html += "<li style=\"width: 70px; float: left; height: 100px;\"><a id=\"rem" + this.CarrinhoID + "\" class=\"RemoverCarrinho\" style=\"cursor: pointer\"><img src=\"Images/remove.gif\" alt=\"Remover\" /></a></li>";
                    html += "<li style=\"width: 16px; float: left; height: 100px;\">";
                    html += "<img src=\"Images/borda_dir_car_mid.png\" width=\"16\" height=\"100\" />";
                    html += "</li>";
                    html += "</ul>";
                    $(html).appendTo("#middleRepeat");
                    $("#divCarrinho").fadeIn('fast');
                }
                //Mesa Fechada
                else if (this.Ret == 2) {
                    total++;
                    html = "<ul id=\"lug" + this.LugarID + ";apr" + this.ApresentacaoID + "\" style=\"list-style: none; height: 100px; width: 100%\">";
                    html += "<li style=\"width: 16px; float: left; height: 100px;\">";
                    html += "<img src=\"Images/borda_esq_slice.png\" width=\"16\" height=\"100\" />";
                    html += "</li>";
                    html += "<li style=\"width: 76px; float: left; height: 100px;\">";
                    html += "<img src=\"http://www.ingressorapido.com.br/ImagensSistema/Eventos/eI" + this.EventoID + "thumb.jpg\" width=\"76\" height=\"50\" /> <br />";
                    if (this.Status != "Expirado")
                        html += "<span> " + this.Status + " </span></li>";
                    else
                        html += "<span class='ColorVermelha'> " + this.Status + " </span></li>";
                    html += "<li style=\"width: 250px; margin-left: 4px; float: left; height: 100px; text-align: left\">";
                    html += "<span class='ColorAzulEscuro'>Evento:<span> " + this.Evento + " <br />";
                    html += "<span class='ColorAzulEscuro'>Apresentação:</span> " + this.Apresentacao + "<br />";
                    html += "<span class='ColorAzulEscuro'>Local:</span> " + this.Local + "<br />";
                    html += "<span class='ColorAzulEscuro'>Setor:</span> " + this.Setor + "<br />";
                    html += "<span class='ColorAzulEscuro'>Preço:</span> ";
                    var id = this.PrecoID;
                    if (this.Status != "Expirado") {
                        html += "<select id=\"precos" + this.LugarID + "\" class=\"SelectFechada ColorCinza\"onchange=\"javascript:MudarPreco(" + this.LugarID + "," + this.ApresentacaoID + ", 0); void(0);\">";
                        $.each(this.Precos, function () {
                            if (this.ID == id)
                                html += "<option selected=\"selected\" value=\"" + this.ID + "\">" + this.Nome + "</option>";
                            else
                                html += "<option value=\"" + this.ID + "\">" + this.Nome + "</option>";
                        });
                        html += "</select>";
                    }
                    else {
                        $.each(this.Precos, function () {
                            if (this.ID == id)
                                html += this.Nome;
                        });
                    }
                    html += "</li>";
                    html += "<li class=\"ColorBranco\" style=\"width: 16px; float: left; height: 100px;\">";
                    html += " <img src=\"Images/separador.png\" width=\"18\" height=\"100\" style=\"vertical-align: bottom;\" />";
                    html += "</li>";
                    html += "<li style=\"width: 75px; float: left; height: 100px;\"> " + this.Codigo + "</li>";
                    html += "<li class=\"ColorBranco\" style=\"width: 16px; float: left; height: 100px;\">";
                    html += "<img src=\"Images/separador.png\" width=\"18\" height=\"100\" style=\"vertical-align: bottom;\" />";
                    html += "</li>";
                    html += "<li style=\"width: 100px; float: left; height: 100px;\">" + this.Valor + "</li>";
                    html += "<li class=\"ColorBranco\" style=\"width: 16px; float: left\">";
                    html += "<img src=\"Images/separador.png\" width=\"18\" height=\"100\" style=\"vertical-align: bottom;\" />";
                    html += "</li>";
                    html += "<li style=\"width: 100px; float: left; height: 100px;\">" + this.Conveniencia + "</li>";
                    html += "<li class=\"ColorBranco\" style=\"width: 16px; float: left; height: 100px;\">";
                    html += " <img src=\"Images/separador.png\" width=\"18\" height=\"100\" style=\"vertical-align: bottom;\" />";
                    html += "</li>";
                    html += "<li style=\"width: 100px; float: left; height: 100px;\">" + this.Total + "</li>";
                    html += "<li style=\"width: 70px; float: left; height: 100px;\"><a id=\"rem" + this.LugarID + ";apr" + this.ApresentacaoID + "\" class=\"RemoverLugar\" style=\"cursor: pointer\"><img src=\"Images/remove.gif\" alt=\"Remover\" /></a></li>";
                    html += "<li style=\"width: 16px; float: left; height: 100px;\">";
                    html += "<img src=\"Images/borda_dir_car_mid.png\" width=\"16\" height=\"100\" />";
                    html += "</li>";
                    html += "</ul>";
                    $(html).appendTo("#middleRepeat");
                    $("#divCarrinho").fadeIn('fast');
                }
                else if (this.Ret == 3) {
                    total++;
                    html = "<ul id=\"mos" + this.PacoteGrupo + "\" style=\"list-style: none; height: 100px;width: 100%\">";
                    html += "<li style=\"width: 16px; float: left; height: 100px;\">";
                    html += "<img src=\"Images/borda_esq_slice.png\" width=\"16\" height=\"100\" />";
                    html += "</li>";
                    html += "<li style=\"width: 76px; float: left; height: 100px;\">";
                    html += "<img src=\"http://www.ingressorapido.com.br/ImagensSistema/Eventos/eI" + this.EventoID + "thumb.jpg\" width=\"76\" height=\"50\" /> <br />";
                    if (this.Status != "Expirado")
                        html += "<span> " + this.Status + " </span></li>";
                    else
                        html += "<span class='ColorVermelha'> " + this.Status + " </span></li>";
                    html += "<li style=\"width: 250px; margin-left: 4px; float: left; height: 100px; text-align: left\">";
                    html += "<span class='ColorAzulEscuro'>Pacote(s):</span> ";
                    
                    $.each(this.Pacotes, function (index, item) {
                        html += item + "<br />";
                    });

                    html += "Local";
                    $.each(this.Locais, function (index, item) {
                        html += " - " + item;
                    });

                    html += "</li>";
                    html += "<li class=\"ColorBranco\" style=\"width: 16px; float: left; height: 100px;\">";
                    html += " <img src=\"Images/separador.png\" width=\"18\" height=\"100\" style=\"vertical-align: bottom;\" />";
                    html += "</li>";
                    html += "<li style=\"width: 75px; float: left; height: 100px;\"> " + this.Codigo + " </li>";
                    html += "<li class=\"ColorBranco\" style=\"width: 16px; float: left; height: 100px;\">";
                    html += "<img src=\"Images/separador.png\" width=\"18\" height=\"100\" style=\"vertical-align: bottom;\" />";
                    html += "</li>";
                    html += "<li style=\"width: 100px; float: left; height: 100px;\">" + this.Valor + "</li>";
                    html += "<li class=\"ColorBranco\" style=\"width: 16px; float: left\">";
                    html += "<img src=\"Images/separador.png\" width=\"18\" height=\"100\" style=\"vertical-align: bottom;\" />";
                    html += "</li>";
                    html += "<li style=\"width: 100px; float: left; height: 100px;\">" + this.Conveniencia + "</li>";
                    html += "<li class=\"ColorBranco\" style=\"width: 16px; float: left; height: 100px;\">";
                    html += " <img src=\"Images/separador.png\" width=\"18\" height=\"100\" style=\"vertical-align: bottom;\" />";
                    html += "</li>";
                    html += "<li style=\"width: 100px; float: left; height: 100px;\">" + this.Total + "</li>";
                    html += "<li style=\"width: 70px; float: left; height: 100px;\"><a id=\"gru" + this.PacoteGrupo + "\" class=\"RemoverPacote\" style=\"cursor: pointer\"><img src=\"Images/remove.gif\" alt=\"Remover\" /></a></li>";
                    html += "<li style=\"width: 16px; float: left; height: 100px;\">";
                    html += "<img src=\"Images/borda_dir_car_mid.png\" width=\"16\" height=\"100\" />";
                    html += "</li>";
                    html += "</ul>";
                    $(html).appendTo("#middleRepeat");
                    $("#divCarrinho").fadeIn('fast');
                }
                else if (this.Ret == 4) {
                    total++;
                    html += "<ul id=\"vir" + this.ValeIngressoID + "\" style=\"list-style: none; height: 70px; width: 100%\">";
                    html += "<li style=\"width: 16px; float: left; height: 70px;\">";
                    html += " <img src=\"Images/borda_esq_slice.png\" width=\"18\" height=\"70\" style=\"vertical-align: bottom;\" />";
                    html += "</li>";
                    html += "<li style=\"width: 76px; float: left; height: 70px;\">";
                    html += "<img src=\"http://www.ingressorapido.com.br/ImagensSistema/ValeIngressoInternet/ivir" + this.ValeIngressoTipoID + "thumb.jpg\" width=\"76\" height=\"50\" />";
                    if (this.Status != "Expirado")
                        html += "<span> " + this.Status + " </span></li>";
                    else
                        html += "<span class='ColorVermelha'> " + this.Status + " </span></li>";

                    html += "<li style=\"width: 520px; margin-left: 4px; float: left; height: 70px; text-align: left\">";
                    html += this.Nome + "<br />";
                    html += this.Validade;
                    html += "</li>";
                    html += "<li style=\"width: 16px; float: left; height: 70px;\">";
                    html += " <img src=\"Images/separador.png\" width=\"18\" height=\"70\" style=\"vertical-align: bottom;\" />";
                    html += "</li>";
                    html += "<li style=\"width: 120px; float: left; height: 70px;\">" + this.Valor + "</li>";
                    html += "<li style=\"width: 103px; float: left; height: 70px;\"><img id=\"rem" + this.ValeIngressoID + "\" class=\"RemoverVIR\" src=\"Images/remove.gif\" alt=\"Remover\" style=\"cursor: pointer;\" /></li>";
                    html += "<li style=\"width: 16px; float: left\">";
                    html += "<img src=\"Images/borda_dir_car_mid.png\" width=\"18\" height=\"70\" style=\"vertical-align: bottom;\" />";
                    html += "</li>";
                    html += "</ul>"
                    isVir = true;

                }
                else {
                    $("#acoes").text(this.Msg);
                    return false;
                }
                if (isVir) {
                    //                    $(html).appendTo("#middleRepeatVir");
                    $("#we").html(html);
                    $("#divVIR").fadeIn('fast');
                }
            });
        },
        error: function (e) {
            alert(e.Message);
        }
    });

    $(".RemoverCarrinho").live('click', function () {

        var carrinhoID = $(this).attr("ID");
        carrinhoID = carrinhoID.substring(3, carrinhoID.length);
        $("#car" + carrinhoID).css('opacity', '0.5');


        $.ajax({
            type: "POST",
            url: "carrinho.aspx/LiberarReserva",
            contentType: "application/json; charset=utf-8",
            data: "{'CarrinhoID':" + carrinhoID + "}",
            dataType: "json",
            beforeSend: function () {
                $("#acoes").text("Liberando Reserva, Por favor aguarde...");
            },
            complete: function () {
                ControlarExibicao();
            },
            success: function (result) {
                var resultado = eval('(' + result.d + ')');
                if (resultado.Ret == "L") {
                    $("#acoes").text(resultado.Msg);
                    total--;
                    $("#car" + carrinhoID).remove();

                }
                else {
                    $("#acoes").text("Não foi possível liberar a reserva deste ingresso. " + resultado.Msg);
                    $("#car" + carrinhoID).css('opacity', '1');
                }
            },
            error: function (e) {
                alert(e.Message);
                $("#acoes").text(e);
            }
        });
    });

    $(".RemoverLugar").live('click', function () {
        var item = $(this).attr("ID");
        var arrayLugApr = item.split(';');
        var lugarID = arrayLugApr[0];
        var apresentacaoID = arrayLugApr[1];

        item = item.substring(3, item.length);

        var lugar = $(this).parent('li').parent('ul');
        $(lugar).css('opacity', '0.5');


        lugarID = lugarID.substring(3, lugarID.length);
        apresentacaoID = apresentacaoID.substring(3, apresentacaoID.length);

        $.ajax({
            type: "POST",
            url: "carrinho.aspx/LiberarMesaFechada",
            contentType: "application/json; charset=utf-8",
            data: "{'LugarID':" + parseInt(lugarID) + ",'ApresentacaoID':" + parseInt(apresentacaoID) + "}",
            dataType: "json",
            beforeSend: function () {
                $("#acoes").text("Liberando Reserva, Por favor aguarde...");
            },
            complete: function () {
                ControlarExibicao();
            },
            success: function (result) {
                var resultado = eval('(' + result.d + ')');
                if (resultado.Ret == "L") {
                    $("#acoes").text(resultado.Msg);
                    total--;
                    $(lugar).remove();
                }
                else {
                    $("#acoes").text("Não foi possível liberar a reserva deste ingresso. " + resultado.Msg);
                    $(lugar).css('opacity', '1');
                }
            },
            error: function (e) {
                alert(e.Message);
                $("#acoes").text(e);
            }
        });

    });

    $(".RemoverPacote").live('click', function () {

        var grupo = $(this).attr("ID");
        grupo = grupo.substring(3, grupo.length);

        $("#mos" + grupo).css('opacity', '0.5');

        $.ajax({
            type: "POST",
            url: "carrinho.aspx/LiberarPacote",
            contentType: "application/json; charset=utf-8",
            data: "{'PacoteGrupo':'" + grupo + "'}",
            dataType: "json",
            beforeSend: function () {
                $("#acoes").text("Liberando Reserva, Por favor aguarde...");
            },
            complete: function () {
                ControlarExibicao();
            },
            success: function (result) {
                var resultado = eval('(' + result.d + ')');
                if (resultado.Ret == "L") {
                    $("#mos" + grupo).remove();
                    $("#acoes").text(resultado.Msg);
                    total--;
                }
                else {
                    $("#acoes").text("Não foi possível liberar a reserva deste ingresso. " + resultado.Msg);
                    $("#mos" + grupo).css('opacity', '1');
                }
            },
            error: function (e) {
                alert(e.Message);
                $("#acoes").text(e);
            }
        });
    });

    $(".RemoverVIR").live('click', function () {
        var virID = $(this).attr("ID");
        virID = virID.substring(3, virID.length);
        $("#vir" + virID).css('opacity', '0.5');


        $.ajax({
            type: "POST",
            url: "carrinho.aspx/LiberarVIR",
            contentType: "application/json; charset=utf-8",
            data: "{'ValeIngressoID':" + virID + "}",
            dataType: "json",
            beforeSend: function () {
                $("#acoes").text("Liberando Reserva, Por favor aguarde...");
            },
            complete: function () {
                ControlarExibicao();
            },
            success: function (result) {
                var resultado = eval('(' + result.d + ')');
                if (resultado.Ret == "L") {
                    $("#acoes").text(resultado.Msg);
                    total--;
                    $("#vir" + virID).remove();
                }
                else {
                    $("#acoes").text("Não foi possível liberar a reserva deste ingresso. " + resultado.Msg);
                    $("#car" + virID).css('opacity', '1');
                }
            },
            error: function (e) {
                alert(e.Message);
                $("#acoes").text(e);
            }
        });
    });
});
