<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/8/14
 * Time: 10:43
 */

namespace App\Http\Controllers\Web\Train;


use App\Exceptions\ResponseException;
use App\Models\Category;
use App\Repositories\Train\TrainTeacherRepository;
use App\Services\Train\TeacherService;
use App\Validators\Train\TeacherSaveValidatorRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;


class TeacherController
{
    protected $teacherRepository;
    protected $teacherService;
    public function __construct(TeacherService $teacherService,TrainTeacherRepository $teacherRepository)
    {
        $this->teacherRepository=$teacherRepository;
        $this->teacherService=$teacherService;
    }

    public function teacher()
    {
        $user=auth('web-company')->user();

        $teachers=$this->teacherRepository->teachers(['created_by'=>$user->id]);

        return view('app.train.teacher.index',['teachers'=>$teachers]);
    }

    public function listAction(){
        $teachers=$this->teacherRepository->teachers(['audit'=>1]);
        return AjaxSuccess($teachers);
    }

    public function delete($id){
        $user=auth('web-company')->user();
        $ids=explode(',',$id);

        $isHave=$this->teacherRepository->teachersCourse($ids);

        if (!empty($isHave)){
            return AjaxError("此讲师有对应的课程,请先删除对应的课程");
        }

        $isOk=$this->teacherRepository->teacherDel($ids,$user->id);

        if ($isOk){
            return AjaxSuccess("删除成功!");
        }else{
            return AjaxError("删除失败!");
        }


    }

    public function save(TeacherSaveValidatorRequest $request){

        $user=auth('web-company')->user();

        $this->teacherService->addTeacher($request->all(),$user);

    }

    public function add()
    {

        $education=Category::categoryType("AIX_education");

        $teacher=null;
        if (!empty(\request('id'))){
            $teacher=$this->teacherRepository->find(request('id'));
        }

        return view('app.train.teacher.add',['education'=>$education,'teacher'=>$teacher]);

    }


}