<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Users\Report;
use App\Models\UserReport;
use App\Models\UserReportModel;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class UserReportController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new UserReport());
        $grid->model()->with(['user:id,name,avatar'])->orderByDesc('id');
        $grid->column('id')->sortable();
//        $grid->column('user_id');
        $grid->column('user.avatar')->image("",50);
        $grid->column('user.name');
        $grid->column('to_id');
        $grid->column('type')->using([1=>trans('user-report.fields.feeds'),2=>trans('user-report.fields.users')])->label(['blue','green']);
        $grid->column('content');
        $grid->column('files')->display(function ($v){
            if(isset($v)&&!empty($v)){
                $v = json_decode($v,true);
                $str = '';
                if(!empty($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('result');
        $grid->column('created_at');
        $grid->column('updated_at')->sortable();

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

        $grid->disableCreateButton();

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

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new UserReport(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('to_id');
            $show->field('type');
            $show->field('content')->unescape();
            $show->html(function ($res){
                $str = "";
                if($res['files']){
                    $res = json_decode($res['files'],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');
            $show->field('result');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

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

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