<?php

namespace App\Admin\Controllers;

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

class UsersReportController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new UserReportModel());
        $grid->model()->leftJoin('users_info','users_report.user_id','=','users_info.user_id')->select(['users_report.*','users_info.nickname','users_info.avatar','users_info.area'])->orderByDesc('id');

        $grid->column('id')->sortable();

        $grid->column('avatar','头像')->image("",50);
        $grid->column('nickname','昵称');
        $grid->column('type')->using([1=>'用户',2=>'动态'])->label(['gray','blue','green']);
        $grid->column('report_id');
        $grid->column('content');
        $grid->column('info');
        $grid->column('img_url','图片内容')->display(function ($v){
            if(isset($v)&&!empty($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([0=>"未处理",1=>"已处理"])->label(['red','green']);
        $grid->column('created_at');
//        $grid->column('updated_at')->sortable();

        $grid->filter(function (Grid\Filter $filter) {
            $filter->equal('id');
            $filter->like('nickname','昵称');
            $filter->equal('type','类型')->select(['1'=>'用户','2'=>'动态']);
            $filter->equal('status','状态')->select(['0'=>'未处理','1'=>'已处理']);
        });

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

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

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

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