<?php

namespace App\Http\Controllers;

use App\Models\Teams;
use Illuminate\Http\Request;
use App\Http\Controllers\CommonController;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;

class TeamsController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
        $this->common_class_obj = new CommonController();
    }

    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        //
        $teams = Teams::where('status', '=', 1)->orderBy('id', 'DESC')->get();
        return view('team.index', compact('teams'));
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
        return view('team.create');
    }

    public function store(Request $request)
    {
        //
        $validatedata = $request->validate([
            'name' => 'required|max:300|min:2',
        ]);

        if ($request->name == null || empty($request->name)) {
            return redirect()->back()->with('error', 'Name  should not be empty');
        }

        if ($request->designation == null || empty($request->designation)) {
            return redirect()->back()->with('error', 'Designation should not be empty');
        }

        

        $team = new Teams();

        $team->name = $request->name;
        $team->designation = $request->designation;

        $team->linkedin_url = $request->linkedin_url;
        $team->facebook_url = $request->facebook_url;

        $team->created_by = Auth::user()->id;
        $team->updated_by = Auth::user()->id;

        if ($request->image == null) {
            return redirect()->route('team.index')->with('error', 'Image should be empty');
        }

        if ($request->hasFile('image')) {
            // if image is not empty.

            $request->validate([
                'image' => 'required|image|mimes:jpeg,png,jpg',
            ]);

            $slug = Str::slug($request->image);

            $currentDate = Carbon::now()->toDateString();
            $imageName = $slug . '-' . $currentDate . '-' . uniqid() . '.' . $request->image->getClientOriginalExtension();

            if (!file_exists('images/team')) {
                mkdir('images/team', 0777, true);
            }
            $request->image->move('images/team', $imageName);

            $team->image = $imageName;
        }

        $team->save();

        if ($team) {
            return redirect()->route('team.index')->with('success', 'Team Has Been Added Successfully');
        }
    }

    /**
     * Display the specified resource.
     */
    public function show($id)
    {
        //
        $data = Teams::findorFail($id);
        if ($data->status == 0) {
            return redirect()->route('team.index')->with('error', 'No Data Found');
        }

        return view('team.details', compact('data', 'id'));
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit($id)
    {
        //
        $data = Teams::findorFail($id);
        return view('team.edit', compact('data', 'id'));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, $id)
    {
        //
        $validatedata = $request->validate([
            'name' => 'required|max:300|min:2',
        ]);

        $team = Teams::findorFail($id);

        if ($request->name == null || empty($request->name)) {
            return redirect()->back()->with('error', 'Name should not be empty');
        }

        if ($request->designation == null || empty($request->designation)) {
            return redirect()->back()->with('error', 'Designation  should not be empty');
        }

        $existing_image = $team->image;

        if ($request->hasFile('image')) {
            // if image is not empty.

            $request->validate([
                'image' => 'required|image|mimes:jpeg,png,jpg',
            ]);

            $slug = Str::slug($request->image);

            $currentDate = Carbon::now()->toDateString();
            $imageName = $slug . '-' . $currentDate . '-' . uniqid() . '.' . $request->image->getClientOriginalExtension();

            if (!file_exists('images/team')) {
                mkdir('images/team', 0777, true);
            }
            $request->image->move('images/team', $imageName);

            /// Delete Existing Image
            if ($existing_image != null) {
                if (file_exists('images/team/' . $existing_image)) {
                    unlink('images/team/' . $existing_image);
                }
            }

            $team->image = $imageName;
        }

        // saving data

        $team->name = $request->name;
        $team->designation = $request->designation;

        $team->linkedin_url = $request->linkedin_url;
        $team->facebook_url = $request->facebook_url;

        $team->updated_by = Auth::user()->id;

        $team->save();

        if ($team) {
            return redirect()->route('team.index')->with('success', 'Team Has Been updated Successfully');
        }
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy($id)
    {
        //
        $team = Teams::findorFail($id);

        $team->status = 0;
        $team->updated_by = Auth::user()->id;

        $existing_image = $team->image;

        /// Delete Existing Image
        if ($existing_image != null) {
            if (file_exists('images/team/' . $existing_image)) {
                unlink('images/team/' . $existing_image);
            }
        }

        $team->save();

        return redirect()->route('team.index')->with('success', 'Team Has Been deleted Successfully');
    }
}
