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

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

                <div class="panel panel-warning">
                    <div class="panel-heading"><strong>Datos de la Nota de Débito</strong></div>
                    <div class="panel-body">
                        <div class="row">
                            <div class="col-md-2">
                                <strong>N° ND:</strong><br>
                                <strong class="text-warning"><?php echo $nd->serie . '-' . $nd->numero; ?></strong>
                            </div>
                            <div class="col-md-2">
                                <strong>Fecha:</strong><br>
                                <?php echo date('d/m/Y', strtotime($nd->fecha)); ?>
                            </div>
                            <div class="col-md-3">
                                <strong>Tipo ND:</strong><br>
                                <span class="label label-warning"><?php echo $nd->tipo_nd_codigo; ?></span>
                                <?php echo $nd->tipo_nd; ?>
                            </div>
                            <div class="col-md-2">
                                <strong>Estado:</strong><br>
                                <?php echo $nd->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($nd->envio_sunat == 1): ?>
                                    <span class="label label-success"><i class="fa fa-check"></i> Enviado</span>
                                <?php elseif($nd->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 $nd->cliente; ?></h4>
                                <small class="text-muted"><?php echo $nd->cliente_ruc; ?></small>
                            </div>
                            <div class="col-md-4">
                                <strong>Comprobante de referencia:</strong><br>
                                <?php echo $nd->tipo_comprobante; ?>
                                <strong><?php echo ' ' . $nd->comprob_serie . '-' . $nd->comprob_numero; ?></strong>
                                <small class="text-muted">(<?php echo date('d/m/Y', strtotime($nd->comprob_fecha)); ?>)</small>
                                <a href="<?php echo base_url('index.php/ventas/comprobante/ver/'.$nd->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 $nd->moneda; ?>
                            </div>
                        </div>
                        <?php if(!empty($nd->sustento)): ?>
                        <hr>
                        <div class="row">
                            <div class="col-md-12">
                                <strong>Sustento:</strong><br>
                                <?php echo nl2br($nd->sustento); ?>
                            </div>
                        </div>
                        <?php endif; ?>
                    </div>
                </div>

                <!-- DETALLE -->
                <?php if(!empty($detalle)): ?>
                <div class="panel panel-default">
                    <div class="panel-heading"><strong>Detalle del cargo</strong></div>
                    <div class="panel-body" style="padding:0;">
                        <table class="table table-bordered" style="margin:0;">
                            <thead>
                                <tr>
                                    <th>Descripción</th>
                                    <th class="text-right" style="width:120px;">Subtotal</th>
                                    <th class="text-right" style="width:100px;">IGV</th>
                                    <th class="text-right" style="width:120px;">Total</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach($detalle as $item): ?>
                                    <tr>
                                        <td><?php echo $item->descripcion; ?></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>
                        </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)$nd->subtotal, 2); ?></td>
                            </tr>
                            <tr>
                                <th>IGV (<?php echo number_format((float)$nd->igv_porcentaje, 2); ?>%):</th>
                                <td class="text-right"><?php echo number_format((float)$nd->igv, 2); ?></td>
                            </tr>
                            <tr class="active">
                                <th>TOTAL ND:</th>
                                <td class="text-right"><strong><?php echo number_format((float)$nd->total, 2); ?></strong></td>
                            </tr>
                        </table>
                    </div>
                </div>

                <!-- BOTONES -->
                <a href="<?php echo base_url('index.php/ventas/nota_debito'); ?>" class="btn btn-default">
                    <i class="fa fa-arrow-left"></i> Volver
                </a>
                <a href="<?php echo base_url('index.php/ventas/nota_debito/print_nd/'.$nd->id); ?>"
                   class="btn btn-default" target="_blank">
                    <i class="fa fa-print"></i> Imprimir
                </a>
                <?php if(tiene_permiso('nota_debito_anular') && $nd->estado == 1 && $nd->envio_sunat == 0): ?>
                    <button type="button" class="btn btn-danger btn-anular"
                            data-url="<?php echo base_url('index.php/ventas/nota_debito/anular/'.$nd->id); ?>">
                        <i class="fa fa-times"></i> Anular
                    </button>
                <?php endif; ?>

            </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 débito?', 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>
