<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use think\Exception;

class Question extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\Question();
    }

    /**
     * 列表
     * @return void
     */
    public function getList()
    {
        try {
            $field = 'id,title,desc';
            $where['status'] = 1;
            $result = $this->model->field($field)->with(['questionItem'=>function($query){
                $query->field('id,title,content')->where(['status'=>1]);
            }])->where($where)->select();
            $rows = [];
            if (!empty($result)) {
                foreach ($result as $key => &$value) {
                    $itemData = isset($value['question_item']) ? $value['question_item'] : '';
                    $questionItem = [];
                    foreach ($itemData as $itemKey => $itemVal) {
                        $questionItem[] = [
                            'id' => $itemVal['id'],
                            'title' => $itemVal['title'],
                            'content' => $itemVal['content'],
                        ];
                    }
                    $rows[] = [
                        'id' => $value['id'],
                        'title' => $value['title'],
                        'desc' => $value['desc'],
                        'question_item' => $questionItem,
                    ];
                }
            }
            $this->success('获取成功',$rows);
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }

    /**
     * 详情
     * @return void
     */
    public function getInfo()
    {
        try {
            $id = $this->request->param('id',0);
            $field = 'id,title,content';
            $where['id'] = $id;
            $result = model('QuestionItem')->field($field)->where($where)->find();

            $this->success('获取成功',$result);
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }


}