<?php

namespace App\Admin\Controllers\UserManagement\DocterManagement;

use App\Admin\Actions\backstage\Evaluate\Ignore;
use App\Admin\Actions\backstage\Evaluate\Pass;
use App\Admin\Filters\date;
use App\Models\Evaluate;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class UserEvaluatesController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '评价管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Evaluate());
        $grid->disableCreateButton();
        $grid->actions(function ($actions) {
            // 去掉删除
            $actions->disableDelete();
            // 去掉编辑
            $actions->disableEdit();
            //去掉查看
            $actions->disableView();
            //只有状态为待审核才可以显示通过和忽略
            if ($actions->row->status ==1)
            {
                //审核通过
                $actions->add(new Pass());
                //忽略
                $actions->add(new Ignore());
            }

        });

        $grid->filter(function ($filter){
            $filter->disableIdFilter();
            $filter->like('user.nickname','用户名字');
            $filter->like('docter.name','医生名字');
            $filter->like('content','评价内容');
            $filter->timestampBetween('audit_time', "审核时间")->datetime();
            $filter->equal('status','审核状态')->select([
                1   => '待审核',
                2    => '审核通过',
                3    => '忽略',
            ]);
        });
        $grid->model()->orderBy('id','desc');
        $grid->column('id', __('Id'));
        $grid->column('order_id', __('订单ID'));
        $grid->column('user.nickname', __('用户姓名'));
        $grid->column('docter.name', __('医生名称'));
        $grid->column('content', __('评价内容'));
        $grid->column('score', __('评分'));
        $grid->column('status', __('状态'))->using([1=>'待审核',2=>'审核通过',3=>'忽略']);
        $grid->column('audit_time', __('审核时间'))->display(function ($time){
            if($time == 0)
                return '';
            else
            {
                return date('Y-m-d H:i:s',$time);
            }
        });
        $grid->column('created_at', __('创建时间'));
        $grid->column('updated_at', __('更新时间'));

        return $grid;
    }

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

        $show->field('id', __('Id'));
        $show->field('user_id', __('User id'));
        $show->field('order_id', __('Order id'));
        $show->field('docter_id', __('Docter id'));
        $show->field('status', __('Status'));
        $show->field('audit_time', __('Audit time'));
        $show->field('content', __('Content'));
        $show->field('score', __('Score'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

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

        $form->number('user_id', __('User id'));
        $form->number('order_id', __('Order id'));
        $form->number('docter_id', __('Docter id'));
        $form->switch('status', __('Status'))->default(1);
        $form->number('audit_time', __('Audit time'));
        $form->text('content', __('Content'));
        $form->switch('score', __('Score'));

        return $form;
    }
}
