<?= $this->extend('layout/main') ?>

<?= $this->section('content_header') ?>
<div class="row">
    <div class="col-sm-6"><h3 class="mb-0">Harga</h3></div>
    <div class="col-sm-6">
        <ol class="breadcrumb float-sm-end">
        <li class="breadcrumb-item"><a href="<?=base_url()?>">Home</a></li>
        <li class="breadcrumb-item active" aria-current="page"><a href="<?=base_url()?>harga">Harga</a></li>
        <li class="breadcrumb-item active" aria-current="page"><?=$produk->kode_nama_produk?></li>
        </ol>
    </div>
</div>
<?= $this->endSection() ?>

<?= $this->section('content') ?>
<div class="row mb-5">
    <div class="col-lg-8">
        <div class="card">
            <div class="card-header">
                <h4 class="card-title">Detail Produk</h4>
            </div>
            <div class="card-body">
                <table class="table">
                    <tr>
                        <td>Kategori</td><td>:</td>
                        <td><b><?=$produk->kategori?></b></td>
                    </tr>
                    <tr>
                        <td>Kode / Nama</td><td>:</td>
                        <td><b><?=$produk->kode_nama_produk?></b></td>
                    </tr>
                    <tr>
                        <td>Created At</td><td>:</td>
                        <td><b><?=$produk->created_at?></b></td>
                    </tr>
                    <tr>
                        <td>Updated At</td><td>:</td>
                        <td><b><?=$produk->updated_at?></b></td>
                    </tr>
                </table>
            </div>
        </div>
    </div>
    <div class="col-lg-4">
        <div class="card">
            <div class="card-header">
                <h4 class="card-title">Gambar Produk</h4>
            </div>
            <div class="card-body">
                <img src="<?=base_url()?>gambar/<?=$produk->gambar?>">
            </div>
        </div>
    </div>
</div>
<div class="row g-4">
    <div class="col-lg-8">
        <table id="tbl1" class="table table-bordered table-sm nowrap">
            <thead>
                <tr>
                    <th>No.</th>
                    <th class="dt-filterable">Harga Modal</th>
                    <th class="dt-filterable">Harga Jual</th>
                    <th class="dt-filterable">Aktif</th>
                    <th class="dt-filterable">Updated At</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>
            </tbody>
        </table>
    </div>
</div>
<?= $this->endSection() ?>

<?= $this->section('page_script') ?>
<?= $this->include('layout/css/datatables') ?>
<?= $this->include('layout/js/datatables') ?>

<script type="text/javascript">
$(document).ready(function () {
    var table1 = $("#tbl1").DataTable({
        processing: true,
        serverSide: true,
        scrollX: true,
        lengthMenu: [ [5, 10, 25, 50, -1], [5, 10, 25, 50, "All"] ],
        pageLength:50,
        responsive:false,
        order: [[1, 'asc']],
        dom: 'Bfrtip',
        language : {
            searchBuilder: {
                button: '<span class="fas fa-search"> Filter</span>',
                columns: [".dt-filterable"]
            }
        },
        ajax: {
            url : '<?=base_url()?>harga/ajax_dt_detail/<?=$produk->id_produk?>',
            type : 'POST'
        },
        columns: [
            { data: null,
                 width: '1%',
                searchable : false,
                orderable : false,
            className: 'dt-center', targets: '_all' },
            { data: 'harga_modal', render: function (data, type, row) {
                    // Saat export / sort tetap pakai nilai asli numerik
                    if (type === 'display' || type === 'filter') {
                    if (data == null || data === '') return '-';
                    // Format ke Rupiah dengan Intl.NumberFormat
                    return new Intl.NumberFormat('id-ID', { 
                        style: 'currency', 
                        currency: 'IDR', 
                        minimumFractionDigits: 0 
                    }).format(data);
                    }
                    return data;
                }
            },
            { data: 'harga_jual', render: function (data, type, row) {
                    // Saat export / sort tetap pakai nilai asli numerik
                    if (type === 'display' || type === 'filter') {
                    if (data == null || data === '') return '-';
                    // Format ke Rupiah dengan Intl.NumberFormat
                    return new Intl.NumberFormat('id-ID', { 
                        style: 'currency', 
                        currency: 'IDR', 
                        minimumFractionDigits: 0 
                    }).format(data);
                    }
                    return data;
                }
            },
            { data: 'aktif' },
            { data: 'updated_at' },
            { data: null,
                searchable : false,
                orderable : false,
            className: 'dt-center', targets: '_all' },
        ],
        columnDefs: [
            {
                targets: 5,
                data: null,
                defaultContent: '<button class="btn-view btn btn-block btn-success btn-sm"><span class="fas fa-search"></span> View</button>',
            },
        ],
        buttons: [
            'pageLength',
            'searchBuilder',
            {
                text: '<span class="fas fa-plus"> Add New</span>',
                action: function ( e, dt, node, config ) {
                    document.location.href="<?=base_url()?>harga/add/<?=$produk->id_produk?>";
                }
            },
            'copyHtml5',
            {
                extend: 'excelHtml5',
                text: 'Export Excel',
                orientation: 'landscape',
                exportOptions: {
                    columns: [ ':not(.not-export)' ]
                }
            },
            {
                extend: 'csvHtml5',
                text: 'Export CSV',
                orientation: 'landscape',
                exportOptions: {
                    columns: [ ':not(.not-export)' ]
                }
            },
            {
                extend: 'pdfHtml5',
                text: 'Export PDF',
                orientation: 'landscape',
                exportOptions: {
                    columns: [ ':not(.not-export)' ]
                }
            }
        ],
        "initComplete": function(settings, json) {
        }
    });

    table1.on( 'draw.dt', function () {
        var PageInfo = $('#tbl1').DataTable().page.info();
        table1.column(0, { page: 'current' }).nodes().each( function (cell, i) {
            cell.innerHTML = i + 1 + PageInfo.start;
        });
    });

    $('#tbl1 tbody').on('click','.btn-view',function () {
        var data = table1.row($(this).parents('tr')[0]).data();
        document.location.href='<?=base_url()?>harga/view/'+data['id_harga'];
    });
});
</script>
<?= $this->endSection() ?>