<?php

namespace App\Http\Controllers\calonsiswa;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

use Input as Input;

use Illuminate\Support\Facades\Auth;
use App\Models\Pendaftar;
use App\Models\Tahunajaran;
use App\Models\AsalSekolah;
use App\User;


class PendaftarController extends Controller
{
    public function index()
    {  
        return view('calonsiswa.dashboard');
    }

    public function hasilujian()
    {  
        $this->data['title'] = 'Hasil Ujian';
        $this->data['pendaftar'] = Pendaftar::all();
        $this->data['asalsekolah'] = AsalSekolah::all();
        return view('calonsiswa.hasilujian.index',$this->data);
    }

    public function create()
    {
    	 $this->data['title'] = 'Lengkapi Form Data Pribadi';
         $this->data['tahunajaran'] = Tahunajaran::all();
        return view('calonsiswa.pendaftar.create',$this->data);
    }

    public function store(Request $request)
    {
        $this->validate($request,[
            'no_pendaftar'=>'required',
            'nama'=>'required',
            'jeniskelamin'=>'required',
            'tempatlahir'=>'required',
            'tanggallahir'=>'required',
            'agama'=>'required',
            'alamat'=>'required',
            'kebutuhan_khusus'=>'required',
            'no_tlp_rumah'=>'required',
            'thnajaran_id'=>'required'

        ]);

            
            $input['user_id'] = $request->user()->id;

            $input['no_pendaftar'] = $request->input('no_pendaftar');
            $input['nama'] = $request->input('nama');
            $input['jeniskelamin'] = $request->input('jeniskelamin');
            $input['tempatlahir'] = $request->input('tempatlahir');
            $input['tanggallahir'] = $request->input('tanggallahir');
            $input['agama'] = $request->input('agama');
            $input['alamat'] = $request->input('alamat');
            $input['kebutuhan_khusus'] = $request->input('kebutuhan_khusus');
            $input['no_tlp_rumah'] = $request->input('no_tlp_rumah');


            $id = $request->input('id');


            $id=Pendaftar::find('user_id');

            if($id !== $request->user()->user_id){
                return redirect()->route('pendaftar.index')->with('info','Sudah terdaftar');
            }
            else{
            Pendaftar::updateOrCreate($input);
         	return redirect()->route('pendaftar.index')->with('info','Selesai Mendaftar');
         }
         
    }

        public function show($id)
            {
                //
                $this->data['pendaftar'] = Pendaftar::where('user_id',$id)->get();

                return view('calonsiswa.pendaftar.show',$this->data);
            }

        public function destroy($id)
        {
            Pendaftar::destroy($id);

            Session::flash('flash_message', 'pengdaftar dihapus!');

            return redirect()->route('pendaftar.index');
        }
    }
