<?php

namespace App\Http\Controllers\Teacher\Student;

use App\Http\Controllers\Admin\Controller;
use App\Models\Course;
use App\Models\Student;
use App\Models\StudentCourse;
use App\Models\StudentCourseTeacher;
use App\Models\Teacher;
use Carbon\Carbon;
use Illuminate\Http\Request;

class CourseController extends Controller
{
    protected $redirect_index = '/teacher/Student/Course/index';

    protected $view_path = 'teacher.students.courses.';

    protected $pre_uri = '/teacher/Student/Course/';

    protected $model_name = '课程';

    protected $model;

    public function __construct()
    {
        parent::__construct();
        $this->model = new StudentCourse();
    }

    public function index(Request $request)
    {
        if(empty($request->input('student_id')) || empty($student = Student::find($request->input('student_id')))) {
            return $this->showWarning('找不到学员');
        }
        $list = $this->model->where('student_id', $student->id)->orderBy('created_at', 'desc');

        if(!empty($request->input('keyword')) && !empty(trim($request->input('keyword')))) {
            $keyword = '%' . trim($request->input('keyword')) . '%';
            $list = $list->where('name', 'like', $keyword);
        }

        $list = $list->paginate();

        foreach($list as $item) {
            $item->end_date = Carbon::createFromTimestamp(strtotime($item->apply_date))->addDays($item->duration)->toDateString();
            $now = Carbon::now()->toDateString();
            if($now > $item->end_date) {
                $item->remain_days = 0;
            } else {
                $item->remain_days = Carbon::now()->diffInDays($item->end_date) + 1;
            }
        }

        list($pre_uri, $model_name) = array($this->pre_uri, $this->model_name);
        return view($this->view_path . 'index', compact('list', 'pre_uri', 'model_name', 'student'));
    }

    public function create(Request $request)
    {
        if(empty($request->input('student_id')) || empty($student = Student::find($request->input('student_id')))) {
            return $this->showWarning('找不到学员');
        }
        $courses = Course::orderBy('created_at', 'desc')->get();
        $teachers = Teacher::orderBy('created_at', 'desc')->get();
        list($pre_uri, $model_name, $model) = array($this->pre_uri, $this->model_name, $this->model);
        return view($this->view_path . 'create', compact('pre_uri', 'model_name', 'model', 'courses', 'teachers', 'courses', 'student'));
    }

    public function store(Request $request)
    {
        if(!$request->isMethod('POST')) {
            return $this->showWarning('访问错误');
        }

        if(empty($request->input('data')) || !is_array($request->input('data'))) {
            return $this->showWarning('数据错误');
        }

        $data = $request->input('data');
        $data['assign_teacher'] = $request->input('assign_teacher') == 1 ? 1 : 2;
        $res = $this->model->create($data);
        if(!$res) {
            return $this->showWarning('数据库保存失败！');
        }

        $res->updateStudentCourseTeachers($request->input('teachers'), $data['assign_teacher']);

        return $this->showMessage('操作成功', $this->redirect_index . '?student_id=' . $res->student_id);
    }

    public function edit(Request $request)
    {
        if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) {
            return $this->showWarning('数据错误！');
        }

        $courses = Course::orderBy('created_at', 'desc')->get();
        $teachers = Teacher::orderBy('created_at', 'desc')->get();
        list($pre_uri, $model_name, $model) = array($this->pre_uri, $this->model_name, $this->model);
        return view($this->view_path . 'edit', compact('item','pre_uri', 'model_name', 'model', 'courses', 'teachers'));
    }

    public function update(Request $request)
    {
        if(!$request->isMethod('POST')) {
            return $this->showWarning('访问错误');
        }

        if(empty($request->input('id')) || empty($request->input('data')) || !is_array($request->input('data'))) {
            return $this->showWarning('数据错误');
        }
        if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) {
            return $this->showWarning('找不到合同');
        }

        $data = $request->input('data');
        $data['assign_teacher'] = $request->input('assign_teacher') == 1 ? 1 : 2;
        $res = $this->model->where('id', $item->id)->update($data);
        if(!$res) {
            return $this->showWarning('数据库保存失败！');
        }
        if(!empty($item)) {
            $item->updateStudentCourseTeachers($request->input('teachers'), $data['assign_teacher']);
        }

        return $this->showMessage('操作成功', $this->redirect_index . '?student_id=' . $item->student_id);
    }

    public function delete(Request $request)
    {
        if(!$request->isMethod('POST')) {
            return $this->showWarning('访问错误');
        }
        if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) {
            return $this->showWarning('访问错误');
        }
        StudentCourseTeacher::where('student_course_id', $item->id)->delete();
        $res = $item->delete();
        if(!$res) {
            return $this->showWarning('数据库删除失败');
        }
        return $this->showMessage('操作成功');
    }
}
