<?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);
    }









    /**
     * 添加课程
     */
    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' => '']);
        }
    }


    /**
     * 编辑课程信息
     * @param Request $request
     * @return mixed
     */
    public function editCourse(Request $request)
    {
        $course_id = Request::param('course_id');
        $course_cate_id = Request::param('course_cate_id');
        $gr_id = Request::param('gr_id');
        $sort = Request::param('sort');
        $course_topic = Request::param('course_topic');
        $course_price = Request::param('course_price');
        $course_price2 = Request::param('course_price2');
        $course_price3 = Request::param('course_price3');
        $deduct = Request::param('deduct');
        $course_only_for_employee = Request::param('course_only_for_employee');
        $make_code = Request::param('make_code');
        $parent_course = Request::param('parent_course');
        $ad_pid = Request::param('ad_pid');
        $course_cover = Request::param('course_cover');
        $course_speaker_intro = Request::param('course_speaker_intro');
        $course_play_count = Request::param('course_play_count');
        $course_intro = Request::param('course_intro');
        $course_audio = Request::param('course_audio');
        $course_sp = Request::param('course_sp');


        $data = [
            'course_id' => $course_id,
            'course_cate_id' => $course_cate_id,
            'gr_id' => $gr_id,
            'sort' => $sort,
            'course_topic' => $course_topic,
            'course_price' => $course_price,
            'course_price2' => $course_price2,
            'course_price3' => $course_price3,
            'deduct' => $deduct,
            'course_only_for_employee' => $course_only_for_employee,
            'make_code' => $make_code,
            'parent_course' => $parent_course,
            'ad_pid' => $ad_pid,
            'course_cover' => $course_cover,
            'course_speaker_intro' => $course_speaker_intro,
            'course_play_count' => $course_play_count,
            'course_intro' => $course_intro,
            'course_audio' => $course_audio,
            'course_sp' => $course_sp,

        ];
        var_dump($course_speaker_intro);

        $groupModel = new CourseModel();
        $result = $groupModel->updateCourse($course_id, $data);

        if ($result !== false) {
            return json(['code' => 200, 'msg' => '保存成功']);
        }
        return json(['code' => 1, 'msg' => '保存失败']);
    }


    /**
     * 修改课程状态
     */
    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' => '']);
        }
    }



    /**
     * 修改审核状态
     * @return mixed
     */
    public function updateCourseAudit()
    {
        $course_id = Request::param('course_id');
        $course = CourseModel::where('course_id', $course_id)->find();

        if (!$course) {
            return app('json')->fail('课程不存在');
        }

        $course->course_audit = !$course->course_audit;

        if ($course->save()) {
            return app('json')->success();
        } else {
            return app('json')->fail();
        }
    }


    /**
     * 修改课程信息
     */
    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' => '']);
        }
    }


}