<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    <%@include file="/views/common.jsp" %>
</head>
<!-- PVC名片21/会员卡24 -->
<style>
    .edge .layui-edge {
        right: 20px;
    }
</style>
<body>
<div class="big_box">
    <div class="left_div">
        <h1 class="h1">PVC名片</h1> <span style="color:red;font-weight:700;"></span>
        <hr>
        <form class="layui-form">
            <input type="hidden" name="proTypeValue" id="proTypeValue" class="layui-input">
            <p>种类</p>
            <div class="kind">
                <input type="radio" lay-filter="kind" name="kind" value="21" title="PVC名片">
                <input type="radio" lay-filter="kind" name="kind" value="24" title="会员卡">
            </div>
            <p>
                材质
            </p>
            <div class="layui-form-item">
                <select name="kindValue" id="kindValue" lay-filter="kindValue" class="select">
                </select>
            </div>
            <p>
                尺寸
            </p>
            <div class="layui-form-item">
                <select name="kind2Value" id="kind2Value" lay-filter="kind2Value" class="select"></select>
            </div>
            <p>
                数量（张） <input type="checkbox" name="switchCount" lay-filter="switchCount" title="自定义数量"></span>
            </p>
            <div class="count">
                <div class="layui-form-item">
                    <select name="count" class="select">
                        <option value="200">200</option>
                        <option value="500">500</option>
                        <option value="1000">1000</option>
                        <option value="2000">2000</option>
                        <option value="3000">3000</option>
                        <option value="5000">5000</option>
                        <option value="10000">10000</option>
                    </select>
                </div>
            </div>
            <div class="diyCount" style="display: none">
                <div class="layui-form-item">
                    <input type="text" name="count" id="count" placeholder="请输入整数" class="layui-input"
                           autocomplete="off">
                </div>
            </div>
            <p>
                款数
            </p>
            <div class="layui-form-item">
                <input type="text" placeholder="请输入整数" autocomplete="off" name="number" id="number" value="1"
                       class="layui-input" lay-verify="number">
            </div>
            <p>
                客户旺旺
            </p>
            <div class="layui-form-item">
                <input type="text" placeholder="请输入客户旺旺号" autocomplete="off" name="wangwang" id="wangwang"
                       class="layui-input">
            </div>
            <p>印刷</p>
            <input type="radio" lay-filter="craftMo" name="craftMo" value="单面印刷" title="单面印刷" class="danmian"
                   checked="checked">
            <input type="radio" lay-filter="craftMo" name="craftMo" value="双面印刷" title="双面印刷" class="shuangmian">

            <div class="craftDiv">
                <p>工艺</p>
                <span>形状 : </span><input type="radio" lay-filter="craftJiao" name="craftJiao" value="圆角" title="圆角"
                                         checked="checked">
                <div class="layui-input-block craft">
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <input type="checkbox" name="craft" lay-filter="switch" value="号码" title="号码">
                    <div class="layui-inline edge haomaHide" style="width:120px; display:none">
                        <select name="haoma" class="select" lay-filter="haoma">
                            <option value="喷码" selected>喷码</option>
                            <option value="激光码">激光码</option>
                            <option value="小凸金码">小凸金码</option>
                        </select>
                    </div>
                    <input type="checkbox" name="craft" lay-filter="switch" value="签名条" title="签名条">
                    <input type="checkbox" name="craft" lay-filter="switch" value="磁条" title="磁条">
                </div>
            </div>
            <div class="layui-input-block">
                <input type="checkbox" name="craft" lay-filter="ui_craft" value="抠图" title="抠图">
                <div class="layui-inline crop" style="display:none;width: 80px">
                    <input class="layui-input" type="text" name="cropNumber">
                </div>
            </div>
            <hr>
            <div class="layui-form-item">
                <button class="layui-btn" lay-submit="" lay-filter="acount_btn" id="queryCondBtn">计算</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
            <h2>计算结果-
                <button type="button" class="layui-btn layui-btn-primary layui-btn-sm copyResult"
                        onclick="copyResult()">点击复制
                </button>
            </h2>
            <div>
                <textarea rows="11" cols="75" id="span_result" readonly="readonly"></textarea>
                <%@include file="../acountExpressFee.jsp" %>
            </div>
            <!--<h2>下单备注-<button type="button" class="layui-btn layui-btn-primary layui-btn-sm copyResult" onclick="copyBz()">点击复制</button></h2>
            <div>
                <textarea rows="5" cols="75" id="bz_result" readonly="readonly"></textarea>
            </div>-->
            <div>
                <table class="layui-hide" id="priceTable" lay-filter="priceTable"></table>
            </div>
        </form>
    </div>
    <div class="right_div">
        <div class="layui-carousel" id="test1">
            <div carousel-item id="carousel"></div>
            <br>
            <div id="remark" style="font-size:20px;color:red"></div>
        </div>
    </div>
