@extends('website.layouts.app')

@section('title')
    Kalkukasi referensi
@endsection

@section('content')
    <div class="row">
        <div class="col-12">
            <div class="page-title-box d-sm-flex align-items-center justify-content-between">
                <h4 class="mb-sm-0">Perhitungan TOPSIS</h4>

                <div class="page-title-right">
                    <ol class="breadcrumb m-0">
                        <li class="breadcrumb-item"><a href="javascript: void(0);">Perhitungan TOPSIS</a></li>
                        <li class="breadcrumb-item active">Alternatif (Tanaman)</li>
                    </ol>
                </div>

            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-lg-12">
            {{-- <div class="card border card-border-warning">
                <div class="card-header">
                    <h6 class="card-title mb-0">Baca Terlebih Dahulu !!!</h6>
                </div>
                <div class="card-body">
                    <p class="card-text">
                        Berikut adalah pengambilan parameter pada setiap kecamatan di Kabupaten Bondowoso.
                        Parameter dibawah ini terdapat data ketinggian per kecamatan. Data ketinggian bersumber dari
                        publikasi <a href="https://bondowosokab.bps.go.id/id/publication" target="_blank">Website BPS
                            Kabupaten Bondowoso</a>.
                    </p>
                </div>
            </div> --}}
            <div class="card">
                <div class="card-header d-flex align-items-center justify-content-between">
                    <h4 class="card-title mb-0 flex-grow-1">Perhitungan Topsis | Alternatif
                    </h4>

                    <div class="d-flex align-items-center">
                        <button class="btn btn-md btn-primary ms-2" data-bs-toggle="modal" data-bs-target="#btn-add">
                            tambah alternatif
                        </button>
                    </div>
                </div><!-- end card header -->

                <div class="card-body">
                    <div id="table-alternatif-referensi" class="table-responsive">
                    </div>
                    <div class="mt-2 d-flex justify-content-end">
                        <span id="btn-count-topsis" class="btn btn-success waves-effect waves-light">
                            Hitung
                        </span>
                    </div>
                </div><!-- end card-body -->
            </div><!-- end card -->
        </div>
        <!-- end col -->

        {{-- matriks ternomalisasi --}}
        <div class="col-lg-12 d-none" id="matriks-ternomalisasi-id">
            <div class="card">
                <div class="card-header d-flex align-items-center justify-content-between">
                    <h4 class="card-title mb-0 flex-grow-1">Perhitungan Topsis | Matriks Ternomalisasi
                    </h4>
                </div><!-- end card header -->

                <div class="card-body">
                    <div id="table-matriks-ternomalisasi-referensi" class="table-responsive">
                    </div>
                </div><!-- end card-body -->
            </div><!-- end card -->
        </div>

        {{-- matriks ternomalisasi terbobot --}}
        <div class="col-lg-12 d-none" id="matriks-ternomalisasi-terbobot-id">
            <div class="card">
                <div class="card-header d-flex align-items-center justify-content-between">
                    <h4 class="card-title mb-0 flex-grow-1">Perhitungan Topsis | Matriks Ternomalisasi Terbobot
                    </h4>
                </div><!-- end card header -->

                <div class="card-body">
                    <div id="table-ternomalisasi-terbobot-referensi" class="table-responsive">
                    </div>
                </div><!-- end card-body -->
            </div><!-- end card -->
        </div>

        {{-- solusi ideal positif dan negatif --}}
        <div class="col-lg-12 d-none" id="solusi-ideal-id">
            <div class="card">
                <div class="card-header d-flex align-items-center justify-content-between">
                    <h4 class="card-title mb-0 flex-grow-1">Perhitungan Topsis | Solusi Ideal
                    </h4>
                </div><!-- end card header -->

                <div class="card-body">
                    <div id="table-solusi-ideal-referensi" class="table-responsive">
                    </div>
                </div><!-- end card-body -->
            </div><!-- end card -->
        </div>

        {{-- solusi ideal terbobot --}}
        <div class="col-lg-12 d-none" id="solusi-ideal-terbobot-id">
            <div class="card">
                <div class="card-header d-flex align-items-center justify-content-between">
                    <h4 class="card-title mb-0 flex-grow-1">Perhitungan Topsis | Solusi Ideal Terbobot
                    </h4>
                </div><!-- end card header -->

                <div class="card-body">
                    <div id="table-solusi-ideal-terbobot-referensi" class="table-responsive">
                    </div>
                </div><!-- end card-body -->
            </div><!-- end card -->
        </div>

        {{-- hasil preferensi kecamatan --}}
        <div class="col-lg-12 d-none" id="hasil-preferensi-id">
            <div class="card">
                <div class="card-header d-flex align-items-center justify-content-between">
                    <h4 class="card-title mb-0 flex-grow-1">Perhitungan Topsis | Hasil Preferensi
                    </h4>
                </div><!-- end card header -->

                <div class="card-body">
                    <div id="table-preferensi-referensi" class="table-responsive">
                    </div>
                </div><!-- end card-body -->
            </div><!-- end card -->
        </div>
    </div>

    <!-- update alternatif referensi modals -->
    @foreach ($groupByPlant as $plant)
        <div class="modal fade" id="btn-edit-{{ $plant['plant_id'] }}" tabindex="-1" aria-labelledby="btn-editLabel"
            aria-modal="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="btn-editLabel">Ubah Data Alternatif (Tanaman)</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div class="modal-body">
                        <form action="{{ route('alternatif-referensi.updateAlternatifReferensi') }}" method="POST">
                            @csrf
                            <div class="row g-3">
                                <input type="hidden" name="plantId" value="{{ $plant['plant_id'] }}">
                                <div class="col-lg-12">
                                    <div>
                                        <label for="tanamanId" class="form-label">
                                            Jenis Tanaman
                                        </label>
                                        <input type="text" class="form-control mb-1" id="criteria" name=""
                                            placeholder="masukkan nilai kriteria" value="{{ $plant['plant'] }}" required
                                            readonly>
                                    </div>
                                </div><!--end col-->
                                @foreach ($criterias as $criteria)
                                    @php
                                        // Cari nilai yang sesuai dengan criteria_id, jika tidak ada, kosongkan
                                        $foundCrit = collect($plant['criteria'])->firstWhere(
                                            'criteria_id',
                                            $criteria['id'],
                                        );
                                        $value = $foundCrit ? $foundCrit['value_alternatif'] : '';
                                    @endphp

                                    <div class="col-lg-6">
                                        <div>
                                            <label for="criteria{{ $criteria->criteria }}" class="form-label">
                                                Masukkan {{ $criteria->criteria }}
                                            </label>
                                            <input type="text" class="form-control mb-1"
                                                id="criteria{{ $criteria->criteria }}" name="{{ $criteria->criteria }}"
                                                placeholder="masukkan nilai kriteria" required value="{{ $value }}"
                                                oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/^\.+|\.\.+/g, '').replace(/(\.\d*)\./g, '$1')">
                                        </div>
                                    </div><!--end col-->
                                @endforeach
                                <div class="col-lg-12">
                                    <div class="hstack gap-2 justify-content-end">
                                        <button type="button" class="btn btn-light"
                                            data-bs-dismiss="modal">Close</button>
                                        <button type="submit" class="btn btn-primary">Submit</button>
                                    </div>
                                </div><!--end col-->
                            </div><!--end row-->
                        </form>
                    </div>
                </div>
            </div>
        </div>
    @endforeach

    <!-- add alternatif referensi modals -->
    <div class="modal fade" id="btn-add" tabindex="-1" aria-labelledby="btn-addLabel" aria-modal="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="btn-addLabel">Tambah Data Alternatif (Tanaman)</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <form action="{{ route('alternatif-referensi.addAlternatifReferensi') }}" method="POST">
                        @csrf
                        <div class="row g-3">
                            @if ($plants->count() == 0)
                                <div class="col-lg-12">
                                    <div class="d-flex align-items-center justify-content-center text-success">
                                        <span>data alternatif tanaman sudah lengkap</span>
                                    </div>
                                </div>
                            @else
                                <div class="col-lg-12">
                                    <div>
                                        <label for="plantId" class="form-label">Pilih jenis tanaman</label>
                                        <select class="form-select mb-3" aria-label="Default select example"
                                            name="plantId" id="plantId">
                                            @foreach ($plants as $plant)
                                                <option value="{{ $plant->id }}">{{ $plant->plant }}</option>
                                            @endforeach
                                        </select>
                                    </div>
                                </div><!--end col-->
                                @foreach ($criterias as $criteria)
                                    <div class="col-lg-6">
                                        <div>
                                            <label for="criteria{{ $criteria->criteria }}" class="form-label">
                                                Masukkan {{ $criteria->criteria }}
                                            </label>
                                            <input type="text" class="form-control mb-1"
                                                id="criteria{{ $criteria->criteria }}" name="{{ $criteria->criteria }}"
                                                placeholder="masukkan nilai kriteria" required
                                                oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/^\.+|\.\.+/g, '').replace(/(\.\d*)\./g, '$1')">
                                        </div>
                                    </div><!--end col-->
                                @endforeach
                                <div class="col-lg-12">
                                    <div class="hstack gap-2 justify-content-end">
                                        <button type="button" class="btn btn-light"
                                            data-bs-dismiss="modal">Close</button>
                                        <button type="submit" class="btn btn-primary">Submit</button>
                                    </div>
                                </div><!--end col-->
                            @endif
                        </div><!--end row-->
                    </form>
                </div>
            </div>
        </div>
    </div>

    <script src="{{ asset('assets/libs/prismjs/prism.js') }}"></script>
    <script src="{{ asset('assets/libs/gridjs/gridjs.umd.js') }}"></script>
    <script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>

    {{-- <script src="{{ asset('assets/js/pages/gridjs.init.js') }}"></script> --}}

    {{-- custom js --}}
    <script src="{{ asset('assets/js/custom/referensi-alternatif.js') }}"></script>
@endsection
