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

        $grid->disableCreateButton(false);
        $grid->column('id', __('ID'));
        $grid->column('name', __('疫苗名称'));
        $grid->column('organizations', '机构库存')->display(function (){
            return '点击查看';
        })->modal('机构库存', function ($model) {
            $org = $model->organizationvaccines()->get()->map(function ($comment) {
                return $comment->only(['org_id','vaccine_id','stock']);
            });
            $org = $org->toArray();
            for($i=0;$i<count($org);$i++)
            {
                $id = $org[$i]['org_id'];
                $org[$i]['vaccine_id'] = $this->name;
                $org[$i]['org_name'] = Organization::where('id',$id)->value('name');
            }
            return new Table(['ID','疫苗','库存','机构名称'], $org);
        });
        $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->filter(function ($fliter){
//            $fliter->equal('type','类别')->select(OrganizationVaccine::getType());
            $fliter->like('name','疫苗');
        });
        $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>');

            $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('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');
        $org_id = Admin::user()->org_id;
        if($org_id){
            $orglist = Organization::where(['id'=>$org_id])->pluck('name','id');
        }
//        $form->editing(function ($f){
//            $f->model()->price /= 100;
//        });
//        $form->select('type', __('类型'))->options([1=>'一类',2=>'二类'])->default(1);
//        $form->select('org_id','机构')->options($orglist)->rules('required',['requried'=>'请选择机构']);
        $form->text('name', __('疫苗名称'))->creationRules(['required', "unique:vaccines"])
            ->updateRules(['required', "unique:vaccines,name,{{id}}"]);
        $form->hidden('states')->value(1);
//        $form->text('stock', __('库存'))->rules('required',['requried'=>'请填写库存']);
//        $form->textarea('remark', __('备注'));
//        $form->saving(function ($form){
//           $form->price = $form->price*100;
//        });

        return $form;
    }
}
