<?php
$id_cobro = $_POST['id_cobro'];
require '../../conexion.php';
$conexion = conexion::con();
$total_cobro = pg_fetch_all(pg_query($conexion, "SELECT SUM(monto) AS monto FROM v_cobros_detalles WHERE id_cobro = $id_cobro;"));
$total_forma = pg_fetch_all(pg_query($conexion, "SELECT SUM(monto) AS monto FROM v_cobros_forma_detalle WHERE id_cobro = $id_cobro;"));
$cobros_forma_detalle = pg_fetch_all(pg_query($conexion, "SELECT * FROM v_cobros_forma_detalle WHERE id_cobro = $id_cobro;"));
$cobro_tarjeta = pg_fetch_all(pg_query($conexion, "SELECT * FROM v_cobro_tarjeta WHERE id_cobro = $id_cobro;"));
$cobro_cheques = pg_fetch_all(pg_query($conexion, "SELECT * FROM v_cobro_cheques WHERE id_cobro = $id_cobro;"));
$cab = pg_fetch_all(pg_query($conexion, "SELECT * FROM v_cobros WHERE id_cobro = $id_cobro;"));
$mostrar_efectivo = 'SI';
?>
<div class="modal-dialog">
    <div class="modal-content modal-lg">
        <div class="modal-default">
            <div class="modal-header">
                <div class="col-md-6" >
                    <h4>Formas de Cobro</h4>
                </div>
                <div class="col-md-6" style="left: 390px">
                    <button type="reset" data-dismiss="modal" class="btn btn-default"><i class="fa fa-close "></i></button>
                </div>
            </div>
            <div class="modal-body">
                <div class="form-group">
                    <?php if(empty($cobros_forma_detalle)){ ?>
                    <label class="text-danger"><i class="fa fa-exclamation-circle"></i> Aún no se asignó forma de cobro, Gs. <?= $total_cobro[0]['monto'];?></label>
                    <?php }else{ ?>
                    <table class="table table-bordered">
                        <tr>
                            <th>Tipo</th>
                            <th>Monto</th>
                            <th>Acciones</th>
                        </tr>
                        <?php foreach($cobros_forma_detalle as $c){ ?>
                        <tr>
                            <td><?= $c['fc_descrip'];?></td>
                            <td><?= $c['monto'];?></td>
                            <td>
                                <?php if($c['id_fc'] == '1'){ $mostrar_efectivo = 'NO'; ?>
                                   <?php if($cab[0]['estado']!='PAGADO'){?>
                                    <button class="btn btn-danger" onclick="agregar_forma(4,0,0);"><i class="fa fa-minus-circle"></i></button>
                                   <?php } ?>
                                <?php } ?>
                            </td>
                        </tr>
                        <?php } ?>
                        <tr>
                            <th>TOTAL</th>
                            <td><?= $total_forma[0]['monto'];?></td>
                            <?php if($total_cobro[0]['monto'] > $total_forma[0]['monto']){ ?>
                            <td>
                                <label class="text-danger">Pendiente de asignar Gs. <?= $total_cobro[0]['monto'] - $total_forma[0]['monto']; ?></label>
                            </td>
                            <?php } ?>
                        </tr>
                    </table>
                    <?php } ?>
                </div>
                <?php if(!empty($cobro_tarjeta)){ ?>
                <table class="table table-bordered">
                    <tr>
                        <th>Nro Tarjeta</th>
                        <th>Entidad Emisora</th>
                        <th>Marca</th>
                        <th>Nro Cupón</th>
                        <th>Tipo</th>
                        <th>Monto</th>
                        <th>Acciones</th>
                    </tr>
                    <?php foreach($cobro_tarjeta as $c){ ?>
                    <tr>
                        <td><?= $c['nro_tarjeta']; ?></td>
                        <td><?= $c['ee_nombre']; ?></td>
                        <td><?= $c['mt_descri']; ?></td>
                        <td><?= $c['nro_cupon']; ?></td>
                        <td><?= $c['tipo']; ?></td>
                        <td><?= $c['monto']; ?></td>
                        <td>
                            <?php if($cab[0]['estado']!='PAGADO'){?>
                            <button class="btn btn-danger" onclick="agregar_forma(5,<?= $c['id_ct'];?>,0)"><i class="fa fa-minus-circle"></i></button>
                             <?php } ?>
                        </td>
                    </tr>
                    <?php } ?>
                </table>
                <?php } ?>
                <?php if(!empty($cobro_cheques)){ ?>
                <table class="table table-bordered">
                    <tr>
                        <th>Banco</th>
                        <th>Nro Cheque</th>
                        <th>A Orden De</th>
                        <th>Tipo</th>
                        <th>Monto</th>
                        <th>Vencimiento</th>
                        <th>Acciones</th>
                    </tr>
                    <?php foreach($cobro_cheques as $c){ ?>
                    <tr>
                        <td><?= $c['descripcion']; ?></td>
                        <td><?= $c['nro_cheque']; ?></td>
                        <td><?= $c['a_orden_de']; ?></td>
                        <td><?= $c['tipo']; ?></td>
                        <td><?= $c['monto']; ?></td>
                        <td><?= $c['cheque_venc']; ?></td>
                        <td>
                           <?php if($cab[0]['estado']!='PAGADO'){?>
                            <button class="btn btn-danger" onclick="agregar_forma(6,0,<?= $c['id_cch'];?>)"><i class="fa fa-minus-circle"></i></button>
                           <?php } ?>
                        </td>
                    </tr>
                    <?php } ?>
                </table>
                <?php } ?>
                <?php if($total_cobro[0]['monto'] > $total_forma[0]['monto']){ ?>
                <div class="form-group">
                    <label>Forma de Cobro</label>
                    <select id="id_fc" class="select2 form-control" onchange="panel_forma_detalle();">
                        <?php if($mostrar_efectivo == 'SI'){ ?>
                            <option value="1">EFECTIVO</option>
                        <?php } ?>
                        <option value="2">TARJETA</option>
                        <option value="3">CHEQUE</option>
                    </select>
                    <script type="text/javascript">panel_forma_detalle();</script>
                </div>
                <div id="panel-forma-detalle">
                    
                </div>
                <?php } ?>
            </div>
            <div class="modal-footer">
            </div>
        </div>
    </div>
</div>