<?php
$v->layout("_themesis");
?>
<div class="container-fluid nav-hidden" id="content">
    <div id="">
        <div class="container-fluid">
            <div class="row">
                <div class="col-sm-12">
                    <div class="box">
                        <div class="box-title">
                            <h3>
                                Minhas Compras
                            </h3>
                        </div>
                        <div class="box-content">
                            <div class="row">
                                <div class="col-lg-4">
                                    <label>Quantidade Chaves</label>
                                    <input type="number" class="form-control quantify_keys" value="0" onkeyup="load_buy();" onkeydown="load_buy();" onkeypress="load_buy();" onblur="load_buy();" onclick="load_buy();" />
                                </div>
                                <div class="col-lg-4">
                                    <label>Valor Chave</label>
                                    <input type="text" disabled readonly class="form-control value_keys" value="R$ 0,10" />
                                </div>
                                <div class="col-lg-4">
                                    <label>Valor a Pagar</label>
                                    <input type="text" disabled readonly class="form-control value_buy" value="R$ 0,00" />
                                </div>
                            </div>
                            <hr />
                            <div class="row">
                                <div class="col-lg-2">
                                    <button class="btn btn-primary" onclick="buy();">FINALIZAR COMPRA</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    function load_buy(){
        var quantify = $(".quantify_keys").val();
        var key = '<?=VALUE_KEY?>';

        var total = key * quantify;

        var f = total.toLocaleString('pt-br',{style: 'currency', currency: 'BRL'});

        $(".value_buy").val(f);
    }
    function buy(){
        var quantify = $(".quantify_keys").val();

        var Data = "&quantify="+quantify;
        $.ajax({
            url: "<?= $router->route("finance.buy"); ?>",
            data: Data,
            type: 'POST',
            dataType: 'json',
            beforeSend: load_in(),
            success: function (data) {
                if(data[0].type === 'error'){
                    notify(data[0].title, data[0].message);
                }else{
                    notify(data[0].title, data[0].message);
                    setTimeOutPage('<?php echo url('finance/credits');?>');
                }
                load_out();
            },
            error: function (request, status, error) {
                notify('Opss', 'Operação não pode ser finalizada!');
            }
        });
        return false;
    }
    $(function(){
        load_balance();
    });
</script>