<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <!-- <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,member-scalable=no" /> -->
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <!--[if lt IE 9]>
    <script type="text/javascript" src="${pageContext.request.contextPath }/common/admin/lib/html5.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath }/common/admin/lib/respond.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath }/common/admin/lib/PIE_IE678.js"></script>
    <![endif]-->
    <link href="${pageContext.request.contextPath }/common/css/bootstrap.min-3.3.css" rel="stylesheet" type="text/css" />
    <link href="${pageContext.request.contextPath }/common/admin/css/H-ui.min.css" rel="stylesheet" type="text/css" />
    <link href="${pageContext.request.contextPath }/common/admin/css/H-ui.admin.css" rel="stylesheet" type="text/css" />
    <link href="${pageContext.request.contextPath }/common/admin/lib/select2/select2.min.css" rel="stylesheet" type="text/css" />
    <link href="${pageContext.request.contextPath }/common/admin/lib/Hui-iconfont/1.0.1/iconfont.css" rel="stylesheet" type="text/css" />
    <!--[if IE 6]>
    <script type="text/javascript" src="http://lib.h-ui.net/DD_belatedPNG_0.0.8a-min.js" ></script>
    <script>DD_belatedPNG.fix('*');</script>
    <![endif]-->

    <style>
        #datainit{
            margin-left:5px;
        }

        ul {
            width: 800px;
        }
        li {
            width: 400px;
            float: left;
            display: block;
        }

    </style>
    <title>分销商管理操作</title>
</head>
<body>
<div class="codeView">
    <form action="${pageContext.request.contextPath }/admin/product/update_product_price" method="post" class="form form-horizontal" id="form-Validform" novalidate="novalidate">
        <div id="pre_product" class="row cl">
            <input type="hidden" id="qrcodeId" name="qrcodeId" value="${qrcodeId}" />
            <label class="form-label col-xs-4 col-sm-3">商品信息：</label>
            <div class="col-xs-6 col-sm-6">
                <select id="sel_product" name="qrcodeColorId">

                </select>
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3">扫描优惠二维码优惠价格：</label>
            <div class="col-xs-6 col-sm-6">
                <input type="text" class="input-text" style="width: 300px;" nullmsg="优惠价不能为空" name="pre_price" id="pre_price">
                <input type="hidden" class="input-text" style="width: 300px;" name="qrcodePreDiscount" id="qrcodePreDiscount">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3">扫描购买二维码优惠价格：</label>
            <div class="col-xs-6 col-sm-6">
                <input type="text" class="input-text" style="width: 300px;" nullmsg="优惠价不能为空" name="sales_price" id="sales_price">
                <input type="hidden" class="input-text" style="width: 300px;" name="qrcodeSalesDiscount" id="qrcodeSalesDiscount">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><input type="checkbox" name="pre_qrcode" id="pre_qrcode" checked="checked" value="1" />优惠二维码</label>
        </div>
        <div id="pre_div">
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3">赠品：</label>
                <div class="col-xs-6 col-sm-6">
                    <ul id="pro_gift" class="my-table-list">

                    </ul>
                </div>
            </div>
        </div>
        <div class="row cl"  id="isHide">
            <label class="form-label col-xs-4 col-sm-3"><input type="checkbox" name="sales_qrcode" id="sales_qrcode" value="2"/>购买二维码</label>
        </div>
        <div id="sales_div">
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3">赠品：</label>
                <div class="formControls col-xs-6 col-sm-6">
                    <ul class="my-ul-list">

                    </ul>
                </div>
            </div>
        </div>
        <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
            <input class="btn btn-primary" type="submit" onclick="return checkValue();" value="&nbsp;&nbsp;提交&nbsp;&nbsp;">
        </div>
    </form>
</div>



