<?php

namespace App\Admin\Controllers\UserManagement\DocterManagement;

use App\Admin\Actions\backstage\Docters\AppointAction;
use App\Admin\Actions\backstage\Docters\ChatAction;
use App\Admin\Actions\backstage\Docters\PhoneAction;
use App\Admin\Actions\Community\Docter\Chat;
use App\Models\Docter;
use App\Models\ServiceApply;
use App\Models\Serviceapplys;
use function Couchbase\defaultDecoder;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class ServiceManagementController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '服务开通列表';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Docter());
        $id = \request('id');
        $grid->filter(function ($filter){
            $filter->disableIdFilter();
        });
        $grid->disableBatchActions();
        $grid->disableCreateButton();
        $grid->actions(function ($actions){
           $actions->disableView();
           $actions->disableDelete();
           $actions->add (new ChatAction());
           $actions->add (new PhoneAction());
           $actions->add (new AppointAction());
        });
        $grid->model()->where('id',$id);
        $grid->column('id', __('id'));
        $grid->column('name', __('医生姓名'));
        $grid->column('avatar', __('头像'))->lightbox(['width' =>'', 'height' => 30]);
        $states = [
            'on'  => ['value' => 2, 'text' => '已开通', 'color' => 'primary'],
            'off' => ['value' => 3, 'text' => '未开通', 'color' => 'danger'],
        ];
        //图文模块
        $grid->column('in_chat', __('图文咨询'))->display(function ()use ($grid){
            $status = Serviceapplys::where('docter_id',$this->id)->where('service_type',1)->value('status');
            if ($status == 2)
            {
                return '已开通';
            }else{
                return '未开通';
            }
        });
        $grid->column('is_chat', __('图文服务状态'))->using([0=>'未开启',1=>'已开启']);

        //电话模块
        $grid->column('in_phone', __('电话咨询'))->display(function ()use ($grid){
            $status = Serviceapplys::where('docter_id',$this->id)->where('service_type',2)->value('status');
            if ($status == 2)
            {
                return '已开通';
            }else{
                return '未开通';
            }
        });
        $grid->column('is_phone', __('电话服务状态'))->using([0=>'未开启',1=>'已开启']);

        //预约模块
        $grid->column('in_appoint', __('门诊预约'))->display(function ()use ($grid){

            $status = Serviceapplys::where('docter_id',$this->id)->where('service_type',3)->value('status');
            if ($status == 2)
            {
                return '已开通';
            }else{
                return '未开通';
            }
        });
        $grid->column('is_appoint', __('门诊预约状态'))->using([0=>'未开启',1=>'已开启']);

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

            $form->hidden('id');
            $states = [
                'on'  => ['value' => 2, 'text' => '已开通', 'color' => 'primary'],
                'off' => ['value' => 3, 'text' => '未开通', 'color' => 'danger'],
            ];
            $form->switch('in_chat','图文状态')->states($states)->default(function ()use ($form){
                $a = Serviceapplys::where('docter_id',$form->model()->id)->where('service_type',1)->value('status');
                return $a;
            });
            $form->switch('in_phone','电话状态')->states($states)->default(function ()use ($form){
                $a = Serviceapplys::where('docter_id',$form->model()->id)->where('service_type',2)->value('status');
                return $a;
            });
            $form->switch('in_appoint','预约状态')->states($states)->default(function ()use ($form){
                $a = Serviceapplys::where('docter_id',$form->model()->id)->where('service_type',3)->value('status');
                return $a;
            });
        $form->setAction('/admin/putdata');

        return $form;
    }
}
