<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Problem\Reply;
use App\Models\UserProblemModel;
use App\Models\UsersProblem;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;

class UsersProblemController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new UserProblemModel());
        $grid->model()->orderBy('id','desc');

        $grid->column('id')->sortable();
        $grid->column('user_id');
        $grid->column('content')->limit(50);
        $grid->column('img_url')->display(function ($v){
            if(isset($v)){
                $v = json_decode($v,true);
                $str = '';
                if(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(['待处理','已处理'])->label(['warning','success']);
        $grid->column('reply','回复内容');
        $grid->column('created_at');
        $grid->column('updated_at')->sortable();

        $grid->filter(function (Grid\Filter $filter) {
            $filter->equal('id');

        });
        //操作管理
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableEdit();
            $actions->disableDelete();
            if ($actions->row->status == 0) {
                $actions->append(new Reply(UserProblemModel::class));
            }
        });

        $grid->disableCreateButton();
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new UserProblemModel(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('content')->unescape();
            $show->html(function ($res){
                $str = "";
                if($res['img_url']){
                    $res = json_decode($res['img_url'],true);
                    if(count($res)>0){

                        foreach ($res as $k=>$v){
                            $str.='<img data-action="preview-img" src="'.$v.'" style="max-width:100px;max-height:100px;cursor:pointer" class="img img-thumbnail">';
                        }
                    }else{
                        $str = "无";
                    }
                }else{
                    $str = "无";
                }
                $html = '<div class="show-field form-group row">
                    <div class="col-sm-2 control-label">
                        <span>图片</span>
                    </div>

                    <div class="col-sm-8">
                        <div class="box box-solid box-default no-margin box-show">
                            <div class="box-body">
                                      '.$str.'
                            </div>
                        </div>
                    </div>
                </div>';
                return $html;
            });
            $show->field('status')->using(['待处理','已处理']);
            $show->field('reply','回复内容');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

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

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