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

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

        $grid->disableCreateButton(false);
        $grid->column('id', __('ID'));
        $grid->column('name', __('疫苗名称'));
        $grid->column('type', __('类别'))->editable('select',[1=>'一类',2=>'二类']);
        $grid->column('stocks', __('剩余库存'))->display(function (){
            return OrganizationVaccine::where(['vaccine_id'=>$this->id])->sum('stock');
        });
        $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('price', __('价格'))->editable();
        $grid->column('remark', __('备注'))->editable('textarea');
        $grid->column('supplier', __('厂家'));
//        $grid->column('created_at', __('Created at'));
//        $grid->column('updated_at', __('Updated at'));
        $grid->filter(function ($fliter){
            $fliter->equal('type','类别')->select([1=>'一类',2=>'二类']);
        });
        $grid->actions(function ($actions) {

            // append一个操作
            $actions->append('<a href=""><i class="fa fa-header">跳转</i></a>');

            // prepend一个操作
            $actions->prepend('<a href="http://www.baidu.com" target="_blank"><i class="fa fa-cab"></i></a>');
        });
        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('type', __('Type'));
        $show->field('price', __('Price'));
        $show->field('name', __('Name'));
        $show->field('remark', __('Remark'));
        $show->field('supplier', __('Supplier'));
        $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 Vaccine());

        $orglist = Organization::pluck('name','id');
        $org_id = Admin::user()->org_id;
        if($org_id){
            $orglist = Organization::where(['id'=>$org_id])->pluck('name','id');
        }
        $form->select('type', __('类型'))->options([1=>'一类',2=>'二类'])->default(1);
        $form->select('org_id','机构')->options($orglist)->rules('required',['requried'=>'请选择机构']);
        $form->text('name', __('疫苗名称'))->rules('required',['requried'=>'请填写疫苗名称']);
        $form->text('supplier', __('厂家'))->rules('required',['requried'=>'请填写厂家信息']);
        $form->number('price', __('价格'))->rules('required',['requried'=>'请填写价格']);
        $form->number('stock', __('库存'))->rules('required',['requried'=>'请填写库存']);
        $form->textarea('remark', __('备注'));

        return $form;
    }
}
