<?php

namespace App\Controllers;
use App\Models\ModelDarah;

class Demam extends BaseController
{
  

    protected $DarahModel;

    function __construct()
    {
        $this->DarahModel = new ModelDarah();
    }



    // public function tes(){
        
    //     return view('template');
    // }

    public function index() 
    {
    
        $data = array(
    
            'body' => 'Demam_berdarah/list',
            'data_darah' => $this->DarahModel->getDarah(),
    
        );
        return view('template', $data);
    
    } 



    public function input_darah()
    {
        $data = array(
    
            'body' => 'Demam_berdarah/input',
    
        );
        return view('template', $data);
    }

    
    public function insert_darah()
{
    
    $data = array(
        'Tahun'    => $this->request->getPost('Tahun'),
        'Januari'    => $this->request->getPost('Januari'),
        'Februari'    => $this->request->getPost('Februari'),
        'Maret'    => $this->request->getPost('Maret'),
        'April'    => $this->request->getPost('April'),
        'Mei'    => $this->request->getPost('Mei'),
        'Juni'    => $this->request->getPost('Juni'),
        'Juli'    => $this->request->getPost('Juli'),
        'Agustus'    => $this->request->getPost('Agustus'),
        'September'    => $this->request->getPost('September'),
        'Oktober'    => $this->request->getPost('Oktober'),
        'November'    => $this->request->getPost('November'),
        'Desember'    => $this->request->getPost('Desember'),
    );

    if ($this->DarahModel->insert_darah($data)) {
        session()->setFlashdata('success', 'Berhasil Menambahkan Data');
        return redirect()->to(base_url() . 'demam_berdarah/list');
    } else {
        session()->setFlashdata('error', 'Gagal Menambahkan Data');
        return redirect()->to(base_url() . 'demam_berdarah/input');
    }
}


public function edit($id)
{
    $data        = array(
        'body'   => 'Demam_berdarah/edit',
        'data'   => $this->DarahModel->getById($id)->getRow(),
       
    );
    return view('template', $data);
}

public function update($id)
{
    $data = $this->request->getPost();
    $result = $this->DarahModel->update($id, $data);

    if ($result) {
        return redirect()->to(base_url() . 'demam_berdarah/list')->with('success', 'Data Updated Successfully');
    } else {
        return redirect()->to(base_url() . 'demam_berdarah/edit/' . $id)->with('error', 'Data Updated Failed');
    }
}

public function delete($id)
{
    $this->DarahModel->delete($id);
    return redirect()->to(base_url() . 'demam_berdarah/list')->with('success', 'Data Deleted Successfully');
}




}
