<?php

namespace App\Http\Controllers\Website;

use App\Http\Controllers\Controller;
use App\Models\HeightSubDistrict;
use App\Models\SubDistrict;
use Illuminate\Http\Request;

class ParameterListHeightSubDistrictController extends Controller
{
    public function index()
    {
        $listHeightSubDistrict = HeightSubDistrict::all();

        $years = array_unique(array_column($listHeightSubDistrict->toArray(), 'year'));
        // Urutkan dari tahun terkecil ke terbesar
        sort($years);

        $subDistricts = SubDistrict::all();

        return view('website.app.parameter-list-height-sub-district', compact('years', 'subDistricts'));
    }

    public function getDataParameterListHeightSubDistrict($years)
    {
        $listHeightSubDistrict = HeightSubDistrict::select(
            'height_sub_district.id as hsd_id',
            'height_sub_district.sub_district_id as hsd_sub_district_id',
            'height_sub_district.height',
            'height_sub_district.year',
            'sub_district.id as sd_id as sd_id',
            'sub_district.sub_district'
        )->join('sub_district', 'height_sub_district.sub_district_id', '=', 'sub_district.id')
            ->where('year', $years)->get();

        return response()->json([
            'listHeightSubDistrict' => $listHeightSubDistrict
        ]);
    }

    public function addListHeightSubDistrict(Request $request)
    {
        $subDistrictId = $request->input('subDistrictId');
        $year = $request->input('year');
        $height = $request->input('height');

        $heightBySubDistrictYear = HeightSubDistrict::where('sub_district_id', '=', $subDistrictId)
            ->where('year', '=', $year)
            ->get();

        if (count($heightBySubDistrictYear) > 0) {
            toast('Gagal, Data ketingian tempat kecamatan (tahunan) sudah tersedia!', 'error');
            return redirect()->back();
        } else {
            HeightSubDistrict::create([
                'id' => $year . $subDistrictId,
                'sub_district_id' => $subDistrictId,
                'height' => $height,
                'year' => $year,
            ]);

            toast('Berhasil, Data ketingian tempat kecamatan (tahunan) disimpan!', 'success');
            return redirect()->back();
        }
    }

    public function updateListHeightSubDistrict(Request $request)
    {
        $heightSubDistrictId = $request->input('heightSubDistrictId');
        $subDistrictId = $request->input('subDistrictId');
        $year = $request->input('year');
        $height = $request->input('height');

        $heightSubDistrictById = HeightSubDistrict::where('id', $heightSubDistrictId)->first();

        $heightSubDistrictById['sub_district_id'] = $subDistrictId;
        $heightSubDistrictById['year'] = $year;
        $heightSubDistrictById['height'] = $height;
        $heightSubDistrictById->save();

        toast('Berhasil, Data ketingian tempat kecamatan (tahunan) diubah!', 'success');
        return redirect()->back();
    }

    public function deleteListHeightSubDistrict($id)
    {
        $heightSubDistrictById = HeightSubDistrict::where('id', $id)->first();
        $heightSubDistrictById->delete();

        toast('Berhasil, Data ketinggian tempat kecamatan (tahunan) dihapus', 'success');
        return redirect()->back();
    }
}
