<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <meta name="token" value="{{ csrf_token() }}">
    <title>阿拉德之怒</title>
    <script>
        function reSizeRem() {
            var ClientWidth = document.documentElement.clientWidth > 540 ? 540 : document.documentElement.clientWidth,
                UiWidth = 1080,
                FontSize = ClientWidth / UiWidth * 100;
            document.documentElement.style.fontSize = FontSize + 'px';
        }

        reSizeRem();
        window.onresize = function () {
            reSizeRem();
        }
        window.onerror = function (e) {
            alert(e)
        }
    </script>
    <link rel="stylesheet" href="{{ asset('postcard/css/style.css?v=20210607') }}">
    <script src="{{ asset('js/jquery.2.1.3.min.js') }}"></script>
    <script src="{{ asset('postcard/js/html2canvas.js') }}"></script>
</head>
<body>
<div class="container postcard-container" id="test">
    <div class="login-info">
        欢迎您，{{ $userName }}<a href="/logout">【注销】</a>
    </div>
    <!--角色信息-->
    <div class="role-container">
        <span class="icon-red"></span>
        <div class="left">
            <div class="role-avatar">
                <img src="{{ asset('postcard/images/img_border.png') }}" class="img-border" alt="">
                @if($jobImg == '')
                    <img src="{{ asset('postcard/images/default_img.png') }}" class="img-header" alt="">
                @else
                    <img src="{{ $jobImg }}" class="img-header" alt="">
                @endif
            </div>
            <ul>
                <li class="role1"><span>{{ $info->role_name ?? '' }}</span></li>
                <li class="role2"><span>{{ $serverName }}</span></li>
            </ul>
        </div>
        <div class="right">
            <ul>
                <li class="role3"><span>{{ $info->job ?? '' }}</span></li>
                <li class="role4"><span>{{ $info->guild ?? '' }}</span></li>
                <li class="role5"><span>
                    @if($info->online_time > 0)
                            {{ ceil($info->online_time / 3600) }}
                        @else
                            0
                        @endif
                小时</span></li>
            </ul>
        </div>
        <div class="bottom">
            <img src="{{ asset('postcard/images/img1.png') }}" class="img1" alt="">
            <div id="bNum" class="b-num"></div>
            <img src="{{ asset('postcard/images/img2.png') }}" class="img2" alt="">
        </div>
    </div>
    <!--角色等级-->
    <div class="range-container">
        <div id="rangeNums" class="range-num">

        </div>
    </div>
    <!--武器信息-->
    <div class="arms-container">
        <div class="arms">
            <div class="left">
                <li><span>武器名</span><em>{{ $armsStrongName }}</em></li>
                <li><span>武器名</span><em>{{ $armsExcitingName }}</em></li>
                <li><span>武器名</span><em>{{ $armsScoreName }}</em></li>
            </div>
            <div class="right">
                <ul>
                    <li>
                        <em>当前武器最高强化等级</em>
                        <div id="wq1"></div>
                    </li>
                    <li>
                        <em>当前武器最高激化等级</em>
                        <div id="wq2"></div>
                    </li>
                    <li>
                        <em>当前武器最高评分</em>
                        <div id="wq3"></div>
                    </li>
                </ul>
            </div>
        </div>
    </div>
    <!--佣兵团信息-->
    <div class="mr-container">
        <div class="mr">
            <span class="{{ $ybImg }}"></span>
        </div>
    </div>
    <!--勇者之塔-->
    <div class="yzzt-container">
        <span class="num1">{{ $info->brave_num }}</span>
        <span class="num2">{{ $braveTime }}</span>
    </div>
    <!--战斗生涯-->
    <div class="zdsy-container">
        <span class="num1">{{ $info->duel_total }}</span>
        <span class="num2">
            @if($info->duel_total > 0 && $info->duel_win > 0)
                {{ sprintf('%.2f',($info->duel_win * 100)/$info->duel_total) }}%
            @else
                00%
            @endif
        </span>
    </div>
    <!--最高段位-->
    <div class="zgdw-container">
        <span class="num1">{{ $rank }}</span>
    </div>

    <!--高光时刻-->
    <div class="ggsk-container">
        <div class="img-container">
            @if($userImgStatus == -1) <img src="{{ asset('postcard/images/img_add.jpg') }}" class="img img-add" alt="">
            <input type="file" id="file" accept="image/jpg, image/jpeg, image/png"> @endif
            @if($userImgStatus == 0)  <img src="{{ asset('postcard/images/img_sh.jpg') }}" class="img img-sh" alt=""> @endif
            @if($userImgStatus == 1)  <img src="{{ $userImg }}" class="img img-sh" alt=""> @endif
            @if($userImgStatus == 2)  <img src="{{ asset('postcard/images/sh_fail.jpg') }}" class="img img-sh" alt="">
            <input type="file" id="file" accept="image/jpg, image/jpeg, image/png"> @endif
        </div>
        @if($userImgStatus != 1)
        <span>建议尺寸（710*414px）jpg,jpeg,png</span>
        @endif

    </div>
    <div class="bottom-container">
        <!--分享-->
        <a href="javascript:" class="btn-share"></a>
        <!--本地保存-->
        <a href="javascript:" class="btn-save"></a>
    </div>

</div>
<div class="dialog dialog-img hide">
    <div class="dialog-img-content">
        <div class="img-scroll">
            <img src="" alt="">
        </div>
    </div>
