<?php

namespace App\Admin\Controllers\OrdersManagement;

use App\Models\Evaluate;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class OrdersEvaluate 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->filter(function ($filter){
            $filter->disableIdFilter();
            $filter->like('user.nickname','用户名字');
            $filter->like('docter.name','医生名字');
            $filter->like('content','评价内容');
            $filter->equal('order_id','订单id');
            $filter->equal('status','审核状态')->select([
                1   => '待审核',
                2    => '审核通过',
                3    => '忽略',
            ]);
        });
        $grid->disableActions();
        $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;
    }
}
