<?php

namespace App\Http\Controllers\Website;

use App\Exports\ParameterHeightSubDistrictExport;
use App\Http\Controllers\Controller;
use App\Models\HeightSubDistrict;
use App\Models\SubDistrict;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

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

    public function getDataHeightSubDistrict()
    {
        $allSubDistrict = SubDistrict::select('id', 'sub_district', 'latitude', 'longitude')->get();

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

    public function excelParameterHeightSubDistrict($year)
    {
        $parameterHeightSubDistrict = HeightSubDistrict::select('height_sub_district.id as height_id', 'height_sub_district.height', 'height_sub_district.year', 'sub_district.id as sub_district_id', 'sub_district.sub_district')
            ->join('sub_district', 'height_sub_district.sub_district_id', '=', 'sub_district.id')
            ->where('height_sub_district.year', $year)
            ->get();

        return Excel::download(new ParameterHeightSubDistrictExport($parameterHeightSubDistrict), 'parameter-ketinggian-kecamatan-tahun-' . $year . '.xlsx');
    }
}
