<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Konten\Kegiatan;
use App\Models\Kuesioner;
use App\Models\ProfileSekolah;
use App\Models\User;
use App\Models\Master\Sekolah;

class FrontController extends Controller
{
    function index() {
        $data['side_kegiatan'] = Kegiatan::orderBy('KegiatanId','DESC')->limit(3)->get();
        $data['kegiatan'] = Kegiatan::orderBy('KegiatanId','DESC')->limit(8)->get();
        $data['sekolah'] = number_format(Sekolah::count());
        $data['usulan'] = [
            // 'kota' => Kuesioner::where('level',0)->where('tahun',date('Y'))->count(),
            // 'provinsi' => Kuesioner::where('level',1)->where('tahun',date('Y'))->count(),
            // 'nasional' => Kuesioner::where('level',2)->where('tahun',date('Y'))->count(),

            'kota' => Kuesioner::where('level',0)->count(),
            'provinsi' => Kuesioner::where('level',1)->count(),
            'nasional' => Kuesioner::where('level',2)->count(),
            'mandiri' => Kuesioner::where('level',3)->count(),
        ];
        $data['title'] = 'Beranda';
        return view('index',$data);
    }
    function sekolah() {
        $data['title'] = 'Daftar Sekolah Adiwiyata';
        return view('sekolah',$data);
    }
    function kegiatan() {
        $data['title'] = 'Kegiatan Sekolah Adiwiyata';
        $data['kegiatan'] = Kegiatan::orderBy('KegiatanId','DESC')->paginate(8);
        return view('kegiatan',$data);
    }
    function kegiatan_detail($slug) {
        $data['title'] = 'Kegiatan Sekolah Adiwiyata';
        $data['kegiatan'] = Kegiatan::where('slug',$slug)->first();
        $data['lainnya'] = Kegiatan::where('KegiatanId','!=',$data['kegiatan']->KegiatanId)->orderBy('KegiatanId','DESC')->limit(4)->get();
        return view('kegiatan_detail',$data);
    }
    function galeri() {
        $data['title'] = 'Galeri Sekolah Adiwiyata';
        $data['kegiatan'] = Kegiatan::orderBy('KegiatanId','DESC')->paginate(8);
        return view('galeri',$data);
    }
    function dataSekolah(){
        // $data = Kuesioner::where('status',11)->where('tahun',date('Y'))->get();
        $data = Kuesioner::where('status',11)->get();

        // $data = [
        //         ['name' => 'SMPN 253','kota' => 'Jakarta Selatan','skor' => '89,86','tahun' => '2024'],
        //         ['name' => 'SDI Al Irsyad Islamiyah','kota' => 'Jakarta Pusat','skor' => '89,85','tahun' => '2024'],
        //         ['name' => 'SDN Kemanggisan 17','kota' => 'Jakarta Barat','skor' => '89,45','tahun' => ''],
        //         ['name' => 'SDN Sukabumi Utara 07','kota' => 'Jakarta Barat','skor' => '89,54','tahun' => ''],
        //         ['name' => 'SDS Strada Santo Ignatius','kota' => 'Jakarta Utara','skor' => '89,24','tahun' => ''],
        //         ['name' => 'SDN Jatinegara 06 Pagi','kota' => 'Jakarta Timur','skor' => '89,33','tahun' => ''],
        //         ['name' => 'SDN Pekayon 03 Pagi','kota' => 'Jakarta Timur','skor' => '89,43','tahun' => ''],
        //         ['name' => 'SDN Lubang Buaya 06 Pagi','kota' => 'Jakarta Timur','skor' => '89,71','tahun' => ''],
        //     ];

        $_data = [];

        // dd($data);
        foreach ($data as $key => $row) {

            $_data[] = [
                'no'       => $key+1,
                'sekolah'  => @$row->sekolah->nama_sekolah,
                'kota'     => @$row->sekolah->kabupaten->name,
                'skor'     => '89.9',
                'tahun'    => @$row->tahun,
            ];

        }

        return response()->json($_data);
    }
}
