<?php

namespace App\Admin\Controllers;

use App\Models\DynamicModel;
use App\Models\DynamicTag;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\DB;

class DynamicController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new DynamicModel());
        $grid->model()->with(['user','user_info'])->orderBy('id','desc');

        $grid->column('id')->sortable();
        $grid->column('user_info.avatar','头像')->image("",50);;
        $grid->column('user_id','用户')->display(function (){
            if(isset($this->user_info->nickname)&&isset($this->user->mobile)){
                return $this->user->mobile.'<br>'.$this->user_info->nickname;
            }else{
                return "";
            }

        });
        $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->tableCollapse(false);
        $grid->column('content','动态内容')->limit(30);
        $grid->column('zan_num');
        $grid->column('city','所在位置');
        $grid->column('status','状态')->switch();
        $grid->column('tag')->display(function ($res){
            $tag = DB::table("dynamic_tag")->whereIn('id',explode(',',$res))->get();
            $str = '<div>';
            foreach ($tag as $k=>$v){
                $str .= '<span class="label" style="background:#21b978;margin-right: 5px">'.$v->title.'</span>';
            }
            $str .= '</div>';
            return $str;
        });
        $grid->column('created_at','发布时间');

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

        $grid->disableCreateButton();

        $grid->filter(function (Grid\Filter $filter) {
            $filter->equal('user_id','用户ID');
            $filter->like('user_info.nickname','用户昵称');
            $filter->like('user.mobile','手机号');
            $filter->like('content','内容');
            $filter->between('created_at', '发布时间')->datetime();

            $tag = DynamicTag::query()->get();
            $tag_arr = array();
            foreach ($tag as $v){
                $tag_arr[$v['id']] = $v['title'];
            }
            $filter->like('tag', '标签')->select($tag_arr);
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {

        return Show::make($id, new DynamicModel(), function (Show $show) {
            $show->field('id');
            $show->field('user_id','用户ID');
            $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('content')->unescape();
//            $show->field('status');
            $show->field('zan_num');
            $show->html(function ($res){
                $str = "";
                $tag = DB::table("dynamic_tag")->whereIn('id',[$res['tag']])->get();

                if(count($tag)>0){
                    foreach ($tag as $k=>$v){
                        $str .= '<span class="label" style="background:#21b978;margin-right: 5px">'.$v->title.'</span>';
                    }
                }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('city','城市');
//            $show->field('latitude','纬度');
//            $show->field('longitude','经度');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new DynamicModel(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('img_url');
            $form->switch('status')->default(1);
            $form->text('zan_num');
            $form->text('tag');
        });
    }
}
