<?php

namespace App\Http\Controllers\admin;

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

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

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

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

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.media.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(), [
            'title' => 'required',
            'slug' => 'required',
            'media_img' => 'required'
        ]);

        Media::create([
            'title' => request()->get('title'),
            'slug' => request()->get('slug'),
            'media_type' => request()->get('media_type'),
            'media_img' => request()->get('media_img'),
            'description' => request()->get('descp'),
            'status' => 'ACTIVE'
        ]);

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

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

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

        $media->update([
            'title' => request()->get('author_name'),
            'slug' => request()->get('slug'),
            'media_type' => request()->get('media_type'),
            'media_img' => request()->get('media_img'),
            'description' => request()->get('descp'),
        ]);

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

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

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

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

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

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