<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Author;
use App\Country;
use File;


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

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

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

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {

        $countries = Country::get();
        return view('admin.author.add')->with(compact('countries'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate(request(), [
            'title' => 'required',
            'slug' => 'required',
            'designation' => 'required',
            'country' => 'required|not_in:none',
            'dob' => 'required',
            
        ]);

        $fileName = null;
        if (request()->hasFile('author_img')) 
        {
            $file = request()->file('author_img');
            $fileName = md5($file->getClientOriginalName()) . time() . "." . $file->getClientOriginalExtension();
            $file->move('./uploads', $fileName);
        }


        Author::create([
            'title' => request()->get('title'),
            'slug' => request()->get('slug'),
            'designation' => request()->get('designation'),
            'country_id' => request()->get('country_id'),
            'dob' => request()->get('dob'),
            'description' => request()->get('descp'),
            'author_feature' => request()->get('email'),
            'facebook_id' => request()->get('facebook_id'),
            'twitter_id' => request()->get('twitter_id'),
            'youtube_id' => request()->get('youtube_id'),
            'pinterest_id' => request()->get('pinterest_id'),
            'author_img' =>$fileName,
            'status' => 'ACTIVE'
        ]);

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

    /**
     * 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)
    {
        $countries = Country::get();
        $author = Author::find($id);
        return view('admin.author.edit')->with(compact('author', 'countries'));
    }

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

        $currentImage = $author->author_img;
        $fileName = null;
        if (request()->hasFile('author_img')) {
            $file = request()->file('author_img');
            $fileName = md5($file->getClientOriginalName()) . time() . "." . $file->getClientOriginalExtension();
			$file->move('./uploads', $fileName);
        }

        $author->update([
            'title' => request()->get('fullname'),
            'slug' => request()->get('slug'),
            'designation' => request()->get('designation'),
            'dob' => request()->get('dob'),
            'description' => request()->get('descp'),
            'author_feature' => request()->get('email'),
            'facebook_id' => request()->get('facebook_id'),
            'twitter_id' => request()->get('twitter_id'),
            'youtube_id' => request()->get('youtube_id'),
            'pinterest_id' => request()->get('pinterest_id'),
            'author_img' => ($fileName) ? $fileName : $currentImage,
            'country' => request()->get('country'),
        ]);

        if ($fileName) {
			File::delete('./uploads/' . $currentImage);
		}

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

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

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

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

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

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