@extends('layout.main')
@section('body')
    @php
        $menu = 'Data Master';
        $pageTitle = 'Data Kriteria';
    @endphp

    @include('layout.navbar')
    @include('layout.sidebar')

    {{-- Main Content --}}
    <div class="main-container">
        <div class="pd-ltr-20 xs-pd-20-10">
            <div class="min-height-200px">
                <div class="page-header">
                    <div class="row">
                        <div class="col-md-6 col-sm-12">
                            <div class="title">
                                <h4>{{ $menu }}</h4>
                            </div>
                            <nav aria-label="breadcrumb" role="navigation">
                                <ol class="breadcrumb">
                                    <li class="breadcrumb-item"><a href="{{ route('dashboard.index') }}">Dashboard</a></li>
                                    @if (isset($pageTitle))
                                        <li class="breadcrumb-item active" aria-current="page">{{ $pageTitle }}</li>
                                    @else
                                        <li class="breadcrumb-item active" aria-current="page"></li>
                                    @endif
                                </ol>
                            </nav>
                        </div>
                    </div>
                </div>
                <div class="card-box mb-30">
                    <div class="pd-20">
                        <h4 class="text-blue h4 mb-0">Data Kriteria</h4>
                    </div>
                    <div class="card-header d-flex align-items-center">
                        <!-- Button trigger modal -->
                        <button type="button" class="btn btn-primary mx-2" data-toggle="modal" data-target="#add-criteria">
                            <i class="icon dw dw-file"></i> Tambah Kriteria</button>
                        <button type="button" class="btn btn-primary mx-2" id="calculate-bobot">
                            <i class="icon dw dw-calculator"></i> Hitung Bobot
                        </button>
                        <div id="bobot-kriteria"></div>
                        <form class="form-inline ml-auto" action="{{ route('kriteria.index') }}" method="GET">
                            <div class="input-group">
                                <input type="search" class="form-control" name="search" placeholder="Search">
                                <div class="input-group-append">
                                    <button class="btn btn-primary" type="submit">
                                        <i class="icon dw dw-search"></i>
                                    </button>
                                </div>
                            </div>
                        </form>
                    </div>
                    <div class="pb-10">
                        <div class="table-responsive">
                            <table class="data-table table stripe hover nowrap">
                                <thead>
                                    <tr>
                                        <th>No</th>
                                        <th>Nama Kriteria</th>
                                        <th>Prioritas</th>
                                        <th>Bobot</th>
                                        <th>Aksi</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @forelse ($criterias as $index => $criteria)
                                        <tr>
                                            <td>{{ $index + $criterias->firstItem() }}</td>
                                            <td>{{ $criteria->nama }}</td>
                                            <td>{{ $criteria->prioritas }}</td>
                                            <td>
                                                @if ($criteria->bobot)
                                                    {{ $criteria->bobot }}
                                                @else
                                                    <em>Belum dihitung</em>
                                                @endif
                                            </td>
                                            <td>
                                                <a href="{{ route('kriteria.edit', $criteria->id) }}"
                                                    class="badge btn-primary" data-toggle="modal"
                                                    data-target="#edit-criteria-{{ $criteria->id }}" title="Edit">
                                                    <i class="icon dw dw-edit2"></i>
                                                </a>
                                                {{-- <form action="{{ route('kriteria.destroy', $criteria->id) }}" method="POST">
                                                    @csrf
                                                    @method('DELETE')
                                                    <button type="submit" class="badge btn-danger" title="Delete">
                                                        <i class="icon dw dw-delete-3"></i>
                                                    </button>
                                                </form> --}}
                                                <a href="{{ route('kriteria.destroy', $criteria->id) }}"
                                                    class="badge btn-danger" data-toggle="modal"
                                                    data-target="#delete-criteria-{{ $criteria->id }}" title="Delete">
                                                    <i class="icon dw dw-delete-3"></i>
                                                </a>
                                            </td>
                                        </tr>
                                    @empty
                                        <tr>
                                            <td colspan="5" class="text-center">Tidak ada data.</td>
                                        </tr>
                                     @endforelse
                                </tbody>
                            </table>
                            <div>
                                <div style="display: flex; justify-content: space-between; align-items: center; margin: 0 20px;">
                                    <div style="font-size: 14px;">
                                        Showing data {{ $criterias->firstItem() }} to {{ $criterias->lastItem() }} of {{ $criterias->total() }} entries
                                    </div>
                                    <div style="font-size: 14px;">{{ $criterias->links() }}</div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="footer-wrap pd-20 mb-20 card-box">
                <strong>Sistem Pendukung Keputusan Pemilihan Ekstrakurikuler Metode SMARTER</strong>
            </div>
        </div>
    </div>

    {{-- Modal Add --}}
    <div class="modal fade" id="add-criteria" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel"
        style="display: none;" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered">
            <div class="modal-content">
                @if (session('error'))
                    <div class="alert alert-danger">{{ session('error') }}</div>
                @endif
                <div class="modal-header">
                    <h4 class="modal-title" id="myLargeModalLabel">
                        Tambah Kriteria
                    </h4>
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
                </div>
                <div class="modal-body">
                    <form action="{{ route('kriteria.store') }}" class="needs-validation" novalidate="" method="POST">
                        @csrf
                        <div class="card-body">
                            <div class="form-group row">
                                <label class="col-sm-2 col-form-label">Nama</label>
                                <div class="col-sm-10">
                                    <input type="text" class="form-control" id="nama" name="nama" required="">
                                    <div class="invalid-feedback">
                                        Tolong isi Nama Kriteria!
                                    </div>
                                </div>
                            </div>
                            @if ($errors->has('nama'))
                                <div class="alert alert-danger">{{ $errors->first('nama') }}</div>
                            @endif
                            <div class="form-group row">
                                <label class="col-sm-2 col-form-label">Prioritas</label>
                                <div class="col-sm-10">
                                    <input type="text" class="form-control" id="prioritas" name="prioritas"
                                        required="">
                                    <div class="valid-feedback">
                                        Lengkap!
                                    </div>
                                </div>
                            </div>
                            @if ($errors->has('prioritas'))
                                <div class="alert alert-danger">{{ $errors->first('prioritas') }}</div>
                            @endif
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" data-dismiss="modal">Batal</button>
                            <button type="submit" class="btn btn-primary">Simpan</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    {{-- Modal Edit --}}
    @foreach ($criterias as $criteria)
        <div class="modal fade" id="edit-criteria-{{ $criteria->id }}" tabindex="-1" role="dialog"
            aria-labelledby="myLargeModalLabel" style="display: none;" aria-hidden="true">
            <div class="modal-dialog modal-dialog-centered">
                <div class="modal-content">
                    <div class="modal-header">
                        <h4 class="modal-title" id="myLargeModalLabel">
                            Edit Kriteria
                        </h4>
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
                    </div>
                    <div class="modal-body">
                        <form action="{{ route('kriteria.update', $criteria->id) }}" class="needs-validation"
                            novalidate="" method="POST">
                            @csrf
                            @method('PUT')
                            <div class="card-body">
                                <div class="form-group row">
                                    <label class="col-sm-2 col-form-label">Nama</label>
                                    <div class="col-sm-10">
                                        <input type="text" class="form-control" id="nama" name="nama"
                                            required="" value="{{ $criteria->nama }}">
                                        <div class="invalid-feedback">
                                            Tolong isi Nama Kriteria!
                                        </div>
                                    </div>
                                </div>
                                @if ($errors->has('nama'))
                                    <div class="alert alert-danger">{{ $errors->first('nama') }}</div>
                                @endif
                                <div class="form-group row">
                                    <label class="col-sm-2 col-form-label">Prioritas</label>
                                    <div class="col-sm-10">
                                        <input type="text" class="form-control" id="prioritas" name="prioritas"
                                            required="" value="{{ $criteria->prioritas }}">
                                        <div class="invalid-feedback">
                                            Tolong isi Urutan Prioritas!
                                        </div>
                                    </div>
                                </div>
                                @if ($errors->has('prioritas'))
                                    <div class="alert alert-danger">{{ $errors->first('prioritas') }}</div>
                                @endif
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-secondary" data-dismiss="modal">Batal</button>
                                <button type="submit" class="btn btn-primary">Simpan</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>

    <!-- Modal Delete -->
    <div class="modal fade" id="delete-criteria-{{ $criteria->id }}" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel"
        style="display: none;" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title" id="myLargeModalLabel">
                        Konfirmasi Hapus Data Kriteria
                    </h4>
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
                </div>
                <div class="modal-body">
                    <p>Apakah Anda yakin ingin menghapus data kriteria {{ $criteria->nama }} ini?</p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Batal</button>
                    <form action="{{ route('kriteria.destroy', $criteria->id) }}" method="POST">
                        @csrf
                        @method('DELETE')
                        <button type="submit" class="btn btn-danger">Hapus</button>
                    </form>
                </div>
            </div>
        </div>
    </div>
    @endforeach
    <script>
        $(document).ready(function() {
            $('#calculate-bobot').click(function() {
                $.ajax({
                    type: 'POST',
                    url: '{{ route('kriteria.calculate') }}',
                    data: {
                        _token: '{{ csrf_token() }}'
                    },
                    success: function(response) {
                        location.reload();
                    },
                    error: function(xhr, status, error) {
                        console.error('Error:', error);
                    }
                });
            });
        });
    </script>
@endsection
