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

namespace App\Http\Controllers\Api\V1;

use App\Models\Order;
use App\Models\Patient;

class PatientController extends AuthController
{
    public function createPatient()
    {
        $req = request()->post();
        $this->validate(request(), [
            'name' => 'required|max:50',
            'sex' => 'required|in:1,2',
            'avatar' => 'required|url',
            'birthday' => 'required|date',
            'relationship_type' => 'required|integer',
            'info' => 'max:1000',
            'card_type' => 'in:0,1,2',
            'card_number' => 'max:50',
            'email' => 'email',
            'phone' => 'required',
            'guardian_name' => 'required',
            'address' => 'required',
            'born_hospital' => 'required',
            'social_card_number' => 'max:50',
        ]);
        $user = $this->user;

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

        return out();
    }

    public function updatePatient()
    {
        $req = request()->post();
        $this->validate(request(), [
            'id' => 'required|integer',
            'name' => 'required|max:50',
            'sex' => 'required|in:1,2',
            'avatar' => 'required|url',
            'birthday' => 'required|date',
            'relationship_type' => 'required|integer',
            'info' => 'max:1000',
            'card_type' => 'in:0,1,2',
            'card_number' => 'max:50',
            'email' => 'email',
            'phone' => 'required',
            'guardian_name' => 'required',
            'address' => 'required',
            'born_hospital' => 'required',
            'social_card_number' => 'max:50',
        ]);
        $user = $this->user;

        Patient::where('id', $req['id'])->where('user_id', $user['id'])->update($req);

        return out();
    }

    public function submitCardImg()
    {
        $req = request()->post();
        $this->validate(request(), [
            'id' => 'required|integer',
            'card_img_url' => 'required|url',
            'card_back_img_url' => 'required|url',
        ]);
        $user = $this->user;

        Patient::where('id', $req['id'])->where('user_id', $user['id'])->update($req);

        return out();
    }

    public function patientList()
    {
        $user = $this->user;
        $data = Patient::where('user_id', $user['id'])->orderBy('id', 'desc')->paginate();
        foreach ($data as $k => $v) {
            $v->append(['order_num', 'pack_docter_num', 'perfectd_degree']);
        }
        return out($data);
    }

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

        $data = [];
        $data['patient'] = Patient::where('id', $req['patient_id'])->where('user_id', $user['id'])->first()->append(['order_num', 'pack_docter_num', 'perfectd_degree']);

        $data['orders']['cases'] = Order::with(['docter.office', 'docter.qualification', 'organization.docter'])->where('user_id', $user['id'])->where('patient_id', $req['patient_id'])->whereIn('order_status', [2,3])->where('product_type', '<', 6)->orderBy('id', 'desc')->get();
        $data['orders']['service_packs'] = Order::with('orderPack')->where('user_id', $user['id'])->where('patient_id', $req['patient_id'])->whereIn('order_status', [2,3])->where('product_type', 6)->orderBy('id', 'desc')->get();

        return out($data);
    }
}
