<?php
/**
 * Created by PhpStorm.
 * Author: 莫名疯
 * DateTime: 2017/3/8 10:09
 * Description:
 */

namespace api\modules\v1\controllers;


use api\common\controllers\Controller;
use api\modules\v1\models\Category;
use common\helpers\Tree;
use yii\helpers\ArrayHelper;
use api\common\behaviors\QueryParamAuth;


class CategoryController extends Controller {

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => QueryParamAuth::className(),
                'tokenParam' => 'token',
                'optional' => ['index']
            ]
        ]);
    }

    /**
     * @api {get} /v1/categorys 列表
     * @apiVersion 1.0.0
     * @apiName index
     * @apiGroup Category
     *
     *
     */
    public function actionIndex()
    {

        $lists = Category::find()->active()->orderBy(['sort' => SORT_ASC])->all();
        $lists = ArrayHelper::toArray($lists);
        $lists = Tree::build($lists);

        //添加热门推荐
//        array_unshift($lists, [
//            'title' => '热门推荐',
//            'children' => Cat::find()->hot()->all()
//        ]);

        return ['data' => $lists];

    }



    /**
     * @api {get} /v1/category/id:\d+ 分类详情
     * @apiVersion 1.0.0
     * @apiName view
     * @apiGroup Category
     *
     */
    public function actionView($id)
    {
        $model = Category::find()->where(['id' => $id])->one();

        return ['data' => $model];
    }


}