</div>
</body>
<%@include file="/views/copyResult.jsp" %>
<script>
    layui.use(['table', 'form', 'carousel'], function () {
        var form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功
        var carousel = layui.carousel;
        var table = layui.table;

        // 监听回车键，如果敲了回车，调用点击‘计算’
        $(document).keydown(function (e) {
            if (e.keyCode === 13) {
                $("#queryCondBtn").trigger("click");
            }
        });
        //建造实例
        ins = carousel.render({});

        function getProductImage(option) {
            var html = " ";
            var remark = " ";
            let crafts = []
            // 清空轮播图
            $("#carousel").empty();
            $("#remark").empty();
            let kindValue = $('select[name="kindValue"] option:checked').val();
            let craftMo = $('input[name="craftMo"]:checked').val();
            $('input[name="craft"]:checked').each(function () {
                crafts.push($(this).val())
            })
            let data = {
                proTypeValue: $('input[name="kind"]:checked').val()
            }
            if (!!kindValue) {
                data.kindValue = kindValue;
            }
            if (kindValue) {
                data.craftValue = craftMo
            }
            if (data.proTypeValue == 24) {
                delete data.craftValue;
            }
            if (crafts.length > 0 && option != "") {
                delete data.kindValue;
                data.craftValue = option
            }
            $.ajax({
                url: "${pageContext.request.contextPath}/getImgs",
                type: "GET",
                data,
                //dataType : "json",
                success: function (result) {
                    for (let i = 0; i < result.length; i++) {
                        // 只留一个remark
                        remark = "";
                        html += '<div><img style="width:' + result[0].imgWidth + 'px" src="' + result[i].imgUrl + '"></div>';
                        remark += '<div><span>' + result[i].remark + '<span/></div>';
                    }
                    $("#carousel").append(html);
                    // 如果没有说明，就不显示null
                    if (remark.indexOf("null") < 0) {
                        $("#remark").append(remark);
                    }
                    // 如果没有轮播图就隐藏
                    if (result.length == 0) {
                        document.getElementById("test1").style.display = "none";   //隐藏
                    } else {
                        document.getElementById("test1").style.display = "block";   //显示
                        ins.reload({
                            elem: '#test1',
                            width: result[0].imgWidth,  //设置容器宽度
                            height: result[0].imgHeight
                        });
                    }
                }
            });
        }

        form.on('radio(craftMo)', function (data) {
            getProductImage()
        })
        // 监听种类--PVC名片/会员卡
        form.on('radio(kind)', function (data) {
            // 选择会员卡的时候禁用单面印刷，切换成双面印刷
            if (data.value == 24) {
                $(".danmian").attr("disabled", true);
                $('input[class = shuangmian]').prop('checked', true);
                $(".shuangmian").attr("disabled", false);// 加这个让按钮取消禁用模式

                $(".craftDiv").show();
                $(".craftDiv").find(":input").attr("disabled", false);
            } else {
                $(".danmian").attr("disabled", false);
                $(".craftDiv").hide();
                $(".craftDiv").find(":input").attr("disabled", true);
            }
            form.render('radio');
            // 切换按钮清空下拉框(防止一直重复加)
            $("#kindValue").empty();
            // 下拉框第一列添加空的option
            $('#kindValue').append(new Option("", ""));
            // 一访问页面就获取材质
            $.ajax({
                url: "${pageContext.request.contextPath}/getKindsByPro",
                type: "GET",
                dataType: "json",
                data: {
                    proTypeValue: $('input[name="kind"]:checked').val()
                },
                success: function (result) {
                    $.each(result, function (index, item) {
                        $('#kindValue').append(new Option(item, index));//往下拉菜单里添加元素
                    });
                    layui.form.render("select");
                }
            });
            getProductImage()

        });
        // 根据材质选择获取产品尺寸
        form.on('select(kindValue)', function (data) {
            // 透明材质的时候禁用双面印刷，切换成单面印刷
            if (data.value == 3 || data.value == 4) {
                if ($('input[name="kind"]:checked').val() == 21) {
                    $(".shuangmian").attr("disabled", true);
                    $('input[class = danmian]').prop('checked', true);
                }
            } else {
                $(".shuangmian").attr("disabled", false);
            }
            form.render('radio');
            // 获取产品名称
            $("#kind2Value").empty();
            form.render("select");
            $.ajax({
                url: "${pageContext.request.contextPath}/getKind2sByKind",
                type: "GET",
                dataType: "json",
                data: {
                    proTypeValue: $('input[name="kind"]:checked').val(),
                    kindValue: data.value
                },
                success: function (result) {
                    $.each(result, function (index, item) {
                        $('#kind2Value').append(new Option(item, index));//往下拉菜单里添加元素
                    });
                    layui.form.render("select");
                }
            });
            getProductImage();
        });

        // 自定义数量
        form.on('checkbox(switchCount)', function (data) {
            if (data.elem.checked) {
                $(".diyCount").show();
                $(".count").hide();
                $(".diyCount").find(":input").attr("disabled", false);
                $(".count").find(":input").attr("disabled", true);
            } else {
                $(".count").show();
                $(".diyCount").hide();
                $(".count").find(":input").attr("disabled", false);
                $(".diyCount").find(":input").attr("disabled", true);
                // 恢复标准数量的时候会被禁用，加这个解决
                form.render();
            }
        })

        // 选中工艺‘号码’的时候，才显示出对应下拉框
        form.on('checkbox(switch)', function (data) {
            // 打码选中的时候，显示下拉框
            if (data.value == '号码') {
                if (data.elem.checked) {
                    $(".haomaHide").show();
                } else {
                    $(".haomaHide").hide();
                }
            }
            getProductImage(data.elem.checked ? data.value : "")
        });
        form.on('checkbox(ui_craft)', function (data) {
            let craft_list = [];
            let kindValue = $('input[name="kindValue"]:checked').val();
            craft_list.push($("select[name='craft'] option:selected").val());
            $("input[name='craft']:checked").each(function () {
                if (!$(this).is(':disabled')) {
                    craft_list.push($(this).val());
                }
            });

            if (data.value == '抠图') {
                $(".crop").hide();
                $(".crop").find("input").prop("disabled", true)
                if (data.elem.checked) {
                    $(".crop").show();
                    $(".crop").find("input").prop("disabled", false)
                }
            }
            form.render();
        })
        // 点击计算，计算价格
        form.on('submit(acount_btn)', function (data) {
            var proTypeValue = $('input[name="kind"]:checked').val();
            if (!proTypeValue) {
                layer.msg("请选择产品种类！", {offset: ['300px', '300px']}, function () {
                });
                return false;
            }
            // 赋值proTypeValue
            $("#proTypeValue").val(proTypeValue);
            var proTypeValue = $('input[name="kind"]:checked').val();

            // 选中‘自定义数量’
            if ($('input[name="switchCount"]:checked').length != 0) {
                if ($("#count").val() < 10000 || $("#count").val() % 1000 != 0) {
                    layer.msg("自定义数量需要大于10000 且是1000的整数倍", {offset: ['300px', '300px']}, function () {
                    });
                    return false;
                }
            }
            var kindValue = $("#kindValue option:checked").text();
            if (kindValue == "") {
                layer.msg("请选择材质！", {offset: ['300px', '300px']}, function () {
                });
                return false;
            }
            var kind2Value = $("#kind2Value option:checked").text();
            var craftMo = [$('input[name="craftMo"]:checked').val()];
            var craftJiao = [$('input[name="craftJiao"]:checked:not(:disabled)').val()];
            var number = $("#number").val();
            var count = $("select[name='count'] option:selected").text();
            var proTypeValueData = {
                21: "PVC名片",
                24: "会员卡"
            };
            $.ajax({
                url: "${path}/getThanSum",
                type: "GET",
                data: $(".big_box form").serialize(),
                success: function (result) {
                    if (result.code == 100) {
                        layer.msg(result.msg, {offset: ['300px', '300px']}, function () {
                        });
                        return false;
                    }
                    let arr = []
                    $("input[name='craft']:checked").each(function () {
                        if (!$(this).is("disabled")) {
                            if ($(this).val() == "抠图") {
                                arr.push($(this).val() + $("input[name='cropNumber']").val());
                                return;
                            }
                        }
                    })
                    arr.push(craftMo)
                    arr.push(craftJiao)
                    var data = result.data.proList;
                    var span_result = proTypeValueData[proTypeValue] + ' - ' + kindValue + ' - ' + kind2Value + ' -(同款内容)\n';
                    span_result += "工艺 :" + arr.join(",") + "\n";
                    if (number > 1) {
                        for (let i = 0; i < data.length; i++) {
                            span_result += number + '款 各' + data[i].count + "张，共" + data[i].price + "元" + '\n'
                            data[i].number = number;
                        }
                    } else {
                        for (let i = 0; i < data.length; i++) {
                            span_result += number + '款 ' + data[i].count + "张，共" + data[i].price + "元" + '\n'
                            data[i].number = number;
                        }
                    }
                    span_result += '包邮，免费设计呢~（偏远地区需补邮费）'
                    if (window.parent.system_isGai) {
                        span_result += "\n\n亲 现在下单可以参加淘宝活动8.5折折扣哦!"
                    }
                    addLog(span_result);
                    $("#span_result").val(span_result);

                    var size = kind2Value.replace("MM", "");
                    var craft = craftMo + craftJiao;
                    if ($(".haomaHide").css("display") != "none") {
                        craft += $("select[name='haoma'] option:selected").val();
                    }
                    var other = [];
                    $("input:checkbox[name='craft']:checked").each(function (i) {
                        if (!$(this).is(':disabled')) {
                            if ($(this).val() != "号码")
                                other.push($(this).val());
                        }
                    });
                    console.log(size);
                    if (other != "") {
                        craft += other;
                    }
                    getRemark(proTypeValueData[proTypeValue], size + "mm", count + "张", kindValue, number, craft, "");
                    //计算完自动复制文本
                    var e = document.getElementById("span_result");//对象是content
                    if (e.value != "") {
                        e.select();//选择对象
                        document.execCommand("Copy");//执行浏览器复制命令
                    }

                    //生成表格
                    table.render({
                        elem: '#priceTable',
                        even: true, //隔行变色
                        data: data, // 赋值已知数据
                        width: 500,
                        cols: [[
                            {
                                field: 'number',
                                width: '12%',
                                align: "center",
                                title: '款数'
                            }, {
                                field: 'count',
                                width: '16%',
                                align: "center",
                                title: '数量'
                            }, {
                                field: 'price',
                                width: '16%',
                                align: "center",
                                title: '报价'
                            }, {
                                field: 'wangwang',
                                align: "center",
                                width: '16%',
                                title: '折扣价'
                            }, {
                                field: 'wangwang',
                                align: "center",
                                width: '19%',
                                title: '跳楼价'
                            }, {
                                field: 'weight',
                                width: '21%',
                                align: "center",
                                title: '重量（kg）'
                            }
                        ]],
                        done: function () {
                        }
                    });
                }
            });
            return false;
        });
    });
</script>
</html>