<?php
/**
 * Created by PhpStorm.
 * User: zilongs
 * Date: 20-10-5
 * Time: 上午11:44
 */

namespace App\Http\Controllers\Api\V1;

use App\Models\DocterMessage;
use App\Models\Evaluate;
use App\Models\Order;

class EvaluateController extends AuthController
{
    public function evaluateList()
    {
        $user = $this->user;

        $data = Evaluate::with('order.docter')->where('user_id', $user['id'])->orderBy('id', 'desc')->paginate();

        return out($data);
    }

    public function evaluateDetail()
    {
        $req = request()->post();
        $this->validate(request(), [
            'evaluate_id' => 'required|integer',
        ]);

        $data = Evaluate::with('order.docter')->where('id', $req['evaluate_id'])->first();

        return out($data);
    }

    public function sumbitEvaluate()
    {
        $req = request()->post();
        $this->validate(request(), [
            'order_id' => 'required|integer',
            'docter_id' => 'required|integer',
            'score' => 'required|integer',
            'content' => 'max:500',
        ]);
        $user = $this->user;

        $req['user_id'] = $user['id'];
        Evaluate::create($req);

        //发送医生端消息
        $order = Order::select(['order_sn'])->where('id', $req['order_id'])->first();
        DocterMessage::saveMessage($req['docter_id'], $req['user_id'], 4, $req['order_id'], [$order['order_sn']]);

        return out();
    }
}
