<?php

namespace App\Http\Controllers\Web;

use App\Model\MediaCategoryModel;
use App\Model\MediaModel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class MediaController extends Controller
{
    public function index(Request $request){

        $medias = MediaModel::mediaList($request);

        return view('admin.media.media_list', [
            'medias'      => $medias,
            'start_time' => substr($request->start_time, 0, 10),
            'end_time'   => substr($request->end_time, 0, 10),
            'name'       => $request->name,
            'STATUS'     => ['启用','停用'],
        ]);

    }

    /**
     * 编辑房间
     * @param Request $request
     * @param $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @throws \App\Exceptions\ControllerException
     */
    public function edit(Request $request,$id){

        $media = handler_drive(function () use ($request,$id){
            return MediaModel::getMediaDetail($id);
        });
        $media_types = MediaCategoryModel::catgList($request);

        return view('admin.media.media_edit', [
            'media'         => $media,
            'media_types'   => $media_types
        ]);

    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\ControllerException
     */
    public function update(Request $request,$id){

        $res = handler_drive(function () use ($request,$id){
            return MediaModel::updateMedia($request,$id);
        });
        return success($res);
    }

}
