<?php

namespace App\Http\Controllers\calonsiswa;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\AsalSekolah;
use Input as Input;
use App\User;


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

    public function create()
    {
    	 $this->data['title'] = 'Lengkapi Form Asal Sekolah';
        return view('calonsiswa.asalsekolah.create',$this->data);
    }

    public function store(Request $request)
    {
        $this->validate($request,[
            'nama_sekolah'=>'required',
            'alamat_sekolah'=>'required',
            'nisn'=>'required',
            'nik'=>'required',
            'no_ijazah'=>'required',
            'no_skhun'=>'required',
            'no_ujiannasional'=>'required',
        ]);

        
            $input['id_pendaftar'] = $request->user()->Pendaftar->id;
            $input['nama_sekolah'] = $request->input('nama_sekolah');
            $input['alamat_sekolah'] = $request->input('alamat_sekolah');
            $input['nisn'] = $request->input('nisn');
            $input['nik'] = $request->input('nik');
            $input['no_ijazah'] = $request->input('no_ijazah');
            $input['no_skhun'] = $request->input('no_skhun');
            $input['no_ujiannasional'] = $request->input('no_ujiannasional');
            $id = $request->input('id');

            $id=AsalSekolah::find('id_pendaftar');

            if($id == $request->user()->id_pendaftar){
                do {
                   AsalSekolah::updateOrCreate($input);
                    } while ($id !== $request->user()->id_pendaftar); 
                return redirect()->route('asalsekolah.index')->with('info','Selesai Mendaftar');
                }
            else{
                return redirect()->route('asalsekolah.index')->with('info','Sudah terdaftar');
            
         }
         
    }

        public function show($id)
            {
                //
                $this->data['asalsekolah'] = AsalSekolah::where('id_pendaftar',$id)->get();

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

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

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

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