</div>
<div class="loading hide">
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
</div>
<div class="share-img hide">
    <img src="{{ asset('postcard/images/share_img.png') }}" alt="">
</div>
<script type="text/javascript" src="//res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<script>

    $(function () {
        var isWeixin = function () {
            //判断是否是微信
            var ua = navigator.userAgent.toLowerCase();
            return ua.match(/MicroMessenger/i) == "micromessenger";
        };
        function getQueryString(name) {
            let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
            let r = window.location.search.substr(1).match(reg);
            if (r != null) {
                return decodeURIComponent(r[2]);
            }
            return null;
        }
        function renderHtml(dom, num) {
            var str = num.toString();
            var arr = str.split("");
            var html = ''
            $(arr).each(function (index, item) {
                html += '<span>' + item + '</span>';
            })
            $(dom).html(html);
        }
        @if($info->register_time != '')
            renderHtml('#bNum',{{ ceil((strtotime(date('Y-m-d H:i:s')) - strtotime($info->register_time)) / 86400 ) }});
        @else
            renderHtml('#bNum', 0);
        @endif
        renderHtml('#rangeNums', '{{ (string)$roleScore }}');
        renderHtml('#wq1', '{{ $armsStrong }}');
        renderHtml('#wq2', '{{ $armsExciting }}');
        renderHtml('#wq3', '{{ $armsScore }}');
        // 上传图片
        $(document).on('change', '#file', function (e) {
            var formData = new FormData();
            var file = $("#file")[0].files[0];
            var acceptFileTypes = /^image\/(gif|jpe?g|png)$/i;
            var roleId = getQueryString('roleId');
            var serverId = getQueryString('serverId');
            try{
                // //文件类型判断
                if (file && file.type && !acceptFileTypes.test(file.type)) {
                    alert('请上传jpg、jpeg或png格式的文件');
                    return false;
                }
                //文件大小判断
                if (file && file.size > (2 * 1024 * 1024)) {
                    alert('请上传不超过2M的文件');
                    return false;
                }
            }catch (e) {
                console.log(e)
            }
            formData.append("imgFile", file);
            formData.append("roleId", roleId);
            formData.append("serverId", serverId);
            formData.append("_token", "{{ csrf_token() }}")
            $.ajax({
                url: "/upload/img",
                type: "post",
                data: formData,
                cache: false,
                processData: false,
                contentType: false,
                dataType: "json",
                success: function (data) {
                    $('.img-add').hide();
                    $('.img-container').html('<img src="{{ asset('postcard/images/img_sh.jpg') }}" class="img img-sh" alt="">');
                },
                error: function (data) {

                }
            });
        });

        function baseImageUrl() {
            setTimeout(function () {
                html2canvas(document.querySelector("#test"), {  //关键在于new
                    removeContainer: true,
                    allowTaint: false,
                    width: window.screen.availWidth,
                    windowWidth: document.body.scrollWidth,
                    x: 0,
                    y: 0,
                    useCORS: true,
                    logging:true
                }).then(canvas => {
                    var image = canvas.toDataURL('image/jpeg', 1.0);
                    var img = '<img src="' + image + '">';
                    $('.dialog-img-content .img-scroll').html(img);
                    $('.dialog-img').show();
                    $('.loading').hide();
                });
                $('.postcard-container').addClass('fixed');
            }, 2000)
        }

        $('.btn-save').click(function () {
            var height = $('.postcard-container').height();
            $('.postcard-container').height(height);
            $('.loading').show();
            $('.btn-save,.btn-share,.login-info').hide();
            $('html,body').animate({
                scrollTop: 0
            }, 0);
            baseImageUrl();
            $('.postcard-container').addClass('fixed');
        });
        $('.dialog-img,.share-img').click(function () {
            $(this).fadeOut();
            $('.btn-save,.btn-share,.login-info').show();
            $('.postcard-container').removeClass('fixed');
        });

        $('.btn-share').click(function () {
            if(isWeixin()){
                $('.share-img').show();
            }
        });


        function _wechatConfig(o) {

            wx.config({
                debug: false,
                appId: o.appId,
                timestamp: o.timestamp,
                nonceStr: o.nonceStr,
                signature: o.signature,
                jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage'] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
            });
        }

        function _getWechatJsSdkInfo(callback) {
            $.ajax({
                type:"GET",
                url:"/wx/config",
                data:{
                    url:location.href
                },
                async:false,
                success:function(data){
                    callback(data.data);
                }
            });
        }

        if (isWeixin()) {
            _getWechatJsSdkInfo(_wechatConfig);
        }

        wx.checkJsApi({
            jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage'],
            success: function (res) {
            }
        });

        function wx_share(title, link, imgurl, desc) {
            //朋友圈
            wx.onMenuShareTimeline({
                title: title,
                link: link,
                imgUrl: imgurl,
                success: function() {

                },
                cancel: function() {

                }
            });
            //微信好友
            wx.onMenuShareAppMessage({
                title: title,
                desc: desc,
                link: link,
                imgUrl: imgurl,
                type: 'link',
                dataUrl: '',
                success: function() {

                },
                cancel: function() {

                }
            });
        }

        wx.ready(function () {
            var  title = '阿拉德之怒',
                link = 'http://uc-card.aldzn.cn',
                img  = "http://uc-card.aldzn.cn/static/postcard/images/default_img.png",
                desc = "阿拉德之怒-明信片";
            wx_share(title,link,img,desc);
        });

        wx.error(function (res) {
            console.log(res);
        });
    });
</script>
</body>
</html>