<?php

namespace App\Controllers;
use App\Models\ModelCurah;
use Mpdf\Mpdf;
use Dompdf\Dompdf;




use function PHPUnit\Framework\once;

class Curah extends BaseController
{
    protected $CurahModel;

    function __construct()
    {
        $this->CurahModel = new ModelCurah();
    }


    public function index()
    {
    
        $data = array(
    
            'body' => 'curah_hujan/list',
            'data_curah' => $this->CurahModel->getCurah(),
    
        );
        return view('template', $data);
    
    } 


    public function input_darah()
    {
        $data = array(
    
            'body' => 'curah_hujan/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->CurahModel->insert_darah($data)) {
        session()->setFlashdata('success', 'Berhasil Menambahkan Data');
        return redirect()->to(base_url() . 'curah_hujan/list');
    } else {
        session()->setFlashdata('error', 'Gagal Menambahkan Data');
        return redirect()->to(base_url() . 'curah_hujan/input');
    }
}


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

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

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

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

public function cetak()
{


   
    // Create an instance of the class:
    $dompdf = new Dompdf();
    $dompdf->loadHtml(view('curah_hujan/laporan'));
    
    // (Optional) Setup the paper size and orientation
    $dompdf->setPaper('A4', 'landscape');
    
    // Render the HTML as PDF
    $dompdf->render();
    
    // Output the generated PDF to Browser
    $dompdf->stream();
    
}



    
}
