<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Team;
use App\Http\Controllers\AdminController;
use App\Aws\FileSystem;
use App\Media;
class TeamController extends AdminController
{
    public function __construct() {
        $this->middleware('auth');

        $this->breadcrumbs = $this->getBreadcrumbs();
        $this->breadcrumbs[1]['link'] = "admin/teams";
        $this->breadcrumbs[1]['name'] = "Team";
    }
    
    public function index() {

        $model = Team::get();

        $data["pageHeader"] = false;
        $data["model"] = $model;
        $data["title"] = "Team";
        $data["SubTitle"] = "List";
        $data['breadcrumbs'] = $this->breadcrumbs;
        return view('admin/teams/index', $data);
    }
    
    public function create(Request $request) {

        $data["pageHeader"] = false;
        $data["model"] = new Team();
        $data["model"]["status"] = 1;
        $data["title"] = "Team";
        $data["subTitle"] = "Create";
        $this->breadcrumbs[2]['link'] = "#_";
        $this->breadcrumbs[2]['name'] = "Create";

        $data['breadcrumbs'] = $this->breadcrumbs;
        return view('admin/teams/create', $data);
    }
    
     public function save(Request $request) {

        $input = $request->all();
        $validator = Validator::make($request->all(), [
           'first_name' => 'required|max:255',
           'last_name' => 'required|max:255',
           'email' => 'required|max:255',
           'phone' => 'required|max:255',
        ]);

        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator->errors())->withInput();
        } else {

            unset($input["_token"]);
//            if (empty($input["key"])) {
//                $input["key"] = slugify($input["first_name"]);
//            } else {
//                $input["key"] = slugify($input["key"]).rand(11111,11111);
//            }
            $model = new Team();
            $input["created_at"] = date("Y-m-d H:i:s");
            
             if (!empty($request->image) && $request->image != null) {
                $media_id = Media::getMediaID($request->image);
                $input["image"] = $request->image;
                $input["image_media_id"] = $media_id;
            }
            $id = $model->insertGetId($input);
            
//            if ($request->hasFile('image')) {
//
//                $file = $request->file('image');
//                $fileName = rand(111, 999) . time();
//                $input['image'] = FileSystem::storeTeamImage($fileName, $file, 'uploads/teams/', 'uploads/teams/thumbnails/', 525, 523);
//                $a=Team::where('id', $id)->update(array("image" => $input['image']));
//               
//            }


            \Session::flash('success', 'Team added successfully!');
            return redirect('admin/teams');
        }
    }
    
    public function edit($id) {
        $data["model"] = Team::find($id);
        $data["pageHeader"] = false;
        $data["title"] = "Team";
        $data["subTitle"] = "Edit";
        $data["id"] = $id;
        $this->breadcrumbs[2]['link'] = "#_";
        $this->breadcrumbs[2]['name'] = "Edit";
        $data['breadcrumbs'] = $this->breadcrumbs;
        return view('admin/teams/edit', $data);
    }
    
     public function update($id, Request $request) {

        $input = $request->all();
        $validator = Validator::make($request->all(), [
           'first_name' => 'required|max:255',
           'last_name' => 'required|max:255',
           'email' => 'required|max:255',
           'phone' => 'required|max:255',
        ]);

        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator->errors())->withInput();
        } else {

            unset($input["_token"]);
//           if (empty($input["key"])) {
//                $input["key"] = slugify($input["first_name"]);
//            } else {
//                $input["key"] = slugify($input["key"]).rand();
//            }

            if (!isset($input["status"])) {
                $input["status"] = 0;
            }
//            if ($request->hasFile('image')) {
//            
//                $file = $request->file('image');
//                $fileName = rand(111, 999) . time();
//                $input['image'] = FileSystem::storeTeamImage($fileName, $file, 'uploads/teams/', 'uploads/teams/thumbnails/', 550, 550);
//               
//            }
             if (!empty($request->image) && $request->image != null) {
                $media_id = Media::getMediaID($request->image);
                $input["image"] = $request->image;
                $input["image_media_id"] = $media_id;
            }
            
//              $team= Team::where("id",$id)->first();
//            if ($request->hasFile('image')) {
//               FileSystem::removeImageFromUploads($team->image,"/uploads/teams/","/uploads/teams/thumbnails/");
//
//                $file = $request->file('image');
//                $fileName = rand(111, 999) . time();
//                $input['image'] = FileSystem::storeTeamImage($fileName, $file, 'uploads/teams/', 'uploads/teams/thumbnails/', 525, 523);
//               
//            }

            $input["updated_at"] = date("Y-m-d H:i:s");
            Team::where('id', $id)->update($input);
            \Session::flash('success', 'Team updated successfully!');
            return redirect('admin/teams');
        }
    }
    
     public function delete($id) {
            $team= Team::where("id",$id)->first();
            
            if($team->image !== ""){
               FileSystem::removeImageFromUploads($team->image,"/uploads/teams/","/uploads/teams/thumbnails/");

            }

        Team::where('id', $id)->delete();
        \Session::flash('error', 'Team deleted successfully!');
        return redirect('admin/teams');
    }
    
     public function show($id) {

        $q = new Team();
        $data['model'] = $q->find($id);
        $data["title"] = "Team";

        $this->breadcrumbs[2]['link'] = "#_";
        $this->breadcrumbs[2]['name'] = $data['model']->first_name;

        $data['breadcrumbs'] = $this->breadcrumbs;

        return view('admin/teams/show', $data);
    }
}
