<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models;
use Session;

class StaffMembersController extends Controller
{
    public function index(){
        $staff_members_arr = Models\StaffMember::paginate(10);
        // dd($staff_members_arr);

        $data = [
            'page_title' => 'Staff Members',
            'staff_members' => $staff_members_arr,
            'current_user' => session('current_user')
        ];
        return view('staff_members.index-two', $data);
    }
    public function profile($staff_member_id){
        $staff_member = Models\StaffMember::find($staff_member_id);
        // dd($staff_member);

        $data = [
            'page_title' => 'Staff Profile',
            'staff_member' => $staff_member,
            'current_user' => session('current_user')
        ];
        return view('staff_members.profile', $data);
    }
    public function indexOld(){
        $staff_members_arr = new Models\StaffMember;
        $data = [
            'page_title' => 'Staff Members',
            'current_user' => session('current_user')
        ];
        return view('staff_members.index', $data);
    }
    public function getstaffMemberssJson(Request $request)
    {
        #$this->log_query();
        $staff_members_arr = \DB::table('staff_members')
            ->join('departments', 'departments.id', '=', 'staff_members.department_id')    
            ->join('auth_users AS staffcreate', 'staffcreate.id', '=', 'staff_members.created_by')
            ->join('auth_users AS staffmodify', 'staffmodify.id', '=', 'staff_members.modified_by')
            ->select('staff_members.id', 'staffcreate.name AS createdBy', 'staff_members.name','staff_members.phone', 'staffmodify.name AS modifiedBy', 'departments.name AS deptName', 'staff_members.gender', 'staff_members.profile_pic', 'staff_members.email', 'staff_members.personal_email', 'staff_members.staff_number', 'staff_members.staff_number', 'staff_members.status', 'staff_members.location_country', 'staff_members.hire_date')
            ->orderBy('staff_members.name', 'ASC')
            ->paginate(10);

        if($request->has('keyword')){
            $keyword = $request->keyword;
            $staff_members_arr = \DB::table('staff_members')
            ->join('departments', 'departments.id', '=', 'staff_members.department_id')    
            ->join('auth_users AS staffcreate', 'staffcreate.id', '=', 'staff_members.created_by')
            ->join('auth_users AS staffmodify', 'staffmodify.id', '=', 'staff_members.modified_by')
            ->select('staff_members.id', 'staffcreate.name AS createdBy','staff_members.name', 'staff_members.phone', 'staffmodify.name AS modifiedBy', 'departments.name AS deptName', 'staff_members.gender', 'staff_members.profile_pic', 'staff_members.email', 'staff_members.personal_email', 'staff_members.staff_number', 'staff_members.status', 'staff_members.location_country', 'staff_members.hire_date')
            ->whereRaw("staffcreate.name LIKE '%$keyword%' OR staffmodify.name LIKE '%$keyword%' OR staff_members.name LIKE '%$keyword%' OR departments.name LIKE '%$keyword%' OR staff_members.gender LIKE '%$keyword%' OR staff_members.email LIKE '%$keyword%' OR staff_members.personal_email LIKE '%$keyword%' OR staff_members.staff_number LIKE '%$keyword%' OR 'staff_members.staff_number' LIKE '%$keyword%' OR 'staff_members.location_country' LIKE '%$keyword%' OR 'staff_members.hire_date' LIKE '%$keyword%'")
            ->orderBy('staff_members.name', 'ASC')
            ->paginate(10);
        }     
        return response()->json($staff_members_arr);
    }
    public function create()
    {
        $countries = Models\Country::pluck('en_short_name','en_short_name');
        $departments = Models\Department::pluck('name', 'id');
        $status = ['Inactive' => 'Inactive', 'Active' => 'Active', 'Pending Confirmation' => 'Pending Confirmation'];
        $gender_arr = ['Female' => 'Female', 'Male' => 'Male'];
        $data = [
            'page_title' => 'Add Staff Member',
            'countries' => $countries,
            'status' => $status,
            'gender_arr' => $gender_arr,
            'departments' => $departments,
        ];

        return view('staff_members.create', $data);
    }
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required',
            'gender' => 'required',
            'department_id' => 'required',
            'email' => 'required',
            'personal_email' => 'sometimes',
            'phone' => 'required',
            'location_country' => 'required',
            'hire_date' => 'sometimes|date',
            'profile_pic' => 'sometimes',
            'staff_number' => 'sometimes'
        ]);
        $staff_members_arr = $request->except('_token');
        $staff_members_arr['created_by'] = session('current_user.id');
        $staff_members_arr['modified_by'] = session('current_user.id');
        


        $result = Models\StaffMember::create($staff_members_arr);
        Session::flash('success_message', 'Staff Member successfully added');
        return redirect(url('staffmembers'));
    }
    public function edit($id){
        $staff_member = Models\StaffMember::findOrFail($id);
      
        $countries = Models\Country::pluck('en_short_name','en_short_name');
        $departments = Models\Department::pluck('name', 'id');
        $status = ['Inactive' => 'Inactive', 'Active' => 'Active', 'Pending Confirmation' => 'Pending Confirmation'];
        $gender_arr = ['Female' => 'Female', 'Male' => 'Male'];

        $data = [
            'page_title' => 'Edit Staff Member Details',
            'countries' => $countries,
            'status' => $status,
            'staff_member' => $staff_member,
            'gender_arr' => $gender_arr,
            'departments' => $departments
        ];

        return view('staff_members.edit', $data);
    }
    public function update(Request $request, $id)
    {
        $request->validate([
            'name' => 'required',
            'gender' => 'required',
            'department_id' => 'required',
            'email' => 'required',
            'personal_email' => 'required',
            'phone' => 'required',
            'location_country' => 'required',
            'hire_date' => 'sometimes|date'
        ]);
        //dd($request->all());
        $staff_member = Models\StaffMember::findOrFail($id);
      
        $staff_member->name = $request->name;
        $staff_member->email = $request->email;
        $staff_member->status = $request->status;
        $staff_member->personal_email = $request->personal_email;
        $staff_member->department_id = $request->department_id;
        $staff_member->location_country = $request->location_country;
        $staff_member->phone = $request->phone;
        $staff_member->hire_date = $request->hire_date;
        $staff_member->staff_number = $request->staff_number;
        $staff_member->gender = $request->gender;

        $staff_member->emergency_contact_person = $request->emergency_contact_person;
        $staff_member->emergency_contact_phone = $request->emergency_contact_phone;
        $staff_member->known_health_condition = $request->known_health_condition;
        $staff_member->blood_group = $request->blood_group;

        if ($request->has('profile_pic')) {
            $staff_member->profile_pic = $request->profile_pic;
        }
        $staff_member->save();

        Session::flash('success_message', 'Staff Member Details successfully Updated');
        return redirect(url('staffmembers'));
    }
}
