<?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\OrangTua;
use App\Models\Pendaftar;
use Session;
use DB;


class OrangTuaController extends Controller
{
    public function index()
    {
        $this->data['title'] = 'Lengkapi Form Data OrangTua';
        $this->data['orangtua'] = Auth::user()->pendaftar;
        return view('calonsiswa.orangtua.index',$this->data);
    }

    public function create()
    {
        return view('calonsiswa.orangtua.create');
    }


    public function store(Request $request)
    {
        $this->validate($request,[
            'nama_ayah'=>'required',
            'tahun_lahir_ayah'=>'required',
            'nik_ayah'=>'required',
            'kebutuhan_khus_ayah'=>'required',
            'pekerjaan_ayah'=>'required',
            'pendidikan_ayah'=>'required',
            'penghasilan_bulanan_ayah'=>'required',
            'nama_ibu'=>'required',
            'tahun_lahir_ibu'=>'required',
            'nik_ibu'=>'required',
            'kebutuhan_khus_ibu'=>'required',
            'pekerjaan_ibu'=>'required',
            'pendidikan_ibu'=>'required',
            'penghasilan_bulanan_ibu'=>'required'
        ]);


            $input['nama_ayah'] = $request->input('nama_ayah');
            $input['tahun_lahir_ayah'] = $request->input('tahun_lahir_ayah');
            $input['nik_ayah'] = $request->input('nik_ayah');
            $input['kebutuhan_khus_ayah'] = $request->input('kebutuhan_khus_ayah');
            $input['pekerjaan_ayah'] = $request->input('pekerjaan_ayah');
            $input['pendidikan_ayah'] = $request->input('pendidikan_ayah');
            $input['penghasilan_bulanan_ayah'] = $request->input('penghasilan_bulanan_ayah');

            $input['nama_ibu'] = $request->input('nama_ibu');
            $input['tahun_lahir_ibu'] = $request->input('tahun_lahir_ibu');
            $input['nik_ibu'] = $request->input('nik_ibu');
            $input['kebutuhan_khus_ibu'] = $request->input('kebutuhan_khus_ibu');
            $input['pekerjaan_ibu'] = $request->input('pekerjaan_ibu');
            $input['pendidikan_ibu'] = $request->input('pendidikan_ibu');
            $input['penghasilan_bulanan_ibu'] = $request->input('penghasilan_bulanan_ibu');

            //wali
            $input['nama_wali'] = null;
            $input['tahun_lahir_wali'] = null;
            $input['nik_wali'] = null;
            $input['kebutuhan_khus_wali'] = null;
            $input['pekerjaan_wali'] = null;
            $input['pendidikan_wali'] = null;
            $input['penghasilan_bulanan_wali'] = null;

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

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

            OrangTua::UpdateOrCreate($input);
        
        	return redirect()->route('orangtua.index')->with('info','Berhasil menambah data orang tua');
        }

        public function storewali(Request $request){
            
            $this->validate($request,[
            'nama_wali'=>'required',
            'tahun_lahir_wali'=>'required',
            'nik_wali'=>'required',
            'kebutuhan_khus_wali'=>'required',
            'pekerjaan_wali'=>'required',
            'pendidikan_wali'=>'required',
            'penghasilan_bulanan_wali'=>'required'
            ]); 

            $input['nama_ayah'] = null;
            $input['tahun_lahir_ayah'] = null;
            $input['nik_ayah'] = null;
            $input['kebutuhan_khus_ayah'] = null;
            $input['pekerjaan_ayah'] = null;
            $input['pendidikan_ayah'] = null;
            $input['penghasilan_bulanan_ayah'] = null;

            $input['nama_ibu'] = null;
            $input['tahun_lahir_ibu'] = null;
            $input['nik_ibu'] = null;
            $input['kebutuhan_khus_ibu'] = null;
            $input['pekerjaan_ibu'] = null;
            $input['pendidikan_ibu'] = null;
            $input['penghasilan_bulanan_ibu'] = null;

            //wali
            $input['nama_wali'] = $request->input('nama_wali');
            $input['tahun_lahir_wali'] = $request->input('tahun_lahir_wali');
            $input['nik_wali'] = $request->input('nik_wali');;
            $input['kebutuhan_khus_wali'] = $request->input('kebutuhan_khus_wali');;
            $input['pekerjaan_wali'] = $request->input('pekerjaan_wali');;
            $input['pendidikan_wali'] = $request->input('pendidikan_wali');;
            $input['penghasilan_bulanan_wali'] = $request->input('penghasilan_bulanan_wali');;

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

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

            OrangTua::UpdateOrCreate($input);
        
            return redirect()->route('orangtua.index')->with('info','Berhasil menambah data orang tua');
        }

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

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

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

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

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