<?php
session_start();
if(!isset($_SESSION['usuario']))
{
 	echo "<script languaje='JavaScript'>";       
	echo "parent.location.href='login.php'; ";
	echo "</script>";
}

require_once "clases/Desembolso.class.php";
include ("clases/Usuario.class.php"); 
$obju = new Usuario();
$obja = new Desembolso();
/* ===============================
   OBTENER DESEMBOLSOS
================================ */


if($obju->BuscarPorUsuario($_SESSION['usuario']))
{
   $idb = $obju->getidbeneficiario(); 	
  /* if($obja->BuscarPorCodigo($idb))
   {
     $nombre = $obja->getNombre();
   }*/
   $usuario = $obju->getusuario();
   $tipouser = $obju->gettipo();
}
$lista = $obja->ListarPorBenef($idb);
?>

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Mis Desembolsos</title>

    <link rel="stylesheet"
          href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">

    <style>
        body { background: #f5f5f5; }
        .panel { border-radius: 12px; }
        .panel-heading { font-size: 16px; font-weight: bold; }
        .table th { background: #222; color: #fff; }
        .btn { border-radius: 20px; }
    </style>
</head>

<body>

<div class="container">

    <div class="row">
        <div class="col-md-12 text-center">
            <h2 style="margin-top:20px;">📌 Mis Desembolsos Asignados</h2>
            <hr>
        </div>
    </div>

    <!-- ================= TABLA ESCRITORIO ================= -->
    <div class="panel panel-primary hidden-xs">
        <div class="panel-heading">
            <span class="glyphicon glyphicon-list-alt"></span>
            Desembolsos por Proyecto
        </div>

        <div class="panel-body table-responsive">
            <table class="table table-bordered table-hover">
                <thead>
                    <tr>
                        <th>Proyecto</th>
                        <th>Monto</th>
                        <th>Fecha</th>
                        <th>Observaciones</th>
                        <th width="120">Acción</th>
                    </tr>
                </thead>

                <tbody>
                <?php if($lista != null){ foreach($lista as $d){ ?>
                    <tr>
                        <td>
                            <?= $d['proyecto']; ?><br>
                            <small>Costo total: S/. <?= number_format($d['costo_total'],2); ?></small>
                        </td>
                        <td><strong>S/. <?= number_format($d['monto'],2); ?></strong></td>
                        <td><?= date("d/m/Y", strtotime($d['fecha'])); ?></td>
                        <td><?= $d['obs']; ?></td>
                        <td class="text-center">
                            <a href="mangasto.php?iddesembolso=<?= $d['id']; ?>"
                               class="btn btn-success btn-xs">
                                <span class="glyphicon glyphicon-plus"></span> Gastos
                            </a>
                        </td>
                    </tr>
                <?php }} else { ?>
                    <tr>
                        <td colspan="5" class="text-center">
                            No tienes desembolsos asignados.
                        </td>
                    </tr>
                <?php } ?>
                </tbody>
            </table>
        </div>
    </div>

    <!-- ================= ACORDEÓN MÓVIL ================= -->
    <div class="visible-xs">
        <div class="panel-group" id="accordionMobile">

        <?php
        if($lista != null){
            foreach($lista as $d){
                $idAcc = "des".$d['id'];

                echo "
                <div class='panel panel-default'>
                    <div class='panel-heading'>
                        <h4 class='panel-title'>
                            <a data-toggle='collapse' data-parent='#accordionMobile' href='#$idAcc'>
                                ".$d['proyecto']." (S/. ".number_format($d['monto'],2).")
                            </a>
                        </h4>
                    </div>

                    <div id='$idAcc' class='panel-collapse collapse'>
                        <div class='panel-body'>
                            <p><b>Proyecto:</b> ".$d['proyecto']."</p>
                            <p><b>Costo total:</b> S/. ".number_format($d['costo_total'],2)."</p>
                            <p><b>Monto asignado:</b> S/. ".number_format($d['monto'],2)."</p>
                            <p><b>Fecha:</b> ".$d['fecha']."</p>
                            <p><b>Observaciones:</b> ".$d['obs']."</p>

                            <div class='text-center'>
                                <a href='mangasto.php?iddesembolso=".$d['id']."'
                                   class='btn btn-success btn-sm'>
                                   <span class='glyphicon glyphicon-plus'></span> Registrar Gastos
                                </a>
                            </div>
                        </div>
                    </div>
                </div>";
            }
        }
        ?>

        </div>
    </div>

</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>

</body>
</html>