<%@ 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>
<!-- 不干胶 -->
<style>
    .layui-carousel-ind ul {
        display: none
    }
</style>
<body>
<div class="big_box">
    <div class="left_div">
        <h1 class="h2">插旗</h1>
        </br></br>
        <span style="color:red;font-size:14px">
        使用方法:选择对应的产品种类。输入正确的尺寸、数量后点击“计算”出价格(自动复制)；
        </span>
        <form class="layui-form">
            <div class="layui-form-item">
                <input type="radio" lay-filter="kind2Value" name="kind2Value" value="常规" title="常规" checked>
                <input type="radio" lay-filter="kind2Value" name="kind2Value" value="数码" title="数码">
            </div>
        </form>
        <form class="layui-form" id="kindValueForm">
            <div class="kindValueSelect3">
                <div class="layui-form-item">
                    <p>
                        品种
                    </p>
                    <select name="kindValue" lay-filter="kindValue1" class="select" lay-search>
                        <option value=""></option>
                        <option value="铜版纸不干胶">铜纸板不干胶</option>
                        <option value="250克白卡纸对裱">250克白卡纸对裱</option>
                        <option value="PVC不干胶">PVC不干胶</option>
                    </select>
                </div>
            </div>
        </form>
        <form class="layui-form" id="stickersForm">
            <input type="hidden" name="proTypeValue" id="proTypeValue" class="layui-input" value="0">
            <input type="hidden" name="stickerKind" id="stickerKind" class="layui-input" value="插旗">
            <div class="ui_cq">
                <p>
                    尺寸(CM/厘米) <span style="font-size:14px;color:red">输入格式: 长 * 宽</span>
                </p>
                <div class="layui-form-item">
                    <input type="text" placeholder="格式:长*宽" name="size" id="size" class="layui-input" lay-verify="required" autocomplete="off">
                </div>
                <div class="layui-form-item ui_guaguasize" style="display: none;">
                    <p>
                        刮刮膜尺寸(MM/毫米)
                    </p>
                    <select name="size1" id="size1" class="select">
                        <option value="6*18">6*18 mm</option>
                    </select>
                </div>
                <div class="layui-form-item ui_toothpick">
                    <p>
                        牙签尺寸(CM/厘米)
                    </p>
                    <select name="toothpick_size" id="toothpick_size" class="select">
                        <option value="6.5">6.5 CM</option>
                        <option value="10">10 CM</option>
                        <option value="12">12 CM</option>
                    </select>
                </div>
                <p>
                    数量(张)
                </p>
                <div class="count">
                    <div class="layui-form-item">
                        <select name="count" id="selectCount" class="select">
                            <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>
                            <option value="20000">20000</option>
                            <option value="30000">30000</option>
                            <option value="40000">40000</option>
                            <option value="50000">50000</option>
                        </select>
                    </div>
                    <div class="layui-form-item" style="display: none">
                        <input name="count" class="layui-input" placeholder="请输入数量"/>
                    </div>
                </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>
            <hr>
        </form>
        <form class="layui-form" id="craftForm">
            <p>
                工艺
            </p>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    裁切工艺：
                    <input type="checkbox" name="craftQie" lay-filter="ui_craft" value="模切" title="模切" checked>
                    <span class="craftCaiQie">
                        <input type="checkbox" name="craftQie" lay-filter="ui_craft" value="裁切" title="裁切">
                    </span>
                </div>
                <div class="layui-input-block">
                    覆膜工艺：
                    <input type="checkbox" name="craftMo" lay-filter="ui_craft" value="覆哑膜" title="覆哑膜" checked>
                    <span class="craftLiangMo">
                        <input type="checkbox" name="craftMo" lay-filter="ui_craft" value="覆亮膜" title="覆亮膜">
                    </span>
                    <span class="craftNoMo">
                        <input type="checkbox" name="craftMo" lay-filter="ui_craft" value="不覆膜" title="不覆膜">
                    </span>
                </div>
                <div class="layui-input-block">
                    配件：
                    <span class="craftPeiYaQian">
                        <input type="checkbox" name="craftShua" lay-filter="ui_craft" value="配牙签" title="配牙签" checked>
                    </span>
                    <input type="checkbox" name="craftShua" lay-filter="ui_craft" value="粘牙签" title="粘牙签">
                    <span class="guaguaCraft">
                        <input type="checkbox" name="craft" lay-filter="ui_craft" value="配刮刮膜" title="配刮刮膜">
                        <input type="checkbox" name="craft" lay-filter="ui_craft" value="粘刮刮膜" title="粘刮刮膜">
                    </span>

                </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>
        </form>
        <form class="layui-form">
            <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="13" cols="75" id="span_result" readonly="readonly"></textarea>
                <%@include file="../acountExpressFee.jsp" %>
            </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>
