<?php

namespace App\Community\Controllers;

use App\Admin\Actions\Service\ApplysAction;
use App\Admin\Actions\Service\RefuseAction;
use App\Models\Docter;
use App\Models\Order;
use App\Models\DocterTimes;
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());
        $grid->model()->orderByDesc('id');

        $status = request('status',2);
//
        $grid->tools(function ($tools) {
            $tools->batch(function ($batch) {
                $batch->disableDelete();
            });
        });

        $grid->column('ondocter.name','医生姓名');
        $grid->column('ondocter.phone_price','咨询价格');
        $grid->column('step_price','步进价格')->display(function (){
            $step = DocterTimes::where(['docter_id'=>$this->id])->value('step_price');
            return $step;
        });

        $grid->column('orders','总订单数')->display(function (){

            $order_type = Order::where(['docter_id'=>$this->id,'product_type'=>1])->count();
            return $order_type;
        });
        $grid->column('orders_actions','进行中数')->display(function (){
            $action = Order::where(['docter_id'=>$this->id,'product_type'=>1,'order_status'=>3])->count();
            return $action;
        });
        $grid->column('orders_inishs','完成数')->display(function (){
            $finish = Order::where(['docter_id'=>$this->id,'product_type'=>1,'order_status'=>4])->count();
            return $finish;
        });
        $grid->column('orders_anomaly','异常数')->display(function (){
            $anomaly = Order::where(['docter_id'=>$this->id,'product_type'=>1])->whereIn('order_status',[1,2,5])->count();
            return $anomaly;
        });

        $grid->column('status', __('状态'))->using([1=>'审核',2=>'已通过',3=>'已拒绝'])->label([
            1 => 'warning',
            2 => 'success',
            3 => 'default'
        ]);
        if($status == 7) {

            $grid->column('id', __('Id'));
            $grid->column('ondocter.avatar', __('头像'))->image('',50,50);
            $grid->column('ondocter.id', __('医生ID'));
            $grid->column('ondocter.name','医生名称');
            $grid->column('ondocter.sex','性别')->using([1=>'男',2=>'女']);
            $grid->column('ondocter.DocterOrganization', __('所属机构'));
            $grid->column('ondocter.label', __('标签'))->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=>'已拒绝']);
        });
        $grid->actions(function ($actions) {
            $actions->disableEdit();
            $actions->disableView();
            $actions->disableDelete();
//            if($actions->row->status == 1){
//                $actions->add(new ApplysAction());
//                $actions->add(new RefuseAction());
//            }
        });


        return $grid;
    }

}
