<?php
namespace app\system\controller;

use app\BaseController;
use app\model\api\GroupModel;
use think\facade\Request;
use think\model;


class GroupController extends BaseController
{

    /**
     * 分组列表
     *
     * @return mixed
     */
    public function index()
    {
        $title = Request::param('title');
        $page = Request::param('page', 1, 'intval');
        $pagesize = Request::param('pagesize', 10, 'intval');

        $groupModel = new GroupModel();
        $query = $groupModel->order('gr_id ASC');

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

        $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 add(Request $request)
    {
        $uname = Request::param('uname');
        $type1 = Request::param('type1');
        $type2 = Request::param('type2');
        $title = Request::param('title');
        $ins = Request::param('ins');
        $indeximg = Request::param('indeximg');

        $data = [
            'uname' => $uname,
            'type1' => $type1,
            'type2' => $type2,
            'title' => $title,
            'ins' => $ins,
            'indeximg' => $indeximg,
        ];

        $groupModel = new GroupModel();
        $result = $groupModel->addGroups($data);

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



    /**
     * 编辑分组信息
     * @param Request $request
     * @return mixed
     */
    public function edit(Request $request)
    {
        $grId = Request::param('gr_id');
        $uname = Request::param('uname');
        $type1 = Request::param('type1');
        $type2 = Request::param('type2');
        $title = Request::param('title');
        $ins = Request::param('ins');
        $indeximg = Request::param('indeximg');


        $data = [
            'gr_id' => $grId,
            'uname' => $uname,
            'type1' => $type1,
            'type2' => $type2,
            'title' => $title,
            'ins' => $ins,
            'indeximg' => $indeximg,

        ];

        $groupModel = new GroupModel();
        $result = $groupModel->updateGroups($grId, $data);

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



    /**
     * 修改审核状态
     * @return mixed
     */
    public function changeStatus()
    {
        $gr_id = Request::param('gr_id');
        $audit = Request::param('audit');

        $courseModel = new GroupModel();
        $result = $courseModel->updateAuditStatus($gr_id, ['audit' => $audit]);

        if ($result !== false) {
            return app('json')->success();
        } else {
            return app('json')->fail();
        }
    }


    /**
     * 修改是否轮播状态Recommend
     *
     * @return mixed|\think\response\Json
     */
    public function changeRecommend()
    {
        $gr_id = Request::param('gr_id');
        $recommend = Request::param('recommend');

        $courseModel = new GroupModel();
        $result = $courseModel->updateAuditStatus($gr_id, ['recommend' => $recommend]);

        if ($result !== false) {
            return app('json')->success();
        } else {
            return app('json')->fail();
        }
    }



    /**
     * 删除分组信息
     * @param Request $request
     * @return mixed
     */
    public function delete(Request $request)
    {
        $grId = Request::param('gr_id');

        $groupModel = new GroupModel();
        $result = $groupModel->deleteGroups($grId);

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







}