<?php

namespace App\Http\Controllers\HR;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\Hr\BlackOutModel;
use Yajra\DataTables\DataTables;

class BlackOutController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        if($request->ajax())
        {
            $count_filter   = BlackOutModel::where('status', 1)->count();
            $data           = BlackOutModel::where('status', 1);
            $count_total    = $data->count();
            return DataTables::of($data->take(20))
                            ->addIndexColumn()
                            ->with([
                                'recordsFiltered' => $count_filter,
                                'recordsTotal' => $count_total
                            ])
                            ->editColumn('date', function($data){
                                return $data->date ? date('d-m-Y', strtotime($data->date)) : '';
                            })
                            ->addColumn('action', function($data){
                                $btn_edit  = '';
                                $btn_edit .= '<a data-href="'.route('blackout.edit', encrypt($data->id)).'" class="button_warning edit_blackout"><span class="fa fa-pencil-square-o"></span></a></td>';
                                return $btn_edit;
                            })
                            ->rawColumns(['action'])
                            ->make(true);
        }
        return view('hr.blackout.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('hr.blackout.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'title_blackout'    => 'required',
            'deduct'            => 'required',
            'date'              => 'required',
            'duration'          => 'required'
        ]);

        $blackout           = new BlackOutModel();
        $blackout->title    = $request->title_blackout;
        $blackout->deduct   = $request->deduct;
        $blackout->duration = $request->duration;
        $blackout->date     = $request->date ? date('Y-m-d', strtotime($request->date)) : null;
        $blackout->note     = $request->note;
        $blackout->save();

        session()->put('success', 'Added Successfully!');
        return response()->json('success');

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $id = decrypt($id);
        $blackout = BlackOutModel::find($id);
        return response()->json($blackout);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request)
    {
        $this->validate($request, [
            'title_blackout'    => 'required',
            'deduct'            => 'required',
            'date'              => 'required',
            'duration'          => 'required'
        ]);

        $blackout           = BlackOutModel::find($request->id);
        $blackout->title    = $request->title_blackout;
        $blackout->deduct   = $request->deduct;
        $blackout->duration = $request->duration;
        $blackout->date     = $request->date ? date('Y-m-d', strtotime($request->date)) : null;
        $blackout->note     = $request->note;
        $blackout->update();

        session()->put('success', 'Updated Successfully!');
        return response()->json('success');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
