<?php
defined('BASEPATH') or exit('No direct script access allowed');
error_reporting(0);

class Dashboard extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        if (!$_SESSION['username']) {
            echo "<script>alert('Anda harus login terlebih dahulu untuk dapat mengakses halaman ini'); location.href = '" . base_url() . "login'</script>";
        }
    }

    public function index()
    {
        $data['total_penyakit'] = $this->db->query('SELECT count(*) as total from penyakit')->row_array();
        $data['total_gejala'] = $this->db->query('SELECT count(*) as total from gejala')->row_array();
        $data['total_basis_pengetahuan'] = $this->db->query('SELECT count(*) as total from basis_pengetahuan')->row_array();
        $data['total_users'] = $this->db->query('SELECT count(*) as total from `admin`')->row_array();
        $data['total_diagnosa'] = $this->db->query('SELECT count(*) as total from `hasil`')->row_array();
        $data['total_diagnosa_saya'] = $this->db->query('SELECT count(*) as total from `hasil` WHERE id_user = ' . $_SESSION['id'])->row_array();

        $data['active'] = 'dashboard';

        $this->load->view('v_header', $data);
        $this->load->view('v_dashboard', $data);
        $this->load->view('v_footer');
    }

    ########################### START MODUL PENGGUNA ###########################
    public function daftar_pengguna()
    {
        if ($_SESSION['role'] != 'admin') {
            echo "<script>alert('Anda tidak memiliki akses untuk melihat halaman ini'); location.href = '" . base_url() . "dashboard'</script>";
        }

        $data['total_penyakit'] = $this->db->query('SELECT count(*) as total from penyakit')->row_array();
        $data['total_gejala'] = $this->db->query('SELECT count(*) as total from gejala')->row_array();
        $data['total_basis_pengetahuan'] = $this->db->query('SELECT count(*) as total from basis_pengetahuan')->row_array();
        $data['total_users'] = $this->db->query('SELECT count(*) as total from `admin`')->row_array();

        $data['datasets'] = $this->db->query('SELECT * FROM `admin` ORDER BY `role`')->result_array();
        $data['active'] = 'daftarpengguna';

        $this->load->view('v_header', $data);
        $this->load->view('v_daftar_pengguna', $data);
        $this->load->view('v_footer');
    }

    public function tambah_pengguna()
    {
        if ($_SESSION['role'] != 'admin') {
            echo "<script>alert('Anda tidak memiliki akses untuk melihat halaman ini'); location.href = '" . base_url() . "dashboard'</script>";
        }

        if ($this->input->server('REQUEST_METHOD') === 'POST') {
            $params = $this->input->post();

            if ((int) $params['id'] > 0) {
                $input = [
                    'username' => $params['username'],
                    'nama_lengkap' => $params['nama_lengkap'],
                    'role' => $params['role'],
                    'alamat' => $params['alamat'] ? $params['alamat'] : '',
                    'no_telp' => $params['no_telp'] ? $params['no_telp'] : ''
                ];
                if ($params['password']) {
                    $input['password'] = md5($params['password']);
                }
                $this->db->where('id', $params['id']);
                $this->db->update('admin', $input);

                if ($params['id'] == $_SESSION['id']) {
                    $_SESSION['nama_lengkap'] = $input['nama_lengkap'];
                    $_SESSION['username'] = $input['username'];
                    $_SESSION['alamat'] = $input['alamat'];
                    $_SESSION['no_telp'] = $input['no_telp'];
                    if ($input['password']) {
                        $_SESSION['password'] = $input['password'];
                    }
                }
            } else {
                $this->db->insert('admin', [
                    'username' => $params['username'],
                    'password' => md5($params['password']),
                    'nama_lengkap' => $params['nama_lengkap'],
                    'role' => $params['role'],
                    'alamat' => $params['alamat'] ? $params['alamat'] : '',
                    'no_telp' => $params['no_telp'] ? $params['no_telp'] : '',
                ]);
            }

            echo "<script>alert('Data Pengguna berhasil disimpan'); location.href = '" . base_url() . "dashboard/daftar_pengguna';</script>";
        } else {
            $params = $_REQUEST;
            $data['fetch'] = [];
            $id = $params['id'];
            if ((int) $id > 0) {
                $this->db->where('id', $id);
                $data['fetch'] = $this->db->get('admin')->row_array();
            }

            $data['id'] = $id;
            $data['active'] = 'daftarpengguna';
            $this->load->view('v_header', $data);
            $this->load->view('v_tambah_pengguna');
            $this->load->view('v_footer');
        }
    }

    public function hapus_pengguna()
    {
        if ($_SESSION['role'] != 'admin') {
            echo "<script>alert('Anda tidak memiliki akses untuk melihat halaman ini'); location.href = '" . base_url() . "dashboard'</script>";
        }

        $params = $_REQUEST;
        $this->db->where('id', $params['id']);
        $this->db->delete('admin');

        echo "<script>alert('Data Pengguna berhasil terhapus'); location.href = '" . base_url() . "dashboard/daftar_pengguna';</script>";
    }
    ########################### END MODUL PENGGUNA ###########################

    ########################### START MODUL PENYAKIT ###########################
    public function daftar_penyakit()
    {
        if ($_SESSION['role'] != 'admin') {
            echo "<script>alert('Anda tidak memiliki akses untuk melihat halaman ini'); location.href = '" . base_url() . "dashboard'</script>";
        }

        $data['total_penyakit'] = $this->db->query('SELECT count(*) as total from penyakit')->row_array();
        $data['total_gejala'] = $this->db->query('SELECT count(*) as total from gejala')->row_array();
        $data['total_basis_pengetahuan'] = $this->db->query('SELECT count(*) as total from basis_pengetahuan')->row_array();
        $data['total_users'] = $this->db->query('SELECT count(*) as total from `admin`')->row_array();

        $data['datasets'] = $this->db->query('SELECT * FROM penyakit ORDER BY kode_penyakit')->result_array();
        $data['active'] = 'daftarpenyakit';

        $this->load->view('v_header', $data);
        $this->load->view('v_daftar_penyakit', $data);
        $this->load->view('v_footer');
    }

    public function tambah_penyakit()
    {
        if ($_SESSION['role'] != 'admin') {
            echo "<script>alert('Anda tidak memiliki akses untuk melihat halaman ini'); location.href = '" . base_url() . "dashboard'</script>";
        }

        if ($this->input->server('REQUEST_METHOD') === 'POST') {
            $params = $this->input->post();

            if ((int) $params['id'] > 0) {
                $this->db->where('kode_penyakit', $params['id']);
                $this->db->update('penyakit', [
                    'nama_penyakit' => $params['nama_penyakit'],
                    'det_penyakit' => $params['det_penyakit'] ? $params['det_penyakit'] : '',
                    // 'srn_penyakit' => $params['srn_penyakit'] ? $params['srn_penyakit'] : '',
                ]);

                if ($_FILES['gambar']['name']) {
                    $upload_dir = './aset/penyakit/';

                    if (!is_dir($upload_dir)) {
                        mkdir($upload_dir, 0755, true);
                    }

                    $temp_file = $_FILES['gambar']['tmp_name'];
                    $original_file = $_FILES['gambar']['name'];

                    $unique_filename = uniqid() . '_' . $original_file;
                    move_uploaded_file($temp_file, $upload_dir . $unique_filename);

                    $this->db->where('kode_penyakit', $params['id']);
                    $this->db->update('penyakit', [
                        'gambar' => $unique_filename,
                    ]);
                }
            } else {
                $upload_dir = './aset/penyakit/';

                if (!is_dir($upload_dir)) {
                    mkdir($upload_dir, 0755, true);
                }

                $temp_file = $_FILES['gambar']['tmp_name'];
                $original_file = $_FILES['gambar']['name'];

                $unique_filename = uniqid() . '_' . $original_file;
                move_uploaded_file($temp_file, $upload_dir . $unique_filename);

                $this->db->insert('penyakit', [
                    'nama_penyakit' => $params['nama_penyakit'],
                    'det_penyakit' => $params['det_penyakit'] ? $params['det_penyakit'] : '',
                    'gambar' => $unique_filename,
                    'srn_penyakit' => '',
                    // 'srn_penyakit' => $params['srn_penyakit'] ? $params['srn_penyakit'] : '',
                ]);
            }

            echo "<script>alert('Data Penyakit berhasil disimpan'); location.href = '" . base_url() . "dashboard/daftar_penyakit';</script>";
        } else {
            $params = $_REQUEST;
            $data['fetch'] = [];
            $id = $params['id'];
            if ((int) $id > 0) {
                $this->db->where('kode_penyakit', $id);
                $data['fetch'] = $this->db->get('penyakit')->row_array();
            }

            $data['id'] = $id;
            $data['active'] = 'daftarpenyakit';
            $this->load->view('v_header', $data);
            $this->load->view('v_tambah_penyakit');
            $this->load->view('v_footer');
        }
    }

    public function hapus_penyakit()
    {
        if ($_SESSION['role'] != 'admin') {
            echo "<script>alert('Anda tidak memiliki akses untuk melihat halaman ini'); location.href = '" . base_url() . "dashboard'</script>";
        }

        $params = $_REQUEST;
        $this->db->where('kode_penyakit', $params['id']);
        $this->db->delete('penyakit');

        echo "<script>alert('Data Penyakit berhasil terhapus'); location.href = '" . base_url() . "dashboard/daftar_penyakit';</script>";
    }
    ########################### END MODUL PENYAKIT ###########################

    ########################### START MODUL GEJALA ###########################
    public function daftar_gejala()
    {
        if ($_SESSION['role'] != 'admin') {
            echo "<script>alert('Anda tidak memiliki akses untuk melihat halaman ini'); location.href = '" . base_url() . "dashboard'</script>";
        }

        $data['total_penyakit'] = $this->db->query('SELECT count(*) as total from penyakit')->row_array();
        $data['total_gejala'] = $this->db->query('SELECT count(*) as total from gejala')->row_array();
        $data['total_basis_pengetahuan'] = $this->db->query('SELECT count(*) as total from basis_pengetahuan')->row_array();
        $data['total_users'] = $this->db->query('SELECT count(*) as total from `admin`')->row_array();

        $data['datasets'] = $this->db->query('SELECT * FROM gejala ORDER BY kode_gejala')->result_array();
        $data['active'] = 'daftargejala';

        $this->load->view('v_header', $data);
        $this->load->view('v_daftar_gejala', $data);
        $this->load->view('v_footer');
    }

    public function tambah_gejala()
    {
        if ($_SESSION['role'] != 'admin') {
            echo "<script>alert('Anda tidak memiliki akses untuk melihat halaman ini'); location.href = '" . base_url() . "dashboard'</script>";
        }

        if ($this->input->server('REQUEST_METHOD') === 'POST') {
            $params = $this->input->post();

            if ((int) $params['id'] > 0) {
                $this->db->where('kode_gejala', $params['id']);
                $this->db->update('gejala', [
                    'nama_gejala' => $params['nama_gejala'],
                ]);
            } else {
                $this->db->insert('gejala', [
                    'nama_gejala' => $params['nama_gejala'],
                ]);
            }

            echo "<script>alert('Data Gejala berhasil disimpan'); location.href = '" . base_url() . "dashboard/daftar_gejala';</script>";
        } else {
            $params = $_REQUEST;
            $data['fetch'] = [];
            $id = $params['id'];
            if ((int) $id > 0) {
                $this->db->where('kode_gejala', $id);
                $data['fetch'] = $this->db->get('gejala')->row_array();
            }

            $data['id'] = $id;
            $data['active'] = 'daftargejala';
            $this->load->view('v_header', $data);
            $this->load->view('v_tambah_gejala');
            $this->load->view('v_footer');
        }
    }

    public function hapus_gejala()
    {
        if ($_SESSION['role'] != 'admin') {
            echo "<script>alert('Anda tidak memiliki akses untuk melihat halaman ini'); location.href = '" . base_url() . "dashboard'</script>";
        }

        $params = $_REQUEST;
        $this->db->where('kode_gejala', $params['id']);
        $this->db->delete('gejala');

        echo "<script>alert('Data Gejala berhasil terhapus'); location.href = '" . base_url() . "dashboard/daftar_gejala';</script>";
    }
    ########################### END MODUL GEJALA ###########################

    ########################### START MODUL PENGETAHUAN ###########################
    public function daftar_pengetahuan()
    {
        if ($_SESSION['role'] != 'admin') {
            echo "<script>alert('Anda tidak memiliki akses untuk melihat halaman ini'); location.href = '" . base_url() . "dashboard'</script>";
        }

        $data['total_penyakit'] = $this->db->query('SELECT count(*) as total from penyakit')->row_array();
        $data['total_gejala'] = $this->db->query('SELECT count(*) as total from gejala')->row_array();
        $data['total_basis_pengetahuan'] = $this->db->query('SELECT count(*) as total from basis_pengetahuan')->row_array();
        $data['total_users'] = $this->db->query('SELECT count(*) as total from `admin`')->row_array();

        $data['datasets'] = $this->db->query(
            'SELECT basis_pengetahuan.*, penyakit.nama_penyakit, gejala.nama_gejala 
            FROM basis_pengetahuan 
            JOIN penyakit ON penyakit.kode_penyakit = basis_pengetahuan.kode_penyakit 
            JOIN gejala ON gejala.kode_gejala = basis_pengetahuan.kode_gejala 
            ORDER BY kode_pengetahuan'
        )->result_array();
        $data['active'] = 'daftarpengetahuan';

        $this->load->view('v_header', $data);
        $this->load->view('v_daftar_pengetahuan', $data);
        $this->load->view('v_footer');
    }

    public function tambah_pengetahuan()
    {
        if ($_SESSION['role'] != 'admin') {
            echo "<script>alert('Anda tidak memiliki akses untuk melihat halaman ini'); location.href = '" . base_url() . "dashboard'</script>";
        }

        if ($this->input->server('REQUEST_METHOD') === 'POST') {
            $params = $this->input->post();

            if ((int) $params['id'] > 0) {
                $this->db->where('kode_pengetahuan', $params['id']);
                $this->db->update('basis_pengetahuan', [
                    'kode_penyakit' => $params['kode_penyakit'],
                    'kode_gejala' => $params['kode_gejala'],
                    'mb' => $params['mb'],
                    'md' => $params['md'],
                ]);
            } else {
                $this->db->insert('basis_pengetahuan', [
                    'kode_penyakit' => $params['kode_penyakit'],
                    'kode_gejala' => $params['kode_gejala'],
                    'mb' => $params['mb'],
                    'md' => $params['md'],
                ]);
            }

            echo "<script>alert('Data Basis Pengetahuan berhasil disimpan'); location.href = '" . base_url() . "dashboard/daftar_pengetahuan';</script>";
        } else {
            $params = $_REQUEST;
            $data['fetch'] = [];
            $id = $params['id'];
            if ((int) $id > 0) {
                $this->db->where('kode_pengetahuan', $id);
                $data['fetch'] = $this->db->get('basis_pengetahuan')->row_array();
            }

            $data['penyakit'] = $this->db->get('penyakit')->result_array();
            $data['gejala'] = $this->db->get('gejala')->result_array();

            $data['id'] = $id;
            $data['active'] = 'daftargejala';
            $this->load->view('v_header', $data);
            $this->load->view('v_tambah_pengetahuan');
            $this->load->view('v_footer');
        }
    }

    public function hapus_pengetahuan()
    {
        if ($_SESSION['role'] != 'admin') {
            echo "<script>alert('Anda tidak memiliki akses untuk melihat halaman ini'); location.href = '" . base_url() . "dashboard'</script>";
        }

        $params = $_REQUEST;
        $this->db->where('kode_pengetahuan', $params['id']);
        $this->db->delete('basis_pengetahuan');

        echo "<script>alert('Data Basis Pengetahuan berhasil terhapus'); location.href = '" . base_url() . "dashboard/daftar_pengetahuan';</script>";
    }
    ########################### END MODUL PENGETAHUAN ###########################

    ########################### START MODUL PERAWATAN ###########################
    public function daftar_perawatan()
    {
        if ($_SESSION['role'] != 'admin') {
            echo "<script>alert('Anda tidak memiliki akses untuk melihat halaman ini'); location.href = '" . base_url() . "dashboard'</script>";
        }

        $data['total_penyakit'] = $this->db->query('SELECT count(*) as total from penyakit')->row_array();
        $data['total_gejala'] = $this->db->query('SELECT count(*) as total from gejala')->row_array();
        $data['total_basis_pengetahuan'] = $this->db->query('SELECT count(*) as total from basis_pengetahuan')->row_array();
        $data['total_users'] = $this->db->query('SELECT count(*) as total from `admin`')->row_array();

        $data['datasets'] = $this->db->query('SELECT * FROM penyakit ORDER BY kode_penyakit')->result_array();
        $data['active'] = 'daftarperawatan';

        $this->load->view('v_header', $data);
        $this->load->view('v_daftar_perawatan', $data);
        $this->load->view('v_footer');
    }

    public function tambah_perawatan()
    {
        if ($_SESSION['role'] != 'admin') {
            echo "<script>alert('Anda tidak memiliki akses untuk melihat halaman ini'); location.href = '" . base_url() . "dashboard'</script>";
        }

        if ($this->input->server('REQUEST_METHOD') === 'POST') {
            $params = $this->input->post();

            if ((int) $params['id'] > 0) {
                $this->db->where('kode_penyakit', $params['id']);
                $this->db->update('penyakit', [
                    // 'nama_penyakit' => $params['nama_penyakit'],
                    // 'det_penyakit' => $params['det_penyakit'] ? $params['det_penyakit'] : '',
                    'srn_penyakit' => $params['srn_penyakit'] ? $params['srn_penyakit'] : '',
                    'srn_pencegahan' => $params['srn_pencegahan'] ? $params['srn_pencegahan'] : '',
                ]);
            }

            echo "<script>alert('Data Perawatan berhasil disimpan'); location.href = '" . base_url() . "dashboard/daftar_perawatan';</script>";
        } else {
            $params = $_REQUEST;
            $data['fetch'] = [];
            $id = $params['id'];
            if ((int) $id > 0) {
                $this->db->where('kode_penyakit', $id);
                $data['fetch'] = $this->db->get('penyakit')->row_array();
            }

            $data['id'] = $id;
            $data['active'] = 'daftarperawatan';
            $this->load->view('v_header', $data);
            $this->load->view('v_tambah_perawatan');
            $this->load->view('v_footer');
        }
    }
    ########################### END MODUL PERAWATAN ###########################

    ########################### START MODUL PROFIL SAYA ###########################
    public function profil_saya()
    {
        $data['total_penyakit'] = $this->db->query('SELECT count(*) as total from penyakit')->row_array();
        $data['total_gejala'] = $this->db->query('SELECT count(*) as total from gejala')->row_array();
        $data['total_basis_pengetahuan'] = $this->db->query('SELECT count(*) as total from basis_pengetahuan')->row_array();
        $data['total_users'] = $this->db->query('SELECT count(*) as total from `admin`')->row_array();
        $data['total_diagnosa'] = $this->db->query('SELECT count(*) as total from `hasil`')->row_array();
        $data['total_diagnosa_saya'] = $this->db->query('SELECT count(*) as total from `hasil` WHERE id_user = ' . $_SESSION['id'])->row_array();

        if ($this->input->server('REQUEST_METHOD') === 'POST') {
            $params = $this->input->post();

            $this->db->where('id', $_SESSION['id']);
            $this->db->update('admin', [
                'nama_lengkap' => $params['nama_lengkap'],
                'username' => $params['username'],
                'password' => md5($params['password']),
                'alamat' => $params['alamat'] ? $params['alamat'] : '',
                'no_telp' => $params['no_telp'] ? $params['no_telp'] : '',
            ]);
            $_SESSION['nama_lengkap'] = $params['nama_lengkap'];
            $_SESSION['username'] = $params['username'];
            $_SESSION['password'] = $params['password'];
            $_SESSION['alamat'] = $params['alamat'];
            $_SESSION['no_telp'] = $params['no_telp'];

            echo "<script>alert('Profil berhasil diperbarui'); location.href = '" . base_url() . "dashboard/profil_saya';</script>";
        } else {
            $data['active'] = 'data_admin';
            $this->load->view('v_header', $data);
            $this->load->view('v_profil_saya');
            $this->load->view('v_footer');
        }
    }
    ########################### END MODUL PROFIL SAYA ###########################

    ########################### START MODUL DIAGNOSA ###########################
    public function diagnosa()
    {
        if ($this->input->server('REQUEST_METHOD') === 'POST') {
            $params = $this->input->post();

            $arcolor = array('#ffffff', '#cc66ff', '#019AFF', '#00CBFD', '#00FEFE', '#A4F804', '#FFFC00', '#FDCD01', '#FD9A01', '#FB6700');
            date_default_timezone_set("Asia/Jakarta");
            $inptanggal = date('Y-m-d H:i:s');

            $arbobot = array('0', '1', '0.8', '0.6', '0.4', '-0.2', '-0.4', '-0.6', '-0.8', '-1');
            $argejala = array();

            for ($i = 0; $i < count($params['kondisi']); $i++) {
                $arkondisi = explode("_", $params['kondisi'][$i]);
                if (strlen($params['kondisi'][$i]) > 1) {
                    $argejala += array($arkondisi[0] => $arkondisi[1]);
                }
            }

            $sqlkondisi = $this->db->query("SELECT * FROM kondisi order by id+0")->result_array();
            foreach ($sqlkondisi as $rkondisi) {
                $arkondisitext[$rkondisi['id']] = $rkondisi['kondisi'];
            }

            $sqlpkt = $this->db->query("SELECT * FROM penyakit order by kode_penyakit+0")->result_array();
            foreach ($sqlpkt as $rpkt) {
                $arpkt[$rpkt['kode_penyakit']] = $rpkt['nama_penyakit'];
                $ardpkt[$rpkt['kode_penyakit']] = $rpkt['det_penyakit'];
                $arspkt[$rpkt['kode_penyakit']] = $rpkt['srn_penyakit'];
                $argpkt[$rpkt['kode_penyakit']] = $rpkt['gambar'];
            }

            // -------- perhitungan certainty factor (CF) ---------
            // --------------------- START ------------------------
            $sqlpenyakit = $this->db->query("SELECT * FROM penyakit order by kode_penyakit")->result_array();
            $arpenyakit = array();
            foreach ($sqlpenyakit as $rpenyakit) {
                $cftotal_temp = 0;
                $cf = 0;
                $sqlgejala = $this->db->query("SELECT * FROM basis_pengetahuan where kode_penyakit = '" . $rpenyakit['kode_penyakit'] . "' ")->result_array();
                $cflama = 0;
                foreach ($sqlgejala as $rgejala) {
                    $arkondisi = explode("_", $params['kondisi'][0]);
                    $gejala = $arkondisi[0];

                    for ($i = 0; $i < count($params['kondisi']); $i++) {
                        $arkondisi = explode("_", $params['kondisi'][$i]);
                        $gejala = $arkondisi[0];
                        if ($rgejala['kode_gejala'] == $gejala) {
                            $cf = ($rgejala['mb'] - $rgejala['md']) * $arbobot[$arkondisi[1]];
                            if (($cf >= 0) && ($cf * $cflama >= 0)) {
                                $cflama = $cflama + ($cf * (1 - $cflama));
                            }
                            if ($cf * $cflama < 0) {
                                $cflama = ($cflama + $cf) / (1 - Math . Min(Math . abs($cflama), Math . abs($cf)));
                            }
                            if (($cf < 0) && ($cf * $cflama >= 0)) {
                                $cflama = $cflama + ($cf * (1 + $cflama));
                            }
                        }
                    }
                }
                if ($cflama > 0) {
                    $arpenyakit += array($rpenyakit["kode_penyakit"] => number_format($cflama, 4));
                }
            }

            arsort($arpenyakit);

            $inpgejala = serialize($argejala);
            $inppenyakit = serialize($arpenyakit);

            $np1 = 0;
            foreach ($arpenyakit as $key1 => $value1) {
                $np1++;
                $idpkt1[$np1] = $key1;
                $vlpkt1[$np1] = $value1;
            }

            if (empty($vlpkt1[1])) {
                echo "<script>alert('Maaf untuk konsultasi ini Hasil Anda tidak tersedia, silahkan mengisi data dan mulai konsultasi kembali'); location.href = '" . base_url() . "dashboard/diagnosa';</script>";
            }

            $this->db->insert('hasil', [
                "tanggal" => $inptanggal,
                "gejala" => $inpgejala,
                "penyakit" => $inppenyakit,
                "hasil_id" => $idpkt1[1],
                "hasil_nilai" => $vlpkt1[1],
                "id_user" => $_SESSION['id'],
                "nama_gejala" => $params['nama_gejala'],
            ]);

            $id_hasil = $this->db->insert_id();
            redirect('dashboard/diagnosa_detail?id=' . $id_hasil);
        }

        $data['total_penyakit'] = $this->db->query('SELECT count(*) as total from penyakit')->row_array();
        $data['total_gejala'] = $this->db->query('SELECT count(*) as total from gejala')->row_array();
        $data['total_basis_pengetahuan'] = $this->db->query('SELECT count(*) as total from basis_pengetahuan')->row_array();
        $data['total_users'] = $this->db->query('SELECT count(*) as total from `admin`')->row_array();
        $data['total_diagnosa'] = $this->db->query('SELECT count(*) as total from `hasil`')->row_array();
        $data['total_diagnosa_saya'] = $this->db->query('SELECT count(*) as total from `hasil` WHERE id_user = ' . $_SESSION['id'])->row_array();

        $data['datasets'] = $this->db->query('SELECT * FROM gejala ORDER BY kode_gejala')->result_array();
        $data['kondisi'] = $this->db->query('SELECT * FROM kondisi ORDER BY id')->result_array();
        $data['active'] = 'daftarperawatan';

        $this->load->view('v_header', $data);
        $this->load->view('v_diagnosa', $data);
        $this->load->view('v_footer');
    }

    public function diagnosa_detail()
    {
        $params = $_REQUEST;
        $id = (int) $params['id'];

        $arcolor = array('#ffffff', '#cc66ff', '#019AFF', '#00CBFD', '#00FEFE', '#A4F804', '#FFFC00', '#FDCD01', '#FD9A01', '#FB6700');
        date_default_timezone_set("Asia/Jakarta");
        $inptanggal = date('Y-m-d H:i:s');

        $arbobot = array('0', '1', '0.8', '0.6', '0.4', '-0.2', '-0.4', '-0.6', '-0.8', '-1');
        $argejala = array();

        try {
            for ($i = 0; $i < count($_POST['kondisi']); $i++) {
                $arkondisi = explode("_", $_POST['kondisi'][$i]);
                if (strlen($_POST['kondisi'][$i]) > 1) {
                    $argejala += array($arkondisi[0] => $arkondisi[1]);
                }
            }
        } catch (\Throwable $th) {}

        $sqlkondisi = $this->db->query("SELECT * FROM kondisi order by id+0")->result_array();
        foreach ($sqlkondisi as $rkondisi) {
            $arkondisitext[$rkondisi['id']] = $rkondisi['kondisi'];
        }

        $sqlpkt = $this->db->query("SELECT * FROM penyakit order by kode_penyakit+0")->result_array();
        foreach ($sqlpkt as $rpkt) {
            $arpkt[$rpkt['kode_penyakit']] = $rpkt['nama_penyakit'];
            $ardpkt[$rpkt['kode_penyakit']] = $rpkt['det_penyakit'];
            $arspkt[$rpkt['kode_penyakit']] = $rpkt['srn_penyakit'];
            $argpkt[$rpkt['kode_penyakit']] = $rpkt['gambar'];
        }

        $sqlhasil = $this->db->query("SELECT * FROM hasil where id_hasil = " . $id)->result_array();
        foreach ($sqlhasil as $rhasil) {
            $arpenyakit = unserialize($rhasil['penyakit']);
            $argejala = unserialize($rhasil['gejala']);
        }

        $np1 = 0;
        foreach ($arpenyakit as $key1 => $value1) {
            $np1++;
            $idpkt1[$np1] = $key1;
            $vlpkt1[$np1] = $value1;
        }

        $data['argejala'] = $argejala;
        $data['arpenyakit'] = $arpenyakit;
        $data['arcolor'] = $arcolor;
        $data['arkondisitext'] = $arkondisitext;
        $data['arpkt'] = $arpkt;
        $data['ardpkt'] = $ardpkt;
        $data['arspkt'] = $arspkt;
        $data['argpkt'] = $argpkt;
        $data['sqlhasil'] = $sqlhasil;

        $this->load->view('v_header', $data);
        $this->load->view('v_diagnosa_detail', $data);
        $this->load->view('v_footer');
    }

    public function history_diagnosa()
    {
        $data['datasets'] = $this->db->query('SELECT * FROM hasil WHERE id_user = ' . (int) $_SESSION['id'] . ' ORDER BY tanggal DESC')->result_array();
        $data['active'] = 'historydiagnosa';
        $data['total_diagnosa'] = $this->db->query('SELECT count(*) as total from `hasil`')->row_array();
        $data['total_diagnosa_saya'] = $this->db->query('SELECT count(*) as total from `hasil` WHERE id_user = ' . $_SESSION['id'])->row_array();

        $sqlgjl = $this->db->query("SELECT * FROM gejala order by kode_gejala+0")->result_array();
        foreach ($sqlgjl as $rgjl) {
            $argjl[$rgjl['kode_gejala']] = $rgjl['nama_gejala'];
        }

        $sqlpkt = $this->db->query("SELECT * FROM penyakit order by kode_penyakit+0")->result_array();
        foreach ($sqlpkt as $rpkt) {
            $arpkt[$rpkt['kode_penyakit']] = $rpkt['nama_penyakit'];
            $ardpkt[$rpkt['kode_penyakit']] = $rpkt['det_penyakit'];
            $arspkt[$rpkt['kode_penyakit']] = $rpkt['srn_penyakit'];
        }

        $data['argjl'] = $argjl;
        $data['arpkt'] = $arpkt;
        $data['ardpkt'] = $ardpkt;
        $data['arspkt'] = $arspkt;

        $this->load->view('v_header', $data);
        $this->load->view('v_history_diagnosa', $data);
        $this->load->view('v_footer');
    }

    public function hapus_hasil()
    {
        $params = $_REQUEST;
        $this->db->where('id_hasil', $params['id']);
        $this->db->delete('hasil');

        echo "<script>alert('Data Hasil Diagnosa berhasil terhapus'); location.href = '" . base_url() . "dashboard/history_diagnosa';</script>";
    }
    ########################### END MODUL DIAGNOSA ###########################

    public function logout()
    {
        $this->session->sess_destroy();
        redirect('/login', 'refresh');
    }
}
