<?php
/**
 * Created by PhpStorm.
 * User: Yuanhang Liu
 * Date: 20-10-14
 * Time: 上午00:01
 */

namespace App\Http\Controllers\Api\V2;

use App\Models\Evaluate;
use App\Models\Order;
use App\Models\Suggest;
use App\Models\SuggestDetail;

/**
 * 意见管理类
 * Class SuggestController
 * @author Yuanhang Liu & Xiaoyun Liu
 * @package App\Http\Controllers\Api\V2
 */
class SuggestController extends AuthController
{
    /**
     * 意见单查看
     * @author Yuanhang Liu & Xiaoyun Liu
     * @date 2020/10/14 00:03
     * @return mixedsuggestList
     */
    public function suggestList(){
        $req = request()->post();
        $this->validate(request(), [
            'curPage' => 'required|integer',
            'pageSize' => 'required|integer',
            'orders' => 'required',
        ]);
        $pageSize = ($req['curPage']-1)*$req['pageSize'];
        $user = $this->user;
        $docter_id = $user['id'];
        $order =[];
        if($req['orders']==1){
            // 正序
            $order = ['created_at','ASC'];
        }else if($req['orders']==2){
            // 倒叙
            $order = ['created_at','desc'];
        }else{
            $order =['id','desc'];
        }

        $order_ids = Order::where('docter_id',$docter_id)->select('id')->get();
        $data = [];
        $ids = [];
        if ($order_ids){
            foreach($order_ids as $k=>$v){
                $ids[$k] = $v['id'];
            }
            $data = Suggest::with(['order','Patient'])->whereIn('order_id',$ids)->orderBy($order[0], $order[1])->skip($pageSize)->take($req['pageSize'])->get()->toArray();
            foreach ($data as $k=>$v){
                if (!$data[$k]['order']||$data[$k]['order']==null||!$data[$k]['patient']||$data[$k]['patient']==null){
                    unset($data[$k]);
                }
                else{
                    $data[$k]['numBirthday'] = numBirthday($v['patient']['birthday']);
                    $data[$k]['patient_name'] = $v['patient']['name'];
                    $data[$k]['order_sn'] = $v['order']['order_sn'];
                    unset($data[$k]['patient'],$data[$k]['order']);
                }

            }
        }

        return out($data);
    }

    /**
     * 获取意见单
     * @author Yuanhang Liu & Xiaoyun Liu
     * @date 2020/10/14 00:10
     * @return mixed
     */
    public function getSuggestDetail($id=null)
    {
        if (empty($id)){
            $req = request()->post();
            $this->validate(request(), [
                'suggest_id' => 'required|integer',
                'type'  => 'integer',
            ]);
            if($req['type']==2){
                $data = Suggest::where('order_id',$req['suggest_id'])->get()->first();
                if($data){
                    $data = $data->toArray();
                    $id = $data['id'];
                }else{
                    return out('',500,'数据错误！');
                }
            }else{
                $id = $req['suggest_id'];
            }
        }
        $data = Suggest::with(['details','Patient','user','order'])->where('id',$id)->get()->toArray();
//        var_dump($data);die();
        if ($data){
            $data[0]['patient_name'] = $data[0]['patient']['name'];
            $data[0]['patient_card_number'] = $data[0]['patient']['card_number'];
            $data[0]['birthday'] = numBirthday($data[0]['patient']['birthday']);
            $data[0]['username'] = $data[0]['user']['nickname'];
            $data[0]['order_sn'] = $data[0]['order']['order_sn'];
            unset($data[0]['patient'],$data[0]['user'],$data[0]['order']);
            return out($data[0]);
        }else{
            return out([]);
        }
    }




    /**
     * 补充意见单
     * @author Yuanhang Liu & Xiaoyun Liu
     * @date 2020/10/14 00:20
     * @return mixed
     */
    public function addSuggestDetail()
    {
        $req = request()->post();
        $this->validate(request(), [
            'suggest_id' => 'required|integer',
            'supplement_reason' => 'required|max:500',
            'supplement_content' => 'required|max:500',
        ]);

        $num = 0;
        // 查询补充过几次了！
        $count_array = $this->getSuggestDetail($req['suggest_id']);
        $count_array = json_decode($count_array->getContent(),true);
        if ($count_array){
            $count_array = $count_array['data']['details'];
            $num = count($count_array);
        }
//        var_dump($num);die();
        if ($num>=2){
            return out(null, 500, '您已经补充过两次意见了！');
        }
        $data = SuggestDetail::create([
            'suggest_id'=>$req['suggest_id'],
            'supplement_reason'=>$req['supplement_reason'],
            'supplement_content'=>$req['supplement_content'],
        ]);
        return out();
    }

    public function getWeek(){
        $data=getWeek(date("w"));
        return out($data);
    }
}
