<?php

    /**
     * 状态码说明
     * 200 成功
     * 201 未登录
     * 202 失败
     * 203 空值
     * 204 无结果
     */

	// 页面编码
	header("Content-type:application/json");
	
	// 判断登录状态
    session_start();
    if(isset($_SESSION["yinliubao"])){
        
        // 已登录
        // 接收参数
    	@$page = $_GET['p']?$_GET['p']:1;
    	$chatroom_id = $_GET['chatroom_id'];
    	
        // 当前登录的用户
        $LoginUser = $_SESSION["yinliubao"];
        
        // 数据库配置
    	include '../../../../Db.php';
        
        // 实例化类
    	$db = new DB_API($config);
    	
    	// 验证操作权限
        $checkAdmin = $db->set_table('huoma_user')->find(['user_name' => $_SESSION["yinliubao"]]);
        $user_admin = $checkAdmin['user_admin'];
        
        if($user_admin == 2) {
            
            $result = array(
                'code' => 206,
                'msg' => '无操作权限'
            );
            echo json_encode($result,JSON_UNESCAPED_UNICODE);
            exit;
        }
    	
    	// 获取当前登录用户
        // 创建的总数
        if($chatroom_id && $chatroom_id !== 'undefined') {
            
            // 获取当前登录用户当前chatroom_id的所有订单
            $getCondition = ['Action_User'=>$LoginUser, 'chatroom_id'=>$chatroom_id];

            // 获取已支付的总订单数
            $isPayCount = $db->set_table('ylb_ChatroomPayOrders')->getCount(
                ['chatroom_id' => $chatroom_id,'order_status' => 1]
            );
            
            // 获取未支付的总订单数
            $unPayCount = $db->set_table('ylb_ChatroomPayOrders')->getCount(
                ['chatroom_id' => $chatroom_id,'order_status' => 2]
            );
            
            // 更新已支付和未支付的订单数
            $updatePayCount = 'UPDATE ylb_ChatroomPay SET chatroom_isPay="'.$isPayCount.'",chatroom_unPay="'.$unPayCount.'" WHERE chatroom_id="'.$chatroom_id.'"';
            $db->set_table('ylb_ChatroomPay')->findSql($updatePayCount);
            
        }else {
            
            // 获取当前登录用户的所有订单
            $getCondition = ['Action_User'=>$LoginUser];
        }
    	$allNum = $db->set_table('ylb_ChatroomPayOrders')->getCount($getCondition);
    
    	// 每页数量
    	$lenght = 12;
    
    	// 每页第一行
    	$offset = ($page-1)*$lenght;
    
    	// 总页码
    	$allpage = ceil($allNum/$lenght);
    
    	// 上一页     
    	$prepage = $page-1;
    	if($page == 1){
    		$prepage=1;
    	}
    
    	// 下一页
    	$nextpage = $page+1;
    	if($page == $allpage){
    		$nextpage=$allpage;
    	}
    
    	// 获取当前登录用户创建的
        // 每页12个，DESC排序
    	$getOrderList = $db->set_table('ylb_ChatroomPayOrders')->findAll(
    	    $conditions = $getCondition,
    	    $order = 'ID DESC',
    	    $fields = null,
    	    $limit = ''.$offset.','.$lenght.''
    	);
    	
        // 判断获取结果
    	if($getOrderList){
    	    
    	    // 验证操作权限
            $checkAdmin = $db->set_table('huoma_user')->find(['user_name' => $_SESSION["yinliubao"]]);
            $user_admin = $checkAdmin['user_admin'];
            
            if($user_admin == 1) {
                
                // 获取成功
        		$result = array(
        		    'orderList' => $getOrderList,
        		    'prepage' => $prepage,
        		    'nextpage' => $nextpage,
        		    'allpage' => $allpage,
        		    'page' => $page,
        		    'code' => 200,
        		    'msg' => '获取成功'
        		);
            }else {
                
                // 无权限
        		$result = array(
        		    'code' => 200,
        		    'msg' => '无权限'
        		);
            }
    	}else{
    	    
    	    // 获取失败
            $result = array(
                'code' => 204,
                'msg' => '暂无订单'
            );
    	}
    }else{
        
        // 未登录
        $result = array(
			'code' => 201,
            'msg' => '未登录'
		);
    }

	// 输出JSON
	echo json_encode($result, JSON_UNESCAPED_UNICODE);
	
?>