<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
    <title>站点设置</title>
    <!-- 引入 WeUI -->
    <link rel="stylesheet" href="https://res.wx.qq.com/open/libs/weui/1.1.2/weui.min.css"/>
    <link rel="stylesheet" href="https://cdn.bootcss.com/jquery-weui/1.2.0/css/jquery-weui.css"/>
    <style type="text/css">
        body, html {
            height: 100%;
            background-color: #f4f4f4;
            -webkit-tap-highlight-color: transparent;
        }

        .basic-color {
            background-color: #1E88E5;
            color: #FFFFFF;
        }

        .basic-weui-btn {
            background-color: #1E88E5;
            color: #FFFFFF;
        }

        .basic-weui-btn:hover {
            background-color: #1E88E5;
            color: #FFFFFF;
        }

        .basic-weui-btn:active {
            background-color: #1E88E5 !important;
            color: #FFFFFF;
        }

        footer {
            text-align: center;
            font-size: 14px;
            padding: 20px;
        }

        .weui-loadmore_line .weui-loadmore__tips {
            background-color: #f4f4f4;
        }

        footer a {
            color: #999;
            text-decoration: none;
        }

    </style>
</head>

<body ontouchstart="">
	<div class="weui-cells__title weui-cell" style="margin-top: 0px;">
	    站点基本设置<a href="https://github.com/akaxincom/faq/blob/master/basic_config.md" class="weui-cell_link">-设置说明</a>
	</div>
	<div class="weui-cells weui-cells_form">
	    <div class="weui-cell">
	        <div class="weui-cell__hd">
	            <label class="weui-label">站点名称</label>
	        </div>
	        <div class="weui-cell__bd">
	            <input class="weui-input site-name" name='site_name' type="text" th:value="${site_name}"
	                   placeholder="请输入站点名称" pattern="[^'\x22]+" style="text-align: right;">
	        </div>
	    </div>
	    <div class="weui-cell">
	        <div class="weui-cell__hd">
	            <label class="weui-label">站点地址</label>
	        </div>
	        <div class="weui-cell__bd">
	            <input class="weui-input site-addr site_address_info" type="text"
	                   th:value="${siteAddressAndPort}" placeholder="请输入站点地址" autocapitalize="off"
	                   pattern="[a-zA-Z0-9:：]*" style="text-align: right;">
	        </div>
	    </div>
	    <div class="weui-cell">
	        <div class="weui-cell__hd">
	            <label class="weui-label">扩展API地址</label>
	        </div>
	        <div class="weui-cell__bd">
	            <input class="weui-input site-port" type="text"
	                   th:value="${httpAddressAndPort}" placeholder="InnerAPI服务器地址"
	                   style="text-align: right;" disabled>
	        </div>
	    </div>
	    <div class="weui-cell">
	        <div class="weui-cell__hd">
	            <label class="weui-label">群成员限制</label>
	        </div>
	        <div class="weui-cell__bd">
	            <input class="weui-input site-group-maxnum" type="number" th:value="${group_members_count}"
	                   pattern="[0-9]*" placeholder="请输入群成员总量限制" style="text-align: right;">
	        </div>
	    </div>
	    <div class="weui-cell">
	        <div class="weui-cell__hd">
	            <label class="weui-label">站点根目录</label>
	        </div>
	        <div class="weui-cell__bd">
	            <input class="weui-input site-img-addr" type="text" name='pic_path' th:value="${pic_path}"
	                   placeholder="请输入站点根目录" style="text-align: right;" disabled>
	        </div>
	    </div>
	    <div class="weui-gallery" id="gallery">
	        <span class="weui-gallery__img" id="galleryImg"></span>
	        <div class="weui-gallery__opr">
	            <a href="javascript:" class="weui-gallery__del">
	                <i class="weui-icon-delete weui-icon_gallery-delete"></i>
	            </a>
	        </div>
	    </div>
	    <div class="weui-cell">
	        <div class="weui-cell__bd">
	            <div class="weui-uploader">
	                <div class="weui-uploader__hd">
	                    <p class="weui-uploader__title">站点Logo上传</p>
	                    <div class="weui-uploader__info pic_num" th:if="${site_logo}==null">0/1</div>
	                    <div class="weui-uploader__info pic_num" th:unless="${site_logo}==null">1/1</div>
	                </div>
	                <div class="weui-uploader__bd">
	                    <ul class="weui-uploader__files uploaderFiles" id="uploaderFiles">
	                        <li class="weui-uploader__file site_logo imgage_id"
	                            th:attr="image-id=${site_logo},site-logo=${site_logo}" id="site_logo"
	                            th:if="${site_logo}!=null"></li>
	                    </ul>
	                    <div class="weui-uploader__input-box">
	                        <input id="uploaderInput" class="weui-uploader__input uploaderInput" type="file"
	                               accept="image/*" multiple="">
	                    </div>
	                </div>
	            </div>
	        </div>
	    </div>
	</div>
	
	<!-- 登陆站点权限设置 -->
	<div class="weui-cells">
	    <div class="weui-cell weui-cell_switch">
	        <div class="weui-cell__bd">是否开启绝密功能</div>
	        <div class="weui-cell__ft" th:switch="${u2_encryption_status}">
	
	            <input class="weui-switch open_secret" key-data='U2_ENCRYPTION_STATUS' type="checkbox"
	                   checked="checked" th:case="1">
	
	            <input class="weui-switch open_secret" key-data='U2_ENCRYPTION_STATUS' type="checkbox" th:case="0">
	        </div>
	    </div>
	
	    <div class="weui-cell weui-cell_switch">
	        <div class="weui-cell__bd">是否要求实名认证</div>
	        <div class="weui-cell__ft" th:switch="${realName_status}">
	
	            <input class="weui-switch realName_status" key-data='REALNAME_STATUS_VALUE' type="checkbox"
	                   checked="checked" th:case="1">
	
	            <input class="weui-switch realName_status" key-data='REALNAME_STATUS_VALUE' type="checkbox" th:case="0">
	        </div>
	    </div>
	
	    <div class="weui-cell weui-cell_switch">
	        <div class="weui-cell__bd">是否要求邀请码</div>
	        <div class="weui-cell__ft" th:switch="${uic_status}">
	
	            <input class="weui-switch uic_status" key-data='INVITE_CODE_STATUS_VALUE' type="checkbox"
	                   checked="checked" th:case="1">
	
	            <input class="weui-switch uic_status" key-data='INVITE_CODE_STATUS_VALUE' type="checkbox" th:case="0">
	        </div>
	    </div>
	    
	</div>
	
	<div class="weui-cells">
        <div class="weui-cell weui-cell_switch">
            <div class="weui-cell__bd">是否允许用户添加好友</div>
            <div class="weui-cell__ft" th:switch="${add_friends_status}">
                <input class="weui-switch add_friends_status" key-data='ADD_FRIENDS_STATUS' type="checkbox" value="0" checked="checked" th:case="0">
                <input class="weui-switch add_friends_status" key-data='ADD_FRIENDS_STATUS' type="checkbox" value="1" th:case="1">
            </div>
        </div>
        
        <div class="weui-cell weui-cell_switch">
            <div class="weui-cell__bd">是否允许用户创建群组</div>
            <div class="weui-cell__ft" th:switch="${create_groups_status}">
                <input class="weui-switch create_groups_status" key-data='CREATE_GROUPS_STATUS' type="checkbox" value="0" checked="checked" th:case="0">
                <input class="weui-switch create_groups_status" key-data='CREATE_GROUPS_STATUS' type="checkbox" value="1" th:case="1">
            </div>
        </div>
    </div>  

    <!-- 登陆站点权限设置 -->
	<div class="weui-cells">
	   <a class="weui-cell weui-cell_access group_qrcode_expire_time" th:attr="data=${group_qrcode_expire_time}" href="javascript:;">
            <div class="weui-cell__bd">
                <p>群二维码过期时间</p>
            </div>
            <div th:switch="${group_qrcode_expire_time}">
                <div class="weui-cell__ft group_qrcode_expire_time—txt" th:case="1">
                    1天
                </div>
                <div class="weui-cell__ft group_qrcode_expire_time—txt" th:case="7">
                    1周
                </div>
                <div class="weui-cell__ft group_qrcode_expire_time—txt" th:case="14">
                    2周
                </div>
            </div>
        </a>
	    <a class="weui-cell weui-cell_access site_push" th:attr="data=${push_client_status}" href="javascript:;">
	        <div class="weui-cell__bd">
	            <p>消息通知</p>
	        </div>
	        <div th:switch="${push_client_status}">
	            <div class="weui-cell__ft site_push—txt" th:case="0">
	                关闭通知
	            </div>
	            <div class="weui-cell__ft site_push—txt" th:case="1">
	                不显示文本内容
	            </div>
	            <div class="weui-cell__ft site_push—txt" th:case="2">
	                显示文本内容
	            </div>
	        </div>
	    </a>
	    <a class="weui-cell weui-cell_access log_level" th:attr="data=${log_level}" href="javascript:;">
	        <div class="weui-cell__bd">
	            <p>日志级别</p>
	        </div>
	        <div th:switch="${log_level}">
	            <div class="weui-cell__ft log_level—txt" th:case="DEBUG">
	                DEBUG
	            </div>
	            <div class="weui-cell__ft log_level—txt" th:case="*">
	                INFO
	            </div>
	            <div class="weui-cell__ft log_level—txt" th:case="ERROR">
	                ERROR
	            </div>
	        </div>
	    </a>
	</div>
	
	<div class="weui-btn-area">
	    <a class="weui-btn basic-weui-btn weui-btn_primary basic-color" href="javascript:" url-data="/manage/updateConfig"
	       id="setBasicConfigButton">确定</a>
	</div>
	
	<div class="weui-gallery" id="gallery">
	    <span class="weui-gallery__img" id="galleryImg"></span>
	    <div class="weui-gallery__opr">
	        <a href="javascript:" class="weui-gallery__del">
	            <i class="weui-icon-delete weui-icon_gallery-delete"></i>
	        </a>
	    </div>
	</div>
	
