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

namespace App\Http\Controllers\Api\V1;

use App\Models\Area;
use App\Models\Organization;
use DB;

class OrganizationController extends AuthController
{
    public function organizationList()
    {
        $req = request()->post();
        $this->validate(request(), [
            'latitude' => 'numeric',
            'longitude' => 'numeric',
            'city_id' => 'integer',
        ]);
        $user = $this->user;

        $distance_field = get_user_distance_field($user);

        $builder = Organization::with('docter')->select(['id', 'type', 'name', 'province_id', 'city_id', 'area_id', 'address', 'latitude', 'longitude','nurse_notice','vaccine_notice', DB::raw($distance_field)]);
        if (!empty($req['city_id'])) {
            $builder->where('city_id', $req['city_id']);
        }
        $data = $builder->orderBy('distance', 'asc')->paginate();

        return out($data);
    }

    public function organizationCityList()
    {
        $city_ids = Organization::pluck('city_id')->toArray();

        $data = Area::select(['id', 'name'])->whereIn('id', $city_ids)->get();

        return out($data);
    }
}
