@extends('employee.layouts.app')
@section('title', 'Laboratorium | Career Development and Consultation (CDC)')
@section('navigation')
    <h1 class="my-1 d-flex align-items-center text-dark fw-bolder fs-3">Teori
        <span class="mx-2 border-gray-200 h-20px border-start ms-3"></span>
        <small class="my-1 text-muted fs-7 fw-bold ms-1">Laboratorium Hasil</small>
    </h1>
@endsection
@section('content')
    <div class="card">
        <div class="card-body p-lg-20 pb-lg-0">
            <div class="mb-17">
                <div class="mb-8">
                    <span class="text-dark text-hover-primary fs-1 fw-bolder">Calc</span>
                    <div class="my-5 border-2 separator"></div>
                </div>
                <div class="text-gray-600 fs-4 fw-bold">
                    <div class="table-responsive">
                        <table class="table border table-rounded table-row-bordered gy-5 gs-7">
                            <thead>
                                <tr class="text-gray-800 fw-bolder fs-6">
                                    <th>No</th>
                                    {{-- <th>Statement Code</th> --}}
                                    <th>Statement</th>
                                    <th>Personality</th>
                                    <th>Believe</th>
                                    <th>Plausability</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach ($rules as $index => $statement)
                                    <tr>
                                        <td>{{ $index + 1 }}</td>
                                        {{-- <td>System Architect</td> --}}
                                        @if (isset($statementData[$index]))
                                            <td>{{ $statementData[$index]['statement_code'] }} |
                                                {{ $statementData[$index]['statement'] }}</td>
                                        @endif
                                        <td>{{ $statement[0] }}</td>
                                        <td>{{ $statement[1] }}</td>
                                        <td>{{ 1 - $statement[1] }}</td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                    <div>
                        <div class="my-10 border-2 separator"></div>
                        @php
                            $fod = $personalitiesData[0][0];
                            $densitas_baru = [];
                            $urutan = 1;
                        @endphp
                        @while (!empty($rules))
                            @php
                                $densitas1[0] = array_shift($rules);
                                $densitas1[1] = [$fod, 1 - $densitas1[0][1]];
                                $Y2 = 1 - $densitas1[0][1];
                                $densitas2 = [];
                            @endphp
                            <p class="text-grey fs-2 fw-bolder">Iterasi ke {{ $urutan }}</p>
                            @php
                                $urutan++;
                            @endphp
                            <p class="text-grey fs-4 fw-bolder">Nilai: </p>
                            <p>({{ $densitas1[0][0] }}) {{ $densitas1[0][1] }}</p>
                            <p>(θ) {{ 1 - $densitas1[0][1] }}</p>

                            @if (empty($densitas_baru))
                                @php
                                    $densitas2[0] = array_shift($rules);
                                @endphp
                                {{-- nilai pada X1 baris 1 --}}
                                <p>({{ $densitas2[0][0] }}) {{ $densitas2[0][1] }}</p>
                            @else
                                @foreach ($densitas_baru as $k => $r)
                                    {{-- nilai pada X1 baris 2; jika ada densitas baru --}}
                                    @if ($k != 'θ')
                                        @php
                                            $densitas2[] = [$k, $r];
                                        @endphp
                                        <p>({{ $k }}) {{ ROUND($r, 3) }}</p>
                                    @endif
                                @endforeach
                            @endif
                            @php
                                $theta = 1;
                                // nilai X1 baris 2 teta
                                foreach ($densitas2 as $d) {
                                    $theta -= $d[1];
                                }
                            @endphp
                            <p>(θ) {{ ROUND($theta, 3) }}</p>
                            @php
                                $densitas2[] = [$fod, $theta];
                                $m = count($densitas2);
                                $densitas_baru = [];
                            @endphp
                            @for ($y = 0; $y < $m; $y++)
                                @for ($x = 0; $x < 2; $x++)
                                    @if (!($y == $m - 1 && $x == 1))
                                        @php
                                            $v = explode(',', $densitas1[$x][0]);
                                            $w = explode(',', $densitas2[$y][0]);
                                            sort($v);
                                            sort($w);
                                            $vw = array_intersect($v, $w); //mencari irisan nilai
                                        @endphp
                                        @if (empty($vw))
                                            @php
                                                $k = 'θ';
                                                $nilaiX1Y1 = $densitas1[$x][1] * $densitas2[$y][1];
                                            @endphp
                                            <p>({{ $k }}) {{ ROUND($nilaiX1Y1, 3) }}</p>
                                        @else
                                            @php
                                                $k = implode(',', $vw);
                                                $nilaiX1Y1 = $densitas1[$x][1] * $densitas2[$y][1];

                                                $combine = '';
                                            @endphp
                                            @foreach ($vw as $vwHasil)
                                                @php
                                                    $combine .= $vwHasil;
                                                @endphp
                                            @endforeach
                                            @php
                                                $combine = '(' . $combine . ')';
                                                $combine .= ' ' . ROUND($nilaiX1Y1, 3);
                                            @endphp
                                            <p>{{ $combine }} </p>
                                        @endif
                                        @if (!isset($densitas_baru[$k]))
                                            @php
                                                $densitas_baru[$k] = $densitas1[$x][1] * $densitas2[$y][1];
                                                $k = implode(',', $vw);
                                            @endphp
                                        @else
                                            @php
                                                $densitas_baru[$k] += $densitas1[$x][1] * $densitas2[$y][1];
                                            @endphp
                                        @endif
                                    @endif
                                @endfor
                            @endfor
                            @php
                                $dataX2 = $theta;
                                $dataY2 = $Y2;
                                $Y3 = $dataX2 * $dataY2;
                            @endphp
                            <p>(θ) {{ ROUND($Y3, 3) }}</p>
                            <div class="my-10 border-2 separator"></div>
                            <p class="text-grey fs-4 fw-bolder">Perhitungan: </p>
                            @foreach ($densitas_baru as $k => $d)
                                {{-- Cetak nilai densitas dan kunci yang sesuai --}}
                                <p>({{ $k }}) = {{ ROUND($d, 3) }}</p>
                                {{-- Periksa apakah kunci bukan &theta; --}}
                                @if ($k != 'θ')
                                    @php
                                        // Tentukan nilai untuk theta, jika tidak ada konflik maka 0
                                        $theta_value = isset($densitas_baru['θ']) ? $densitas_baru['θ'] : 0;

                                        // Hitung densitas baru sesuai dengan rumus Dempster-Shafer
                                        $densitas_baru[$k] = $d / (1 - $theta_value);
                                    @endphp
                                    <p>({{ $k }}) = {{ ROUND($d, 3) }} / (1 - {{ ROUND($theta_value, 3) }})
                                    </p>
                                    <p>({{ $k }}) = {{ ROUND($densitas_baru[$k], 3) }}</p>
                                @endif
                            @endforeach

                            <div class="my-10 border-2 separator"></div>
                            <p class="text-grey fs-4 fw-bolder">Hasil Perhitungan: </p>
                            @foreach ($densitas_baru as $statement => $value)
                                <p>({{ $statement }}) = {{ ROUND($value, 3) }}</p>
                            @endforeach
                            <div class="my-10 border-2 separator"></div>
                        @endwhile
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection
