<?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 plugin\wechat\admin\controller;
use \temmoku\db;
use \temmoku\controller;
use \temmoku\lib\pages;
use \plugin\wechat\lib\common;
class chatrecord extends controller{
	
	public function record(){
		$rows='20';
		!isset($_GET['page']) && $_GET['page']=1;
		$WHERE='1';
		$URL="";
		if($_GET['type']){
			$URL="type_".$_GET['type']."/";
			$WHERE.=" AND msgtype ='$_GET[type]'";
		}
		
		if($_GET['startime']){
			$star=strtotime($_GET['startime']);
			if($_GET['endtime']) {
				$URL.="startime_$_GET[startime]/endtime_$_GET[endtime]";
				$end = strtotime($_GET['endtime']." 23:59:59") ;
			}else{
				$URL.="startime_$_GET[startime]";
				$end=time();
			}
			$WHERE.=" AND posttime > $star AND posttime < $end";
			//防止同时存在
			unset($_GET['day']);
		}
		
		if($_GET['day']){
			if($_GET['day']=='1'){
				$star=strtotime(date('Y-m-d'));
				$end=time();
			}elseif($_GET['day']=='2'){
				$zt=strtotime("-1 day");
				$star = strtotime(date("Y-m-d",$zt));
				$end = strtotime(date("Y-m-d 23:59:59",$zt));
			}elseif($_GET['day']=='3'){
				$zt=strtotime("-2 day");
				$star = strtotime(date("Y-m-d",$zt));
				$end = strtotime(date("Y-m-d 23:59:59",$zt));
			}elseif($_GET['day']=='7'){
				$zt=strtotime("-6 day");
				$star = strtotime(date("Y-m-d",$zt));
				$end = time();
			}
			$URL.="day_".$_GET['day'];
			$WHERE.=" AND posttime > $star AND posttime < $end";
		}
		
		
		$dataDB=db::select('*')->from(jab."plugin_wechat_record")->where($WHERE)->orderByDESC(['id'])->setPaging($rows)->page($_GET['page'])->query();
		$Get_Page=(new Pages)->Pages(P_jab."record",$WHERE,PLUGIN_ACTION."chatrecord/record/".$URL."/",$rows,$_GET['page']);
		$this->assign('dataDB', $dataDB);
		$this->assign('Get_Page', $Get_Page);
		$this->display(tpl('chat/record'));
	}
	
	public function reply_send(){
		$rows=db::select('*')->from(jab.'plugin_wechat_record')->where("id='$_POST[id]'")->row();
		if($rows['is_reply']=='1'){
			$arr=[
				'state'=>'0',	
				'text'=>"已经回复过，请勿在此回复"
			];
			echo json_encode($arr);
			exit;
		}
		$time=time()-$rows['posttime'];
		$time=floor($time/3600);
		if($time>48){
			$arr=[
				'state'=>'0',
				'text'=>"此消息一经超过了48小时，不允许回复"
			];
			echo json_encode($arr);
			exit;
		}
		$data=(new common)->message_custom_send($_POST['openid'],$_POST);
		$sqlDB=[
				'is_read'=>'1',
				'is_reply'=>'1',
				'reply_content'=>$_POST['content'],
				'reply_state_code'=>$data['errcode'],
				'reply_username'=>C('mydb.username'),
				'reply_uid'=>C('mydb.uid'),
				'reply_time'=>time()
			];
		db::update(jab.'plugin_wechat_record')->cols($sqlDB)->where("id='$_POST[id]'")->query();
		if($data['errcode']=='0'){
			$arr=[
				'state'=>'1',	
				'text'=>"发送成功"
			];
		}else{
			$arr=[
				'state'=>'0',
				'text'=>C('wechat|errcode.'.$data['errcode'])
			];
		}
		echo json_encode($arr);
	}
	//向用户发送输入状态
	public function message_custom_typing(){
		$DATA=(new common)->message_custom_typing($_POST['openid']);
		echo json_decode($DATA);
	}
	
	//删除用户发送的微信消息
	public function del(){
		if($_GET['id']){
			db::delete(jab.'plugin_wechat_record')->where("id='$_GET[id]'")->query();
			$this->success('删除信息成功');
		}else{
			foreach($_POST['iddb'] AS $id){
				db::delete(jab.'plugin_wechat_record')->where("id='$id'")->query();
			}
			$this->success('批量删除信息成功');
		}
	}
	
	public function wxdown(){
		$access_token=(new common)->get_token();
		$url="http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=".$access_token."&media_id=".$_GET['mediaid'];
		
		header("location:".$url);
	}
	
}
?>