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

namespace App\Http\Controllers\Api\V1;

use App\Models\Vaccine;

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

        $builder = Vaccine::join('organization_vaccines', 'organization_vaccines.vaccine_id', '=', 'vaccines.id')->select(['vaccines.id', 'vaccines.type', 'vaccines.price', 'vaccines.name', 'vaccines.remark', 'vaccines.supplier', 'organization_vaccines.stock'])->where('organization_vaccines.stock', '>', 0);
        if (!empty($req['type'])) {
            $builder->where('vacciness.type', $req['type']);
        }
        if (!empty($req['name'])) {
            $builder->where('vacciness.name', 'like', '%'.$req['name'].'%');
        }
        if (!empty($req['organization_id'])) {
            $builder->where('organization_vaccines.org_id', $req['organization_id']);
        }
        if (!empty($req['sort_type'])) {
            if ($req['sort_type'] == 1) {
                $builder->orderBy('vaccines.price', 'asc');
            }
            elseif ($req['sort_type'] == 2) {
                $builder->orderBy('vaccines.price', 'desc');
            }
        }

        $data = $builder->paginate();

        return out($data);
    }
}
