<?php
$id_cobro = $_POST['id_cobro'];
require '../../conexion.php';
$conexion = conexion::con();
?>
<?php if($id_cobro == '0'){
}else{ ?>
<?php   if($id_cobro == '-1'){
            $cab = pg_fetch_all(pg_query($conexion,"SELECT * FROM v_cobros WHERE id_cobro = (select max(id_cobro) from cobros);")); 
        }else{
            $cab = pg_fetch_all(pg_query($conexion,"SELECT * FROM v_cobros WHERE id_cobro = $id_cobro;"));
        }
    $apertura = pg_fetch_all(pg_query($conexion, "SELECT * FROM v_apertura WHERE id_apecie = ".$cab[0]['id_apecie']));
    $detalles = pg_fetch_all(pg_query($conexion, "SELECT * FROM v_cobros_detalles WHERE id_cobro = ".$cab[0]['id_cobro']));
?>
<div class="col-md-9">
    <?php if(empty($detalles)){ ?>
    <label class="text-danger"><i class="fa fa-exclamation-circle"></i> Sin detalles cargados</label>
    <?php }else{ ?>
    <div class="form-group">
        <input type="hidden" id="btn-panel-forma-cobro" data-target="#panel-forma-cobro" data-toggle="modal">
        <label>Detalles Cargados</label><?php if($cab[0]['id_doc']!== "2"){ ?><button class="btn btn-success" onclick="agregar_panel_forma();"><i class="fa fa-money"></i></button><?php } ?>
        <table class="table table-bordered">
            <thead>
                <tr>
                    <th>Concepto</th>
                    <th>Monto</th>
                    <th>Exenta</th>
                    <th>5%</th>
                    <th>10%</th>
                    <th>Acciones</th>
                </tr>
            </thead>
            <tbody>
                <?php $total = 0; foreach($detalles as $d){ $total = $total + $d['monto']; ?>
                <tr>
                    <td><?= $d['cta_descrip'];?></td>
                    <td><?= $d['monto'];?></td>
                    <td>0</td>
                    <td>0</td>
                    <td><?= intval($d['monto']/11);?></td>
                    <?php if($cab[0]['estado']!='PAGADO'){ ?>
                    <td>
                        <button class="btn btn-danger" onclick="quitar_concepto(<?= $d['cta_id'];?>);"><i class="fa fa-minus-circle"></i></button>
                    </td>
                     <?php } ?>
                </tr>
                <?php } ?>
                <tr>
                    <th>Total</th>
                    <td><?= $total;?></td>
                    <td colspan="3"></td>
                </tr>
            </tbody>
        </table>
    </div>
    <?php } ?>
<?php if($cab[0]['estado']!='PAGADO'){ ?>
    <div class="form-group">
        <?php $articulos = pg_fetch_all(pg_query($conexion, "SELECT * FROM v_stock WHERE cod_depo IN (select cod_depo FROM deposito WHERE cod_suc = ".$apertura[0]['cod_suc'].") AND tarti_cod = 1;")); ?>
        <label>Producto</label>
        <select id="vid_articulo" class="select2">
            <option value="0">Seleccione Articulo</option>
            <?php foreach($articulos as $a){ ?>
                <option value="<?= $a['id_articulo'];?>"> <?= $a['art_descri']." (Gs. ".$a['pre_venta'].")";?></option>
            <?php } ?>
        </select><br><br>
        <label>Cantidad</label>
        <input type="number" id="vcantidad" class="form-control" value="1" max="1"><br>
        <button class="btn btn-success" onclick="agregar_articulo();"><i class="fa fa-plus-circle"></i> Agregar Artículo</button>
    </div>
    <?php if($cab[0]['id_doc']!== "2"){ ?>
    <div class="form-group">
        <?php $cuentas = pg_fetch_all(pg_query($conexion, "SELECT * FROM v_cuentas_cobrar WHERE id_cliente = ".$cab[0]['id_cliente']." AND cta_estado in ('PENDIENTE','CREDITO') AND cta_id NOT IN (select cta_id from cobros_detalle WHERE id_cobro = ".$cab[0]['id_cobro'].")"));?>
        <label>Cuentas Pendientes</label><br>
        <?php if(empty($cuentas)){ ?>
        <label class="text-danger"><i class="fa fa-exclamation-circle"></i> No existen cuentas pendientes</label>
        <?php }else{ ?>
        <table class="table table-bordered">
            <thead>
                <tr>
                    <td>Concepto</td>
                    <td>Saldo</td>
                    <td>Vencimiento</td>
                    <td>Monto</td>
                    <td>Acciones</td>
                </tr>
            </thead>
            <tbody>
                <?php foreach($cuentas as $c){ ?>
                <tr>
                    <td><?= $c['cta_descrip']; ?></td>
                    <td><?= $c['cta_saldo']; ?></td>
                    <td><?= $c['cta_vto']; ?></td>
                    <td>
                        <input value="<?= $c['cta_saldo']; ?>" type="number" class="form-control" id="vmonto_<?= $c['cta_id'];?>">
                    </td>
                    <td>
                        <button class="btn btn-primary" onclick="agregar_cuenta(<?= $c['cta_id'];?>);"><i class="fa fa-plus-circle"></i></button>
                    </td>
                </tr>
                <?php } ?>
            </tbody>
        </table>
        <?php } ?>
    </div>
    <?php } ?>
</div>
<?php } ?>
  <?php } ?>