@extends('server-side.layouts.server-app')
@section('pageTitle', 'Tambah Data Basis Pengetahuan | KlinikSapi.Go')
@section('mainContent')
    <div class="content d-flex flex-column flex-column-fluid" id="kt_content">
        <div class="toolbar" id="kt_toolbar">
            <div id="kt_toolbar_container" class="container-fluid d-flex flex-stack">
                <div data-kt-swapper="true" data-kt-swapper-mode="prepend"
                    data-kt-swapper-parent="{default: '#kt_content_container', 'lg': '#kt_toolbar_container'}"
                    class="page-title d-flex align-items-center flex-wrap me-3 mb-5 mb-lg-0">
                    <h1 class="d-flex align-items-center text-dark fw-bolder fs-3 my-1">Manajemen Data
                        <span class="h-20px border-gray-200 border-start ms-3 mx-2"></span>
                        <small class="text-muted fs-7 fw-bold my-1 ms-1">Basis Pengetahuan / Tambah Pengetahuan</small>
                    </h1>
                </div>
            </div>
        </div>
        <div class="post d-flex flex-column-fluid" id="kt_post">
            <div id="kt_content_container" class="container-xxl">
                <div class="card mb-5 mb-xl-8 px-10 px-lg-15 py-15">
                    <form action="{{ route('rule.store') }}" method="POST" class="form" autocomplete="off">
                        @csrf
                        <div class="mb-10">
                            <h1 class="mb-3">Formulir Penambahan Data Basis Pengetahuan</h1>
                            <div class="text-muted fw-bold fs-5">Silahkan lengkapi formulir penambahan <strong>Data Basis Pengetahuan</strong> di bawah ini!</div>
                        </div>
                        <div class="row g-9 mb-8">
                            <div class="col-md-12 fv-row">
                                <label class="d-flex align-items-center fs-6 fw-bold mb-2">
                                    <span class="required">Nama Penyakit</span>
                                </label>
                                <select class="form-select form-select-solid" data-control="select2" name="penyakitId" required>
                                    <option value="">---</option>
                                    @foreach ($penyakit as $item)
                                        <option value="{{ $item->penyakitId }}">{{ $item->penyakit }}</option>
                                    @endforeach
                                </select>
                                @if ($errors->has('penyakit'))
                                    <p class="mt-2 text-sm" style="color: red; font-weight: 500;"><span
                                            class="font-medium">Oops!</span> {{ $errors->first('penyakit') }}</p>
                                @endif
                            </div>
                        </div>
                        <hr class="mt-10 mb-6 shadow">
                        <div class="row g-9 mb-8">
                            <div class="col-md-8 fv-row">
                                <label class="d-flex align-items-center fs-6 fw-bold mb-2">
                                    <span class="required">Gejala 1</span>
                                </label>
                                <select class="form-select form-select-solid" data-control="select2" name="gejalaId[]" required>
                                    <option value="">---</option>
                                    @foreach ($gejala as $item)
                                        <option value="{{ $item->gejalaId }}">{{ $item->gejala }}</option>
                                    @endforeach
                                </select>
                            </div>
                            <div class="col-md-3 fv-row">
                                <label class="d-flex align-items-center fs-6 fw-bold mb-2">
                                    <span class="required">Nilai Pakar</span>
                                </label>
                                <select class="form-select form-select-solid" data-control="select2" name="nilaiPakar[]" required>
                                    <option value="">---</option>
                                    @foreach ($bobot as $itemb)
                                        <option value="{{ $itemb->nilai }}">{{ $itemb->nilai }}</option>
                                    @endforeach
                                </select>
                            </div>
                            {{-- <div class="col-md-3 fv-row">
                                <label class="d-flex align-items-center fs-6 fw-bold mb-2">
                                    <span class="required">Nilai Pakar</span>
                                </label>
                                <input type="number" class="form-control form-control-solid" name="nilaiPakar[]" min="0.05" max="1" step="0.01" required />
                            </div> --}}
                            <div class="col-md-1">
                                <label class="d-flex align-items-center text-white fs-6 fw-bold mb-2">
                                    <span>.</span>
                                </label>
                                <button type="button" class="btn btn-success w-100 fw-bold" id="tambahGejala">+</button>
                            </div>
                        </div>
                        <div id="multipleBox"></div>
                        <div class="float-start">
                            <button type="submit" class="btn btn-primary">
                                <span class="indicator-label">Simpan Data</span>
                            </button>
                            <button type="reset" class="btn btn-light me-3">Batal</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
@endsection
@push('addScripts')
    <script>
        const buttonadd = document.getElementById('tambahGejala');
        const box = document.getElementById('multipleBox');
        let numberSelect = 2;

        buttonadd.addEventListener('click', (event) => {
            const newRow = document.createElement('div');
            newRow.classList.add('row', 'g-9', 'mb-8');
            newRow.innerHTML = `
                <div class="col-md-8 fv-row">
                    <label class="d-flex align-items-center fs-6 fw-bold mb-2">
                        <span class="required">Gejala ${numberSelect++}</span>
                    </label>
                    <select class="form-select form-select-solid select2-custom" data-control="select2" name="gejalaId[]" required>
                        <option value="">---</option>
                        @foreach ($gejala as $item)
                        <option value="{{ $item->gejalaId }}">{{ $item->gejala }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-3 fv-row">
                    <label class="d-flex align-items-center fs-6 fw-bold mb-2">
                        <span class="required">Nilai Pakar</span>
                    </label>
                    <select class="form-select form-select-solid select2-custom-nilai" data-control="select2" name="nilaiPakar[]" required>
                        <option value="">---</option>
                        @foreach ($bobot as $itemb)
                            <option value="{{ $itemb->nilai }}">{{ $itemb->nilai }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-1">
                    <label class="d-flex align-items-center text-white fs-6 fw-bold mb-2">
                        <span>.</span>
                    </label>
                    <button type="button" class="btn btn-danger w-100 fw-bold hapus-obat">x</button>
                </div>
            `;
            box.appendChild(newRow);

            const newSelectGejala = newRow.querySelector('.select2-custom');
            $(newSelectGejala).select2();

            const newSelectNilai = newRow.querySelector('.select2-custom-nilai');
            $(newSelectNilai).select2();

            const hapusObatButton = newRow.querySelector('.hapus-obat');
            hapusObatButton.addEventListener('click', (event) => {
                newRow.remove();
            });
        });
    </script>
@endpush
