<!-- Main content -->
<x-admin.contents>
    <x-slot:title>{{ $data['title'] }}</x-slot:title>
    <x-slot:menuOpen>{{ $data['menuOpen'] }} </x-slot:menuOpen>
    <x-slot:menuActive>{{ $data['menuActive'] }} </x-slot:menuActive>
    <x-slot:page>{{ $data['page'] }}</x-slot:page>
    <x-slot:head>

        <link rel="stylesheet"
            href="{{ asset('assets/') }}/admin/plugins/datatables-bs4/css/dataTables.bootstrap4.min.css">
        <link rel="stylesheet"
            href="{{ asset('assets/') }}/admin/plugins/datatables-responsive/css/responsive.bootstrap4.min.css">
        <link rel="stylesheet"
            href="{{ asset('assets/') }}/admin/plugins/datatables-buttons/css/buttons.bootstrap4.min.css">
        <link rel="stylesheet" href="{{ asset('') }}assets/admin/plugins/sweetalert2/sweetalert2.min.css">
    </x-slot:head>


    <section class="content">

        <!-- Default box -->
        <div class="row">
            <div class="col-lg-8">
                <x-message></x-message>
                <div class="card card-primary card-outline">
                    <div class="card-header">
                        <h3 class="card-title"><i class="fa fa-server text-primary"></i> {{ $data['page'] }}</h3>
                    </div>
                    <div class="card-body">
                        <table class="table table-bordered table-hover myTable tab">
                            <thead>
                                <tr>
                                    <th width="5%">No</th>
                                    <th>Nama Satuan</th>
                                    <th>Katerangan</th>
                                    <th>Tgl. Input</th>
                                    <th width="20%">Opsi</th>
                                </tr>
                            </thead>
                            <tbody id="view">
                                @forelse ($satuan as $s)
                                    <tr>
                                        <td class="text-center">{{ $no++ }}</td>
                                        <td>{{ $s->nm_satuan }}</td>
                                        <td>{{ $s->ktr_satuan }}</td>
                                        <td>{{ tgl_inf($s->created_at) }} </td>
                                        <td class="text-center">
                                            <button class="btn btn-primary btn-sm mb-0 mt-0 edit"
                                                data-id="{{ $s->id }}" data-toggle="modal"
                                                data-target="#edit{{ $s->id }}">
                                                <i class="fa fa-edit mr-2"></i> Edit
                                            </button>
                                            <button class="btn btn-danger btn-sm mb-0 mt-0 hapus"
                                                data-id="{{ $s->id }}" data-nm="{{ $s->nm_satuan }}">
                                                <i class="fa fa-trash mr-1"></i> Hapus
                                            </button>

                                        </td>
                                    </tr>
                                @empty
                                    <tr>
                                        <td colspan="6" class="text-center text-danger"> Data Kategori masih
                                            kosong.
                                        </td>
                                    </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>

            <div class="col-lg-4">
                <div class="card card-primary card-outline">
                    <div class="card-header">
                        <h3 class="card-title "><i class="fa fa-database text-primary"></i> Input {{ $data['page'] }}
                        </h3>
                    </div>
                    <form action="{{ route('input_satuan') }}" method="post">
                        <div class="card-body">
                            @csrf
                            <div class="form-group">
                                <label>Nama Satuan</label>
                                <input type="text" name="nm_satuan" class="form-control" placeholder="Nama Satuan"
                                    autofocus required value="{{ old('nm_satuan') }}">
                                @error('nm_satuan')
                                    <small class="text-danger mt-2">
                                        {{ $message }}
                                    </small>
                                @enderror
                            </div>
                            <div class="form-group">
                                <label>Keterangan</label>
                                <textarea name="keterangan" id=""class="form-control" placeholder="Keterangan...">{{ old('keterangan') }}</textarea>
                                @error('keterangan')
                                    <small class="text-danger mt-2">
                                        {{ $message }}
                                    </small>
                                @enderror
                            </div>
                        </div>
                        <div class="card-footer">
                            <button type="reset" class="btn btn-default"><i class="fa fa-sync-alt"></i>
                                Reset</button>
                            <button class="btn btn-primary float-right"><i class="fa fa-save"></i> Simpan</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
        <!-- /.card -->

    </section>


    @foreach ($satuan as $u)
        <div class="modal fade" id="edit{{ $u->id }}">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header bg-primary ">
                        <h4 class="modal-title">Edit Data</h4>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <form action="edit-satuan" method="POST">
                        <div class="modal-body">
                            @csrf
                            @method('put')
                            <input type="hidden" name="id" id="" value="{{ $u->id }}">
                            <input type="hidden" name="nmlama" id="" value="{{ $u->nm_satuan }}">
                            <div class="form-group">
                                <label>Nama Satuan</label>
                                <input type="text" name="nm_satuan" class="form-control" placeholder="Nama Satuan"
                                    autofocus required value="{{ $u->nm_satuan }}">
                                @error('nm_satuan')
                                    <small class="text-danger mt-2">
                                        {{ $message }}
                                    </small>
                                @enderror
                            </div>
                            <div class="form-group">
                                <label>Keterangan</label>
                                <textarea name="keterangan" id=""class="form-control">{{ $u->ktr_satuan }}</textarea>
                                @error('keterangan')
                                    <small class="text-danger mt-2">
                                        {{ $message }}
                                    </small>
                                @enderror
                            </div>


                        </div>
                        <div class="modal-footer justify-content-between">
                            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                            <button type="submit" class="btn btn-primary"><i class="fa fa-save"></i> Simpan</button>
                        </div>
                    </form>
                </div>
                <!-- /.modal-content -->
            </div>
            <!-- /.modal-dialog -->
        </div>
    @endforeach



    <x-slot:foot>
        <script src="{{ asset('assets/') }}/admin/plugins/datatables/jquery.dataTables.min.js"></script>
        <script src="{{ asset('assets/') }}/admin/plugins/datatables-bs4/js/dataTables.bootstrap4.min.js"></script>
        <script src="{{ asset('assets/') }}/admin/plugins/datatables-responsive/js/dataTables.responsive.min.js"></script>
        <script src="{{ asset('assets/') }}/admin/plugins/datatables-responsive/js/responsive.bootstrap4.min.js"></script>
        <script src="{{ asset('') }}assets/admin/plugins/sweetalert2/sweetalert2.all.min.js"></script>
        <script>
            $(function() {
                $('.myTable').DataTable();
            });

            $('#view').on('click', '.hapus', function() {
                // alert();
                var id = $(this).attr('data-id');
                var nama = $(this).attr('data-nm');

                Swal.fire({
                    title: "Apa anda yakin?",
                    text: "Sataun  dengan Nama ( " + nama +
                        " ) & data yang berkaitan dengan Data tersebut akan di hapus!",
                    icon: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#3085d6",
                    cancelButtonColor: "#d33",
                    confirmButtonText: "Ya, Hapus!"
                }).then((result) => {
                    if (result.isConfirmed) {
                        document.location.href = "delete-satuan/" + id;
                    }
                });

            });
        </script>
    </x-slot:foot>
</x-admin.contents>
