<div class="container mt-4">
    <div class="card modern-card shadow-sm border-0 rounded-4">
        <div class="card-body p-4">

            <div class="d-flex justify-content-between mb-3">
                <h5>Data Barang Dalam Perjalanan</h5>
                <div class="text-end">
                    <button class="btn btn-warning btn-add btn-add-item mr-2">Tambah Barang</button>
                </div>
            </div>

            <div class="table-responsive">
                <table id="tableItems" class="table modern-table w-100">
                    <thead>
                        <tr>
                            <th>No</th>
                            <th>Tanggal Beli</th>
                            <th>Kode Barang</th>
                            <th>Nama</th>
                            <th style="max-width: 150px;">Gudang</th>
                            <th>Stok</th>
                            <th>Harga Beli</th>
                            <th>Harga Jual</th>
                            <th>Create at</th>
                            <th>Aksi</th>
                        </tr>
                    </thead>
                </table>
            </div>

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

<!-- MODAL -->
<div class="modal fade" id="modalItem">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">

<div class="modal-header bg-warning text-white">
    <h5>Item</h5>
</div>

<div class="modal-body">

<input type="hidden" id="id">

<div id="group_tanggal">
    <label>Tanggal Pembelian</label>
    <input type="date" id="tanggal_beli" class="form-control">
</div>

<div id="group_kode">
    <label class="mt-2">Kode Barang</label>
    <select id="kode_barang" class="form-control select-search"></select>
</div>

<label>Nama Barang</label>
<input type="text" id="nama_barang" class="form-control">

<div id="group_qty">
    <label class="mt-2">Jumlah Barang</label>
    <input type="number" id="qty" class="form-control">
</div>

<label class="mt-2">Harga Beli</label>
<input type="text" id="harga_beli" class="form-control format-rupiah">

<label class="mt-2">Harga Jual</label>
<input type="text" id="harga_jual" class="form-control format-rupiah">

<div  id="group_warehouse">
    <label class="mt-2">Gudang</label>
    <select id="warehouse_id" class="form-control"></select>
</div>

<div id="group_account">
    <label class="mt-2">Akun Kas</label>
    <select id="account_kas" class="form-control"></select>
</div>

</div>

<div class="modal-footer">
    <button class="btn btn-warning" id="btnSimpan">Simpan</button>
</div>

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

<!-- MODAL BARANAG KELUAR -->
<div class="modal fade" id="modalKeluar">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">

<div class="modal-header bg-danger text-white">
    <h5>Barang Keluar</h5>
</div>

<div class="modal-body">

<div id="group_account_biaya">
    <label class="mt-2">Akun Biaya</label>
    <select id="account_biaya" class="form-control"></select>
</div>

<div>
    <label>Tanggal</label>
    <input type="date" id="tanggal_keluar" class="form-control">
</div>

<div>
    <label class="mt-2">Gudang</label>
    <select id="warehouse_id_keluar" class="form-control"></select>
</div>

<div class="d-none" id="group_barang_keluar">
    <label class="mt-2">Barang</label>
    <select id="barang_id" class="form-control select-search"></select>
</div>

<div class="d-none" id="group_qty_keluar">
    <label class="mt-2">Jumlah Barang</label>
    <input type="number" id="qty_keluar" class="form-control">
</div>

<div class="mb-2 d-none" id="group_keterangan">
    <label for="keterangan" class="form-label">Keterangan</label>
    <textarea class="form-control" id="keterangan_keluar" rows="3"></textarea>
</div>

</div>

<div class="modal-footer">
    <button class="btn btn-danger" id="btnKeluar">Keluarkan</button>
</div>

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



<!-- MODAL DETAIL -->
<div class="modal fade" id="modalDetail">
<div class="modal-dialog modal-lg">
<div class="modal-content">

<div class="modal-header bg-warning text-white">
    <h5>Detail Item</h5>
</div>

<div class="modal-body" id="detailContent"></div>

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



<div class="modal fade" id="modalAdjust">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">

<div class="modal-header bg-warning">
    <h5>Penyesuaian Stok</h5>
</div>

<div class="modal-body">

<input type="hidden" id="adjust_item_id">

<label>Qty Adjustment</label>
<input type="number" id="adjust_qty" class="form-control">
<small>Gunakan minus (-) jika mengurangi</small>

<label class="mt-2">Gudang</label>
<select id="adjust_warehouse" class="form-control"></select>

<label class="mt-2">Keterangan</label>
<input type="text" id="adjust_ket" class="form-control">

</div>

<div class="modal-footer">
    <button class="btn btn-warning" id="btnAdjust">Simpan</button>
</div>

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


<script>

