<?php

namespace App\Http\Controllers\Website;

use App\Exports\ParameterHeightVillageExport;
use App\Http\Controllers\Controller;
use App\Models\HeightVillage;
use App\Models\Village;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class ParameterHeightVillageController extends Controller
{
    public function index()
    {
        return view('website.app.parameter-height-village');
    }

    public function getDataHeightVillage()
    {
        $allDataVillage = Village::select('village.id as village_id', 'village.village', 'village.latitude', 'village.longitude', 'sub_district.sub_district')
            ->join('sub_district', 'sub_district.id', '=', 'village.sub_district_id')
            ->get();

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

    public function excelParameterHeightVillage($year)
    {
        $parameterHeightVillage = HeightVillage::select('height_village.id as height_id', 'height_village.height', 'height_village.year', 'village.id as village_id', 'village.village', 'sub_district.sub_district')
            ->join('village', 'height_village.village_id', '=', 'village.id')
            ->join('sub_district', 'village.sub_district_id', '=', 'sub_district.id')
            ->where('height_village.year', $year)
            ->get();

        return Excel::download(new ParameterHeightVillageExport($parameterHeightVillage), 'parameter-ketinggian-desa-tahun-' . $year . '.xlsx');
    }
}