<script type="text/javascript" src="https://cdn.bootcss.com/jquery/2.2.4/jquery.js"></script>
<script type="text/javascript" src="https://res.wx.qq.com/open/libs/weuijs/1.1.3/weui.min.js"></script>
<script type="text/javascript" src="https://cdn.bootcss.com/jquery-weui/1.2.0/js/jquery-weui.js"></script>
<script type="text/javascript">
    function getOsType() {
        var u = navigator.userAgent;
        if (u.indexOf('Android') > -1 || u.indexOf('Linux') > -1) {
            return 'Android';
        } else if (u.indexOf('iPhone') > -1) {
            return 'IOS';
        } else {
            return 'PC';
        }
    }

    function reqUrl(reqUri, params, callbackName) {
        reqUri = "/akaxin" + reqUri;
        var type = getOsType();
        if (type == 'Android') {
            Android.requestPost(reqUri, params, callbackName);
        } else if (type == 'IOS') {
            ios_requestPost(reqUri, params, callbackName);
        } else {
            $.toast('暂时不支持该设备');
        }
    }

    function reqHtml(reqUri, params) {
        reqUri = "/akaxin" + reqUri;
        var type = getOsType();
        if (type == 'Android') {
            Android.requestPage(reqUri, params)
        } else if (type == 'IOS') {
            ios_requestPage(reqUri, params);
        } else {
            $.toast('暂时不支持该设备');
        }
    }

    function reqImageUpload(callback) {
        var type = getOsType();
        if (type == 'Android') {
            Android.imageUpload(callback);
        } else if (type == 'IOS') {
            ios_imageUpload(callback);
        } else {
            $.toast('暂时不支持该设备');
        }
    }

    function reqImageDownload(imageid, callback) {
        var type = getOsType();
        if (type == 'Android') {
            Android.imageDownload(imageid, callback);
        } else if (type == 'IOS') {
            ios_imageDownload(imageid, callback);
        } else {
            $.toast('暂时不支持该设备');
        }
    }

    function toast(msg) {
        var type = getOsType();
        if (type == 'Android') {
            Android.showToast(msg);
        } else if (type == 'IOS') {
            $.toast(msg);
        } else {
            $.toast('暂时不支持该设备');
        }
    }

    function isDomain(siteAddress) {
        siteAddress=siteAddress.replace(/\s/g,"");
        if (siteAddress.indexOf("://") > -1) {
            hostname = siteAddress.split('/')[2];
        } else {
            hostname = siteAddress.split('/')[0];
        }
        var domain = hostname.split(':')[0];
        domain = domain.split('?')[0];

        if (domain) {
            if (/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(domain)) {
                return true;
            }

            var re2 = /^(?=^.{3,255}$)[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+$/;
            if (re2.test(domain)) {
                return true;
            }
        }
        return false;
    }

    $(document).on("click", "#setBasicConfigButton", function () {
        var siteName = $('.site-name').val();
        var siteGroupMaxnum = $('.site-group-maxnum').val();
        var siteImgAddr = $('.site-img-addr').val();
        var uic_status = $(".uic_status").prop('checked');
        var realName_status = $(".realName_status").prop('checked');
        var pushClientStatus = $(".site_push").attr("data");
        var logLevel = $(".log_level").attr("data");
        var siteAddressInfo = $('.site_address_info').val();
        var openSecret = $('.open_secret').prop('checked');
        
        var addFriends = $('.add_friends_status').prop('checked');
        var createGroups = $('.create_groups_status').prop('checked');//创建群组
        //二维码过期时间
        var groupQrcodeExpireTime = $(".group_qrcode_expire_time").attr("data");

        if(addFriends){
        	   addFriends = '0';//表示允许
        } else {
        	   addFriends = '1';
        }
        
        if(createGroups){
        	   createGroups = '0';//表示允许
        } else {
        	   createGroups = '1';
        }
	     
        
        if (openSecret) {
            openSecret = '1';
        } else {
            openSecret = '0';
        }

        if (uic_status) {
            uic_status = '1';
        } else {
            uic_status = '0';
        }

        if (realName_status) {
            realName_status = '1';
        } else {
            realName_status = '0';
        }

        var blobImgUrl = $('.blob_img_url').attr('data');
        if (siteName.length < 1) {
            $.toptip('站点名称不能为空', 'error');
            return false;
        }
        var siteAddres = '';
        var sitePort = '';
        var siteAddressInfos = siteAddressInfo.replace('：', ':').split(':');
        if (siteAddressInfos.length > 2) {
            $.toptip('请填写正确的站点地址', 'error');
            return false;
        } else if (siteAddressInfos.length == 2) {
            var siteAddress = siteAddressInfos[0];
            var sitePort = siteAddressInfos[1];
        } else {
            var siteAddress = siteAddressInfos[0];
        }
        var siteAddressFlag = false;
        siteAddressFlag = isDomain(siteAddress);
        if (!siteAddressFlag) {
            $.toptip('请填写正确的站点地址', 'error');
            return false;
        }

        if (siteGroupMaxnum.length < 1) {
            $.toptip('群组最大的成员数量不能为空', 'error');
            return false;
        }

        if (siteImgAddr.length < 1) {
            $.toptip('图片存储地址不能为空', 'error');
            return false;
        }
        var imageId = $('.imgage_id').attr('image-id');
        var data = {
            'site_name': siteName,
            'site_address': siteAddress,
            'site_port': sitePort,
            'group_members_count': siteGroupMaxnum,
            'pic_path': siteImgAddr,
            'site_logo': imageId,
            'realName_status': realName_status,
            'uic_status': uic_status,
            'u2_encryption_status': openSecret,
            'push_client_status': pushClientStatus,
            'log_level': logLevel,
            'add_friends_status':addFriends,
            'create_groups_status':createGroups,//create group
            'group_qrcode_expire_time':groupQrcodeExpireTime
        };
        var reqUri = $(this).attr('url-data');
        $.confirm({
            title: '修改基本设置',
            text: '确定要修改基本设置？',
            onOK: function () {
                reqUrl(reqUri, JSON.stringify(data), "siteBasic");
                $.closeModal();
            },
            onCancel: function () {
                console.log('cancle');
                toast('取消操作');
                $.closeModal();
            }
        });

    });

    function siteBasic(result) {
        if (result == 'success') {
            $.toast("修改成功");
        } else if (result == 'no-permission') {
            $.toast("没有权限,请稍后再试", "forbidden");
        } else {
            $.toast("修改失败,请稍后再试", "forbidden");
        }
    }

    $(document).on("click", '.uploaderInput', function () {
        reqImageUpload("jsimageUpload");
    });

    function jsimageUpload(result, imageId, imageLocalPath) {
        var type = getOsType();
        if (result == 1) {
            if (type == 'Android') {
                var img = '<li class="weui-uploader__file imgage_id" image-id="' + imageId + '"><img src="http://akaxin/img' + imageLocalPath + '" style="height:75px; width:75px;" /></li>';
            } else {
                var img = '<li class="weui-uploader__file imgage_id" image-id="' + imageId + '"><img src="' + imageLocalPath + '" style="height:75px; width:75px;" /></li>';
            }
            $('#uploaderFiles').html(img);
            $('.pic_num').html('1/1');
        } else {
            $.toast('图片上传失败', 'forbidden');
        }
    }

    $(document).on("click", '.log_level', function () {
        $.actions({
            title: "",
            onClose: function () {
                console.log("close");
            },
            actions: [{
                text: "DEBUG",
                className: "color-primary weui-dialog__btn",
                onClick: function () {
                    $(".log_level—txt").html("DEBUG");
                    $(".log_level").attr("data", "DEBUG");
                }
            },
                {
                    text: "INFO",
                    className: "color-warning weui-dialog__btn",
                    onClick: function () {
                        $(".log_level—txt").html("INFO");
                        $(".log_level").attr("data", "INFO");
                    }
                },
                {
                    text: "ERROR",
                    className: 'color-danger weui-dialog__btn',
                    onClick: function () {
                        $(".log_level—txt").html("ERROR");
                        $(".log_level").attr("data", "ERROR");
                    }
                }
            ]
        });
    });


    $(document).on("click", '.site_push', function () {
        $.actions({
            title: "",
            onClose: function () {
                console.log("close");
            },
            actions: [{
                text: "禁用通知",
                className: "color-primary weui-dialog__btn ",
                onClick: function () {
                    $(".site_push—txt").html("禁用通知");
                    $(".site_push").attr("data", "0");
                }
            },

                {
                    text: "显示文本内容",
                    className: 'color-danger weui-dialog__btn',
                    onClick: function () {
                        $(".site_push—txt").html("显示文本内容");
                        $(".site_push").attr("data", "2");
                    }
                },
                {
                    text: "不显示文本内容",
                    className: "color-warning weui-dialog__btn",
                    onClick: function () {
                        $(".site_push—txt").html("不显示文本内容");
                        $(".site_push").attr("data", "1");
                    }
                }
            ]
        });
    });
    
    //group qrcode expire time
    $(document).on("click", '.group_qrcode_expire_time', function () {
        $.actions({
            title: "修改群二维码过期时间",
            onClose: function () {
                console.log("close");
            },
            actions: [{
                text: "1天",
                className: "color-primary weui-dialog__btn",
                onClick: function () {
                    $(".group_qrcode_expire_time—txt").html("1天");
                    $(".group_qrcode_expire_time").attr("data", "1");
                }
            },

                {
                    text: "1周",
                    className: 'color-primary weui-dialog__btn',
                    onClick: function () {
                    	   $(".group_qrcode_expire_time—txt").html("1周");
                        $(".group_qrcode_expire_time").attr("data", "7");
                    }
                },
                {
                    text: "2周",
                    className: "color-primary weui-dialog__btn",
                    onClick: function () {
                        	$(".group_qrcode_expire_time—txt").html("2周");
                        $(".group_qrcode_expire_time").attr("data", "14");
                    }
                }
            ]
        });
    });

    $(window).load(function () {
        var imageid = $('.site_logo').attr('site-logo');
        reqImageDownload(imageid, "jsimageDownload");
    });

    function jsimageDownload(result, imageid, imageLocalPath) {
        var type = getOsType();
        if (type == 'Android') {
            var html = '<img src="http://akaxin/img' + imageLocalPath + '" style="height:75px; width:75px;" />';
        } else {
            var html = '<img src="' + imageLocalPath + '" style="height:75px; width:75px;" />';
        }
        $('#site_logo').html(html);
    }
</script>
</body>

</html>