<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }

    //科室列表
    public function keshi_list(){
        $list = Db::name('keshi')->where('is_show',1)->order('weigh','desc')->select();
        $this->success(1,$list);
    }

    //职称列表
    public function level_list(){
        $list = Db::name('doctor_level')->select();
        $this->success(1,$list);
    }

    //医生列表
    //首页推荐，搜索
    public function doctor_list(){

        $where = [
            'd.status' => 1,
            'd.doctor_status' => 1,
        ];

        //科室id
        $keshi_id = input('keshi_id',0);
        if($keshi_id){
            $where['d.keshi_id'] = $keshi_id;
        }
        //职称id
        $level_id = input('level_id',0);
        if($level_id){
            $where['d.level_id'] = $level_id;
        }
        //关注
        $folllow = input('follow',0);
        if($folllow){
            $my_follow_ids = controller('Userfollow')->my_follow_uids();
            $where['d.id'] = ['IN',$my_follow_ids];
        }
        //搜索
        $keyword = input('keyword','');
        if(!empty($keyword)){
            $where['d.nickname|d.goodat|keshi.name'] = ['LIKE','%'.$keyword.'%'];

            if($this->auth->isLogin()){
                Db::name('user_search')->insertGetId(['user_id'=>$this->auth->id,'keyword'=>$keyword]);
            }
        }

        //dump($where);

        $field = [
            'd.id','d.nickname','d.avatar','d.keshi_id','d.level_id','d.hospital','d.goodat','d.ordernum',
            'keshi.name as keshi_name',
            'level.name as level_name','level.name_en as level_name_en',
            'info.typing_switch','info.video_switch','info.typing_price','info.video_price',
        ];
        $list = Db::name('doctor')->alias('d')
            ->field($field)
            ->join('doctor_level level','d.level_id = level.id','LEFT')
            ->join('keshi','d.keshi_id = keshi.id','LEFT')
            ->join('doctor_info info','d.id = info.doctor_id','LEFT')
            ->where($where)->order('d.ordernum desc')->autopage()->select();
        $list = list_domain_image($list,['avatar']);

        $this->success(1,$list);
    }

    //医生详情
    public function doctor_info(){
        $id = input('id',0);

        $field = [
            'd.id','d.nickname','d.avatar','d.keshi_id','d.level_id','d.hospital','d.goodat','d.ordernum','d.info',
            'keshi.name as keshi_name',
            'level.name as level_name',
            'info.typing_switch','info.video_switch','info.typing_price','info.video_price',
        ];
        $info = Db::name('doctor')->alias('d')
            ->field($field)
            ->join('doctor_level level','d.level_id = level.id','LEFT')
            ->join('keshi','d.keshi_id = keshi.id','LEFT')
            ->join('doctor_info info','d.id = info.doctor_id','LEFT')
            ->where('d.id',$id)->find();
        $info = info_domain_image($info,['avatar']);

        //是否关注
        $info['is_follw'] = $this->is_follow($this->auth->id,$id);

        //图文说明
        $info['wenzhen_typing_serverrule'] = config('site.wenzhen_typing_serverrule');
        //视频说明
        $info['wenzhen_video_serverrule'] = config('site.wenzhen_video_serverrule');

        $this->success(1,$info);
    }




}
