<?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\NoticeBoard;
use App\Models\NoticeSend;
use App\User;
use DB;

class NoticeBoardController extends Controller
{
    
    public function index()
    {
        $user_id = \Auth::user()->id;
        $data['title'] = "Notice Board";/*
        $data['notices'] = NoticeBoard::with('user')->orderBy('id','DESC')->get();*/
        $data['notices'] = DB::table('notice_boards')
            ->join('users', 'notice_boards.employee_id', '=', 'users.id')
            ->join('notice_sends', 'notice_boards.id', '=', 'notice_sends.notice_id')
            ->select('notice_boards.*', 'users.name')
            ->where('notice_sends.sendto_id',$user_id)
            ->orWhere('notice_sends.sendto_id', 'all')
            ->orderBy('id','DESC')
            ->get();
        return view('admin.notice_board/index',$data);
    }
    public function create()
    {
        $user_id = \Auth::user()->id;
        $data['title'] = "Notice Add";
        $data['employee'] = User::where('employee_id','!=', "")->orWhere('utype',1)->orderBy('id','DESC')->get();
        return view('admin.notice_board/add',$data);
    }

    public function store(Request $request)
    {
        $user_id = \Auth::user()->id;
        $notice = new NoticeBoard;
        $notice->employee_id = $user_id;         
        $notice->notice_title = $request->notice_title;      
        $notice->expire_date = $request->expire_date;      
        $notice->description = $request->description;      
        $notice->added_by = $user_id;      
        $notice->status = 1;
        $notice->save();
        $tags = $request->input('send_to');
        foreach($tags as $key => $file) {
                $document = new NoticeSend;
                $document->notice_id = $notice->id;
                $document->sendto_id = $request['send_to'][$key];
                $document->save();
            }
         return redirect('admin/notice_board')->with('msg','Notice Send Successfu.');
        }
    public function show($id)
    {
        $user_id = \Auth::user()->id;
        $data['title'] = "Notice Details";
        $data['notice'] = DB::table('notice_boards')
                        ->join('users', 'notice_boards.employee_id', '=', 'users.id')
                        ->select('notice_boards.*', 'users.name')
                        ->where('notice_boards.id',$id)->first();
        return view('admin.notice_board/details',$data);
    }
    public function myNotice(){
        $user_id = \Auth::user()->id;
        $data['title'] = "My add Notice";
        $data['notices'] = NoticeBoard::where('employee_id',$user_id)->orderBy('id','DESC')->get();
        return view('admin.notice_board/my_notice',$data);
    }

    public function edit($id)
    {
         $user_id = \Auth::user()->id;
        $data['title'] = "Notice Edit";
        $data['employee'] = User::where('employee_id','!=', "")->orWhere('utype',1)->orderBy('id','DESC')->get();
        $data['sendto'] = NoticeSend::where('notice_id',$id)->pluck('sendto_id')->toArray();
        $data['notice'] = NoticeBoard::find($id);
        return view('admin.notice_board/edit',$data);
    }

    
    public function update(Request $request, $id)
    {
        //
    }

   
    public function destroy($id)
    {
        NoticeBoard::whereId($id)->delete();
        NoticeSend::where('notice_id',$id)->delete();
        return redirect('admin/notice_board/my_notice')->with('msg','Successfully Deleted!');
    }
}
