<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Team;
use Auth;
class TeamMemberController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = Team::all();
        return view('back-end.teams.index', compact('data'));
    }

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

    /**
     * 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|max:255",
            "email" => "required",
            "address" => "required",
        ]);
        $data = new Team();
        $data->user_id = Auth::guard('admin')->user()->id;
        $data->name = $request->name;
        $data->position = $request->position;
        $data->email = $request->email;
        $data->address = $request->address;
        $data->details = $request->details;
        if (request()->hasFile('image')) {
            $date = date('Y-M');
            $filename = 'data_images-' . time() . '-' . request()->file('image')->getClientOriginalName();
            $path = request()->file('image')->storeAs('data_images/' . $date, $filename, 'public');
        } else {
            $path = "image/noimage.jpg";
        }
        $data->image = $path;
        $is_saved = $data->save();
        if ($is_saved) {
            session()->flash("msg", "Success!");
            return redirect()->route('team.index');
        }
    }

    /**
     * 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)
    {
        $data = Team::find($id);
        return view('back-end.teams.edit', compact('data'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $request->validate([
            "name" => "required|max:255",
            "email" => "required",
            "address" => "required",
        ]);
        $data = Team::find($id);
        $data->user_id = Auth::guard('admin')->user()->id;
        $data->name = $request->name;
        $data->position = $request->position;
        $data->email = $request->email;
        $data->address = $request->address;
        $data->details = $request->details;
        if (request()->hasFile('image')) {
            $date = date('Y-M');
            $filename = 'data_images-' . time() . '-' . request()->file('image')->getClientOriginalName();
            $path = request()->file('image')->storeAs('data_images/' . $date, $filename, 'public');
        } else {
            $path = $data->image;
        }
        $data->image = $path;
        $is_saved = $data->save();
        if ($is_saved) {
            session()->flash("msg", "Success!");
            return redirect()->route('team.index');
        }
    }

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