<!-- <script src="../../js/acountExpressFee.js" charset="utf-8"></script> -->
<%@include file="/views/copyResult.jsp" %>
<script>

    layui.use(['element', 'table', 'form', 'carousel'], function () {
        var form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功
        var carousel = layui.carousel;
        var table = layui.table;
        var element = layui.element;
        var kValue;

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

        //建造实例
        ins = carousel.render({});
        var stickerKind = ""
        var TJValue = ""
        var kindvalue = ""

        function getProductImage(value) {
            let kind = $("input[name='kind2Value']:checked").val();
            let data = {proTypeValue: $("#proTypeValue").val(), kindValue: value, kind2Value: kind}
            $("#carousel").empty();
            $("#remark").empty();
            $.ajax({
                url: "${pageContext.request.contextPath}/getImgs",
                type: "GET",
                data,
                success: function (result) {
                    $("#carousel").empty();
                    let html;
                    if (result.length > 0) {
                        for (let i = 0; i < result.length; i++) {
                            // 只留一个remark
                            remark = "";
                            html += '<div><img style="width:100%;height: 100%;object-fit: contain" 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);
                        }

                        $("#carousel").append(html);
                        // 如果没有轮播图就隐藏
                        document.getElementById("test1").style.display = "block";   //显示
                        ins.reload({
                            elem: '#test1',
                            width: result[0]?.imgWidth,  //设置容器宽度
                            height: result[0]?.imgHeight
                        });
                    }

                }
            });
        }

        const craft_list1 = [["覆哑膜", "覆亮膜", "不覆膜"], ["模切", "裁切"], ["粘牙签", "配牙签"], ["配刮刮膜", "粘刮刮膜"]];

        function setCraftUnset() {
            for (let i = 0; i < craft_list1.length; i++) {
                for (let j = 0; j < craft_list1[i].length; j++) {
                    let name = craft_list1[i][j];
                    let craftChecked = $(`input[value='` + name + `']`).is(":checked");
                    if (craftChecked) {
                        $(`input[value='` + name + `']+div`).click();
                    }
                }
            }
            form.render()
        }

        form.on("radio(kind2Value)", function (data) {
            $("#selectCount").parent().hide();
            $("#selectCount").attr("disabled", true);
            $("input[name='count']").parent().hide();
            $("input[name='count']").attr("disabled", true);
            let html = ` <option value=""></option>
                        <option value="铜版纸不干胶">铜纸板不干胶</option>
                        <option value="250克白卡纸对裱">250克白卡纸对裱</option>
                        <option value="PVC不干胶">PVC不干胶</option>`;
            if (data.value == "常规") {
                $("#selectCount").parent().show();
                $("#selectCount").attr("disabled", false);
            }
            if (data.value == "数码") {
                $("input[name='count']").parent().show();
                $("input[name='count']").attr("disabled", false);
                html = ` <option value=""></option>
                        <option value="铜版纸不干胶">铜纸板不干胶</option>
                        <option value="PVC不干胶">PVC不干胶</option>`;
            }
            $("select[name='kindValue']").empty();
            $("select[name='kindValue']").append(html);
            form.render();
        })

        form.on("select(kindValue1)", function (data) {
            let kind = $("input[name='kind2Value']:checked").val();
            setCraftUnset();
            $(".guaguaCraft").hide();
            $(".guaguaCraft").find("input").attr("disabled", true);
            $(".craftCaiQie").hide();
            $(".craftCaiQie").find("input").attr("disabled", true);
            $(".craftLiangMo").hide();
            $(".craftLiangMo").find("input").attr("disabled", true);
            $(".craftNoMo").hide();
            $(".craftNoMo").find("input").attr("disabled", true);
            $(".craftPeiYaQian").hide();
            $(".craftPeiYaQian").find("input").attr("disabled", true);
            if (data.value == "铜版纸不干胶") {
                $(".guaguaCraft").show();
                $(".guaguaCraft").find("input").attr("disabled", false);
                $(".craftLiangMo").show();
                $(".craftLiangMo").find("input").attr("disabled", false);
                if (kind == "数码") {
                    $(".craftNoMo").show();
                    $(".craftNoMo").find("input").attr("disabled", false);
                }
                $(".craftPeiYaQian").show();
                $(".craftPeiYaQian").find("input").attr("disabled", false);
                $(`input[value='覆亮膜']+div`).click();
                $(`input[value='模切']+div`).click();
                $(`input[value='配牙签']+div`).click();
                getProductImage("14");

            }
            if (data.value == "250克白卡纸对裱") {
                $(".craftCaiQie").show();
                $(".craftCaiQie").find("input").attr("disabled", false);
                $(`input[value='覆哑膜']+div`).click();
                $(`input[value='模切']+div`).click();
                $(`input[value='粘牙签']+div`).click();
                getProductImage("250克白卡纸对裱");
            }
            if (data.value == "PVC不干胶") {
                $(".guaguaCraft").show();
                $(".guaguaCraft").find("input").attr("disabled", false);
                $(".craftLiangMo").show();
                $(".craftLiangMo").find("input").attr("disabled", false);
                $(".craftPeiYaQian").show();
                $(".craftPeiYaQian").find("input").attr("disabled", false);
                $(`input[value='覆亮膜']+div`).click();
                $(`input[value='模切']+div`).click();
                $(`input[value='配牙签']+div`).click();
                getProductImage("14");
            }
            form.render();
        })

        function getCraftInfo(name) {
            let craft = [];
            $("input:checkbox[name='" + name + "']:checked").each(function (i) {
                // 没有被禁用的工艺加到arr中
                if (!$(this).is(':disabled')) {
                    if ($(this).val() == "抠图") {
                        arr.push($(this).val() + $("input[name='cropNumber']").val());
                        return;
                    }
                    craft.push($(this).val());
                }
            });
            return craft
        }


        form.on('checkbox(ui_craft)', function (data) {
            let craft_list = [];
            craft_list = [...craft_list, ...getCraftInfo('craft'), ...getCraftInfo('craftShua'), ...getCraftInfo('craftMo'), ...getCraftInfo('craftQie')]
            for (let i = 0; i < craft_list1.length; i++) {
                const craft1 = craft_list1[i].filter(craft => craft_list.includes(craft));
                if (craft1.length > 1) {
                    $(data.elem).prop("checked", false);
                    layer.msg(craft_list1[i].join("-") + "不能同时选择", {offset: ['300px', '300px']}, function () {
                    });
                    form.render();
                    return false;
                }
            }
            let craftCount = craft_list1[3].filter(craft => craft_list.includes(craft));
            $(".ui_guaguasize").hide();
            if (craftCount.length > 0) {
                $(".ui_guaguasize").show();
            }
            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 kind = $('select[name="kindValue"] option:selected').val();
            var count = $("#count").val();
            var size = $("#size").val();
            var number = $("#number").val();
            var uri = "${pageContext.request.contextPath}/getThanSum";
            let craft_list = [];
            craft_list = [...craft_list, ...getCraftInfo('craft'), ...getCraftInfo('craftShua'), ...getCraftInfo('craftMo'), ...getCraftInfo('craftQie')]

            let craftCount = craft_list1[0].filter(craft => craft_list.includes(craft));
            if (craftCount.length == 0) {
                layer.msg("请选择覆膜工艺", {offset: ['300px', '300px']}, function () {
                });
                return false;
            }
            craftCount = craft_list1[1].filter(craft => craft_list.includes(craft));
            if (craftCount.length == 0) {
                layer.msg("请选择裁切工艺", {offset: ['300px', '300px']}, function () {
                });
                return false;
            }
            craftCount = craft_list1[2].filter(craft => craft_list.includes(craft));
            if (craftCount.length == 0) {
                layer.msg("请选择牙签工艺", {offset: ['300px', '300px']}, function () {
                });
                return false;
            }
            craft_list.push($("select[name='toothpick_size'] option:selected").val() + "cm牙签");
            craftCount = craft_list1[3].filter(craft => craft_list.includes(craft));
            if (craftCount.length > 0) {
                craft_list.push($("select[name='size1'] option:selected").val() + "mm刮刮膜");
            }
            $.ajax({
                url: uri,
                type: "GET",
                data: $(".big_box form").serialize(),
                success: function (result) {
                    if (result.code == 100) {
                        layer.msg(result.msg, {offset: ['300px', '300px']}, function () {
                        });
                        return false;
                    }
                    var data = result.data.proList;
                    let craft = []
                    var span_result = '插旗 - ' + kind + ' - ' + size + ' CM (同款内容)\n';

                    span_result += `工艺：` + craft_list.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折折扣哦!"
                    }
                    if (window.parent.system_isPddPrice) {
                        span_result += "\n\n周年庆专属福利：亲亲  当天下单立享9折优惠  加购其他产品可享85折优惠"
                    }
                    addLog(span_result);
                    $("#span_result").val(span_result);

                    //计算完自动复制文本
                    var e = document.getElementById("span_result");//对象是content
                    if (e.value != "") {
                        e.select();//选择对象
                        document.execCommand("Copy");//执行浏览器复制命令
                    }

                    if ((${ sessionScope.USER_SESSION.role.contains('1010')})) {
                        layer.msg('要加设计费！', {offset: ['300px', '300px']}, function () {
                        });
                    }

                    //生成表格
                    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: '折扣价',
                                templet: function (d) {
                                    /* if(d.kindLabel == "铜版纸不干胶"){
                                        return Math.ceil(d.price)
                                    }
                                    else if(d.kindLabel == "铜版纸不干胶3"){
                                        return Math.floor(d.price * 0.95)
                                    } */
                                    return Math.ceil(d.price)
                                }
                            }, {
                                field: 'wangwang',
                                align: "center",
                                width: '19%',
                                title: '跳楼价',
                                templet: function (d) {
                                    /*if(d.kindLabel == "铜版纸不干胶"){
                                        if(d.count <= 10000){
                                            return Math.ceil(d.price * 0.88)
                                        }
                                    } else if(d.kindLabel == "A级铜版纸不干胶2"){
                                        return Math.floor(d.price * 0.85)
                                    } */
                                    return Math.ceil(d.price)
                                    /* else if(d.kindLabel == "铜版纸不干胶3"){
                                        return Math.floor(d.price * 0.93)
                                    }else{
                                        return Math.ceil(d.price * 0.88)
                                    } */
                                }
                            }, {
                                field: 'weight',
                                width: '21%',
                                align: "center",
                                title: '重量（kg）'
                            }
                        ]],
                        done: function () {
                        }
                    });
                }
            });
            return false;
        });
    });
</script>
</html>