<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
/**
 * 举报
 */
class Report extends Api
{
    protected $noNeedLogin = ['typelist'];
    protected $noNeedRight = ['*'];

    //类型列表
    public function typelist(){
        $list = Db::name('report_type')->order('id asc')->select();
        $this->success('success',$list);
    }

    //新增举报
    public function addone(){
        $user_id = input('user_id', 0, 'intval');
        $type_id = input('type_id', 0, 'intval');
        $content = input('content', '', 'trim');
        $images  = input('images', '', 'trim');

        if (!$user_id) {
            $this->error('您的网络开小差啦~');
        }
        if ($this->auth->id == $user_id) {
            $this->error('您不能举报自己~');
        }
        $user_info = Db::name('user')->find($user_id);
        if (!$user_info) {
            $this->error('您的网络开小差啦~');
        }
        if (!$type_id) {
            $this->error('请选择举报类型');
        }
        $count = Db::name('report_type')->where(['id' => $type_id])->count('id');
        if (!$count) {
            $this->error('举报类型不存在');
        }
        if(empty($content)){
            $this->error('内容不能为空');
        }
        if (iconv_strlen($content, 'utf-8') > 500) {
            $this->error('内容最多500字');
        }
        if(!empty($images) && strpos($images,',')){
            if(count(explode(',',$images)) > 3){
                $this->error('一次最多只能上传3张图片');
            }
        }

        $data = [
            'user_id' => $this->auth->id,
            'type_id' => $type_id,
            'other_id' => $user_id,
            'content' => $content,
            'images' => $images,
            'createtime' => time(),
            'updatetime' => time(),
        ];

        $id = Db::name('report')->insertGetId($data);
        if (!$id) {
            $this->error('您的网络开小差啦~');
        }

        $this->success('举报成功');
    }

    //举报列表
    public function reportlist() {
        $type = input('type', 0, 'intval'); //类型:0我举报的  1被人举报
        $where = [];
        if ($type == 1) {
            $where['other_id'] = $this->auth->id;
        } else {
            $where['user_id'] = $this->auth->id;
        }

        $list = Db::name('report')->where($where)->order('id desc')->autopage()->select();
        if (!$list) {
            $this->success('success', $list);
        }

        $list = list_domain_image($list,['images']);
        $mt_user = Db::name('user');
        $mt_report_type = Db::name('report_type');
        foreach ($list as &$v) {
            if ($type == 0) {
                $nickname = $mt_user->where(['id' => $v['other_id']])->value('nickname');
                $v['nickname'] = $nickname;
            } else {
                $nickname = $mt_user->where(['id' => $v['user_id']])->value('nickname');
                $v['nickname'] = str_replace(mb_substr($nickname, 1), '****', $nickname);
            }

            $v['type_name'] = $mt_report_type->where(['id' => $v['type_id']])->value('name');
            $v['createtime'] = date('Y.m.d H:i');
        }

        $this->success('success',$list);
    }
}