<?php
    
    /**
     * 状态码说明
     * 200 成功
     * 201 未登录
     * 202 失败
     * 203 空值
     */

	// 页面编码
	header("Content-type:application/json");
	
	// 判断登录状态
    session_start();
    if(isset($_SESSION["yinliubao"])){
        
        // 已登录
        $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{
            
            // 数据库配置
        	include '../../../../Db.php';
        
        	// 实例化类
        	$db = new DB_API($config);
        	
            // 为了确保创建出来的群是可以用的
            // 这里要检查插件配置是否已经完成
            $check_ylb_ChatroomPayConfig = $db->set_table('ylb_ChatroomPayConfig')->find(['id' => 1]);
            $payapi_merchant_id = $check_ylb_ChatroomPayConfig['payapi_merchant_id'];
            $payapi_merchant_key = $check_ylb_ChatroomPayConfig['payapi_merchant_key'];
            $payapi_domain = $check_ylb_ChatroomPayConfig['payapi_domain'];
            
            if(!$payapi_merchant_id || !$payapi_merchant_key || !$payapi_domain) {
                
                $result = array(
                    'code' => 201,
                    'msg' => '请完成插件配置再创建'
                );
                echo json_encode($result,JSON_UNESCAPED_UNICODE);
                exit;
            }
            
            // 验证操作权限
            $checkAdmin = $db->set_table('huoma_user')->find(['user_name' => $_SESSION["yinliubao"]]);
            $user_admin = $checkAdmin['user_admin'];
            
            if($user_admin == 2) {
                
                $result = array(
                    'code' => 203,
                    'msg' => '无操作权限'
                );
                echo json_encode($result,JSON_UNESCAPED_UNICODE);
                exit;
            }
            
            // ID生成
            $chatroom_id = rand(100000,999999);
            
            // 验证这个id是否重复
            $checkId = $db->set_table('ylb_ChatroomPay')->findAll(['chatroom_id' => $chatroom_id]);
            if($checkId) {
                
                // 重新生成
                $chatroom_id = rand(123456,900000);
            }
            
            // 随机获取群头像
            $chatroom_avatar = rand(1,50).'.jpg';

        	// 参数
            $createchatRoom = [
                'chatroom_id' => $chatroom_id,
                '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_template' => $chatroom_template,
                'chatroom_memberNum' => $chatroom_memberNum,
                'chatroom_avatar' => $chatroom_domain.dirname(dirname($_SERVER["REQUEST_URI"])).'/img/avatar/'.$chatroom_avatar,
                'chatroom_create_user' => $chatroom_create_user
            ];

            // 执行SQL
            $createchatRoomSQL = $db->set_table('ylb_ChatroomPay')->add($createchatRoom);
            
            // 执行结果
            if($createchatRoomSQL){
                
                // 成功
                $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);
	
?>