<?php

namespace App\Community\Controllers\Vaccine;

use App\Models\Organization;
use App\Models\OrganizationVaccine;
use App\Models\Vaccine;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class OrganizationVaccineController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '机构疫苗';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new OrganizationVaccine());

        $org_id = Admin::user()->org_id;
        if($org_id){
            $grid->model()->where(['org_id'=>$org_id]);
        }
        $grid->disableCreateButton(false);


        $grid->filter(function ($fliter){
            $fliter->equal('organization.name','机构')->select(Organization::pluck('name','id'));
            $fliter->euqal('vaccine.name','疫苗')->select(Vaccine::pluck('name','id'));
        });


        $grid->column('id', __('Id'));
        $grid->column('organization.name', __('机构'));
        $grid->column('vaccine.name', __('疫苗'));
        $grid->column('stock', __('库存'))->editable();
        $grid->column('created_at', __('创建时间'));
        $grid->column('updated_at', __('更新时间'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(OrganizationVaccine::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('org_id', __('Org id'));
        $show->field('vaccine_id', __('Vaccine id'));
        $show->field('stock', __('Stock'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new OrganizationVaccine());
        $is_admin = Admin::user()->inRoles(['administrator','manager']);
        $org_id = Admin::user()->org_id;
        $where = [];
        if(!empty($org_id)){
            $where = ['id'=>$org_id];
        }
        if($is_admin){
            if($form->isEditing()){
                $form->select('org_id','机构')->options(Organization::where($where)->pluck('name','id'))->readOnly();
                $form->select('vaccine_id', __('疫苗'))->options(Vaccine::pluck('name','id'))->readOnly();
            } else {
                $form->select('org_id','机构')->options(Organization::where($where)->pluck('name','id'));
                $form->select('vaccine_id', __('疫苗'))->options(Vaccine::pluck('name','id'));
            }
        } else {
            $form->hidden('org_id')->value(Admin::user()->org_id);
        }
        $form->text('stock', __('库存'))->rules('required',['required'=>'请输入库存']);
        $form->datetime('updated_at', __('更新时间'));
        $form->datetime('created_at', __('创建时间'));

        return $form;
    }
}
