@extends('website.layouts.app')

@section('title')
    Kalkukasi Desa / Kelurahan
@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 (Desa / Kelurahan)</li>
                    </ol>
                </div>

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

    <div class="row">
        <div class="col-lg-12">
            <div class="card">
                <div class="card-header d-flex align-items-center justify-content-between  flex-wrap gap-2">
                    <h4 class="card-title mb-0 flex-grow-1 mb-2">Perhitungan Topsis | Alternatif Desa
                    </h4>

                    <div class="d-flex">
                        <div class="ms-1">
                            <span id="btn-update-alternatif-village" class="btn btn-warning waves-effect waves-light">
                                Perbarui
                            </span>
                        </div>
                        <div class="ms-1">
                            <button class="btn btn-md btn-primary btn-add-alternatif-village" id="btn-add-alternatif"
                                data-bs-toggle="modal" data-bs-target="#btn-add">
                                tambah alternatif
                            </button>
                        </div>
                    </div>
                </div><!-- end card header -->

                <div class="card-body">
                    <div class="d-flex mb-3">
                        <select class="form-select m-1" id="choiceMonth" aria-label="pilih-bulan">
                            <option selected value="01">01</option>
                            <option value="02">02</option>
                            <option value="03">03</option>
                            <option value="04">04</option>
                            <option value="05">05</option>
                            <option value="06">06</option>
                            <option value="07">07</option>
                            <option value="08">08</option>
                            <option value="09">09</option>
                            <option value="10">10</option>
                            <option value="11">11</option>
                            <option value="12">12</option>
                        </select>
                        @php
                            // Tahun yang sudah ada di dropdown
                            $existingYears = [2021, 2022, 2023];

                            // Tahun tambahan dari Controller (misal dari database)
                            $newYears = $years; // Data ini bisa berasal dari database atau controller

                            // Gabungkan array dan hapus duplikat
                            $allYears = array_unique(array_merge($existingYears, $newYears));

                            // Urutkan tahun secara ascending
                            sort($allYears);
                        @endphp
                        <select class="form-select m-1" id="choiceYear" aria-label="pilih-tahun">
                            @foreach ($allYears as $year)
                                <option value="{{ $year }}">{{ $year }}</option>
                            @endforeach
                        </select>
                        <select class="form-select m-1" id="choiceSubDistrict" aria-label="pilih-kecamatan">
                            @foreach ($listSubDistrict as $subDistrict)
                                <option value="{{ $subDistrict->id }}"
                                    {{ $subDistrict->sub_district == 'Kec Cermee' ? 'selected' : '' }}>
                                    {{ $subDistrict->sub_district }}
                                </option>
                            @endforeach
                        </select>
                    </div>
                    <div id="table-alternatif-village" 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 d-none">
                            Hitung
                        </span>
                    </div>
                </div><!-- end card-body -->
            </div><!-- end card -->
        </div>

        {{-- 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-village" 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-village" 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-village" 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-village" class="table-responsive">
                    </div>
                </div><!-- end card-body -->
            </div><!-- end card -->
        </div>

        {{-- hasil preferensi desa --}}
        <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-hasil-preferensi-village" class="table-responsive">
                    </div>
                </div><!-- end card-body -->
            </div><!-- end card -->
        </div>

        {{-- hasil tanaman per desa --}}
        <div class="col-lg-12 d-none" id="hasil-plantByVillage-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 Tanaman Per Desa
                    </h4>
                </div><!-- end card header -->

                <div class="card-body">
                    <div id="table-hasil-plantByVillage" class="table-responsive">
                    </div>
                </div><!-- end card-body -->
            </div><!-- end card -->
        </div>
    </div>

    <!-- update alternatif referensi modals -->
    <div class="modal fade" id="modal-edit" 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 (Desa)</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <form action="{{ route('village-alternatif.updateAlternatifByVillage') }}" method="POST">
                        @csrf
                        <div class="row g-3">
                            <input type="hidden" name="villageId" id="villageId">
                            <div class="col-lg-12">
                                <div>
                                    <label for="village" class="form-label">
                                        Desa
                                    </label>
                                    <input type="text" class="form-control mb-1" id="village" name="village"
                                        required readonly>
                                </div>
                            </div><!--end col-->

                            <div class="col-lg-6">
                                <div>
                                    <label for="month" class="form-label">
                                        Bulan
                                    </label>
                                    <input type="text" class="form-control mb-1" id="month" name="month"
                                        required readonly>
                                </div>
                            </div><!--end col-->
                            <div class="col-lg-6">
                                <div>
                                    <label for="year" class="form-label">
                                        Tahun
                                    </label>
                                    <input type="text" class="form-control mb-1" id="year" name="year"
                                        required readonly>
                                </div>
                            </div><!--end col-->


                            <div class="col-lg-12">
                                <div class="row" id="criteriaFields"></div>
                            </div>

                            <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>

    <!-- 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 (Desa)</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <form action="{{ route('village-alternatif.addAlternatifVillage') }}" method="POST">
                        @csrf
                        <div class="row g-3">
                            @if ($listSubDistrict->count() == 0)
                                <div class="col-lg-12">
                                    <div class="d-flex align-items-center justify-content-center text-success">
                                        <span>data alternatif desa sudah lengkap</span>
                                    </div>
                                </div>
                            @else
                                <div class="col-lg-12">
                                    <div>
                                        <label for="subDistrictId" class="form-label">Pilih Kecamatan</label>
                                        <select class="form-select mb-3" aria-label="Default select example"
                                            name="subDistrictId" id="subDistrictId" required>
                                            @foreach ($listSubDistrict as $subDistrict)
                                                <option value="{{ $subDistrict->id }}">{{ $subDistrict->sub_district }}
                                                </option>
                                            @endforeach
                                        </select>
                                    </div>
                                </div><!--end col-->
                                <div class="col-lg-12">
                                    <div>
                                        <label for="villageId" class="form-label">Pilih Desa atau Kelurahan</label>
                                        <select class="form-select mb-3" name="villageId" id="villageIdInput" required>
                                            <option value="">-- Pilih Desa --</option>
                                        </select>
                                    </div>
                                </div><!--end col-->
                                <div class="col-lg-6">
                                    <div>
                                        <label for="month" class="form-label">
                                            Bulan
                                        </label>
                                        <select class="form-select mb-3" aria-label="Default select example"
                                            name="month" id="month">
                                            <option selected value="01">Januari</option>
                                            <option value="02">Februari</option>
                                            <option value="03">Maret</option>
                                            <option value="04">April</option>
                                            <option value="05">Mei</option>
                                            <option value="06">Juni</option>
                                            <option value="07">Juli</option>
                                            <option value="08">Agustus</option>
                                            <option value="09">September</option>
                                            <option value="10">Oktober</option>
                                            <option value="11">November</option>
                                            <option value="12">Desember</option>
                                        </select>
                                    </div>
                                </div><!--end col-->
                                <div class="col-lg-6">
                                    <div>
                                        <label for="year" class="form-label">
                                            Tahun
                                        </label>
                                        <input type="text" class="form-control mb-1 year-input" id="year"
                                            name="year" required>
                                    </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/custom/village-alternatif.js') }}"></script>
@endsection
