@extends('layouts.main')

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

@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 Resep</a></li>
      </ol>
    </div>

    <!-- row -->
    <div class="row">
      <div class="col-12">
        <div class="card">
          <div class="card-header">
            <h4 class="card-title">Daftar Resep</h4>
            <button type="button" class="btn btn-primary float-right" data-bs-toggle="modal" data-bs-target="#addRecipeModal">
              <i class="fa fa-plus"></i> Tambah Resep
            </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>Kategori</th>
                    <th>Aksi</th>
                  </tr>
                </thead>
                <tbody>
                  @foreach ($recipes as $recipe)
                    <tr>
                      <td>{{ $loop->iteration }}</td>
                      <td>{{ $recipe->title }}</td>
                      <td>{{ $recipe->category }}</td>
                      <td>
                        <button type="button" class="btn btn-warning" data-bs-toggle="modal" data-bs-target="#editRecipeModalCenter{{ $recipe->id }}">
                          <i class="fa fa-edit"></i> Edit
                        </button>
                        <button type="button" class="btn btn-danger" onclick="confirmDelete({{ $recipe->id }})">
                          <i class="fa fa-trash"></i> Hapus
                        </button>
                        <form id="delete-form-{{ $recipe->id }}" action="{{ route('resep.destroy', $recipe->id) }}" method="POST" style="display: none;">
                          @csrf
                          @method('DELETE')
                        </form>
                      </td>
                    </tr>

<!-- Modal Edit Resep -->
<div class="modal fade" id="editRecipeModalCenter{{ $recipe->id }}" tabindex="-1" role="dialog" aria-labelledby="editRecipeModalCenterLabel" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="editRecipeModalCenterLabel">Edit Resep</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <form action="{{ route('resep.update', $recipe->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="{{ $recipe->title }}" required>
                    </div>
                    <div class="form-group">
                        <label for="description">Deskripsi:</label>
                        <textarea name="description" class="form-control">{{ $recipe->description }}</textarea>
                    </div>
                    <div class="form-group">
                        <label for="category">Kategori:</label>
                        <input type="text" name="category" class="form-control" value="{{ $recipe->category }}" required>
                    </div>
                    <div class="form-group">
                        <label for="ingredients">Bahan:</label>
                        <textarea name="ingredients" class="form-control" required>{{ $recipe->ingredients }}</textarea>
                    </div>
                    <div class="form-group">
                        <label for="instructions">Instruksi:</label>
                        <textarea name="instructions" class="form-control" required>{{ $recipe->instructions }}</textarea>
                    </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 Resep -->
<div class="modal fade" id="addRecipeModal" tabindex="-1" role="dialog" aria-labelledby="addRecipeModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="addRecipeModalLabel">Tambah Resep</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>
      <form action="{{ route('resep.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="description">Deskripsi:</label>
            <textarea name="description" class="form-control"></textarea>
          </div>
          <div class="form-group">
            <label for="category">Kategori:</label>
            <input type="text" name="category" class="form-control" required>
          </div>
          <div class="form-group">
            <label for="ingredients">Bahan:</label>
            <textarea name="ingredients" class="form-control" required></textarea>
          </div>
          <div class="form-group">
            <label for="instructions">Instruksi:</label>
            <textarea name="instructions" class="form-control" required></textarea>
          </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(recipeId) {
    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-' + recipeId).submit();
        }
    });
}

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