?>
<div class="content-wrapper">
    <section class="content-header"><h1>Insumos / Materias Primas</h1></section>
    <section class="content">
        <?php if($this->session->flashdata('success')): ?><div class="alert alert-success"><?php echo $this->session->flashdata('success'); ?></div><?php endif; ?>
        <?php if($this->session->flashdata('error')): ?><div class="alert alert-danger"><?php echo $this->session->flashdata('error'); ?></div><?php endif; ?>

        <?php if(tiene_permiso('costos_insumo_crear')): ?>
        <a href="<?php echo base_url('index.php/produccion/costos/insumos/create'); ?>" class="btn btn-success"><i class="fa fa-plus"></i> Nuevo Insumo</a>
        <br><br>
        <?php endif; ?>

        <div class="panel panel-primary">
            <div class="panel-heading"><strong>Listado de Insumos</strong></div>
            <div class="panel-body">
                <div class="table-responsive">
                    <table id="tablaInsumos" class="table table-bordered table-hover table-striped">
                        <thead><tr><th>Nombre</th><th>UM</th><th>Precio</th><th>Proveedor</th><th>Estado</th><th style="width:100px;">Acciones</th></tr></thead>
                        <tbody>
                            <?php foreach($insumos as $i): ?>
                            <tr>
                                <td><?php echo htmlspecialchars($i->nombre); ?><?php if($i->descripcion): ?><br><small class="text-muted"><?php echo htmlspecialchars($i->descripcion); ?></small><?php endif; ?></td>
                                <td><span class="label label-default"><?php echo $i->um_abreviado; ?></span></td>
                                <td><?php echo $i->moneda_abrev; ?> <?php echo number_format($i->precio_unitario, 4); ?></td>
                                <td><?php echo $i->proveedor_nombre ?? '—'; ?></td>
                                <td><?php echo $i->estado ? '<span class="label label-success">Activo</span>' : '<span class="label label-danger">Inactivo</span>'; ?></td>
                                <td>
                                    <?php if(tiene_permiso('costos_insumo_editar')): ?>
                                    <a href="<?php echo base_url('index.php/produccion/costos/insumos/edit/'.$i->id); ?>" class="btn btn-warning btn-xs" title="Editar"><i class="fa fa-edit"></i></a>
                                    <a href="<?php echo base_url('index.php/produccion/costos/insumos/estado/'.$i->id); ?>" class="btn btn-<?php echo $i->estado ? 'danger' : 'success'; ?> btn-xs" onclick="return confirm('¿Cambiar estado?')" title="<?php echo $i->estado ? 'Desactivar' : 'Activar'; ?>"><i class="fa fa-<?php echo $i->estado ? 'ban' : 'check'; ?>"></i></a>
                                    <?php endif; ?>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </section>
</div>
<script>window.addEventListener('load',function(){if(typeof $.fn.DataTable!=='undefined'){$('#tablaInsumos').DataTable({language:{lengthMenu:"Mostrar _MENU_ registros",zeroRecords:"Sin resultados",info:"Del _START_ al _END_ de _TOTAL_",search:"Buscar:",paginate:{first:"Primero",last:"Último",next:"Siguiente",previous:"Anterior"}},pageLength:10});}});</script>

<?php