<?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 \user\model\sms AS model;
use	\temmoku\lib\sms AS add_sms ;
use	\temmoku\lib\pages;
class sms extends user{
	public function index(){
		$rows='20';
		!isset($_GET['page']) && $_GET['page']=1;
		$WHERE='';
		if($_GET['read']==='2' || $_GET['read']==='1'){
			$url="read/".$_GET['read'];
			if($_GET['read']==='2'){
				$_GET['read']=0;
			}
			$WHERE="is_read=".$_GET['read'].' and ';
		}
		$WHERE.="uid=".C('MYDB.uid');
		$data=db::select('*')->from(jab."station_message")->where($WHERE)->orderByDESC(['id'])->setPaging($rows)->page($_GET['page'])->query();
		$Get_Page=(new pages)->Pages(jab."station_message",$WHERE,"",$rows,$_GET['page']);
		$Get_Page = preg_replace_callback('/page_([0-9]+)/',function ($matches) {return "/user/sms/index/{$url}page/".$matches[1];}, $Get_Page );
		$this->assign('data',$data);
		$this->assign('Get_Page',$Get_Page);
		$this->display(tpl('sms/index'));
	}
	public function read(){
		hook_listen('user_sms_read_begin',['id'=>$_POST['id']]);
		if(!$_POST['id']){
			$arr=['code'=>'1','content'=>'缺少必要参数'];
			echo json_encode($arr);
			exit;
		}
		$data=db::select('*')->from(jab."station_message")->where('id='.$_POST['id'].' AND uid='.C('MYDB.uid'))->row();
		if(!$data){
			$arr=['code'=>'501','content'=>'不存在的数据'];
			echo json_encode($arr);
			exit;
		}
		if(!$data['is_read']){
			db::query("UPDATE `".jab.'user'."` SET `message_num` = `message_num`-1 WHERE uid=".C('MYDB.uid'));
			db::update(jab."station_message")->cols(['is_read'=>'1'])->where('id='.$_POST['id'])->query();
		}
		$data['posttime']=date('Y-m-d H:i:s',$data['posttime']);
		$arr=['code'=>'0','data'=>$data];
		hook_listen('user_sms_read_end',['id'=>$_POST['id']]);
		echo json_encode($arr);
	}
	public function del(){
		if(!$_POST['id'] && !$_POST['iddb']){
			$arr=['code'=>'1','content'=>'缺少必要参数'];
			echo json_encode($arr);
			exit;
		}
		$is_array=false;
		if(is_array($_POST['iddb'])){
			$is_array=true;
			$_POST['id'] =implode(',',$_POST['iddb']);
		}

		$data=db::select('*')->from(jab."station_message")->where('id IN ('.$_POST['id'].') AND uid='.C('MYDB.uid'))->query();
		if(!$data){
			$arr=['code'=>'501','content'=>'不存在的数据'];
			echo json_encode($arr);
			exit;
		}
		//批量删除需要进行验证下，不是自己的不能删除
		
		foreach($data as $row){
			if($row['is_read']==0){
				db::query("UPDATE `".jab.'user'."` SET `message_num` = `message_num`-1 WHERE uid=".C('mydb.uid'));
			}
			db::delete(jab."station_message")->where('id IN ('.$row['id'].')')->query();
		}
		$this->Success('短消息删除成功');
	}
	public function post(){
		hook_listen('user_sms_post_begin');
		if(!$_POST['title']){
			$arr=['code'=>'40','text'=>'标题不可以为空'];
			echo json_encode($arr);
			exit;
		}
		if(!$_POST['content']){
			$arr=['code'=>'41','text'=>'内容不可以为空'];
			echo json_encode($arr);
			exit;
		}
		if(!$_POST['username']){
			$arr=['code'=>'43','text'=>'收件人不可为空'];
			echo json_encode($arr);
			exit;
		}
		if($_POST['username']===C('MYDB.username') || $_POST['uid'] === C('MYDB.uid') ){
			$arr=['code'=>'44','text'=>'不可给自己发信息'];
			echo json_encode($arr);
			exit;
		}
		
		if($_POST['uid']){
			$data=db::select('*')->from(jab."user")->where('uid='.$_POST['uid'])->row();
			if(!$data){
				$arr=['code'=>'20','text'=>'用户不存在，请查证后再试'];
				echo json_encode($arr);
				exit;
			}
		}else{
			$data=db::select('*')->from(jab."user")->where('username LIKE "'.$_POST['username'].'"')->row();
			if(!$data){
				$arr=['code'=>'20','text'=>'用户不存在，请查证后再试'];
				echo json_encode($arr);
				exit;
			}
			$_POST['uid']=$data['uid'];
		}
		$id=add_sms::station_message_post(C('MYDB.uid'),$_POST['uid'],$_POST['title'],$_POST['content']);
		hook_listen('user_sms_post_end',['id'=>$id]);
		$this->success('发送成功');
	}
}
?>
