<?php

namespace app\admin\controller;

use think\Db;

/**
 * 举报列表
 */
class Report extends Base
{
    /**
     * 举报列表
     * @return mixed
     */
    public function index()
    {
        $get = $this->request->get();
        $step = 50;
        $page = isset($get['page']) ? intval($get['page']) : 1;

        $items = Db::table('report')->order('id desc')->limit(($page-1)*$step, $step)->select();

        $uids = $userinfos = [];
        foreach ($items as $item) {
            $uids[$item['uid']] = $item['uid'];
            $uids[$item['report_id']] = $item['report_id'];
        }
        if ($uids) {
            $userinfos = Db::table('user')->where('uid', 'in', $uids)->column('uid,avatar,username,nickname,account_state', 'uid');
        }

        foreach ($items as &$item) {
            $uid = $item['uid'];
            $target_uid = $item['report_id'];
            $item['username'] = isset($userinfos[$uid]['username']) ? $userinfos[$uid]['username'] : '用户不存在';
            $item['avatar'] = isset($userinfos[$uid]['avatar']) ? $userinfos[$uid]['avatar'] : '/static/avatar.jpg';

            $item['report_username'] = isset($userinfos[$target_uid]['username']) ? $userinfos[$target_uid]['username'] : '用户不存在';
            $item['report_avatar'] = isset($userinfos[$target_uid]['avatar']) ? $userinfos[$target_uid]['avatar'] : '/static/avatar.jpg';
            $item['report_account_state'] = isset($userinfos[$target_uid]['account_state']) ? $userinfos[$target_uid]['account_state'] : 'deleted';
        }


        $count = Db::table('report')->count('id');
        $this->assign('items', $items);
        $this->assign('count', $count);
        $this->assign('page', $page);
        $this->assign('step', $step);
        $this->assign('section', 'report');

        return $this->fetch();
    }

}
