<div class="content-wrapper">
    <section class="content-header">
        <h1>Nota de Crédito <?php echo $nc->serie . '-' . $nc->numero; ?></h1>
    </section>

    <section class="content">
        <div class="row">
            <div class="col-md-12">

                <!-- DATOS GENERALES -->
                <div class="panel panel-info">
                    <div class="panel-heading"><strong>Datos de la Nota de Crédito</strong></div>
                    <div class="panel-body">
                        <div class="row">
                            <div class="col-md-2">
                                <strong>N° NC:</strong><br>
                                <strong class="text-primary"><?php echo $nc->serie . '-' . $nc->numero; ?></strong>
                            </div>
                            <div class="col-md-2">
                                <strong>Fecha:</strong><br>
                                <?php echo date('d/m/Y', strtotime($nc->fecha)); ?>
                            </div>
                            <div class="col-md-3">
                                <strong>Tipo NC:</strong><br>
                                <span class="label label-info"><?php echo $nc->tipo_nc_codigo; ?></span>
                                <?php echo $nc->tipo_nc; ?>
                            </div>
                            <div class="col-md-2">
                                <strong>Estado:</strong><br>
                                <?php echo $nc->estado == 1
                                    ? '<span class="label label-success">EMITIDA</span>'
                                    : '<span class="label label-danger">ANULADA</span>'; ?>
                            </div>
                            <div class="col-md-3">
                                <strong>SUNAT:</strong><br>
                                <?php if($nc->envio_sunat == 1): ?>
                                    <span class="label label-success"><i class="fa fa-check"></i> Enviado</span>
                                <?php elseif($nc->envio_sunat == 2): ?>
                                    <span class="label label-danger"><i class="fa fa-times"></i> Rechazado</span>
                                <?php else: ?>
                                    <span class="label label-warning"><i class="fa fa-clock-o"></i> Pendiente envío</span>
                                <?php endif; ?>
                            </div>
                        </div>
                        <hr>
                        <div class="row">
                            <div class="col-md-4">
                                <strong>Cliente:</strong><br>
                                <h4 style="margin-top:3px;"><?php echo $nc->cliente; ?></h4>
                                <small class="text-muted"><?php echo $nc->cliente_ruc; ?></small>
                            </div>
                            <div class="col-md-4">
                                <strong>Comprobante de referencia:</strong><br>
                                <?php echo $nc->tipo_comprobante; ?>
                                <strong><?php echo ' ' . $nc->comprob_serie . '-' . $nc->comprob_numero; ?></strong>
                                <small class="text-muted">(<?php echo date('d/m/Y', strtotime($nc->comprob_fecha)); ?>)</small>
                                <a href="<?php echo base_url('index.php/ventas/comprobante/ver/'.$nc->comprob_id); ?>"
                                   class="btn btn-xs btn-default" target="_blank">
                                    <i class="fa fa-eye"></i>
                                </a>
                            </div>
                            <div class="col-md-4">
                                <strong>Moneda:</strong><br>
                                <?php echo $nc->moneda; ?>
                                <?php if($nc->tipocambio > 0): ?>
                                    <small>(TC: <?php echo $nc->tipocambio; ?>)</small>
                                <?php endif; ?>
                            </div>
                        </div>
                        <?php if(!empty($nc->sustento)): ?>
                        <hr>
                        <div class="row">
                            <div class="col-md-12">
                                <strong>Sustento:</strong><br>
                                <?php echo nl2br($nc->sustento); ?>
                            </div>
                        </div>
                        <?php endif; ?>
                    </div>
                </div>

                <!-- DETALLE -->
                <?php if(!empty($detalle)): ?>
                <div class="panel panel-default">
                    <div class="panel-heading"><strong>Detalle</strong></div>
                    <div class="panel-body" style="padding:0;">
                        <table class="table table-bordered table-striped" style="margin:0;">
                            <thead>
                                <tr>
                                    <th style="width:80px;">Código</th>
                                    <th>Descripción</th>
                                    <th class="text-center">Und.</th>
                                    <th class="text-right">Cantidad</th>
                                    <th class="text-right">P. Unitario</th>
                                    <th class="text-right">Subtotal</th>
                                    <th class="text-right">IGV</th>
                                    <th class="text-right">Total</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach($detalle as $item): ?>
                                    <tr>
                                        <td><?php echo $item->producto_codigo; ?></td>
                                        <td><?php echo $item->descripcion ?? $item->producto_nombre; ?></td>
                                        <td class="text-center"><?php echo $item->unidad ?? '-'; ?></td>
                                        <td class="text-right"><?php echo number_format((float)$item->cantidad, 2); ?></td>
                                        <td class="text-right"><?php echo number_format((float)$item->precio_unitario, 4); ?></td>
                                        <td class="text-right"><?php echo number_format((float)$item->subtotal, 2); ?></td>
                                        <td class="text-right"><?php echo number_format((float)$item->igv, 2); ?></td>
                                        <td class="text-right"><strong><?php echo number_format((float)$item->total, 2); ?></strong></td>
                                    </tr>
                                <?php endforeach; ?>
                            </tbody>
                            <tfoot>
                                <tr>
                                    <th colspan="5" class="text-right">Subtotal:</th>
                                    <th class="text-right"><?php echo number_format((float)$nc->subtotal, 2); ?></th>
                                    <th class="text-right"><?php echo number_format((float)$nc->igv, 2); ?></th>
                                    <th class="text-right"><strong><?php echo number_format((float)$nc->total, 2); ?></strong></th>
                                </tr>
                            </tfoot>
                        </table>
                    </div>
                </div>
                <?php endif; ?>

                <!-- TOTALES -->
                <div class="row">
                    <div class="col-md-4 col-md-offset-8">
                        <table class="table table-bordered">
                            <tr>
                                <th>Subtotal:</th>
                                <td class="text-right"><?php echo number_format((float)$nc->subtotal, 2); ?></td>
                            </tr>
                            <tr>
                                <th>IGV (<?php echo number_format((float)$nc->igv_porcentaje, 2); ?>%):</th>
                                <td class="text-right"><?php echo number_format((float)$nc->igv, 2); ?></td>
                            </tr>
                            <tr class="active">
                                <th>TOTAL NC:</th>
                                <td class="text-right"><strong><?php echo number_format((float)$nc->total, 2); ?></strong></td>
                            </tr>
                        </table>
                    </div>
                </div>

                <!-- BOTONES -->
                <a href="<?php echo base_url('index.php/ventas/nota_credito'); ?>" class="btn btn-default">
                    <i class="fa fa-arrow-left"></i> Volver
                </a>

                <?php if(tiene_permiso('nota_credito_anular') && $nc->estado == 1 && $nc->envio_sunat == 0): ?>
                    <button type="button" class="btn btn-danger btn-anular"
                            data-url="<?php echo base_url('index.php/ventas/nota_credito/anular/'.$nc->id); ?>">
                        <i class="fa fa-times"></i> Anular
                    </button>
                <?php endif; ?>

                <a href="<?php echo base_url('index.php/ventas/nota_credito/print/'.$nc->id); ?>"
                   class="btn btn-default" target="_blank">
                    <i class="fa fa-print"></i> Imprimir
                </a>

            </div>
        </div>
    </section>
</div>

<script>
window.addEventListener('load', function () {
    $(document).on('click', '.btn-anular', function() {
        var url = $(this).data('url');
        Swal.fire({
            title: '¿Anular nota de crédito?',
            text: 'Esta acción no se puede revertir.',
            icon: 'warning', showCancelButton: true,
            confirmButtonText: 'Sí, anular', cancelButtonText: 'Cancelar',
            confirmButtonColor: '#d33'
        }).then(function(result) {
            if (result.isConfirmed) window.location.href = url;
        });
    });
});
</script>
