<?php

namespace app\system\controller;

use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use app\model\api\EducationModel;
use think\facade\Db;
use think\Request;


class EducationController
{

    /**
     * 获取所有分类列表
     */
    public function getCateList()
    {
        $educationModel = new EducationModel();
        $cateList = $educationModel->getCate();
        return app('json')->success($cateList);
    }



    /**
     * 获取所有文章列表
     * @return \think\response\Json
     */
    public function gteArticleList()
    {
        $excludeCourseId = $this->request->param('exclude_course_id');
        $educationModel = new EducationModel();

        // 获取所有文章列表
        $courseList = $educationModel->getCourseList($excludeCourseId);

        return json([
            'courseList' => $courseList,
        ]);
    }


    /**
     * 获取顶级分类
     *
     * @return array
     */
    public function getTopCate()
    {
        $educationModel = new EducationModel;

        return $educationModel->getTopCate();
    }


    /**
     * 获取指定分类的子分类 ID 列表
     *
     * @param array $cates 分类数组
     * @return array
     */
    public function getSubcatesByTopCate($cates)
    {
        $educationModel = new EducationModel;

        return $educationModel->getSubcatesByTopCate($cates);
    }



    /**
     * 增加一个子分类
     *
     * @param Request $request 请求对象
     * @return \think\response\Json
     */
    public function subcateadd(Request $request)
    {
        $pid = $request->param('pid/d', 0);
        if ($request->isPost() && $pid) {
            $name = $request->param('name/s', '');
            $sort = $request->param('sort/d', 0);

            if (!$name) {
                return json(['code' => -1, 'msg' => '分类名称不能为空']);
            }

            $data = [
                'pid' => $pid,
                'name' => $name,
                'sort' => $sort,
            ];

            $educationModel = new EducationModel();
            $result = $educationModel->addCate($data);

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

        return json(['code' => -1, 'msg' => '参数错误']);
    }


    /**
     * 修改分类
     *
     * @param Request $request 请求对象
     * @return \think\response\Json
     */
    public function editCate(Request $request)
    {
        $id = $request->param('id');
        $sort = $request->param('sort');
        $name = $request->param('name');

        $educationModel = new EducationModel;
        $result = $educationModel->editCate($id, $sort, $name);
        if ($result) {
            return json(['code' => 0, 'msg' => '修改成功']);
        } else {
            return json(['code' => -1, 'msg' => '修改失败']);
        }
    }


    /**
     * 增加一个分类
     *
     * @param Request $request 请求对象
     * @return \think\response\Json
     */
    public function cateadd(Request $request)
    {
        $pid = $request->param('id/d', 0);
        if ($request->isPost() && $pid) {
            $name = $request->param('name/s', '');
            $sort = $request->param('sort/d', 0);

            if (!$name) {
                return json(['code' => -1, 'msg' => '分类名称不能为空']);
            }

            $data = [
                'pid' => $pid,
                'name' => $name,
                'sort' => $sort,
            ];

            $educationModel = (new EducationModel())->cate;
        if ($educationModel->addCate($data)) {
            return json(['code' => 0, 'msg' => '保存成功']);
        } else {
            return json(['code' => -1, 'msg' => '保存失败']);
        }
    }

        return json(['code' => 0, 'data' => []]);
    }



    /**
     * 删除一个分类
     *
     * @param integer $id 要删除的分类的 ID
     * @return \think\response\Json
     */
    public function delCate($id)
    {
        // 从数据库中删除指定 ID 的分类
        $educationModel = new EducationModel;
        $result = $educationModel->where('id', $id)->delete();

        // 根据删除结果返回响应
        if ($result) {
            return json(['code' => 0, 'msg' => '删除成功']);
        } else {
            return json(['code' => -1, 'msg' => '删除失败']);
        }
    }




}