<?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);


        $is_admin = Admin::user()->isAdministrator();
        $grid->filter(function ($fliter) use ($is_admin) {
            if($is_admin){
                $fliter->equal('organization.name','机构')->select(Organization::pluck('name','id'));
            }
            $fliter->like('vaccine.name','疫苗')->select(Vaccine::pluck('name','id'));
        });


        $grid->column('id', __('Id'));
        $grid->column('organization.name', __('机构'));
        $grid->column('vaccine.name', __('疫苗名称'));
        $grid->column('type', __('疫苗类型'))->using(OrganizationVaccine::getType())->label('info');
        $grid->column('price', __('价格'))->display(function ($p){
            $t = $this->type;
            if($t == 1)return '免费';
            return ($p / 100).'元';
        });
        $grid->actions(function ($actions){
//            $actions->disableEdit();
//            $actions->disableView();
            $actions->disableDelete();
        });

        $grid->column('stock', __('库存'));
        $grid->column('supplier', __('厂家'));
        $grid->column('remark', __('备注'));
        $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('organization.name', __('机构'));
        $show->field('vaccine.name', __('疫苗'));
        $show->field('type', __('疫苗类型'))->using(OrganizationVaccine::getType());
        $show->field('vaccine.name', __('疫苗'));
        $show->field('price', __('价格'));
        $show->field('supplier', __('厂家'));
        $show->field('reamrk', __('备注'));
        $show->field('created_at', __('创建时间'));
        $show->field('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;
        $form->editing(function ($f){
            $f->model()->price /=100;
        });
        $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();
                $form->select('type', __('疫苗类型'))->options(OrganizationVaccine::getType())->when(1,function () use($form){
                    $form->hidden('price','价格')->value(0);
                })->when(2,function () use($form){
                    $form->text('price','价格');
                });
            } else {
                $form->select('org_id','机构')->options(Organization::where($where)->pluck('name','id'));
                $form->select('vaccine_id', __('疫苗'))->options(Vaccine::pluck('name','id'));
                $form->select('type', __('疫苗类型'))->options(OrganizationVaccine::getType())->when(1,function () use($form){
                    $form->hidden('price','价格')->value(0);
                })->when(2,function () use($form){
                    $form->text('price','价格');
                });
            }
        } else {
            $form->hidden('org_id')->value(Admin::user()->org_id);
        }

        $form->saving(function ($f){
           $f->price *=100;
        });

        $form->number('stock', __('库存'))->rules('required',['required'=>'请输入库存']);
        $form->text('supplier', __('厂家'))->rules('required',['required'=>'请输入厂家']);
        $form->textarea('remark', __('备注'));

        return $form;
    }
}
