<?php

namespace App\Http\Controllers\HR;


use App\HrHolidayModel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class HrHolidayController extends Controller
{
    function __construct()
    {
        $this->middleware('permission:Holiday-List');
        $this->middleware('permission:Holiday-Create', ['only' => ['create','store']]);
        $this->middleware('permission:Holiday-Edit', ['only' => ['edit','update']]);
    }
    public function index()
    {
        $hol = HrHolidayModel::where('status',1)->get();
        return view('hr.holiday.index',compact('hol'));
    }
    public function create()
    {
        return view('hr.holiday.create');
    }
    public function store(Request $request)
    {
        
        $this->validate($request,[
            'name' => 'required',
            'from_date' => 'required',
            'to_date' => 'required'
            
        ]);
        
        $hol = new HrHolidayModel();
        $hol->name = $request->name;
        $hol->from_date = date('Y-m-d',strtotime($request->from_date));;
        $hol->to_date = date('Y-m-d',strtotime($request->to_date));;
        $hol->note = $request->note;
        $hol->save();
        return redirect()->route('hr-holiday.index')->with('success','Holiday was saved successful ...');
    }
    public function show($id)
    {
        //
    }
    public function edit($id)
    {
        $hol = HrHolidayModel::find($id);
        return view('hr.holiday.edit',compact('hol'));
    }

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

        //dd($request->all());
        $hol = HrHolidayModel::find($id);
        $hol->name = $request->name;
        $hol->from_date = date('Y-m-d',strtotime($request->from_date));
        $hol->to_date = date('Y-m-d',strtotime($request->to_date));
        $hol->note = $request->note;
        $hol->update();
        return redirect()->route('hr-holiday.index')->with('success','Holiday was updated successful ...');
    }

    public function destroy($id)
    {
        //
    }
}
