<?php
session_start();
require_once 'functions.php';

if (!isset($_SESSION['login_user'])) {
    header('Location: login.php');
    exit;
}

$user = $_SESSION['login_user'];

$rows = $db->get_results("SELECT * FROM tb_gejala WHERE kode_gejala IN (SELECT kode_gejala FROM tb_konsultasi WHERE jawaban='Ya')");
if( !$rows ) :
    print_msg('Belum ada gejala terpilih!', 'warning');
    echo '<p><a class="btn btn-primary" href="aksi.php?m=konsultasi&act=new"><span class="glyphicon glyphicon-refresh"></span> Konsultasi Lagi</a></p>';
else:

?>
<div class="panel panel-default">
    <div class="panel-heading">        
        <h3 class="panel-title"><b>Biodata Konsultasi</b></h3>  
    </div>
    <table class="table table-bordered table-hover">
    <thead>
          <tr style="background-color: #535c68; color: #fff;">
                <th>Nama</th>
                <th>Email</th>
                <th>No. Hp</th>
                <th>Jenis Kelamin</th>
                <th>Usia</th>
                <th>Alamat</th>
            </tr></thead>
            <?php
            $q = esc_field(@$_GET['q']);
            $rowss = $db->get_results("SELECT * FROM tb_user  order by user desc limit 1");
            $no=0;
            foreach($rowss as $rowd):?>
            <tr>
                <td><?=$rowd->nama ?></td>
                <td><?=$rowd->email ?></td>
                <td><?=$rowd->no_hp?></td>
                <td><?=$rowd->jk?></td>
                <td><?=$rowd->usia?></td>
                <td><?=$rowd->alamat?></td>
            </tr>
            <?php endforeach;?>
        </table>
    </div>
<div class="panel panel-default">
    <div class="panel-heading">        
        <h3 class="panel-title"><b>Gejala Terpilih</b></h3>
    </div>
    <table class="table table-bordered table-hover">
    <thead>
        <tr style="background-color: #535c68; color: #fff;">
            <th>No</th>
            <th>Nama Gejala</th>
        </tr>
    </thead>
    <?php
    $no=1;
    foreach($rows as $row):?>
    <tr>
        <td><?=$no++?></td>
        <td><?=$row->nama_gejala?></td>
    </tr>
    <?php endforeach;
    ?>
    </table>
</div>
<?php
$rows = $db->get_results("SELECT * 
    FROM tb_relasi r INNER JOIN tb_diagnosa d ON d.kode_diagnosa = r.kode_diagnosa      
    WHERE r.kode_gejala IN (SELECT kode_gejala FROM tb_konsultasi WHERE jawaban='Ya') ORDER BY r.kode_diagnosa, r.kode_gejala");

$diagnosa = [];

foreach($rows as $row) {
    if (!isset($diagnosa[$row->kode_diagnosa])) {
        // Inisialisasi nilai MB dan MD dengan nilai pertama
        $diagnosa[$row->kode_diagnosa]['mb'] = $row->mb;
        $diagnosa[$row->kode_diagnosa]['md'] = $row->md;
    } else {
        // Kombinasi nilai MB dan MD yang sudah ada dengan yang baru
        $mb_lama = $diagnosa[$row->kode_diagnosa]['mb'];
        $md_lama = $diagnosa[$row->kode_diagnosa]['md'];

        $diagnosa[$row->kode_diagnosa]['mb'] = $mb_lama + $row->mb * (1 - $mb_lama);
        $diagnosa[$row->kode_diagnosa]['md'] = $md_lama + $row->md * (1 - $md_lama);
    }

    // Hitung CF sebagai selisih MB dan MD
    $diagnosa[$row->kode_diagnosa]['cf'] = $diagnosa[$row->kode_diagnosa]['mb'] - $diagnosa[$row->kode_diagnosa]['md'];
}
?>
<div class="panel panel-default">
    <div class="panel-heading">        
        <h3 class="panel-title"><b>Hasil Analisa</b></h3>
    </div>
    <table class="table table-bordered table-hover ">
    <thead>
        <tr style="background-color: #535c68; color: #fff;">
            <th>No</th>
            <th>Vitamin</th>
            <th>Kepercayaan CF</th>
        </tr>
    </thead>
    <?php
    $no=1;
    function ranking($array){
        $new_arr = array();
        foreach($array as $key => $value) {
            $new_arr[$key] = $value['cf'];
        }
        arsort($new_arr);
        $result = array();    
        foreach($new_arr as $key => $value){
            $result[$key] = $value; // Perbaikan pada pengurutan
        }    
        return $result;
    }
    $rank = ranking($diagnosa);

    foreach($rank as $key => $value): ?>
    <tr class="<?=($no==1) ? 'text-primary' : ''; ?>">
        <td><?=$no++; ?></td>
        <td><b><?=$DIAGNOSA[$key]->nama_diagnosa; ?></b></td>
        <td><?=number_format($diagnosa[$key]['cf'] * 100); ?>%</td>
    </tr>
    <?php endforeach;
    reset($rank);
    ?>
    </table>

    <div class="panel-body">
        <table class="table table-bordered">
            <tr>
                <td>Vitamin</td>
                <td><b><?=$DIAGNOSA[key($rank)]->nama_diagnosa; ?></b></td>
            </tr>
            <tr>
                <td>Solusi</td>
                <td><?=$DIAGNOSA[key($rank)]->solusi; ?></td>
            </tr>
            <tr>
                <td>Dosis Vitamin</td>
                <td><?=$DIAGNOSA[key($rank)]->dosis_vitamin; ?></td>
            </tr>
            
        </table>
        <p>Catatan : Jika gejala yang dialami berlebihan atau berkelanjutan maka disarankan untuk evaluasi lebih lanjut ke profesional medis.</p>

        <p>
            <a class="btn edit" href="index.php?m=biodata"><span class=""></span> Konsultasi Lagi</a>
            <a class="btn edit" href="cetak.php?m=konsultasi" target="_blank"><span class=""></span> Cetak</a>
        </p>       
    </div>
</div>

<?php endif;?>
<div class="page-header">
    <?php
    $nama =$rowd->nama;
    $email =$rowd->email;
    $no_hp =$rowd->no_hp;
    $jk=$rowd->jk;
    $usia=$rowd->usia;
    $alamat =$rowd->alamat;
    $hasildiagnosa=$DIAGNOSA[key($rank)]->nama_diagnosa;

    $mmk=$diagnosa[key($rank)]['cf'] * 100;

    // Set timezone ke Asia/Jakarta
    date_default_timezone_set('Asia/Jakarta');
    $tanggal_konsultasi = date('Y-m-d H:i:s');
    
    $db->query("INSERT INTO tb_hasil(user,nama,email, no_hp,jk,usia,alamat,tanggal_konsultasi,hasil_konsultasi,kepercayaan) VALUES('$user','$nama','$email','$no_hp','$jk','$usia','$alamat','$tanggal_konsultasi','$hasildiagnosa','$mmk')");

    //Positif ganti dengan $mmk jika menampilkan nilai CF di Laporan Admin.
    //Nilai CF di Laporan admin belum FIX, karna penyimpanan diambil dari nilai CF terakhir dari daftar hasil diagnosa.
    ?>

