<%@ page import="java.util.ArrayList" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title id="pageTitle">法雷奥</title>
    <meta name="renderer" content="webkit"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <meta name="format-detection" content="telephone=no"/>
    <meta name="apple-mobile-web-app-capable" content="yes"/>
    <meta name="apple-mobile-web-app-status-bar-style" content="black"/>
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0 user-scalable=no"/>
    <meta id="description" name="Description" content="法雷奥"/>
    <meta id="key" name="Keywords" content="法雷奥"/>
    <link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/static/css/common.css"/>
    <script language="javascript" type="text/javascript"
            src="${pageContext.request.contextPath}/static/js/jquery.min.js"></script>
    <script language="javascript" type="text/javascript"
            src="${pageContext.request.contextPath}/static/js/js.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/js/awardRotate.js"></script>

    <script type="text/javascript">
        var turnplate = {
            restaraunts: [],				//大转盘奖品名称
            colors: [],					//大转盘奖品区块对应背景颜色
            outsideRadius: 178,			//大转盘外圆的半径
            textRadius: 150,				//大转盘奖品位置距离圆心的距离
            insideRadius: 0,			//大转盘内圆的半径
            startAngle: 0,				//开始角度
            bRotate: false				//false:停止;ture:旋转
        };

        $(document).ready(function () {
            $.ajax({
                url: "prize", async: false, success: function (date) {
                    var obj = eval("(" + date + ")");
                    var da = [];
                    var co = [];
                    var lv = [];
                    var id = [];
                    for (var index in obj) {
                        da.push(obj[index].pname);
                        co.push(obj[index].color);
                    }
                    $("#dates").val(da);
                    $("#color").val(co);
                }
            });
            var s = []
            var t = []
            s = ($("#dates").val()).split(",");
            t = ($("#color").val()).split(",");
            turnplate.restaraunts = s;
            // turnplate.colors = t;
            turnplate.colors = ["#cee198", "#FFFFFF", "#cee198", "#FFFFFF", "#cee198", "#FFFFFF"];
            var rotateTimeOut = function () {
                $('#wheelcanvas').rotate({
                    angle: 0,
                    animateTo: 2160,
                    duration: 8000,
                    callback: function () {
                        alert('网络超时，请检查您的网络设置！');
                    }
                });
            };

            //旋转转盘 item:奖品位置; txt：提示语;
            var rotateFn = function (item, txt) {
                var angles = item * (360 / turnplate.restaraunts.length) - (360 / (turnplate.restaraunts.length * 2));
                if (angles < 270) {
                    angles = 270 - angles;
                } else {
                    angles = 360 - angles + 270;
                }
                $('#wheelcanvas').stopRotate();
                $('#wheelcanvas').rotate({
                    angle: 0,
                    animateTo: angles + 1800,
                    duration: 8000,
                    callback: function () {
                        $.post("add", {"uremark": txt}, function (date) {
                        });
                        alert(txt)
                        turnplate.bRotate = !turnplate.bRotate;
                        window.location.href = 'results';
                    }
                });
            };

            $('.pointer').click(function () {
                if (turnplate.bRotate) return;
                turnplate.bRotate = !turnplate.bRotate;
                // 获取随机数(奖品个数范围内)
                $.post("gailv", {}, function (data) {
                    rotateFn(data, turnplate.restaraunts[data - 1]);
                });
            });
        });

        function rnd(n, m) {
            var random = Math.floor(Math.random() * (m - n + 1) + n);
            return random;

        }

        //页面所有元素加载完毕后执行drawRouletteWheel()方法对转盘进行渲染
        window.onload = function () {
            drawRouletteWheel();
        };

        function drawRouletteWheel() {
            var canvas = document.getElementById("wheelcanvas");
            if (canvas.getContext) {
                //根据奖品个数计算圆周角度
                var arc = Math.PI / (turnplate.restaraunts.length / 2);
                var ctx = canvas.getContext("2d");
                //在给定矩形内清空一个矩形
                ctx.clearRect(0, 0, 422, 422);
                //strokeStyle 属性设置或返回用于笔触的颜色、渐变或模式
                ctx.strokeStyle = "#5d7d8e";
                //font 属性设置或返回画布上文本内容的当前字体属性
                ctx.font = '16px Microsoft YaHei';
                for (var i = 0; i < turnplate.restaraunts.length; i++) {
                    var angle = turnplate.startAngle + i * arc;
                    ctx.fillStyle = turnplate.colors[i];
                    ctx.beginPath();
                    //arc(x,y,r,起始角,结束角,绘制方向) 方法创建弧/曲线（用于创建圆或部分圆）
                    ctx.arc(211, 211, turnplate.outsideRadius, angle, angle + arc, false);
                    ctx.arc(211, 211, turnplate.insideRadius, angle + arc, angle, true);
                    ctx.stroke();
                    ctx.fill();
                    //锁画布(为了保存之前的画布状态)
                    ctx.save();

                    //----绘制奖品开始----
                    ctx.fillStyle = "#1f292e";
                    var text = turnplate.restaraunts[i];
                    var line_height = 17;
                    //translate方法重新映射画布上的 (0,0) 位置
                    ctx.translate(211 + Math.cos(angle + arc / 2) * turnplate.textRadius, 211 + Math.sin(angle + arc / 2) * turnplate.textRadius);

                    //rotate方法旋转当前的绘图
                    ctx.rotate(angle + arc / 2 + Math.PI / 2);

                    /** 下面代码根据奖品类型、奖品名称长度渲染不同效果，如字体、颜色、图片效果。(具体根据实际情况改变) **/
                    if (text.indexOf("M") > 0) {//流量包
                        var texts = text.split("M");
                        for (var j = 0; j < texts.length; j++) {
                            ctx.font = j == 0 ? 'bold 20px Microsoft YaHei' : '16px Microsoft YaHei';
                            if (j == 0) {
                                ctx.fillText(texts[j] + "M", -ctx.measureText(texts[j] + "M").width / 2, j * line_height);
                            } else {
                                ctx.fillText(texts[j], -ctx.measureText(texts[j]).width / 2, j * line_height);
                            }
                        }
                    } else if (text.indexOf("M") == -1 && text.length > 6) {//奖品名称长度超过一定范围
                        text = text.substring(0, 6) + "||" + text.substring(6);
                        var texts = text.split("||");
                        for (var j = 0; j < texts.length; j++) {
                            ctx.fillText(texts[j], -ctx.measureText(texts[j]).width / 2, j * line_height);
                        }
                    } else {
                        //在画布上绘制填色的文本。文本的默认颜色是黑色
                        //measureText()方法返回包含一个对象，该对象包含以像素计的指定字体宽度
                        ctx.fillText(text, -ctx.measureText(text).width / 2, 0);
                    }

                    //添加对应图标
                    if (text.indexOf("闪币") > 0) {
                        var img = document.getElementById("shan-img");
                        img.onload = function () {
                            ctx.drawImage(img, -15, 10);
                        };
                        ctx.drawImage(img, -15, 10);
                    } else if (text.indexOf("谢谢参与") >= 0) {
                        var img = document.getElementById("sorry-img");
                        img.onload = function () {
                            ctx.drawImage(img, -15, 10);
                        };
                        ctx.drawImage(img, -15, 10);
                    }
                    //把当前画布返回（调整）到上一个save()状态之前
                    ctx.restore();
                    //----绘制奖品结束----
                }
            }
        }


    </script>
    <script>

        $(function () {

            $(".drawbck").click(function () {
                $(".mask").show();
            })
            $(".maskcol").click(function () {
                $(".mask").hide();
            })
            $(".maskbg").click(function () {
                $(".mask").hide();
            })
        })

    </script>
