<?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 App\Models\Designation;
use App\Models\PayrollHead;
use App\Models\GradeStructure;

class GradeStructureController extends Controller
{
    
    public function index()
    {
        $data['title'] = "Grade Structure List";
        $data['grade'] = Grade::with(['gradehead.head'])->orderBy('name','ASC')->get();
        $data['heads'] = PayrollHead::orderBy('name','ASC')->get();
        $data['all'] = GradeStructure::with(['grade','head'])->orderBy('grade_id','ASC')->get();
        return view('admin.grade_structure.index',$data);
    }
    public function edit($id)
    {
        
        $data['title'] = "Grade Structure Edit";
        $data['single'] = GradeStructure::with(['grade','head'])->where('grade_id',$id)->get();
        $data['heads'] = PayrollHead::orderBy('name','ASC')->get();
        $data['grade_id'] = Grade::find($id);
        return view('admin.grade_structure.edit',$data);
    }

    
    public function update(Request $request)
    {
        //dd($request->all());
        $user_id = \Auth::user()->id;
        $grade_id = $request->grade_id;
        $delete = GradeStructure::where('grade_id',$grade_id)->delete();
        if ($delete) {
            $input = $request->all();
            $condition = $input['head_id'];
            foreach ($condition as $key => $condition) {
                $gt = new GradeStructure;
                $gt->grade_id = $grade_id;
                $gt->head_id = $input['head_id'][$key];
                $gt->amount = $input['amount'][$key];
                $gt->status = 1;
                $gt->added_by = $user_id ;
                $gt->save();
            }
        }
        return redirect('admin/grade_structure')->with('msg','Data Update Successful!');
    }

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

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

    }
}
