<?php

namespace addons\exam\controller;

use addons\exam\enum\CateKind;
use app\admin\model\exam\CateModel;

/**
 * 分类接口
 */
class Cate extends Base
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 按种类查询分类
     */
    public function index()
    {
        if (!$kind = input('kind/s')) {
            fail('缺少分类种类参数');
        }

        $model = new CateModel();

        $data['level1'] = $model->where('status', '1')->where('kind', $kind)->where('level', 1)->order('sort desc')->select();
        $data['level2'] = $model->where('status', '1')->where('kind', $kind)->where('level', 2)->order('sort desc')->select();
        $data['level3'] = $model->where('status', '1')->where('kind', $kind)->where('level', 3)->order('sort desc')->select();

        $this->success('', $data);
    }

    /**
     * 3级分类筛选
     */
    public function filter()
    {
        $kind = input('kind/s');

        if (!$kind || !in_array($kind, [CateKind::PAPER, CateKind::QUESTION, CateKind::ROOM])) {
            fail('筛选参数错误');
        }

        $submenu = [];
        switch ($kind) {
            case CateKind::QUESTION:
            case CateKind::PAPER:
                $submenu = [
                    [
                        'name'  => '默认排序',
                        'value' => '',
                    ],
                    [
                        'name'  => '按参与人数从低到高',
                        'value' => "join_count|asc",
                    ],
                    [
                        'name'  => '按参与人数从高到低',
                        'value' => "join_count|desc",
                    ],
                ];
                break;
            case CateKind::ROOM:
                $submenu = [
                    [
                        'name'  => '默认排序',
                        'value' => '',
                    ],
                    [
                        'name'  => '按报考人数从低到高',
                        'value' => "signup_count|asc",
                    ],
                    [
                        'name'  => '按报考人数从高到低',
                        'value' => "signup_count|desc",
                    ],
                    [
                        'name'  => '按考试人数从低到高',
                        'value' => "grade_count|asc",
                    ],
                    [
                        'name'  => '按考试人数从高到低',
                        'value' => "grade_count|desc",
                    ],
                    [
                        'name'  => '按及格人数从低到高',
                        'value' => "pass_count|asc",
                    ],
                    [
                        'name'  => '按及格人数从高到低',
                        'value' => "pass_count|desc",
                    ],
                ];
                break;
        }

        $filter = [
            [
                'name'    => '筛选分类',
                'type'    => 'hierarchy',
                'submenu' => CateModel::threeLevel($kind)
            ],
            [
                'name'    => '排序',
                'type'    => 'hierarchy',
                'submenu' => $submenu
            ]
        ];

        $this->success('', $filter);
    }

    /**
     * 3级分类获取
     */
    public function getThree()
    {
        $kind = input('kind/s');

        if (!$kind || !in_array($kind, [CateKind::PAPER, CateKind::QUESTION, CateKind::ROOM])) {
            fail('筛选参数错误');
        }

        $cates = CateModel::threeLevel2($kind);
        $this->success('', $cates);
    }
}
