<?php

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

	// 页面编码
	header("Content-type:application/json");
	
	// 判断登录状态
    session_start();
    if(isset($_SESSION["yinliubao"])){
        
        // 已登录
        // 接收参数
    	@$chatroom_id = $_GET['chatroom_id'];
    	
        // 当前登录的用户
        $LoginUser = $_SESSION["yinliubao"];
        
        // 数据库配置
    	include '../../../../Db.php';
        
        // 实例化类
    	$db = new DB_API($config);
    	
    	// 验证当前登录用户是否有权限
        $checkUser = $db->set_table('huoma_user')->find(['user_name' => $LoginUser]);
        $user_admin = $checkUser['user_admin'];
        
        if($user_admin == 2) {
            
            // 无权限
            $result = array(
			    'code' => 202,
                'msg' => '无操作权限'
		    );
		    echo json_encode($result, JSON_UNESCAPED_UNICODE);
		    exit;
        }
        
        if($chatroom_id && !empty($chatroom_id &&  $chatroom_id !== 'undefined')) {
            
            // 如果有chatroom_id则删除当前chatroom_id的所有订单
            $db->set_table('ylb_ChatroomPayOrders')->delete(['chatroom_id' => $chatroom_id]);
            
            // 验证清空的结果
            $clean_result = $db->set_table('ylb_ChatroomPayOrders')->find(['chatroom_id' => $chatroom_id]);
        }else {
            
            // 否则清空整个订单表
            $db->set_table('ylb_ChatroomPayOrders')->findSql('TRUNCATE TABLE ylb_ChatroomPayOrders');
            
            // 验证清空的结果
            $clean_result = $db->set_table('ylb_ChatroomPayOrders')->find(['Action_User' => $LoginUser]);
        }
        
        // 处理结果
    	if(!$clean_result){
    	    
    	    // 处理成功
    		$result = array(
    		    'code' => 200,
    		    'msg' => '已清空'
    		);
    	}else{
    	    
    	    // 失败
            $result = array(
                'code' => 202,
                'msg' => '清空失败'
            );
    	}
    
    }else{
        
        // 未登录
        $result = array(
			'code' => 201,
            'msg' => '未登录'
		);
    }

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