<script type="text/javascript">
$(function() {
    $('#qrcode_expire').datetimepicker({
        minView: 'month',
        format: 'yyyy-mm-dd',
        language: 'zh-CN',
        todayBtn: true,
        autoclose: true,
        todayHighlight: true
    });
    
    var $validateForm = $("#validateForm");

    // 表单验证
    $validateForm.validate({
        rules: {
            group_name: {
                required: true
            },
            group_link: {
                required: true
            },
            group_qrcode: {
                required: true
            },
            qrcode_expire: {
                required: true
            },
            sort: {
                required: true
            }
        },
        messages: {
            group_name: {
                required: "请填写微信群名称"
            },
            group_link: {
                required: "请填写微信群链接"
            },
            group_qrcode: {
                required: "请先生成二维码"
            },
            qrcode_expire: {
                required: "请填写二维码有效时间"
            },
            sort: {
                required: "请填写排序"
            }
        }
    });
    
    function preview() {
        var filechooser = document.getElementById('group_logo');
        var previewer = document.getElementById('group_logo_previewer');
        
        // 字节数
        var maxsize = 1024 * 1024;
        
        filechooser.onchange = function() {
            var files = this.files;
            var file = files[0];
        
            // 接受 jpeg, jpg, png 类型的图片
            if (!/\/(?:jpeg|jpg|png|bmp)/i.test(file.type)) {
                filechooser.value = '';
                previewer.src = '';
                $('#group_logo_previewer').hide();
                alert('只能JPG,JPEG,BMP,PNG格式的图片');
                return;
            }
        
            var reader = new FileReader();
            reader.onload = function() {
                var result = this.result;
                if (result.length > maxsize) {
                    filechooser.value = '';
                    previewer.src = '';
                    $('#group_logo_previewer').hide();
                    alert('只能上传小于1M的图片');
                    return;
                }
        
                previewer.src = result;
                $('#group_logo_previewer').show();
            };
        
            reader.readAsDataURL(file);
        };
        
    }
    preview();
    
    // 生成二维码
    $('#generate_qrcode').click(function () {
        var group_link = $.trim($('#group_link').val());
        if (!group_link) {
            alert('请输入微信群链接');
            return false;
        }
        $("#group_qrcode_previewer").html(''); // 不清空的话会出现多个二维码
        $("#group_qrcode_previewer").qrcode({ 
            width: 644, //宽度 
            height:644, //高度 
            text: group_link //任意内容 
        });
        
        setTimeout(function () {
            var dataUrl = $('#group_qrcode_previewer canvas')[0].toDataURL();
            $('#group_qrcode').val(dataUrl);
        }, 100);
    });
});
</script>