<?php
// +----------------------------------------------------------------------
// | 微版本 [ windsns框架系统]
// +----------------------------------------------------------------------
// | Copyright (c) 2010~2028 http://www.windsns.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.weinuo.net ) 常州微诺信息科技有限公司 ● 微版本开发团队
// +----------------------------------------------------------------------
// | Author: liu21st <3339882530@qq.com>
// +----------------------------------------------------------------------
// | frame: windsns框架；版权所有，侵权必究！
// +----------------------------------------------------------------------
namespace app\m\controller;

use app\common\controller\Frontend;
use think\Hook;
use think\Db;
use think\Request;
use think\Session;

class Circle extends Frontend
{

    protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'third'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }

    public function index()
    {
    	$this->view->assign('title', __('圈子'));

        $circle_grouplist = Db::name('circle_group')-> where(['circle_state'=>1,'circle_open_state'=>1])-> order('wscircleid', 'desc')->paginate(10); //圈子列表
        $page = $circle_grouplist->render();
    	$this->assign('circle_grouplist', $circle_grouplist);

        return $this->view->fetch();
    }




    public function show()
    {
    	$this->view->assign('title', __('圈子频道'));

        $getcirclestateype  =  input('get.stateype'); 
    	$getcircleid  =  input('get.circleid'); 
        $sqtypeid   = Db::name('circle_group')->where(['wscircleid'=>$getcircleid])->find();
        $this->assign('sqtypeid', $sqtypeid);

    	$sqkaiqiid = Db::name('circle_group')->where(['circle_open_state'=>$getcirclestateype])->find();


        $windsns_contentlunsum = Db::name('circlecontent')
            ->where('circleid', $getcircleid)
            ->count(); 
           $this->assign('windsns_contentlunsum', $windsns_contentlunsum);

	    $windsns_circlegroup = Db::name('circle_group')->where(['wscircleid'=>$getcircleid])->select();
        $this->assign('windsns_circlegroup', $windsns_circlegroup);
        $circlecontentlists = Db::name('circlecontent')->alias('a')->join('ws_circle_group w','a.circleid = w.wscircleid')-> join('ws_user b','a.user_id = b.id')-> where(['circleid'=>$getcircleid,'ws_circlecontent.circle_state'=>1])-> order('aid', 'desc')->paginate(50,false,['query' => request()->param()]); //帖子列表
        $page = $circlecontentlists->render();
    	$this->assign('circlecontentlists', $circlecontentlists);
        $this->assign('page', $page);

        if(!$sqkaiqiid['circle_open_state'] == 1){

    	echo'抱歉！该圈子已经关闭！';
    	exit;
        }elseif(!$sqtypeid){

        	echo'抱歉！该圈子不存在！';
        	exit;
        }elseif($windsns_contentlunsum > 1 or $windsns_contentlunsum == 1){

        return $this->view->fetch('windsns_circle');

        }else{
        	
        	return $this->view->fetch('windsns_nocircle');
        }


    }



 public function content()
    {


        $getcircleid  =  input('get.circleid'); 
        $getcontentid  =  input('get.contentid'); 
        $this->assign('getcontentid', $getcontentid); 
        $this->assign('getcircleid', $getcircleid); 
        $sqtypeid   = Db::name('circle_group')->where(['wscircleid'=>$getcircleid])->find();
        $this->assign('sqtypeid', $sqtypeid);
        $sqconteninfos   = Db::name('circlecontent')->where(['aid'=>$getcontentid])->find();
        $this->assign('sqconteninfos', $sqconteninfos);
        $tiezilittle = $sqconteninfos['subject'];
        $this->view->assign('title', __($tiezilittle));
		$huihuauserid = $this->auth->getUserinfo()['user_id'];

        if(!$sqconteninfos){

        	echo'抱歉！该帖子不存在！';
        	exit;
        }elseif(!$sqtypeid){

        	echo'抱歉！该圈子不存在！';
        	exit;
        }else{


         $windsns_contentinfos = Db::name('circlecontent')->alias('a')-> join('ws_user b','a.user_id = b.id')-> where(['circleid'=>$getcircleid,'ws_circlecontent.circle_state'=>1,'ws_circlecontent.aid'=>$getcontentid])-> select(); //帖子内容打印

            $tiezihuandnegsum = Db::name('circleattachment') ->where('aid', $getcontentid)-> count();
			$this->assign('tiezihuandnegsum', $tiezihuandnegsum);          
			          
			          
			$slides = Db::name('circleattachment')
			          ->where('aid', $getcontentid)
			          ->where('fileId', 'like', 'WU_FILE%')
			          ->select();
           $this->assign('slides', $slides);

		$content = Db::name('circlecontent') -> where('aid', $getcontentid) -> find();
		$content = htmlspecialchars_decode($content['content']);;
		$this->assign('content', $content);
		$this->assign('windsns_contentinfos', $windsns_contentinfos);
        $sqlliulan = Db::name('circlecontent')->where('aid',$getcontentid)->setInc('circle_infosliulanshu', 1);
        $tiezi_userid = Db::name('circlecontent') -> where('aid', $getcontentid) ->value('user_id');
        $thistieziuserid   = Db::name('user')-> where('id',$tiezi_userid)->select();
        $this->assign('thistieziuserid', $thistieziuserid);//赋值到页面
        $tiezisum = Db::name('circlecontent')->where(['user_id' => $tiezi_userid,'circle_state' => 1])->count();
  	    $this->assign('tiezisum', $tiezisum);
        $user_content = Db::name('circlecontent') -> where('user_id', $tiezi_userid) -> order('aid', 'desc')->paginate(10); 
        $page = $user_content->render();
    	$this->assign('user_content', $user_content);
        $this->assign('page', $page);

		$mysqlReplies = Db::name('circlehuifu')
		    ->where('contentid', $getcontentid)
		    ->alias('a')-> join('ws_user b','a.user_id = b.id')
		    ->order('circlehuifu_id', 'asc')
		    ->select();
		$replies = [];
		foreach ($mysqlReplies as $row) {
		    $key = $row['circlehuifu_id'];
		    if ($row['user_fid'] == 0) {
		        $replies[$key] = $row;
		    } else {
		        $replies[$row['user_fid']]['floor'][$key] = $row;
		    }
		}
		$this->assign('replies', $replies);
          if($tiezi_userid == $huihuauserid){ 

           return $this->view->fetch('windsns_usercontent');
       }elseif($tiezihuandnegsum > 1 or $tiezihuandnegsum == 1){
	        	return $this->view->fetch('windsns_content');
	        }else{
	        	return $this->view->fetch('windsns_contentnopic');
	        }

         
        }

    }



     
    public function user_deldo() 
    {
        $gettieziid  =  input('get.circletiezi'); 
        $tiezi_circleid = Db::name('circlecontent') -> where('aid', $gettieziid) ->value('circleid');
         
  
       $shanchua = Db::name('circlecontent')->where('aid',$gettieziid)->delete(); 
       $shanchub = Db::name('circleattachment')->where('aid',$gettieziid)->delete(); 
       $shanchuc = Db::name('circlehuifu')->where('contentid',$gettieziid)->delete(); 
    
         if($shanchua){
               return $this->view->fetch('windsns_del_ok');
         }else{
               return $this->view->fetch('windsns_del_no');
			exit;
         }


}
















}
