<?php

namespace app\system\controller;

use app\model\api\CourseModel;
use think\facade\Request;

class CourseController
{


//    /**
//     * 获取所有课程列表
//     */
//    public function getCourseList()
//    {
//        $educationModel = new CourseModel();
//        $keyword = input('get.course_topic', ''); // 获取模糊查询关键字
//        $courseList = $educationModel->getCourse($keyword);
//
//        $data = [
//                  'data' => $courseList
//                  ];
//        return app('json')->success($data);
//    }

//    /**
//     * 获取所有课程列表
//     */
//    public function getCourseList()
//    {
//        $educationModel = new CourseModel();
//        $keyword = input('get.course_topic', ''); // 获取模糊查询关键字
//        $page = input('get.page', 1); // 获取页码，默认为1
//        $pageSize = input('get.pageSize', 10); // 获取每页数据量，默认为10
//
//        // 获取课程列表数据和总数
//        $courseList = $educationModel->getCourse($keyword, $page, $pageSize);
//        $total = $educationModel->getCourse($keyword);
//
//        // 封装响应数据
//        $data = [
//            'data' => $courseList,
//            'page' => $page,
//            'pageSize' => $pageSize,
//            'total' => $total,
//        ];
//
//        // 返回分页数据
//        return app('json')->success($data);
//    }


    /**
     * 获取课程列表
     *
     * @return mixed
     */
    public function getCourseList()
    {
        $course_topic = Request::param('course_topic');
        $page = Request::param('page', 1, 'intval');
        $pageSize = Request::param('pageSize', 10, 'intval');

        $courseModel = new CourseModel();
        $query = $courseModel->order('course_id desc');

        if (!empty($course_topic)) {
            $query->whereLike('course_topic', '%' . $course_topic . '%');
        }

        $list = $query->paginate([
            'page' => $page,
            'list_rows' => $pageSize,
        ]);

        $data = [
            'list' => $list->items(),
            'total' => $list->total(),
            'page' => $list->currentPage(),
            'pagesize' => $list->listRows(),
        ];

        return app('json')->success($data);
    }




    /**
     * 获取课程详情并编辑课程
     * @param Request $request
     * @return mixed
     */
    public function editCourse(Request $request)
    {
        $course_id = $request->param('course_id');
        $course = $request->param('course/a');

        $courseModel = new CourseModel();
        $result = $courseModel->where('course_id', $course_id)->update($course);

        if ($result !== false) {
            $course = $courseModel->find($course_id);
            return app('json')->success($course->toArray());
        }
        return app('json')->fail("编辑课程失败");
    }




    /**
     * 添加课程
     */
    public function addCourse()
    {
        $data = Request::param();

        $courseModel = new CourseModel();
        $result = $courseModel->addCourse($data);

        if ($result) {
            return json(['code' => 200, 'msg' => '添加成功', 'data' => $result]);
        } else {
            return json(['code' => 500, 'msg' => '添加失败', 'data' => '']);
        }
    }

    /**
     * 删除课程
     */
    public function deleteCourse()
    {
        $course_id = Request::param('course_id');

        $courseModel = new CourseModel();
        $result = $courseModel->deleteCourse($course_id);

        if ($result) {
            return json(['code' => 200, 'msg' => '删除成功', 'data' => $result]);
        } else {
            return json(['code' => 500, 'msg' => '删除失败', 'data' => '']);
        }
    }

    /**
     * 查找课程
     */
    public function searchCourse()
    {
        $courseId = input('course_id');
        $courseModel = new CourseModel();
        $courseDetail = $courseModel->getCourseDetail($courseId);
        if ($courseDetail) {
            return json(['code' => 200, 'msg' => '查找成功', 'data' => $courseDetail]);
        } else {
            return json(['code' => 500, 'msg' => '查找失败', 'data' => '']);
        }
    }


    /**
     * 修改课程状态
     */
    public function updateCourseStatus()
    {
        $course_id = Request::param('course_id');
        $course_status = Request::param('course_status');

        $courseModel = new CourseModel();
        $result = $courseModel->updateCourseStatus($course_id, $course_status);

        if ($result) {
            return json(['code' => 200, 'msg' => '修改成功', 'data' => $result]);
        } else {
            return json(['code' => 500, 'msg' => '修改失败', 'data' => '']);
        }
    }


    /**
     * 课程是否轮播
     *
     */
    public function Carousel()
    {


    }

    /**
     * 修改课程信息
     */
    public function updateCourseInfo()
    {
        $course_id = Request::param('course_id');
        $data = Request::param();

        $courseModel = new CourseModel();
        $result = $courseModel->updateCourseInfo($course_id, $data);

        if ($result) {
            return json(['code' => 200, 'msg' => '修改成功', 'data' => $result]);
        } else {
            return json(['code' => 500, 'msg' => '修改失败', 'data' => '']);
        }
    }


}