<%@ 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;
        }

        li {
            padding-top: 5px;
        }


    </style>
    <title>代理商管理操作</title>
</head>
<body>
<div class="codeView">
    <form action="" method="post" class="form form-horizontal" id="form-Validform" novalidate="novalidate">
        <div id="pre_product" class="row cl">
            <label class="form-label col-xs-4 col-sm-3">代理商信息：</label>
            <div class="col-xs-6 col-sm-6">
                <select id="sel_agent" name="sel_agent">

                </select>
            </div>
        </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="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3" style="padding-top: 10px;">
            <input class="btn btn-primary" type="button" onclick="submit_value();"  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})))$/;
    var product_color = '';
    var message = '';
    $(function(){
        $.ajax({
            url: '${pageContext.request.contextPath }/admin/agentTooth/select_agent_and_product',
            dataType:'json',//服务器返回json格式数据
            type:'post',	//HTTP请求类型
            error:function(data){

            },
            success:  function(dt){
                if(dt.status){
                    console.log(dt.data);
                    var color_list = [];
                    var agent_list = [];
                    color_list = dt.data.colorList;
                    agent_list = dt.data.agentList;
                    var table = document.body.querySelector('.my-table-list');
                    var sel_agent = $('#sel_agent');
                    $.each(agent_list, function(i,item) {
                        sel_agent.append("<option value='" + item.agentId + "'>" + item.agentName + "</option>");
                    });
                    $.each(color_list, function(i,item){
                        var li = document.createElement('li');
                        li.innerHTML = '<input disabled="disabled" class="input-text" placeholder="请输入产品优惠价" style="width: 150px;" type="text" id="price' + i + '" index="' + i + '" />&nbsp;&nbsp;&nbsp;&nbsp;'
                            + '<input id="che_product'+ i +'" type="checkbox" index="' + i + '" name="che_product" value="'+item.colorId+'" onclick="write_price('+ i +')"/>' + item.colorProductName;
                        table.appendChild(li);
                    });
                }else{
                    layer.msg(data.message,{icon:1,time:3000});
                }
            }
        });
    });

    //校验输入金额格式
    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;
    }

    function write_price (val) {
        var check = $('#che_product'+val).prop('checked');
        if (check) {
            $('#price'+val).prop('disabled','');
        } else {
            $('#price'+val).val('');
            $('#price'+val).prop('disabled','disabled');
        }
    }

    function checkValue () {
        var flag = true;
        var che_product = document.getElementsByName('che_product');
        for(var i=0; i<che_product.length; i++){
            if(che_product[i].checked){
                var che_index = che_product[i].getAttribute("index");
                var price = $('#price'+che_index).val();
                if (price == null || price == '') {
                    flag = false;
                    che_index += 1;
                    message = '第'+che_index+'个产品优惠价不能为空!';
                    return flag;
                }
                price = price * 100;
                product_color+=che_product[i].value+':'+price+',';
            }
        }
        console.log(product_color);
        return flag;
    }

    function submit_value() {
        var flag = checkValue();
        if (!flag) {
            layer.msg(message,{icon:1,time:3000});
            return;
        }
        var data_value = {
            "productColor":product_color,
            "sel_agent":$('#sel_agent').val()
        }
        $.ajax({
            url: '${pageContext.request.contextPath }/admin/agentTooth/add_agent_config',
            dataType:'json',//服务器返回json格式数据
            type:'post',	//HTTP请求类型
            data:data_value,
            error:function(data){

            },
            success:  function(dt){
                if (dt.status) {
                    window.location.href = '${pageContext.request.contextPath }/admin/agentTooth/select_agent_config_list'
                }
            }
        });
    }


</script>

</body>
</html>