<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\SampahTerkelola;

class SampahController extends Controller
{
    protected $template = 'modules.sampah';
    protected $route = 'modules.sampah';
    protected $title = 'Sampah Terkelola';

    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $data['title'] = $this->title;
        $data['route'] = $this->route;    
        return view($this->template.'.index', $data);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    public function grid(Request $request)
    {
        if(session('group_alias') == 'sekolah'){
            $data = SampahTerkelola::where('ms_sekolah_id',session('sekolah_id'))
                ->orderBy('tahun','DESC')
                ->orderBy('bulan','ASC');
        }else{
            if(session('kabupaten_id') != ''){
                $data = SampahTerkelola::whereHas('sekolah',function($query){
                        $query->where('ms_kabupaten_id',session('kabupaten_id'));
                    })->orderBy('tahun','DESC')
                    ->orderBy('bulan','ASC');
                    
            }else{
                $data = SampahTerkelola::with(['sekolah'])->orderBy('tahun','DESC')
                    ->orderBy('bulan','ASC');
            }
        }

        if(@request()->tahun){
            $tahun = request()->tahun;
        }else{
            $tahun = date('Y');
        }

        $data->where('tahun',$tahun);

        
        $_data = [];

        

        foreach ($data->get() as $key => $row) {

            $sekolah = '';
            $sekolah .= @$row->sekolah->nama_sekolah.'<br>';
            $sekolah .= '<span class="badge bg-success-light text-success">'.@$row->sekolah->profile->tingkat->name.'</span>&nbsp;';
            $sekolah .= '<span class="badge bg-primary-light text-primary mt-1">'.@$row->sekolah->profile->status_sekolah.'</span>&nbsp;';
            $sekolah .= '<span class="badge bg-info-light text-info">Level '.levelAdiwiyata($row->sekolah->npsn).'</span>';
            $sekolah .= '<span class="badge bg-secondary text-secondary text-left mt-1">'.@$row->sekolah->profile->alamat_sekolah.'</span>&nbsp;';
            
           $_data[] = [
            'no'                => $key+1,
            'id'                => encode_id($row->SampahTekelolaId),
            'tahun'             => $row->tahun,
            'sekolah'           => $sekolah,
            'bulan'             => monthtString($row->bulan),
            'organik'           => $row->organik,
            'sampah_anorganik'  => $row->sampah_anorganik,
            'b3'                => $row->b3,
            'minyak_jelantah'   => $row->minyak_jelantah,
        ];

        }

        // return response()->json($_data);  // Return the data as a JSON response
        return response()->json($_data);

    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        try {
            $data = SampahTerkelola::updateOrCreate([
                'tahun'                 => date('Y'),
                'bulan'                 => $request->bulan,
                'ms_sekolah_id'         => session('sekolah_id'),
            ],[
                'tahun'                 => date('Y'),
                'ms_sekolah_id'         => session('sekolah_id'),
                'bulan'                 => $request->bulan,
                'organik'               => $request->organik,
                'sampah_anorganik'      => $request->anorganik,
                'b3'                    => $request->b3,
                'minyak_jelantah'       => $request->minyak_jelantah,
            ]);

            return redirect()->back()->with([
                'message' => 'Berhasil update data',
                'type'    => 'success',
            ]);
        } catch (\Throwable $th) {
            //throw $th;

            dd($th);
        }
        
        
        
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        //
    }
}
