@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">Mekanik</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">Daftar Mekanik</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>Nama</th>
                                    <th>No. Telp</th>
                                    <th>Alamat</th>
                                    <th>Username</th>
                                    <th style="width: 15%">Aksi</th>
                                </tr>
                            </thead>

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

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

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

                                    {{-- MODAL EDIT --}}
                                    <div class="modal fade" id="modalEdit{{ $m->id }}" tabindex="-1">
                                        <div class="modal-dialog">
                                            <div class="modal-content">
                                                <form action="{{ route('mekanik.update', $m->id) }}" method="POST"
                                                    enctype="multipart/form-data">
                                                    @csrf
                                                    <div class="modal-header">
                                                        <h5 class="modal-title">Edit Mekanik</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</label>
                                                            <input type="text" name="nama" class="form-control"
                                                                value="{{ $m->nama }}" required>
                                                        </div>

                                                        <div class="mb-3">
                                                            <label class="form-label">No. Telp</label>
                                                            <input type="number" name="no_telp" class="form-control"
                                                                value="{{ $m->no_telp }}">
                                                        </div>

                                                        <div class="mb-3">
                                                            <label class="form-label">Alamat</label>
                                                            <textarea name="alamat" rows="2" class="form-control">{{ $m->alamat }}</textarea>
                                                        </div>

                                                        <div class="mb-3">
                                                            <label class="form-label">Username</label>
                                                            <input type="text" name="username" class="form-control"
                                                                value="{{ $m->username }}" required>
                                                        </div>

                                                        <div class="mb-3">
                                                            <label class="form-label">Password</label>
                                                            <input type="password" name="password" class="form-control"
                                                                placeholder="Biarkan kosong jika tidak diubah">
                                                        </div>

                                                        <div class="mb-3">
                                                            <label class="form-label">Foto</label>
                                                            <input type="file" name="foto" 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('mekanik.add') }}" method="POST" enctype="multipart/form-data">
                    @csrf

                    <div class="modal-header">
                        <h5 class="modal-title">Tambah Mekanik</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</label>
                            <input type="text" name="nama" class="form-control" required>
                        </div>

                        <div class="mb-3">
                            <label class="form-label">No. Telp</label>
                            <input type="number" name="no_telp" class="form-control">
                        </div>

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

                        <div class="mb-3">
                            <label class="form-label">Username</label>
                            <input type="text" name="username" class="form-control" required>
                        </div>

                        <div class="mb-3">
                            <label class="form-label">Password</label>
                            <input type="password" name="password" class="form-control" required>
                        </div>

                        <div class="mb-3">
                            <label class="form-label">Foto</label>
                            <input type="file" name="foto" 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
