<?php
/*
 +----------------------------------------------------------------------
 | TEMMOKUMVC [ NO BEST , ONLY BETTER ]
 +----------------------------------------------------------------------
 | Copyright (c) 2018~2019 https://www.temmoku.cn All rights reserved.
 +----------------------------------------------------------------------
 | Author: 张宗强 Email:webmaster@temmoku.cn QQ:158726877 516669373 TEL:17895221001 微信:temmokumvc
 +---------------------------------------------------------------------- 
*/
namespace user\controller;
use \temmoku\user;
use \temmoku\db;
use	\temmoku\lib\pages;
class comment extends user{
	public function index(){
		!isset($_GET['page']) && $_GET['page']=1;
		!isset($_GET['state']) && $_GET['state']='';
		$rows='20';
		if($_GET['state']==='0' || $_GET['state']==='1'){
			$WHERE="state = '$_GET[state]'";
			$url='state/'.$_GET['state'].'/';
		}else{
			$WHERE="1";
		}
		$WHERE.=' AND uid='.C('MYDB.uid');
		$data=db::select('*')->from(jab."comment")->where($WHERE)->orderByDESC(array('id'))->setPaging($rows)->page($_GET['page'])->query();
		$Get_Page=(new pages)->Pages(jab."comment",$WHERE,"",$rows,$_GET['page']);
		$Get_Page = preg_replace_callback('/page_([0-9]+)/',function ($matches) {return "/user/comment/index/page/".$matches[1];}, $Get_Page );
		$this->assign('data', $data);
    	$this->assign('Get_Page', $Get_Page);
		if(DEVICE != 'pc'){
			$this->assign('webname','我的评论');
		}
    	$this->display(tpl('comment/index'));
	}
	
	public function del(){
		
		if($_POST['id']){
			if(!C('comment_del')){
				echo json_encode(['code'=>'505','text'=>'系统设置不允许删除自己的评论']);
				exit;
			}
			//查询要删除的数据是不是自己的
			$data=db::select('*')->from(jab."comment")->where('id='.$_POST['id'] ." AND uid=".C('MYDB.uid'))->row();
			if($data){
				if($data['reply_id']=='0'){
					//更新主表comment的值
					db::query("UPDATE `".jab."article` SET `comment_num` = comment_num-1 WHERE aid='$data[aid]'");
				}else{
					$this->update_down($rows['reply_id']);
				}
				db::delete(jab."comment")->where("id='$_POST[id]'")->query();
				echo json_encode(['code'=>'0','text'=>'删除成功']);
			}else{
				echo json_encode(['code'=>'505','text'=>'不存在的数据或者没有权限']);
			}
		}elseif(is_array($_POST['iddb'])){
			if(!C('comment_del')){
				$this->err('系统设置不允许删除自己的评论');
			}
			$id=implode(',',$_POST['iddb']);
			//查询一下自己的评论，不能胡乱删除
			$data=db::select('*')->from(jab."comment")->where("id IN ($id) AND uid=".C('MYDB.uid'))->query();
			foreach($data AS $row){
				$rows=db::select('*')->from(jab."comment")->where('id='.$row['id'])->row();
				//主评论
				if($rows['reply_id']=='0'){
					//更新主表comment的值
					db::query("UPDATE `".jab."article` SET `comment_num` = comment_num-1 WHERE aid='$rows[aid]'");
				}else{
					$this->update_down($rows['reply_id']);
				}
				db::delete(jab."comment")->where("id='$row[id]'")->query();
			}
			
			$this->success('删除成功');
		}
	}
	private function update_down($id){
		//查询下自己下面的有没有回复。有就上提一级
		$reply=db::delete(jab."comment")->where("reply_id='$id'")->query();
		if($reply){
			foreach($reply as $row){
				//更新下面的评论继承自己的
				db::update(jab."comment")->cols(['reply_id'=>$id])->where('id='.$row['uid'])->query();
			}
		}
	}
}
?>