<?php

namespace App\Http\Controllers\admin;

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

class teamController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

    public function __construct()
    {
        $this->middleware('auth:admin');
    }

    public function index()
    {
        $teams = Team::orderBy('id', 'DESC')->paginate(25);
        return view('admin.team.index')->with(compact('teams'));
    }

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

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

        Team::create([
            'fullname' => request()->get('fullname'),
            'email' => request()->get('email'),
            'profile' => request()->get('designation'),
            'webmaster_img' => request()->get('webmaster_img'),
            'status' => 'ACTIVE'
        ]);

        return redirect()->to('/admin/team');
    }

    /**
     * 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)
    {
        $team = Team::find($id);
        return view('admin.team.edit')->with(compact('team'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $team = Team::find($id);

        $team->update([
            'fullname' => request()->get('fullname'),
            'email' => request()->get('email'),
            'profile' => request()->get('designation'),
            'webmaster_img' => request()->get('webmaster_img')
        ]);

        return redirect()->to('admin/team');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $team = Team::find($id);
        $team->delete();

        return redirect()->to('admin/team');
    }
    public function status(Request $request, $id)
    {
        if ($request->ajax()) {
            $team = Team::find($id);
            $newStatus = ($team->status == 'DEACTIVE') ? 'ACTIVE' : 'DEACTIVE';
            $team->update([
                'status' => $newStatus
            ]);
            return $newStatus;
        }
    }

    public function statusActive(Request $request)
    {
        if ($request->ajax()) {
            foreach ($request->statusAll as $key => $value) {
                Team::where('id', $value)->update(['status' => 'ACTIVE']);
            }
            $record = Team::find($request->statusAll);
            return $record;
        }
        return false;
    }

    public function statusDeactive(Request $request)
    {
        if ($request->ajax()) {
            foreach ($request->statusAll as $key => $value) {
                Team::where('id', $value)->update(['status' => 'DEACTIVE']);
            }
            $record = Team::find($request->statusAll);
            return $record;
        }
        return false;
    }

    public function deleteAll(Request $request)
    {
        if ($request->ajax()) {
            foreach ($request->statusAll as $key => $value) {
                Team::where('id', $value)->delete();
            }
            $record = Team::find($request->statusAll);
            return $record;
        }
        return false;
    }
}