<script type="text/javascript" src="${pageContext.request.contextPath }/common/admin/lib/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath }/common/admin/lib/layer/1.9.3/layer.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath }/common/admin/lib/laypage/1.2/laypage.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath }/common/admin/lib/My97DatePicker/WdatePicker.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath }/common/admin/lib/datatables/1.10.0/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath }/common/admin/js/H-ui.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath }/common/admin/js/H-ui.admin.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath }/common/admin/lib/select2/select2.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath }/common/admin/lib/select2/zh-CN.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath }/common/admin/lib/Validform/5.3.2/Validform.min.js"></script>
<script type="text/javascript">
    var isNum=/^(([1-9][0-9]*)|(([0]\.\d{1,2}|[1-9][0-9]*\.\d{1,2})))$/;
    $(function(){
        var qrcodeId = $('#qrcodeId').val();
        $.ajax({
            url: '${pageContext.request.contextPath }/admin/product/edit_product_price',
            dataType:'json',//服务器返回json格式数据
            type:'post',	//HTTP请求类型
            data:{'qrcodeId':qrcodeId},
            error:function(data){

            },
            success:  function(dt){
                if(dt.status){
                    var color_list = [];
                    var gift = [];
                    var arr1 = new Array();
                    var arr2 = new Array();
                    color_list = dt.data.colorList;
                    gift = dt.data.gift;
                    if (gift.qrcodePreDiscount != null && gift.qrcodePreDiscount != '') {
                        $('#pre_price').val(gift.qrcodePreDiscount/100);
                    }
                    if (gift.qrcodeSalesDiscount != null && gift.qrcodeSalesDiscount != '') {
                        $('#sales_price').val(gift.qrcodeSalesDiscount/100);
                    }
                    if (gift.qrcodePreGiftColor != null && gift.qrcodePreGiftColor != '') {
                        arr1 = gift.qrcodePreGiftColor.split(",");
                        $('#pre_qrcode').prop('checked',true);
                        $('#pre_div').show();
                    } else {
                        $('#pre_qrcode').prop('checked',false);
                        $('#pre_div').hide();
                    }
                    if (gift.qrcodeSalesGiftColor != null && gift.qrcodeSalesGiftColor != '') {
                        arr2 = gift.qrcodeSalesGiftColor.split(",");
                        $('#sales_qrcode').prop('checked',true);
                        $('#sales_div').show();
                    } else {
                        $('#sales_qrcode').prop('checked',false);
                        $('#sales_div').hide();
                    }
                    var table = document.body.querySelector('.my-table-list');
                    var ul = document.body.querySelector('.my-ul-list');
                    var sel_product = $('#sel_product');
                    $.each(color_list, function(i,item){
                        if (gift.qrcodeColorId == item.colorId) {
                            sel_product.append("<option value='"+item.colorId+"' selected = 'selected'>"+item.colorProductName+"</option>");
                        } else {
                            sel_product.append("<option value='"+item.colorId+"'>"+item.colorProductName+"</option>");
                        }
                        var li = document.createElement('li');
                        var salesLi = document.createElement('li');
                        var num1 = 0;
                        var num2 = 0;
                        $.each(arr1, function(j,color){
                            if (color == item.colorId) {
                                num1++;
                            }
                        })

                        if (num1 > 0) {
                            li.innerHTML = '<input type="checkbox" name="pre_gift" value="'+item.colorId+'" checked="checked"/>' + item.colorProductName;
                        } else {
                            li.innerHTML = '<input type="checkbox" name="pre_gift" value="'+item.colorId+'"/>' + item.colorProductName;
                        }

                        $.each(arr2, function(j,color){
                            if (color == item.colorId) {
                                num2++;
                            }
                        })
                        if (num2 > 0) {
                            salesLi.innerHTML = '<input type="checkbox" name="sales_gift" value="'+item.colorId+'" checked="checked"/>' + item.colorProductName;
                        } else {
                            salesLi.innerHTML = '<input type="checkbox" name="sales_gift" value="'+item.colorId+'"/>' + item.colorProductName;
                        }
                        table.appendChild(li);
                        ul.appendChild(salesLi);
                    });
                }else{
                    layer.msg(data.message,{icon:1,time:3000});
                }
            }
        });
    });

    $('#pre_qrcode').click(function(){
        var check = $('#pre_qrcode').prop('checked');
        if (check) {
            $('#pre_div').show();
        } else {
            $('#pre_div').hide();
        }
    })

    $('#sales_qrcode').click(function(){
        var check = $('#sales_qrcode').prop('checked');
        if (check) {
            $('#sales_div').show();
        } else {
            $('#sales_div').hide();
        }
    })

    //校验输入金额格式
    function checkValue() {
        var check = true;
        var message = '';
        var pre_price = $('#pre_price').val().trim();
        var sales_price = $('#sales_price').val().trim();
        if (pre_price != null && pre_price != '') {
            if (!isNum.test(pre_price)) {
                check = false;
                message = "扫描优惠二维码优惠价格格式不正确，请重新输入!";
            } else {
                $('#qrcodePreDiscount').val(pre_price * 100);
            }
        }
        if (sales_price != null && sales_price != '') {
            if (!isNum.test(sales_price)) {
                check = false;
                message = "扫描购买二维码优惠价格格式不正确，请重新输入!";
            } else {
                $('#qrcodeSalesDiscount').val(sales_price * 100);
            }
        }
        if (!check) {
            alert(message);
        }
        return check;
    }

</script>

</body>
</html>