<?php

namespace App\Community\Controllers;

use App\Models\Order;
use App\Models\Organization;
use App\Models\OrganizationVaccine;
use App\Models\OrganizationVaccines;
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;
use Encore\Admin\Widgets\Table;

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

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Vaccine());
        $grid->model()->orderByDesc('id');
        $org_id = Admin::user()->org_id;
        if(!empty($org_id)){
            $grid->model()->where(['org_id'=>$org_id]);
        }

        $grid->disableCreateButton(false);
        $grid->column('id', __('ID'));
        $grid->column('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->disableDelete();
        });

//        $grid->column('stock', __('库存'));

        $grid->column('stock', __('剩余库存'))->display(function ($w){
            return !empty($w) ? $w :  0;
        });
//        $grid->column('org_id', __('已使用量'))->display(function ()  {
//           return Order::where(['product_type'=>4])->wherehas('orderVaccine',function ($query){
//                $query->where('vaccine_id',$this->id);
//            })->count();
//        });
//        $grid->column('today_num', __('今日预约'))->display(function ()  {
//            return Order::where(['product_type'=>4])->wherehas('orderVaccine',function ($query){
//                $query->where('vaccine_id',$this->id);
//            })->count();
//        });
        $grid->column('supplier', __('厂家'));
        $grid->column('remark', __('备注'));
        $grid->filter(function ($fliter){
            $fliter->like('name','疫苗');
        });

        $grid->column('created_at', __('创建时间'));
        $grid->column('updated_at', __('更新时间'));

        $grid->actions(function ($actions) {

            $actions->disableDelete();
        });
        return $grid;
    }

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

        $show->field('id', __('Id'));
        $show->field('name', __('疫苗'));
        $show->field('type', __('疫苗类型'))->using(OrganizationVaccine::getType());
        $show->field('price', __('价格'))->as(function ($w){
            return round($w/100, 2).'元';
        });
        $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 Vaccine());

        $orglist = Organization::pluck('name','id');
        $is_admin = Admin::user()->isAdministrator();
        $org_id = Admin::user()->org_id;
        if($org_id && $is_admin){
            $orglist = Organization::pluck('name','id');
        }
        $form->editing(function ($f){
            $f->model()->price /= 100;
        });
        if($is_admin ){
            $form->select('org_id','机构')->options($orglist)->rules('required',['requried'=>'请选择机构']);
        } else {
            $form->hidden('org_id', __('类型'))->value($org_id);
        }
        $form->text('name', __('疫苗名称'))->rules('required',['required'=>'请输入疫苗']);
        $form->select('type', __('疫苗类型'))->options(OrganizationVaccine::getType())->when(1,function () use($form){
            $form->hidden('price','价格')->value(0);
        })->when(2,function () use($form){
            $form->text('price','价格');
        });
        $states = [
            'off' => ['value' => 0, 'text' => '禁用', 'color' => 'danger'],
            'on'  => ['value' => 1, 'text' => '启用', 'color' => 'success'],
        ];
        $form->text('stock', __('库存'))->rules('required',['requried'=>'请填写库存']);
        $form->text('supplier','厂家');
        $form->switch('states','状态')->states($states)->default(1);
        $form->textarea('remark', __('备注'));
        $form->saving(function ($form){
           $form->price = $form->price*100;
        });

        return $form;
    }
}
