<?php

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

	// 页面编码
	header("Content-type:application/json");
	
	// 判断登录状态
    session_start();
    if(isset($_SESSION["yinliubao"])){
        
        // 已登录
    	$chatroom_id = trim($_GET['chatroom_id']);
    	
        // 过滤参数
        if(empty($chatroom_id) || !isset($chatroom_id)){
            
            $result = array(
			    'code' => 203,
                'msg' => '非法请求'
		    );
        }else{
            
            // 当前登录的用户
            $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' => 202,
                    'msg' => '无操作权限'
                );
                echo json_encode($result,JSON_UNESCAPED_UNICODE);
                exit;
            }
        	
            // 验证当前要删除的chatroom_id的发布者是否为当前登录的用户
            $getCreateUser = $db->set_table('ylb_ChatroomPay')->find(['chatroom_id' => $chatroom_id]);
            $chatroom_create_user = $getCreateUser['chatroom_create_user'];
            
            // 判断操作结果
            if($chatroom_create_user == $LoginUser){
                
                // 用户一致：允许操作
                $delSQL = $db->set_table('ylb_ChatroomPay')->delete(['chatroom_id' => $chatroom_id]);
                
                // 操作结果
                if($delSQL){
                    
                    // 付费群删除成功
                    // 开始删除当前付费群关联的订单
                    $delOrder = $db->set_table('ylb_ChatroomPayOrders')->delete(['chatroom_id' => $chatroom_id]);
                    if($delOrder) {
                        
                        // 全部删除成功
                        $result = array(
        			        'code' => 200,
                            'msg' => '已删除'
    		            );
                    }else {
                        
                        // 订单删除失败
                        $result = array(
        			        'code' => 202,
                            'msg' => '付费群删除成功，关联的订单删除失败~'
    		            ); 
                    }
                }else{
                    
                    // 删除失败
                    $result = array(
        			    'code' => 202,
                        'msg' => '付费群删除失败'
        		    );
                }
            }else{
                
                // 用户不一致：禁止操作
                $result = array(
        			'code' => 202,
                    'msg' => '删除失败：禁止操作'
        		);
            }
        }
    }else{
        
        // 未登录
        $result = array(
			'code' => 201,
            'msg' => '未登录'
		);
    }

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