<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 Gudang</h5>
                <button class="btn btn-warning btn-add">Tambah Gudang</button>
            </div>

            <div class="table-responsive">
                <table id="tableWarehouse" class="table modern-table align-middle w-100">
                    <thead class="table-light">
                        <tr>
                            <th>No</th>
                            <th>Nama Gudang</th>
                            <th>Aksi</th>
                        </tr>
                    </thead>
                </table>
            </div>

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

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

<div class="modal-header bg-warning text-white">
    <h5 class="modal-title">Gudang</h5>
    <button class="btn-close" data-bs-dismiss="modal"></button>
</div>

<div class="modal-body">

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

    <label>Nama Gudang</label>
    <input type="text" id="nama" class="form-control">

</div>

<div class="modal-footer">
    <button class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
    <button class="btn btn-warning" id="btnSimpan">Simpan</button>
</div>

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

<script>
$(function(){

    let action = 'add';

    let table = $('#tableWarehouse').DataTable({
        ajax:{
            url:"<?= base_url('warehouses/get_data'); ?>",
            type:"POST"
        }
    });

    function resetForm(){
        $('#id').val('');
        $('#nama').val('');
    }

    // ADD
    $('.btn-add').click(function(){
        resetForm();
        action = 'add';
        $('#modalWarehouse').modal('show');
    });

    // EDIT
    $(document).on('click','.btn-editgudang',function(){
        let id = $(this).data('id');

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

            $('#id').val(res.id);
            $('#nama').val(res.nama);

            action = 'edit';
            $('#modalWarehouse').modal('show');

        },'json');
    });

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

        let data = {
            id: $('#id').val(),
            nama: $('#nama').val()
        };

        if(!data.nama){
            Swal.fire('Warning','Nama gudang wajib diisi','warning');
            return;
        }

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

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

            if(res.status){
                $('#modalWarehouse').modal('hide');
                table.ajax.reload(null,false);
                Swal.fire('Sukses',res.message,'success');
            } else {
                Swal.fire('Error',res.message,'error');
            }

        },'json');
    });

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

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

        Swal.fire({
            title:'Hapus gudang?',
            text:'Pastikan tidak dipakai item',
            icon:'warning',
            showCancelButton:true
        }).then(r=>{
            if(r.isConfirmed){

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

                    if(res.status){
                        table.ajax.reload(null,false);
                        Swal.fire('Sukses',res.message,'success');
                    } else {
                        Swal.fire('Error',res.message,'error');
                    }

                },'json');

            }
        });

    });

});
</script>