<?php

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

    // 页面编码
    header("Content-type:application/json");
    
    // 判断登录状态
    session_start();
    if(isset($_SESSION["yinliubao"])){
        
        // 已登录
        $chatroom_id = trim($_POST['chatroom_id']);
        $chatroom_name = trim($_POST['chatroom_name']);
        $chatroom_amount = trim($_POST['chatroom_amount']);
        $chatroom_leader_wxid = trim($_POST['chatroom_leader_wxid']);
        $chatroom_qrcode = trim($_POST['chatroom_qrcode']);
        $chatroom_desc = trim($_POST['chatroom_desc']);
        $chatroom_domain = trim($_POST['chatroom_domain']);
        $chatroom_template = trim($_POST['chatroom_template']);
        $chatroom_memberNum = trim($_POST['chatroom_memberNum']);
        $chatroom_create_user = trim($_SESSION["yinliubao"]);
        
        // 过滤参数
        if(empty($chatroom_name) || !isset($chatroom_name)){
            
            $result = array(
                'code' => 203,
                'msg' => '群名称未填写'
            );
        }else if(empty($chatroom_amount) || !isset($chatroom_amount)){
            
            $result = array(
                'code' => 203,
                'msg' => '进群价格未填写'
            );
        }else if(empty($chatroom_qrcode) || !isset($chatroom_qrcode)){
            
            $result = array(
                'code' => 203,
                'msg' => '群二维码未上传'
            );
        }else if(empty($chatroom_domain) || !isset($chatroom_domain)){
            
            $result = array(
                'code' => 203,
                'msg' => '请选择一个域名'
            );
        }else if(empty($chatroom_template) || !isset($chatroom_template)){
            
            $result = array(
                'code' => 203,
                'msg' => '请选择一个页面模板'
            );
        }else if($chatroom_memberNum > 500){
            
            $result = array(
                'code' => 203,
                'msg' => '群人数最大值是500'
            );
        }else 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的发布者是否为当前登录的用户
            $getCreatUser = $db->set_table('ylb_ChatroomPay')->find(['chatroom_id' => $chatroom_id]);
            $chatroom_create_user = $getCreatUser['chatroom_create_user'];
            
            // 用户一致：允许操作
            if($chatroom_create_user == $LoginUser){
                
                // 需更新的字段
                $updateData = [
                    'chatroom_name' => $chatroom_name,
                    'chatroom_amount' => $chatroom_amount,
                    'chatroom_leader_wxid' => $chatroom_leader_wxid,
                    'chatroom_qrcode' => $chatroom_qrcode,
                    'chatroom_domain' => $chatroom_domain,
                    'chatroom_desc' => $chatroom_desc,
                    'chatroom_memberNum' => $chatroom_memberNum,
                    'chatroom_template' => $chatroom_template,
                ];
                
                // 更新条件
                $updateCondition = [
                    'chatroom_id' => $chatroom_id,
                    'chatroom_create_user' => $LoginUser
                ];
                
                // 执行更新
                $updateSQL = $db->set_table('ylb_ChatroomPay')->update($updateCondition,$updateData);
                if($updateSQL){
                    
                    // 更新成功
                    $result = array(
                        'code' => 200,
                        '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);
    
?>