<?php

namespace App\Http\Controllers\tatausaha;

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

class AkunCtrl extends Controller
{
    public function index()
    {

        $this->data['akunsiswa'] = User::where('role','calonsiswa')->get();

        return view('tatausaha.akunsiswa.index',$this->data);
        
    }

    public function create()
    {
        return view('tatausaha.akunsiswa.create');
    }

    public function store(Request $request)
    {
        $this->validate($request,['name'=> 'required','email'=>'required','username'=>'required','password'=>'required']);

        $akunsiswa = new User();
        $akunsiswa->name = $request->name;
        $akunsiswa->email = $request->email;
        $akunsiswa->username = $request->username;
        $akunsiswa->password = bcrypt($request->password);
        $akunsiswa->role = 'calonsiswa';
        $akunsiswa->save();

        return redirect()->route('akunsiswa.index')->with('info','User berhasil ditambahkan');
    }

    public function edit($id)
    {
        $this->data['akunsiswa'] = User::findOrFail($id);
        $this->data['title'] = 'Edit data user';
        return view('tatausaha.akunsiswa.edit', $this->data);
    }

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

        $akunsiswa = User::findOrFail($id);
        $akunsiswa-> name = $request-> name;
        $akunsiswa-> email = $request-> email;
        $akunsiswa-> username = $request-> username;
        $akunsiswa-> password = bcrypt($request->password);

        $akunsiswa -> save();
        return redirect()->route('akunsiswa.index')->with('alert-success','Data saved');
    }

    public function show($id)
    {
        $this->data['akunsiswa'] = User::all();
        return redirect()->route('akunsiswa.index',$this->data);
    }

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

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

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