<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Models\Employee;
use App\Models\EmployeeEducation;
use App\Models\EmployeeDocument;
use App\Models\EducationLevel;
use App\Models\DocumentType;
use App\Models\Grade;
use Validator;


class GradeController extends Controller
{
    
    public function index()
    {
        $data['title'] = "Grade List";
        $data['all'] = Grade::with(['gradeAmount'])->orderBy('name','ASC')->get();
        return view('admin.grade.index',$data);
    }

    public function create()
    {
        $data['title'] = "Grade Create";
        return view('admin.grade.add',$data);
    }

    
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'name' => 'required'
        ]);
        if ($validator->passes()) {
            $user_id = \Auth::user()->id;
            $grade = new Grade;
            $grade->name = $request->name;
            $grade->added_by = $user_id;
            $grade->status = 1;
            $grade->save();
            return redirect('admin/grade')->with('msg','Data Insert Successful!');
        }
        return response()->json(['error'=>$validator->errors()->all()]);  
    }
    public function edit($id)
    {
        
        $data['title'] = "Grade Edit";
        $data['grade'] = Grade::find($id);
        return view('admin.grade.edit',$data);
    }

    
    public function update(Request $request, $id)
    {
        $this->validate($request,[
            'name'            => 'required'
        ]);
        $user_id = \Auth::user()->id;
        $grade_u = Grade::find($id);
        $grade_u->name = $request->name;
        $grade_u->updated_by = $user_id;
        $grade_u->save();
        return redirect('admin/grade')->with('msg','Data Update Successful!');
    }

    
    public function destroy($id)
    {
        Grade::whereId($id)->delete();
        return redirect('admin/grade')->with('msg','Successfully Deleted!');
    }
    public function statusUpdate($id)
    {

        $user_id = \Auth::user()->id;
        $grade_info = Grade::find($id);
        if($grade_info->status == 0){
            $grade_info->status = 1;
            $status = 'published';
        }else{
            $grade_info->status = 0;
            $status = 'unpublished';
        }
        $grade_info->update();

    }
}