</head>
<body>
<div class="bodbg">
    <c:forEach items="${prize}" var="prize">
        <input hidden="hidden" class="prize" value="${prize.prate}" title="${prize.pid}"/>
    </c:forEach>
    <input hidden="hidden" id="dates" value=""/>
    <input hidden="hidden" id="color" value=""/>
    <input hidden="hidden" id="gailv" value=""/>
    <div class="topim"><img class="topimg" src="${pageContext.request.contextPath}/static/images/top.jpg" alt=""></div>
    <div class="draw">
        <img class="drawimg" src="${pageContext.request.contextPath}/static/images/draw.png" alt="">
        <div class="drawpab">
            <div class="drawtim"><p>活动时间：9月1日-10月31日</p></div>
            <p class="drawbck">抽奖规则</p>
        </div>
    </div>
    <div class="mianbt">
        <div class="mianrstg">
            <div class="drawlun">
                <img src="${pageContext.request.contextPath}/static/images/icolit.png" id="shan-img"
                     style="display:none;"/>
                <img src="${pageContext.request.contextPath}/static/images/mianctx.png" id="sorry-img"
                     style="display:none;"/>
                <div class="banner">
                    <div class="turnplate"
                         style="background-image:url(${pageContext.request.contextPath}/static/images/turnplate-bgd.png);background-size:100% 100%;">
                        <canvas class="item" id="wheelcanvas" width="422px" height="422px"></canvas>
                        <img class="pointer"
                             src="${pageContext.request.contextPath}/static/images/turnplate-pointer.png"/>
                    </div>
                </div>
            </div>
            <p class="drawrem">提醒：只有产生订单才可以兑现奖品哦！<i class="drawremico"><img
                    src="${pageContext.request.contextPath}/static/images/icot02.png" alt=""></i></p>
            <div class="drawrecor">
                <p class="drawrecortil">中奖记录</p>
                <div class="drawrecorman">
                    <ul>
                        <c:forEach items="${userAll}" var="userAll">
                            <li><p><span class="shnamp">${userAll.uphone}</span><span class="shnamt">抽中</span><span
                                    class="shname">${userAll.uremark}</span></p></li>
                        </c:forEach>
                    </ul>
                </div>
            </div>
        </div>
    </div>
