<?php

namespace App\Admin\Controllers\VaccinesManagement;

use App\Models\Organization;
use App\Models\Vaccines;
use App\Admin\Actions\Vaccines\ImportAction;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Table;

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

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Vaccines());
        $grid->model()->orderBy('id','desc');
        $grid->column('id', __('Id'));
//        $grid->column('type', '类型')->using([1=>'Ⅰ类疫苗',2=>'Ⅱ类疫苗']);
        $grid->column('name', __('名称'));
        $states = [
            'on'  => ['value' => 1, 'text' => '启用', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '禁用', 'color' => 'danger'],
        ];

        $grid->column('organizations', '机构')->display(function (){
            return '点击查看';
        })->modal('机构库存', function ($model) {
            $org = $model->organizationvaccines()->get()->map(function ($comment) {
                return $comment->only(['org_id', 'stock']);
            });
            $org = $org->toArray();
            for($i=0;$i<count($org);$i++)
            {
                $id = $org[$i]['org_id'];
                $org[$i]['org_name'] = Organization::where('id',$id)->value('name');
            }
            return new Table(['ID', '库存' ,'机构名称'], $org);
        });

        $grid->column('kc','库存');
        $grid->column('states','状态')->switch($states);
        $grid->column('updated_at', __('更新时间'));

        $grid->filter(function($filter){
            /*
                搜索：按关键字、ID搜索
                筛选：类别筛选
                导入、导出
            */
            // 在这里添加字段过滤器
            $filter->column(1/2, function ($filter) {
                $filter->equal('type', '类型')->select(Vaccines::$_post_type);
                $filter->equal('supplier','厂家');
            });
            $filter->column(1/2, function ($filter) {
               $filter->equal('name','疫苗名称');
                $filter->equal('organizationvaccines.org_id','机构')->select(Organization::pluck('name','id'));
            });
        });
        //按关键字查询
        $grid->quickSearch(function ($model, $query) {
            //$model->whereHas('Vaccines',function ($model) use ($query) {
                $model->where('type', 'like', "%{$query}%")
                    ->orWhere('name', 'like', "%{$query}%")
                    ->orWhere('id',$query)
                    ->orwhere('supplier','like',"%{$query}%")
                    ->orwhere('remark','like',"{$query}");
            //});
        })->placeholder('请输入疫苗id/名称/厂家/备注');

        $grid->export(function ($export) {
            //文件名
            $export->filename('测试');
            //排除字段不用导出
            $export->except(['created_at', 'updated_at']);
            $export->originalValue(['states']);
        });

        $grid->tools(function (Grid\Tools $tools) {
            $tools->append(new ImportAction());
        });

        return $grid;
    }

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

        $show->field('id', __('Id'));
//        $show->field('type', __('分类'))->using([1=>'Ⅰ类疫苗',2=>'Ⅱ类疫苗']);
//        $show->field('introduction','简介');
//        $show->field('price', __('价钱'));
        $show->field('name', __('名称'));
//        $show->field('remark', __('备注'));
//        $show->field('supplier', __('厂家'));
        $show->field('created_at', __('创建时间'));
        $show->field('updated_at', __('更新时间'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Vaccines());

        $form->editing(function ($f){
            $f->model()->price /= 100;
        });

//        $form->select('type', __('分类'))->options(Vaccines::$_post_type)->default('1');
        $form->text('name', __('名称'))->rules('required|min:3|max:255',['required'=>'请填写名称','min'=>'名称不能少于3个字符！','max'=>'名称长度过长！']);
//        $form->text('price', __('价格'));
//        $form->text('remark', __('备注'));
//        $form->text('supplier', __('厂家'))->rules('required',['required'=>'请填写厂家！']);
        $states = [
            'off' => ['value' => 0, 'text' => '禁用', 'color' => 'danger'],
            'on'  => ['value' => 1, 'text' => '启用', 'color' => 'success'],
        ];
        $form->switch('states','状态')->states($states);
        $form->saving(function ($f){
            $f->price *=100;
        });

        return $form;
    }
}
