@extends('layouts.main')

@section('title', 'Daftar Bookmarks')

@section('content')
<div class="content-body">
  <div class="container-fluid">
    <div class="row page-titles">
      <ol class="breadcrumb">
        <li class="breadcrumb-item active"><a href="javascript:void(0)">Beranda</a></li>
        <li class="breadcrumb-item"><a href="javascript:void(0)">Daftar Bookmarks</a></li>
      </ol>
    </div>

    <!-- row -->
    <div class="row">
      <div class="col-12">
        <div class="card">
          <div class="card-header">
            <h4 class="card-title">Daftar Bookmarks</h4>
            <button type="button" class="btn btn-primary float-right" data-bs-toggle="modal" data-bs-target="#addBookmarkModal">
              <i class="fa fa-plus"></i> Tambah Bookmark
            </button>
          </div>
          <div class="card-body">
            <div class="table-responsive">
              <table id="example2" class="display" style="min-width: 845px">
                <thead>
                  <tr>
                    <th>No</th>
                    <th>Judul</th>
                    <th>URL</th>
                    <th>Kategori</th>
                    <th>Aksi</th>
                  </tr>
                </thead>
                <tbody>
                  @foreach ($bookmarks as $bookmark)
                    <tr>
                      <td>{{ $loop->iteration }}</td>
                      <td>{{ $bookmark->title }}</td>
                      <td><a href="{{ $bookmark->url }}" target="_blank">{{ $bookmark->url }}</a></td>
                      <td>{{ $bookmark->category }}</td>
                      <td>
                        <button type="button" class="btn btn-warning" data-bs-toggle="modal" data-bs-target="#editBookmarkModal{{ $bookmark->id }}">
                          <i class="fa fa-edit"></i> Edit
                        </button>
                        <button type="button" class="btn btn-danger" onclick="confirmDelete({{ $bookmark->id }})">
                          <i class="fa fa-trash"></i> Hapus
                        </button>
                        <form id="delete-form-{{ $bookmark->id }}" action="{{ route('bookmarks.destroy', $bookmark->id) }}" method="POST" style="display: none;">
                          @csrf
                          @method('DELETE')
                        </form>
                      </td>
                    </tr>

<!-- Modal Edit Bookmark -->
<div class="modal fade" id="editBookmarkModal{{ $bookmark->id }}" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Edit Bookmark</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <form action="{{ route('bookmarks.update', $bookmark->id) }}" method="POST">
                @csrf
                @method('PUT')
                <div class="modal-body">
                    <div class="form-group">
                        <label for="title">Judul:</label>
                        <input type="text" name="title" class="form-control" value="{{ $bookmark->title }}" required>
                    </div>
                    <div class="form-group">
                        <label for="url">URL:</label>
                        <input type="url" name="url" class="form-control" value="{{ $bookmark->url }}" required>
                    </div>
                    <div class="form-group">
                        <label for="category">Kategori:</label>
                        <input type="text" name="category" class="form-control" value="{{ $bookmark->category }}" required>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Tutup</button>
                    <button type="submit" class="btn btn-success" onclick="setLoading(this)">Update</button>
                </div>
            </form>
        </div>
    </div>
</div>
                  @endforeach
                </tbody>
              </table>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

<!-- Modal Tambah Bookmark -->
<div class="modal fade" id="addBookmarkModal" tabindex="-1" role="dialog" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">Tambah Bookmark</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>
      <form action="{{ route('bookmarks.store') }}" method="POST">
        @csrf
        <div class="modal-body">
          <div class="form-group">
            <label for="title">Judul:</label>
            <input type="text" name="title" class="form-control" required>
          </div>
          <div class="form-group">
            <label for="url">URL:</label>
            <input type="url" name="url" class="form-control" required>
          </div>
          <div class="form-group">
            <label for="category">Kategori:</label>
            <input type="text" name="category" class="form-control" required>
          </div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Tutup</button>
          <button type="submit" class="btn btn-success" onclick="setLoading(this)">Simpan</button>
        </div>
      </form>
    </div>
  </div>
</div>
@endsection

@section('scripts')
<script>
function confirmDelete(bookmarkId) {
    Swal.fire({
        title: 'Apakah kamu yakin?',
        text: "Data ini akan dihapus secara permanen!",
        icon: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33',
        confirmButtonText: 'Ya, hapus!'
    }).then((result) => {
        if (result.isConfirmed) {
            document.getElementById('delete-form-' + bookmarkId).submit();
        }
    });
}

function setLoading(button) {
    button.disabled = true;
    button.innerHTML = 'Loading...';
    button.form.submit();
}
</script>
@endsection
