<?php
namespace app\controller;


use app\model\qw\QwCategory;
use app\model\qw\QwDepartment;
use app\model\qw\QwSub;
use think\facade\Request;
use think\response\Json;


/**
 * 科室知识问题
 */
class Qw extends CommonTwoController
{


    /**
     * 科室列表
     */
    public function categoryList()
    {
        return $this->_json_succ(
            QwDepartment::withSearch('status')->select()->toArray()
        );
    }


    /**
     * 科室下分类列表
     */
    public function departmentList()
    {
        $departmentId = Request::param('department_id') ?? '';
        $where = [];

        if( $departmentId) {
            $where = [
                'department_id' => $departmentId
            ];
        }

        $categoryList = QwCategory::where($where)->withSearch('status')->select()->toArray();
        return $this->_json_succ(
            $categoryList
        );
    }


    /**
     * 分类下问题
     */
    public function questionList()
    {
        $categoryId = Request::param('category_id') ?? '';
        if( ! $categoryId) {
            return $this->_json_error('未获取到分类Id');
        }

        $questionList = \app\model\qw\Qw::where('category_id' , $categoryId)
                        ->withSearch('status' , 1)
                        ->select()->toArray() ?? [];
        return $this->_json_succ(
            $questionList
        );
    }


    /**
     * 热门问题
     */
    public function hotQuestionList()
    {
        $questionList = \app\model\qw\Qw::withSearch('status')->select()->toArray() ?? [];
        return $this->_json_succ(
            [
                'show_title'    => '热门问题',
                'question_list' => $questionList
            ]
        );
    }


    /**
     * 问题对应答案
     */
    public function getAnswerByQuestion()
    {
       $params = Request::only(
           [
               'question_id' , 'search' , 'is_sub_question' , 'category_id'
           ]
       );

       $search        = $params['search']          ?? '';
       $questionId    = $params['question_id']     ?? '';
       $isSubQuestion = $params['is_sub_question'] ?? '';

       /**
        * 输入的情况
        */
       if($search) {
            /**
             * 检测问题 问题主表 / 问题子表
             */
            $question = \app\model\qw\Qw::where("question" , "like" , "%$search%")->select()->toArray() ?? [];
            if( ! empty($question)) {
               return $this->_json_succ($question);
            }

            $subQuestion = QwSub::where('sub_question', 'like' , "%$search%")->select()->toArray() ?? [];
            if( ! empty($subQuestion)) {
                return $this->_json_succ($question);
            }

            if( empty($question) && empty($subQuestion)) {
                return 121212;
                /**
                 * 检索不到返分类
                 */
                $qwCategory = \app\model\qw\QwCategory::select();
                return $this->_json_succ($qwCategory);
            }

            $categoryId = $params['category_id'] ?? '';
            if($categoryId) {
                $qwSubCategory = QwCategory::where('category_id', $categoryId)->select()->toArray();
                return $this->_json_succ($qwSubCategory);
            }
       }


       /**
        * 点选的情况
        */
       if( ! $search) {
            /**
             * 子问题
             */
            if($isSubQuestion) {
                $result = QwSub::field('id as sub_question_id , sub_question as question, sub_answer as answer')->where('id', $questionId)->select()->toArray();
            } else {
                $result = \app\model\qw\Qw::find($questionId);
                if( 1 == $result->is_have_question_select) {
                    /**
                     * 多级问题回答
                     */
                    $result = QwSub::field('id as sub_question_id, question_id , sub_question , is_sub_question')->where('question_id' , $questionId)->select()->toArray();
                } else {
                    /**
                     * 一级问题回答
                     */
                    $result = \app\model\qw\Qw::field('id as question_id , question , answer')->where('id' , $questionId)->select()->toArray();
                }
            }
        }

       return $this->_json_succ(
            $result
       );
    }

}