@extends('layout.app')
@section('content')
    <nav class="page-breadcrumb">
        <ol class="breadcrumb">
            <li class="breadcrumb-item"><a href="{{ route('dashboard') }}">Home</a></li>
            <li class="breadcrumb-item active" aria-current="page">Unit</li>
        </ol>
    </nav>

    <div class="row">
        <div class="col-md-12 grid-margin stretch-card">
            <div class="card">
                <div class="card-body">

                    <div class="d-flex justify-content-between mb-3">
                        <h4 class="card-title mb-0">Unit</h4>
                        <button type="button" class="btn btn-sm btn-primary" data-bs-toggle="modal"
                            data-bs-target="#modalAdd">
                            <i class="fa-solid fa-user-plus me-1"></i> Add New
                        </button>
                    </div>

                    <div class="table-responsive">
                        <table id="dataTableExample" class="table table-striped align-middle">
                            <thead>
                                <tr>
                                    <th>No.</th>
                                    <th>Icon</th>
                                    <th>Nama</th>
                                    <th>Keterangan</th>
                                    <th style="width: 15%">Aksi</th>
                                </tr>
                            </thead>

                            <tbody>
                                @foreach ($unit as $no => $u)
                                    <tr>
                                        <td>{{ $no + 1 }}</td>
                                        <td>
                                            @if ($u->icon)
                                                <img src="{{ asset('img/icon/' . $u->icon) }}" alt=""
                                                    style="width: 30px; border-radius: 50%;">
                                            @endif
                                        </td>
                                        <td>{{ $u->nama }}</td>
                                        <td>{{ $u->keterangan ?? '-' }}</td>
                                        <td>
                                            <button type="button" class="btn btn-sm btn-warning me-1"
                                                data-bs-toggle="modal" data-bs-target="#modalEdit{{ $u->id }}">
                                                <i class="fa-solid fa-pen-to-square"></i>
                                            </button>

                                            <form id="deleteForm{{ $u->id }}"
                                                action="{{ route('unit.delete', $u->id) }}" method="POST" class="d-inline">
                                                @csrf
                                                @method('DELETE')
                                            </form>

                                            <button class="btn btn-sm btn-danger"
                                                onclick="event.preventDefault(); document.getElementById('deleteForm{{ $u->id }}').submit();">
                                                <i class="fa-solid fa-trash"></i>
                                            </button>
                                        </td>
                                    </tr>

                                    <div class="modal fade" id="modalEdit{{ $u->id }}" tabindex="-1">
                                        <div class="modal-dialog">
                                            <div class="modal-content">
                                                <form action="{{ route('unit.update', $u->id) }}" method="POST">
                                                    @csrf
                                                    <div class="modal-header">
                                                        <h5 class="modal-title">Edit Unit</h5>
                                                        <button type="button" class="btn-close"
                                                            data-bs-dismiss="modal"></button>
                                                    </div>

                                                    <div class="modal-body">
                                                        <div class="mb-3">
                                                            <label class="form-label">Nama Unit</label>
                                                            <input type="text" name="nama"
                                                                value="{{ $u->nama }}" class="form-control" required>
                                                        </div>

                                                        <div class="mb-3">
                                                            <label class="form-label">Keterangan</label>
                                                            <textarea name="keterangan" rows="2" class="form-control">{{ $u->keterangan }}</textarea>
                                                        </div>

                                                        <div class="mb-3">
                                                            <label class="form-label">Icon</label>
                                                            <input type="file" name="icon" class="form-control">
                                                        </div>

                                                    </div>

                                                    <div class="modal-footer">
                                                        <button type="button" class="btn btn-secondary"
                                                            data-bs-dismiss="modal">Close</button>
                                                        <button type="submit" class="btn btn-primary">
                                                            <i class="fa-solid fa-floppy-disk me-1"></i> Save Changes
                                                        </button>
                                                    </div>
                                                </form>
                                            </div>
                                        </div>
                                    </div>
                                @endforeach
                            </tbody>
                        </table>
                    </div>

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

    {{-- MODAL ADD --}}
    <div class="modal fade" id="modalAdd" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <form action="{{ route('unit.add') }}" method="POST" enctype="multipart/form-data">
                    @csrf

                    <div class="modal-header">
                        <h5 class="modal-title">Tambah Unit</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                    </div>

                    <div class="modal-body">
                        <div class="mb-3">
                            <label class="form-label">Nama Unit</label>
                            <input type="text" name="nama" class="form-control" required>
                        </div>

                        <div class="mb-3">
                            <label class="form-label">Keterangan</label>
                            <textarea name="keterangan" rows="2" class="form-control"></textarea>
                        </div>

                        <div class="mb-3">
                            <label class="form-label">Icon</label>
                            <input type="file" name="icon" class="form-control">
                        </div>
                    </div>

                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                        <button type="submit" class="btn btn-primary">
                            <i class="fa-solid fa-save me-1"></i> Save
                        </button>
                    </div>

                </form>
            </div>
        </div>
    </div>
@endsection
