<?php

namespace App\Admin\Controllers\ServiceManagement;

use App\Admin\Actions\backstage\Pass;
use App\Admin\Actions\Service\ApplysAction;
use App\Admin\Actions\Service\PassAction;
use App\Admin\Actions\Service\RefuseAction;
use App\Admin\Actions\Service\RevokeAction;
use App\Models\Docter;
use App\Models\DocterLabel;
use App\Models\Order;
use App\Models\DocterTimes;
use App\Models\Organization;
use App\Models\Serviceapplys;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class ServiceController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '服务审核';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Serviceapplys());

//        $status = request('status',2);

        $grid->tools(function ($tools) {
            $tools->batch(function ($batch) {
                $batch->disableDelete();
            });
        });
        $grid->model()->orderBy('id','desc');
        $grid->column('id', __('Id'));
        $grid->column('ondocter.avatar', __('头像'))->lightbox(['width' =>'', 'height' => 30]);
        $grid->column('ondocter.id', __('医生ID'));
        $grid->column('ondocter.name','医生名称');
        $grid->column('service_type','服务类别')->using([1=>'图文咨询',2=>'电话咨询',3=>'门诊预约'])->label('default');
        $grid->column('ondocter.sex','性别')->using([1=>'男',2=>'女']);
        $grid->column('docterorganization', __('所属机构'))->pluck('organization_id')->display(function ($organization){
            $value = Organization::whereIn('id',$organization->toArray())->pluck('name')->toArray();
            return $value;
        })->label('info');
        $grid->column('ondocter.label', __('标签'))->display(function ($label){
            if($label==null || $label == '无')
            {
                return '无';
            }else{
                $label_value = DocterLabel::whereIn('id',$label)->pluck('label_name')->toArray();
                return $label_value;
            }
        })->label(['info']);


        $grid->column('status', __('状态'))->using([1=>'待审核',2=>'已开通',3=>'未开通'])->label([
            1 => 'warning',
            2 => 'success',
            3 => 'default'
        ]);
        $grid->column('created_at', __('创建时间'));
        $grid->column('updated_at', __('更新时间'));

        $grid->filter(function ($filter){
            $filter->equal('service_type','服务类型')->select([1 => '图文', 2 => '电话', 3 => '问诊']);
            $filter->equal('status','服务类型')->select([1=>'待审核',2 =>'已通过 ',3=>'已拒绝']);
            $filter->equal('docterorganization.organization_id','机构')->select(Organization::pluck('name','id'));
        });
        $grid->actions(function ($actions) {
            $actions->disableEdit();
            $actions->disableView();
            $actions->disableDelete();
            if($actions->row->status == 1){
                $actions->add(new ApplysAction());
                $actions->add(new RefuseAction());
            }
            if ($actions->row->status == 2)
            {
                $actions->add(new RevokeAction());
            }
            if ($actions->row->status == 3)
            {
                $actions->add(new PassAction());
            }
        });
        return $grid;
    }

}