$(function(){

let action = 'add';
let selectedItemId = null;

// ================= DATATABLE =================
let table = $('#tableItems').DataTable({
    ajax: {
        url: "<?= base_url('items/get_data'); ?>",
        data: function (d) {
            d.s = `draft`;
        }
    }
});

// ================= LOAD AKUN KAS =================
function loadKas(){
    $.get("<?= base_url('items/get_accounts'); ?>",res=>{
        let opt='<option value="">-- PILIH KAS --</option>';
        res.forEach(a=>{
            opt += `<option value="${a.id}">${a.nama_akun}</option>`;
        });
        $('#account_kas').html(opt);
    },'json');
}

// ================= LOAD AKUN KAS =================
function loadKasOut(){
    $.get("<?= base_url('items/get_accounts_biaya'); ?>",res=>{
        let opt='<option value="">-- PILIH KAS --</option>';
        res.forEach(a=>{
            opt += `<option value="${a.id}">${a.nama_akun}</option>`;
        });
        $('#account_biaya').html(opt);
    },'json');
}

// ================= LOAD GUDANG =================
function loadGudang(){
    $.get("<?= base_url('warehouses/list'); ?>",res=>{
        let opt='<option value="">-- PILIH GUDANG --</option>';
        res.forEach(w=>{
            opt += `<option value="${w.id}">${w.nama}</option>`;
        });
        $('#warehouse_id, #adjust_warehouse').html(opt);
    },'json');
}
// ================= LOAD GUDANG =================
function loadGudangList(){
    $.get("<?= base_url('warehouses/list'); ?>",res=>{
        let opt='<option value="">-- PILIH GUDANG --</option>';
        res.forEach(w=>{
            opt += `<option value="${w.id}">${w.nama}</option>`;
        });
        $('#warehouse_id_keluar').html(opt);
    },'json');
}

function loadKode(){
    $.get("<?= base_url('kodebarang/list'); ?>",res=>{
        let opt='<option value="">-- PILIH KODE --</option>';
        res.forEach(a=>{
            opt += `<option value="${a.id}">${a.kode_barang} - ${a.nama}</option>`;
        });
        $('#kode_barang').html(opt);
    },'json');
}

// ================= RESET FORM =================
function resetForm(){
    $('#id').val('');
    $('#nama_barang').val('');
    $('#qty').val('');
    $('#harga_beli').val('');
    $('#harga_jual').val('');
    $('#tanggal_beli').val('');
}

// ================= INIT LOAD =================
loadGudang();
loadKas();
loadKode();

// ================= ADD =================
$('.btn-add-item').click(function(){
    action='add';
    resetForm();

    $('#qty').closest('div').show();
    $('#kode_barang').closest('div').show();
    $('#warehouse_id').closest('div').show();
    $('#account_kas').closest('div').show();
    $('#modalItem').modal('show');
    loadGudang();
    loadKas();
    loadKode();
});

// ================= EDIT =================
$(document).on('click','.btn-editItem',function(){

    let id = $(this).data('id');
    action='edit';

    $.get("<?= base_url('items/detail_simple/'); ?>"+id,function(res){

        $('#id').val(res.id);
        $('#nama_barang').val(res.nama_barang);
        $('#harga_beli').val(res.harga_beli);
        $('#harga_jual').val(res.harga_jual);

        // 🔥 FIX DI SINI
        $('#group_qty').hide();
        $('#group_account').hide();
        $('#group_kode').hide();
        $('#group_warehouse').hide();
        $('#group_tanggal').hide();

        $('#modalItem').modal('show');

    },'json');


});

// ================= SAVE =================
$('#btnSimpan').click(function(){

    let btn = $(this);

    let data = {
        id: $('#id').val(),
        kode_id: $('#kode_barang').val(),
        nama_barang: $('#nama_barang').val(),
        qty: $('#qty').val(),
        harga_beli: $('#harga_beli').val(),
        harga_jual: $('#harga_jual').val(),
        warehouse_id: $('#warehouse_id').val(),
        account_kas: $('#account_kas').val(),
        tanggal_beli: $('#tanggal_beli').val(),
        status: `draft`
    };

    let url = action==='add'
        ? "<?= base_url('items/save'); ?>"
        : "<?= base_url('items/update'); ?>";

    // 🔥 loading state
    btn.prop('disabled', true).html('Menyimpan...');

    $.post(url, data, function(res){

        if(res.status){

            $('#modalItem').modal('hide');
            table.ajax.reload(null,false);

            Swal.fire({
                icon: 'success',
                title: 'Berhasil',
                text: res.message || 'Data berhasil disimpan',
                timer: 1500,
                showConfirmButton: false
            });

        }else{

            Swal.fire({
                icon: 'error',
                title: 'Gagal',
                text: res.message || 'Terjadi kesalahan'
            });
        }

    },'json')
    .fail(function(){

        Swal.fire({
            icon: 'error',
            title: 'Error',
            text: 'Koneksi ke server gagal'
        });

    })
    .always(function(){
        btn.prop('disabled', false).html('Simpan');
    });

});

// =====================================================
// ================= START URUSAN KELUAR ===============
// =====================================================

// ================= RESET FORM KELUAR =================
function resetFormKeluar(){
    $('#tanggal_keluar').val('');
    $('#qty_keluar').val('');
    $('#keterangan_keluar').val('');
}

loadKasOut();
loadGudangList();
// ================= BARANG KELUAR ================= account_biaya warehouse_id barang_id
$('.btn-out').click(function(){
    // action='add';
    resetFormKeluar();

    $('#warehouse_id').closest('div').show();
    $('#account_biaya').closest('div').show();
    $('#modalKeluar').modal('show');
    
    loadGudangList();
    loadKasOut();
});

$('#warehouse_id_keluar').on('change', function(){
    let warehouse_id = $(this).val();
    
    // $('#wrap_item_select').addClass('d-none');
    $('#barang_id').html('<option value="">Loading...</option>');
    
    loadItems(warehouse_id);
});

// ================= ITEMS =================
function loadItems(warehouse_id){
    return $.get("<?= base_url('items/get_items_by_wh_id/'); ?>" + warehouse_id, function(res){
        if(res && res.length){
            let opt = '<option value="">-- PILIH BARANG --</option>';
            res.forEach(i=>{
                opt += `<option value="${i.id}">
                            ${i.kode_detail} - ${i.nama_barang} (Stok: ${i.stok})
                        </option>`;
            });
            $('#barang_id').html(opt);
            $('#group_barang_keluar').removeClass('d-none');
            $('#group_qty_keluar').removeClass('d-none');
            $('#group_keterangan').removeClass('d-none');
        } else {
            $('#barang_id').html(opt);
        }
    }, 'json');
}

// ================= SAVE =================
$('#btnKeluar').click(function(){

    let btn = $(this);

    let data = {
        account_biaya:          $('#account_biaya').val(),
        tanggal_keluar:         $('#tanggal_keluar').val(),
        warehouse_id_keluar:    $('#warehouse_id_keluar').val(),
        barang_id:              $('#barang_id').val(),
        qty_keluar:             $('#qty_keluar').val(),
        keterangan_keluar:      $('#keterangan_keluar').val()
    };

    let url = "<?= base_url('items/keluarkan'); ?>";

    // 🔥 loading state
    btn.prop('disabled', true).html('Menyimpan...');

    $.post(url, data, function(res){

        if(res.status){

            $('#modalItem').modal('hide');
            table.ajax.reload(null,false);

            Swal.fire({
                icon: 'success',
                title: 'Berhasil',
                text: res.message || 'Data berhasil disimpan',
                timer: 1500,
                showConfirmButton: false
            });

        }else{

            Swal.fire({
                icon: 'error',
                title: 'Gagal',
                text: res.message || 'Terjadi kesalahan'
            });
        }

    },'json')
    .fail(function(){

        Swal.fire({
            icon: 'error',
            title: 'Error',
            text: 'Koneksi ke server gagal'
        });

    })
    .always(function(){
        btn.prop('disabled', false).html('Keluarkan');
    });

});
// =====================================================
// ================= END URUSAN KELUAR ===============
// =====================================================

// ================= DELETE =================
$(document).on('click','.btn-delete',function(){

    let id = $(this).data('id');
    let btn = $(this);

    Swal.fire({
        title: 'Hapus data?',
        text: "Data tidak bisa dikembalikan!",
        icon: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#d33',
        cancelButtonColor: '#3085d6',
        confirmButtonText: 'Ya, hapus!',
        cancelButtonText: 'Batal'
    }).then((result) => {

        if(result.isConfirmed){

            btn.prop('disabled', true).html('Menghapus...');

            $.get("<?= base_url('items/delete/'); ?>"+id,function(res){

                if(res.status){

                    table.ajax.reload(null,false);

                    Swal.fire({
                        icon: 'success',
                        title: 'Berhasil',
                        text: res.message || 'Data berhasil dihapus',
                        timer: 1500,
                        showConfirmButton: false
                    });

                }else{

                    Swal.fire({
                        icon: 'error',
                        title: 'Gagal',
                        text: res.message || 'Gagal menghapus data'
                    });
                }

            },'json')
            .fail(function(){

                Swal.fire({
                    icon: 'error',
                    title: 'Error',
                    text: 'Koneksi ke server gagal'
                });

            })
            .always(function(){
                btn.prop('disabled', false).html('Hapus');
            });

        }
    });

});


// ================= POSTING =================
$(document).on('click','.btn-updatePosting',function(){

    let id = $(this).data('id');
    let btn = $(this);

    if(btn.data('loading')) return;

    Swal.fire({
        title: 'Konfirmasi Penerimaan Barang',
        text: 'Pastikan barang sudah diterima dan sesuai pesanan. Stok akan otomatis ditambahkan ke gudang.',
        icon: 'info',
        showCancelButton: true,
        confirmButtonColor: '#28a745',
        cancelButtonColor: '#6c757d',
        confirmButtonText: 'Ya, Terima Barang',
        cancelButtonText: 'Batal'
    }).then((result) => {

        if(result.isConfirmed){

            btn.data('loading', true)
               .prop('disabled', true)
               .html('Memposting...');

            $.post("<?= base_url('items/posting'); ?>", {
                id: id
                // tambahkan CSRF kalau perlu
            }, function(res){

                if(res && res.status){

                    table.ajax.reload(null,false);

                    Swal.fire({
                        icon: 'success',
                        title: 'Berhasil',
                        text: res.message || 'Data berhasil diposting',
                        timer: 1500,
                        showConfirmButton: false
                    });

                }else{

                    Swal.fire({
                        icon: 'error',
                        title: 'Gagal',
                        text: res?.message || 'Gagal memposting data'
                    });
                }

            },'json')
            .fail(function(xhr){

                Swal.fire({
                    icon: 'error',
                    title: 'Error',
                    text: xhr.responseJSON?.message || 'Server error ('+xhr.status+')'
                });

            })
            .always(function(){
                btn.prop('disabled', false)
                   .html('Posting')
                   .data('loading', false);
            });

        }
    });

});

// ================= DETAIL =================
$(document).on('click','.btn-detail',function(){

    let id = $(this).data('id');

    $.get("<?= base_url('items/detail/'); ?>"+id,function(res){

        let html = `
            <b>Nama :</b> ${res.item.nama_barang}<br>
            <b>Harga Beli :</b> ${formatRupiah(res.item.harga_beli)}<br>
            <b>Harga Jual :</b> ${formatRupiah(res.item.harga_jual)}
            <hr>
            <h6>Riwayat Stok</h6>
            <table class="table table-sm table-bordered">
            <tr>
                <th>Tanggal</th>
                <th>Gudang</th>
                <th>Qty</th>
                <th>Tipe</th>
                <th>Keterangan</th>
            </tr>
        `;

        res.logs.forEach(l=>{
            html += `
            <tr>
                <td>${l.created_at ?? '-'}</td>
                <td>${l.gudang ?? '-'}</td>
                <td>${l.qty}</td>
                <td>${l.tipe}</td>
                <td>${l.keterangan ?? ''}</td>
            </tr>`;
        });

        html += `</table>`;

        $('#detailContent').html(html);
        $('#modalDetail').modal('show');

    },'json');

});

// ================= OPEN ADJUST =================
$(document).on('click','.btn-adjust',function(){

    let id = $(this).data('id');

    if(!id){
        alert('Pilih item dari tabel (gunakan tombol Adjust di baris)');
        return;
    }

    selectedItemId = id;
    $('#adjust_item_id').val(id);
    $('#adjust_qty').val('');
    $('#adjust_ket').val('');

    $('#modalAdjust').modal('show');
});

// ================= SAVE ADJUST =================
$('#btnAdjust').click(function(){

    let btn = $(this);

    let data = {
        item_id: $('#adjust_item_id').val(),
        qty: $('#adjust_qty').val(),
        warehouse_id: $('#adjust_warehouse').val(),
        keterangan: $('#adjust_ket').val()
    };

    // 🔥 loading state
    btn.prop('disabled', true).html('Menyimpan...');

    $.post("<?= base_url('items/adjust'); ?>", data, function(res){

        if(res.status){

            $('#modalAdjust').modal('hide');
            table.ajax.reload(null,false);

            Swal.fire({
                icon: 'success',
                title: 'Berhasil',
                text: res.message || 'Stok berhasil disesuaikan',
                timer: 1500,
                showConfirmButton: false
            });

        }else{

            Swal.fire({
                icon: 'error',
                title: 'Gagal',
                text: res.message || 'Gagal melakukan penyesuaian'
            });
        }

    },'json')
    .fail(function(){

        Swal.fire({
            icon: 'error',
            title: 'Error',
            text: 'Koneksi ke server gagal'
        });

    })
    .always(function(){
        btn.prop('disabled', false).html('Simpan');
    });

});

});
</script>