<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Users\Report;
use App\Models\UserFeedback;
use App\Models\UserReportModel;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Database\Eloquent\Model;

class UserFeedbackController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new UserFeedback());
        $grid->model()->with(['user:id,name'])->orderByDesc('id');

        $grid->column('id')->sortable();
        $grid->column('user.name',trans('user-feedback.fields.user_id'));
        $grid->column('content');
        $grid->column('file')->display(function ($v){
            if(isset($v)&&!empty($v)){
                $v = json_decode($v,true);
                $str = '';
                if(is_array($v)&&count($v)>0){
                    foreach ($v as $item){
                        $str.='<img data-action="preview-img" src="'.$item.'" style="max-width:50px;max-height:200px;cursor:pointer" class="img img-thumbnail">';
                    }
                }
                return $str;
            }else{
                return "";
            }
        });
        $grid->column('status')->using([0=>trans('user-report.fields.untreated'),1=>trans('user-report.fields.processed')])->label(['red','green']);
        $grid->column('created_at');
//        $grid->column('updated_at')->sortable();

        $grid->filter(function (Grid\Filter $filter) {
            $filter->equal('id');
            $filter->like('user.name');
            $filter->like('content');
            $filter->equal('status')->select(['0'=>trans('user-report.fields.untreated'),'1'=>trans('user-report.fields.processed')]);
        });

        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
            $actions->disableEdit();
            $actions->disableDelete();
            if($actions->row->status==0){
                $actions->append(new Report(UserFeedback::class));
            }
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new UserFeedback(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('content');
            $show->field('file');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new UserFeedback(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('content');
            $form->text('file');
            $form->text('status');

            $form->display('created_at');
            $form->display('updated_at');

            $form->footer(function ($footer) {
                // 去掉`查看`checkbox
                $footer->disableViewCheck();
                // 去掉`继续编辑`checkbox
                $footer->disableEditingCheck();
                // 去掉`继续创建`checkbox
                $footer->disableCreatingCheck();
            });
        });
    }
}
