<?php
/**
 * Created by PhpStorm.
 * User: Yuanhang Liu
 * Date: 20-10-15
 * Time: 下午8:46
 */

namespace App\Http\Controllers\Api\V2;

use App\Models\Order;
use App\Models\Patient;
use App\Models\CallLog;
use App\Models\OrderPatient;
use App\Models\DocterOrganization;
use App\Models\Organization;
use App\Models\DocterMessage;
use App\Http\Controllers\Api\V2\CommonController as Commons;

/**
 * 医生通知类
 * Class DocterMessageController
 * @package App\Http\Controllers\Api\V2
 */
class DocterMessageController extends AuthController
{
    /**
     * @return mixed
     * 医生通知列表
     * @author Yuanhang Liu & Xiaoyun Liu
     */
    public function docterMessageList()
    {
        $req = request()->post();
        $this->validate(request(), [
            'curPage' => 'required|integer',
            'pageSize' => 'required|integer',
        ]);
        $pageSize = ($req['curPage']-1)*$req['pageSize'];
        $user = $this->user;
        $doctor_id = $user['id'];

        $where['docter_id'] = $doctor_id;
        $data = DocterMessage::where($where)->orderBy('id', 'desc')->skip($pageSize)->take($req['pageSize'])->get();

        if ($data){
            $data = $data->toArray();

        }else{
            return out([]);
        }
        return out($data);
    }

    /**
     * @return mixed
     * 删除医生通知
     * @author Yuanhang Liu & Xiaoyun Liu
     */
    public function delDocterMessage()
    {
        $req = request()->post();
        $this->validate(request(), [
            'id' => 'required|integer',
        ]);

        $del = DocterMessage::where('id','=',$req['id'])->delete();
        if ($del){
            return out();
        }else{
            return out('',500,'删除错误！');
        }

    }

    /**
     * @return mixed
     * 全部标为已读医生通知
     * @author Yuanhang Liu & Xiaoyun Liu
     */
    public function okDocterMessage()
    {
        $req = request()->post();
//        $this->validate(request(), [
//            'id' => 'required|integer',
//        ]);
        $user = $this->user;
        $del = DocterMessage::where('docter_id','=',$user['id'])->update(['status'=>2]);
        if ($del){
            return out();
        }else{
            return out('',500,'操作失败！');
        }

    }

    /**
     * 标记已读
     * @return \Illuminate\Http\JsonResponse
     * @author Liu-Yh
     * Create By 2020/12/9 10:46
     */
    public function readOk()
    {
        $req = request()->post();
        $this->validate(request(), [
            'id' => 'required|integer',
        ]);
        $user = $this->user;
        $del = DocterMessage::where('id','=',$req['id'])->update(['status'=>2]);
        if ($del){
            return out();
        }else{
            return out('',500,'操作失败！');
        }

    }
}
