<?php

namespace App\Http\Controllers;

use App\Owner;
use App\Tutorial;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Validator;
use Te7aHoudini\LaravelTrix\Models\TrixAttachment;
use Te7aHoudini\LaravelTrix\Models\TrixRichText;

class TutorialController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $tutorials=Tutorial::select('id','owner_id','name','type','created_at')->paginate(50);
        return view('maintenance.tutorial.index',['tutorials'=>$tutorials]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        $response=[];
        if ($request->input('owner_id'))$response['owner_id']=$request->input('owner_id');
        $owners=Owner::get();
        $response['owners']=$owners;
        return view('maintenance.tutorial.create',$response);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        if(!Gate::allows('教程-录入')){ return redirect(url('/'));  }
        $this->validator($request)->validate();
        $tutorial=Tutorial::create(['owner_id'=>$request->input('owner_id'),'name'=>$request->input('name')]);
        $tutorial['tutorial-trixFields']=request('tutorial-trixFields');
        $tutorial['attachment-tutorial-trixFields']=request('attachment-tutorial-trixFields');
        $tutorial->save();
        return redirect('maintenance/tutorial')->with('successTip','新教程“'.$request->input('name').'”添加成功');
    }

    //展示教程内容
    public function showContent($id){
        if(!Gate::allows('教程-查询')){ return redirect(url('/'));  }
        $tutorial=Tutorial::with('trixRichText')->find($id);
        if ($tutorial&&$tutorial->trixRichText)return ['success'=>true,'data'=>$tutorial->trixRichText->content];
        return ['success'=>false];
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $tutorial=Tutorial::with('trixRichText')->find($id);
        return view('maintenance.tutorial.show',compact('tutorial'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        if(!Gate::allows('教程-编辑')){ return redirect(url('/'));  }
        $tutorial=Tutorial::with('trixRichText')->find($id);
        $owners=Owner::get();
        return view('maintenance.tutorial.edit',['owners'=>$owners,'tutorial'=>$tutorial]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        if(!Gate::allows('教程-编辑')){ return redirect(url('/'));  }
        $this->validator($request)->validate();
        $tutorial=Tutorial::find($id);
        $tutorial->owner_id=$request->input('owner_id');
        $tutorial->name=$request->input('name');
        $tutorial['tutorial-trixFields']=request('tutorial-trixFields');
        $tutorial['attachment-tutorial-trixFields']=request('attachment-tutorial-trixFields');
        $tutorial->update();
        return redirect('maintenance/tutorial')->with('successTip','教程“'.$request->input('name').'”修改成功');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return array
     */
    public function destroy($id)
    {
        if(!Gate::allows('教程-删除')){ return redirect(url('/'));  }
        $tutorial=Tutorial::find($id);
        $tutorial->delete();
        TrixRichText::where('model_id',$id)->delete();
        $attachments = TrixAttachment::where('attachable_id', $id)->get();
        foreach ($attachments as $attachment){
            optional($attachment)->purge();
        }
        return ['success'=>true];
    }

    public function validator(Request $request){
        $validator=Validator::make($request->input(),[
            'name'=>['required','max:50'],
            'owner_id'=>['required','integer'],
        ],[
            'required'=>':attribute 为必填项',
            'max'=>':attribute 输入过长',
        ],[
            'name'=>'标题',
            'owner_id'=>'货主',
        ]);
        return $validator;
    }
}
