<?php

namespace App\Controllers\Admin;

use App\Controllers\BaseController;
use App\Models\LokasiKejadianBanjirModel;

class LokasiKejadianBanjir extends BaseController
{
    protected $lokasiModel;

    public function __construct()
    {
        $this->lokasiModel = new LokasiKejadianBanjirModel();

        if (!session()->get('logged_in')) {
            header('Location: ' . base_url('login'));
            exit;
        }

        if (session()->get('role') != 'admin') {
            header('Location: ' . base_url('/'));
            exit;
        }
    }

    // =====================================================
    // INDEX
    // =====================================================

    public function index()
    {
        $data = [
            'title'  => 'Lokasi Kejadian Banjir',
            'lokasi' => $this->lokasiModel
                ->orderBy('tahun', 'DESC')
                ->orderBy('id_kejadian', 'DESC')
                ->findAll()
        ];

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


    // =====================================================
    // STORE / TAMBAH
    // =====================================================

    public function store()
    {
        $this->lokasiModel->insert([
            'latitude'   => $this->request->getPost('latitude'),
            'longitude'  => $this->request->getPost('longitude'),
            'desa'       => $this->request->getPost('desa'),
            'tahun'      => $this->request->getPost('tahun'),
            'keterangan' => $this->request->getPost('keterangan'),
            'marker'     => $this->request->getPost('marker')
        ]);

        return redirect()
            ->to(base_url('admin/lokasi-kejadian-banjir'))
            ->with('success', 'Lokasi kejadian banjir berhasil ditambahkan.');
    }


    // =====================================================
    // UPDATE / EDIT
    // =====================================================

    public function update($id)
    {
        $lokasi = $this->lokasiModel->find($id);

        if (!$lokasi) {
            return redirect()
                ->to(base_url('admin/lokasi-kejadian-banjir'))
                ->with('error', 'Data lokasi tidak ditemukan.');
        }

        $this->lokasiModel->update($id, [
            'latitude'   => $this->request->getPost('latitude'),
            'longitude'  => $this->request->getPost('longitude'),
            'desa'       => $this->request->getPost('desa'),
            'tahun'      => $this->request->getPost('tahun'),
            'keterangan' => $this->request->getPost('keterangan'),
            'marker'     => $this->request->getPost('marker')
        ]);

        return redirect()
            ->to(base_url('admin/lokasi-kejadian-banjir'))
            ->with('success', 'Lokasi kejadian banjir berhasil diperbarui.');
    }


    // =====================================================
    // DELETE / HAPUS
    // =====================================================

    public function delete($id)
    {
        $lokasi = $this->lokasiModel->find($id);

        if (!$lokasi) {
            return redirect()
                ->to(base_url('admin/lokasi-kejadian-banjir'))
                ->with('error', 'Data lokasi tidak ditemukan.');
        }

        $this->lokasiModel->delete($id);

        return redirect()
            ->to(base_url('admin/lokasi-kejadian-banjir'))
            ->with('success', 'Lokasi kejadian banjir berhasil dihapus.');
    }
}
