<?php

namespace App\Controllers\Admin;

use App\Controllers\BaseController;

use App\Models\StrategiMitigasiModel;
use App\Models\WilayahMitigasiModel;
use App\Models\PetaKerawananModel;
use App\Models\WilayahModel;
use App\Models\KemiringanLerengModel;
use App\Models\PenggunaanLahanModel;
use App\Models\JenisTanahModel;
use App\Models\GeologiModel;


class Dashboard extends BaseController
{

    protected $strategi;
    protected $wilayahMitigasi;
    protected $peta;
    protected $wilayah;
    protected $kemiringan;
    protected $lahan;
    protected $tanah;
    protected $geologi;


    public function __construct()
    {

        // Data Strategi Mitigasi
        $this->strategi = new StrategiMitigasiModel();
        // Wilayah Mitigasi
        $this->wilayahMitigasi = new WilayahMitigasiModel();

        // Data WebGIS
        $this->peta = new PetaKerawananModel();
        // Wilayah terbaru (desa)
        $this->wilayah = new WilayahModel();
        $this->kemiringan = new KemiringanLerengModel();
        $this->lahan = new PenggunaanLahanModel();
        $this->tanah = new JenisTanahModel();
        $this->geologi = new GeologiModel();


        if (!session()->get('logged_in')) {
            return redirect()->to('/login');
        }

        if (session()->get('role') != 'admin') {
            return redirect()->to('/');
        }
    }



    public function index()
    {

        $peta = $this->peta->find(21);


        $data = [

            'title' => 'Dashboard',


            // ==========================
            // PETA WEBGIS
            // ==========================

            'peta' => $peta,

            'kemiringan' => $this->kemiringan->findAll(),

            'lahan' => $this->lahan->findAll(),

            'jenis_tanah' => $this->tanah->findAll(),

            'geologi' => $this->geologi->findAll(),

            'wilayah' => $this->wilayah
                ->orderBy('nama_desa', 'ASC')
                ->findAll(),



            // ==========================
            // STATISTIK
            // ==========================

            'jumlahPeta' => $this->peta->countAllResults(),

            'jumlahWilayah' => $this->wilayah->countAllResults(),

            'jumlahKemiringan' => $this->kemiringan->countAllResults(),

            'jumlahLahan' => $this->lahan->countAllResults(),

            'jumlahTanah' => $this->tanah->countAllResults(),

            'jumlahGeologi' => $this->geologi->countAllResults(),

            // ==========================
            // PETA WEBGIS
            // ==========================

            'peta' => $peta,

            'wilayah' => $this->wilayah
                ->orderBy('nama_desa', 'ASC')
                ->findAll(),

            'kemiringan' => $this->kemiringan->findAll(),

            'lahan' => $this->lahan->findAll(),

            'jenis_tanah' => $this->tanah->findAll(),

            'geologi' => $this->geologi->findAll(),



            // ==========================
            // STRATEGI
            // ==========================

            'jumlahStrategi' => $this->strategi->countAllResults(),

            'jumlahStruktural' => $this->strategi
                ->where('jenis', 'struktural')
                ->countAllResults(),

            'jumlahNonStruktural' => $this->strategi
                ->where('jenis', 'non_struktural')
                ->countAllResults(),



            // ==========================
            // WILAYAH MITIGASI TERBARU
            // ==========================

            'wilayahTerbaru' => $this->wilayahMitigasi
                ->getData()

        ];


        return view('admin/dashboard', $data);
    }
}