</div>


<div class="mask">
    <div class="maskbg"></div>
    <div class="maskal">
        <div class="maskcol">×</div>
        <div class="maskalte">
            <p class="ruleim"><img class="ruleimg" src="${pageContext.request.contextPath}/static/images/ruleim.png"
                                   alt=""></p>
            <div class="masktxtdra">
                <ul class="icolit">
                    <li><i class="icolitim"></i>法雷奥员工关注“法雷奥汽车售后”微信号，在对话栏输入“员工内卖”即可参与抽奖活动</li>
                    <li><i class="icolitim"></i>输入法雷奥邮箱和手机号参与抽奖，每个法雷奥邮箱地址最多只可抽奖一次</li>
                    <li><i class="icolitim"></i>抽奖后会生成“法雷奥员工亲友内卖邀请券”，点击右上角保存或者直接截图</li>
                    <li><i class="icolitim"></i>进入法雷奥天猫旗舰店(http://valeo.tmall.com)联系在线客服，提供保存的“法雷奥员工亲友内卖邀请券”截图或者直接提供您的邮箱，即可享受员工优惠（具体购买流程请咨询法雷奥天猫旗舰店客服）
                    </li>
                    <li><i class="icolitim"></i>内卖订单确认后，联系法雷奥天猫旗舰店客服，按75折返现（全国包邮）</li>
                    <li><i class="icolitim"></i>11月第一周，我们会核对您的中奖及内卖订单，然后安排话费充值</li>
                </ul>
                <p>如对于本次抽奖活动或奖品兑现有任何疑问，可以通过“法雷奥汽车售后”微信号给我们留言或者发送邮件至 Lisa Gao (yajie.gao@valeo.com)</p>
                <p>&nbsp;</p>
                <p>备注：</p>
                <p>员工价为天猫旗舰店日常标价基础乘以折扣优惠</p>
                <p>员工内卖不与天猫旗舰店其他活动叠加</p>
                <p>微信抽奖活动时间：20xx年9月1日-10月31日</p>
                <p>本次活动不包括国外及港澳台地区</p>
                <p>活动最终解释权归法雷奥售后中国所有</p>
            </div>
        </div>
    </div>
</div>


<!-- 代码部分end -->
</body>
</html>