@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">Sub 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>Unit</th>
                                    <th>Code Unit</th>
                                    <th>Model Unit</th>
                                    <th style="width: 15%">Aksi</th>
                                </tr>
                            </thead>

                            <tbody>
                                @foreach ($subunit as $no => $su)
                                    <tr>
                                        <td>{{ $no + 1 }}</td>
                                        <td>{{ $su->unit->nama }}</td>
                                        <td>{{ $su->code }}</td>
                                        <td>{{ $su->model }}</td>
                                        <td>
                                            <button type="button" class="btn btn-sm btn-warning me-1"
                                                data-bs-toggle="modal" data-bs-target="#modalEdit{{ $su->id }}">
                                                <i class="fa-solid fa-pen-to-square"></i>
                                            </button>

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

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

                                    <div class="modal fade" id="modalEdit{{ $su->id }}" tabindex="-1">
                                        <div class="modal-dialog">
                                            <div class="modal-content">
                                                <form action="{{ route('subunit.update', $su->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">Unit</label>
                                                            <select name="unit_id" class="form-control">
                                                                @foreach ($unit as $key => $u)
                                                                    <option value="{{ $u->id }}"
                                                                        {{ $su->unit_id == $u->id ? 'selected' : '' }}>
                                                                        {{ $u->nama }}
                                                                    </option>
                                                                @endforeach
                                                            </select>
                                                        </div>

                                                        <div class="mb-3">
                                                            <label class="form-label">Code Unit</label>
                                                            <input type="text" name="code" value="{{ $su->code }}" class="form-control"
                                                                required>
                                                        </div>

                                                        <div class="mb-3">
                                                            <label class="form-label">Model Unit</label>
                                                            <input type="text" name="model" value="{{ $su->model }}" class="form-control"
                                                                required>
                                                        </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('subunit.add') }}" method="POST" enctype="multipart/form-data">
                    @csrf

                    <div class="modal-header">
                        <h5 class="modal-title">Tambah Sub 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">Unit</label>
                            <select name="unit_id" class="form-control">
                                @foreach ($unit as $key => $u)
                                    <option value="{{ $u->id }}">{{ $u->nama }}</option>
                                @endforeach
                            </select>
                        </div>

                        <div class="mb-3">
                            <label class="form-label">Code Unit</label>
                            <input type="text" name="code" class="form-control" required>
                        </div>

                        <div class="mb-3">
                            <label class="form-label">Model Unit</label>
                            <input type="text" name="model" class="form-control" required>
                        </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
