<?php

namespace App\Http\Controllers\tatausaha;
use Exception;
use Illuminate\Support\Facades\Response;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Pendaftar;
use App\Models\Periodik;
use App\Models\Prestasi;
use App\Models\AsalSekolah;
use App\Models\Tahunajaran;
use Session;
use Input as Input;

class DaftarCtrl extends Controller
{
    public function index()
    {
        $this->data['Daftar'] = Pendaftar::all();
        return view('tatausaha.Daftar.index',$this->data);   
    }

    public function edit($id)
    {
        $this->data['Daftar'] = Pendaftar::findOrFail($id);
        $this->data['tahun'] = Tahunajaran::all();
        return view('tatausaha.Daftar.edit', $this->data);
    }

    public function show($id)
    {
        $this->data['Daftar'] = Pendaftar::findOrFail($id);
        $this->data['periodik'] = Periodik::where('id_pendaftar',$id)->first();
        $this->data['prestasi'] = Prestasi::where('id_pendaftar',$id)->first();
        $this->data['asal_sekolah'] = AsalSekolah::where('id_pendaftar',$id)->first();
        return view('tatausaha.Daftar.detail',$this->data);      
    }

    public function update($id, Request $request)
    {
        
        $this->validate($request,[
            'status'=>'required'
        ]);

        $tahun = new Tahunajaran;
        
    
        $Daftar = Pendaftar::findOrFail($id);
        
        $item = Tahunajaran::firstOrNew(array('id' => $request->thnajaran));

        $Daftar -> thnajaran_id = $item->id;
        $Daftar -> status = $request->status;
        $Daftar -> update();
        return redirect()->route('Daftar.index')->with('alert-success','Data saved');
    }
    
    public function destroy($id)
    {
        Pendaftar::destroy($id);

        Session::flash('flash_message', 'Daftar deleted!');

        return redirect()->route('Daftar.index')->with('alert-success','Data deleted');
    }


}
