@extends('layouts/base_view')
@section('title')
{!! $variables['header_routes'] !!}
@endsection
@section('content')
<section class="content">
    <div class="container-fluid">
        <div class="row">
            <div class="col-12">
                <div class="card card-outline card-warning">
                    <div class="card-header">
                        <button type="button" class="btn btn-sm btn-warning" onclick="seedDefaults()">
                            <i class="fas fa-seedling"></i> Isi Data Default
                        </button>
                        <a href="{{ route('reason_adjustments.create') }}" class="btn btn-sm btn-warning button-cek-permission" data-permission="reason_adjustments.create">
                            <i class="fas fa-plus"></i> Tambah
                        </a>
                        </div>
                    </div>
                    <div class="card-body">
                        <div class="alert alert-info">
                            <i class="fa fa-info-circle"></i>
                            <strong>Info:</strong> Alasan Penyesuaian digunakan untuk mencatat alasan penyesuaian stok dalam LSM.
                            Alasan default: Big Order, GM Day, Forecast Issue.
                        </div>
                        <div class="table-responsive">
                            <table id="dataTable" class="table table-hover table-sm">
                                <thead class="bg-warning">
                                    <tr>
                                        <th width="5%">No</th>
                                        <th width="12%">Kode</th>
                                        <th width="25%">Nama</th>
                                        <th width="15%">Kategori</th>
                                        <th width="33%">Deskripsi</th>
                                        <th width="10%">Status</th>
                                    </tr>
                                </thead>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>

<script>
    $(document).ready(function() {
        $('#dataTable').DataTable({
            processing: true,
            serverSide: true,
            ajax: {
                url: "{{ route('reason_adjustments.index') }}",
                type: "GET"
            },
            columns: [{
                    data: 0,
                    name: 'no',
                    orderable: false,
                    searchable: false
                },
                {
                    data: 1,
                    name: 'code'
                },
                {
                    data: 2,
                    name: 'name'
                },
                {
                    data: 3,
                    name: 'category',
                    orderable: false
                },
                {
                    data: 4,
                    name: 'description'
                },
                {
                    data: 5,
                    name: 'is_active',
                    orderable: false
                }
            ],
            order: [
                [1, 'asc']
            ],
            pageLength: 25,
            responsive: false,
            language: {
                processing: "Memuat...",
                search: "Cari:",
                lengthMenu: "Tampilkan _MENU_ data",
                info: "Menampilkan _START_ sampai _END_ dari _TOTAL_ data",
                infoEmpty: "Menampilkan 0 sampai 0 dari 0 data",
                infoFiltered: "(difilter dari _MAX_ total data)",
                paginate: {
                    first: "Pertama",
                    last: "Terakhir",
                    next: "Selanjutnya",
                    previous: "Sebelumnya"
                },
                emptyTable: "Tidak ada data tersedia",
                zeroRecords: "Tidak ada data yang cocok"
            }
        });
    });

    function seedDefaults() {
        Swal.fire({
            title: 'Isi Data Default?',
            text: 'Ini akan membuat alasan penyesuaian default: Big Order, GM Day, Forecast Issue',
            icon: 'question',
            showCancelButton: true,
            confirmButtonText: 'Ya, Isi Data!',
            cancelButtonText: 'Batal'
        }).then((result) => {
            if (result.isConfirmed) {
                $.ajax({
                    url: "{{ route('reason_adjustments.seed_defaults') }}",
                    type: "POST",
                    data: {
                        _token: "{{ csrf_token() }}"
                    },
                    success: function(response) {
                        Swal.fire({
                            title: 'Berhasil!',
                            text: 'Data default berhasil dibuat',
                            icon: 'success',
                            confirmButtonText: 'OK'
                        }).then(() => {
                            $('#dataTable').DataTable().ajax.reload();
                        });
                    },
                    error: function(xhr) {
                        Swal.fire({
                            title: 'Error!',
                            text: 'Gagal membuat data default',
                            icon: 'error',
                            confirmButtonText: 'OK'
                        });
                    }
                });
            }
        });
    }
</script>
@endsection