@include('includes.admin_header')

<div class="d-flex" id="wrapper">
  <!-- Sidebar -->
  @include('includes.admin_sidebar')

    <!-- Main Content -->

    <div class="container-fluid px-4 py-4">
    <!--<h2 class="mb-4"><i class="bi bi-box2-fill me-2"></i>Speed Rating Dashboard</h2>-->
                            <div class="row">
            <div class="col-md-6">
                <h2 class="mb-4"><i class="bi bi-box2-fill me-2"></i>Speed Rating Dashboard</h2>
            </div>
            <div class="col-md-6" style="text-align: right;">
                <a href="{{ url('admin/manage-speed-rating') }}" class="btn btn-primary">Add new Speed Rating</a>
            </div>
        </div>

    <!-- Add Inventory Form -->
    <div class="form-container mb-5">
    @if (session('error'))
    <div class="alert alert-danger alert-dismissible">
    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            {{ session('error') }}
        </div>
    @endif
    @if (session('success'))
    <div class="alert alert-success alert-dismissible">
    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            {{ session('success') }}
        </div>
  @endif
      <h5 class="mb-3">Add New Speed Rating</h5>
      @if(Request::segment(3))
      <form action="{{ route('update-speed-rating') }}" method="post" enctype="multipart/form-data">
      <input type="hidden" name="id" value="{{ $speed_rating_detail->id }}">
      @else
      <form action="{{ route('save-speed-rating') }}" method="post" enctype="multipart/form-data">
        @endif
      @csrf
        <div class="row g-3">
          <!-- Product Name -->
        <div class="col-md-6">
            <label class="form-label">Rating</label>
            <div class="input-group">
              <span class="input-group-text"><i class="bi bi-card-text"></i></span>
              <input type="text" class="form-control" name="rating" placeholder="e.g. A1" value="@if(Request::segment(3)){{ $speed_rating_detail->rating }}@endif" required>
            </div>
          </div>

          <div class="col-md-6">
            <label class="form-label">Speed(In MPH)</label>
            <div class="input-group">
              <span class="input-group-text"><i class="bi bi-card-text"></i></span>
              <input type="number" min="0" class="form-control" name="rating_in_mph" placeholder="e.g. 75" value="@if(Request::segment(3)){{ $speed_rating_detail->rating_in_mph }}@endif" required onkeyup="convert_speed(this);">
            </div>
          </div>

          <div class="col-md-6">
            <label class="form-label">Speed(In KM/H)</label>
            <div class="input-group">
              <span class="input-group-text"><i class="bi bi-card-text"></i></span>
              <input type="number" class="form-control" name="rating_in_kmph" id="rating_in_kmph" placeholder="e.g. 50" value="@if(Request::segment(3)){{ $speed_rating_detail->rating_in_kmph }}@endif" required readonly>
            </div>
          </div>

          <div class="col-12 text-end">
          @if(Request::segment(3))
          <button type="submit" class="btns btns-hover"><span><i class="bi bi-save me-1"></i> Update Speed Rating</span></button>
          @else
            <button type="submit" class="btns btns-hover"><span><i class="bi bi-save me-1"></i> Save Speed Rating</span></button>
          @endif
          </div>
        </div>
      </form>
    </div>

    <!-- Inventory Table -->
    <div class="table-responsive bg-white p-4 rounded shadow-sm">
      <h5 class="mb-3">Current Rim Width</h5>
      <table class="table align-middle table-bordered invt">
        <thead class="table-light">
          <tr>
            <th>#</th>
            <th>Rating</th>
            <th>Rating (In MPH)</th>
            <th>Rating (In KMPH)</th>
            <th>Actions</th>
          </tr>
        </thead>
        <tbody>
           @foreach($speedratingdata as $key => $p)
          <tr>
            <td>{{ $key+1 }}</td>
            <td>{{ $p->rating }}</td>
            <td>{{ $p->rating_in_mph }}</td>
            <td>{{ $p->rating_in_kmph }}</td>
            <td>
              <a href="{{ url('/') }}/admin/edit-speed-rating/{{ $p->id }}" class="btn btn-sm btn-outline-info"><i class="bi bi-pencil"></i></a>
              <button class="btn btn-sm btn-outline-danger" onclick="confirmDelete({{ $p->id }})"><i class="bi bi-trash"></i></button>
            </td>
          </tr>
          @endforeach
        </tbody>
      </table>
    </div>
  </div>

</div>
</div>
@include('includes.admin_footer')

<script>
function convert_speed(e)
{
    var mph = $(e).val();
      const kmph = mph * 1.60934;
      var final_kmph = Math.round(kmph);
      $("#rating_in_kmph").val(final_kmph);
}
function confirmDelete(id) {
        Swal.fire({
            title: 'Are you sure?',
            text: "This action cannot be undone.",
            icon: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#d33',
            cancelButtonColor: '#3085d6',
            confirmButtonText: 'Yes, delete it!'
        }).then((result) => {
            if (result.isConfirmed) {

              $.ajaxSetup({
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    }
                });
                var postdata = {
                  "id":id
                }
                  $.ajax({
                      url: '{{ url('/') }}/admin/delete-speed-rating',
                      type: 'POST',
                      data: postdata,
                      dataType: 'json',
                      success: function (res) {
                        if(res.status == false)
                        {
                            Swal.fire({
                                title: 'Error!',
                                text: 'Something went wrong',
                                icon: 'warning',
                                confirmButtonText: 'OK'
                            });
                        }
                        else
                        {
                            Swal.fire({
                                title: 'Success!',
                                text: 'Data deleted successfully.',
                                icon: 'success',
                                confirmButtonText: 'OK'
                                }).then((result) => {
                                if (result.isConfirmed) {
                                  location.href = '{{ url('/') }}/admin/manage-speed-rating';
                                }
                            });
                        }

                      }
                  });
                
            }
        });
    }

</script>