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

namespace App\Http\Controllers\Api\V1;

use App\Models\Nurse;

class NurseController extends AuthController
{
    public function nurseList()
    {
        $req = request()->post();
        $this->validate(request(), [
            'organization_id' => 'required|integer',
            'name' => 'max:255',
            'free_type' => 'in:0,1,2',
            'sort_type' => 'in:0,1,2'
        ]);

        $builder = Nurse::select(['id', 'org_id', 'price', 'name', 'remark']);
        if (!empty($req['free_type'])) {
            if ($req['free_type'] == 1) {
                $builder->where('price', 0);
            }
            elseif ($req['free_type'] == 2) {
                $builder->where('price', '>', 0);
            }
        }
        if (!empty($req['name'])) {
            $builder->where('name', 'like', '%'.$req['name'].'%');
        }
        if (!empty($req['organization_id'])) {
            $builder->where('org_id', $req['organization_id']);
        }
        if (!empty($req['sort_type'])) {
            if ($req['sort_type'] == 1) {
                $builder->orderBy('price', 'asc');
            }
            elseif ($req['sort_type'] == 2) {
                $builder->orderBy('price', 'desc');
            }
        }

        $data = $builder->paginate();

        return out($data);
    }
}
