<%@ 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>
<body>
<style>
    #z4_craft .layui-form-select .layui-edge {
        right: 20px;
    }
</style>
<div class="big_box">
    <div class="left_div">
        <h1 class="h1">鼠标垫</h1> <span style="color:red;font-weight:700;"></span>
        <hr>
        <form class="layui-form">
            <input type="hidden" name="proTypeValue" id="proTypeValue" class="layui-input" value="鼠标垫"/>
            <p>
                材质
            </p>
            <div class="layui-form-item">
                <select name="kind" class="select" lay-search>
                    <option value="2">2mm</option>
                    <option value="3">3mm</option>
                    <option value="4">4mm</option>
                    <option value="5">5mm</option>
                </select>
            </div>
            <p>
                尺寸(CM/厘米) <span style="font-size: 14px; color: red">输入格式: 长 * 宽</span>
                <span>
                    <input type="checkbox" name="switchSize" lay-filter="switchSize" title="自定义尺寸">
                </span>
            </p>
            <div class="layui-form-item">
                <select name="size" id="size" class="layui-form-select">
                    <option value="21*26">21*26</option>
                    <option value="25*30">25*30</option>
                    <option value="30*70">30*70</option>
                    <option value="30*80">30*80</option>
                    <option value="40*90">40*90</option>
                </select>
            </div>
            <div class="layui-form-item" style="display: none">
                <input type="text" name="size" id="diySize" placeholder="格式:长*宽" class="layui-input" autocomplete="off">
            </div>

            <p>
                数量(个)
            </p>
            <div class="layui-form-item">
                <input type="text" name="count" id="count" placeholder="请输入整数" class="layui-input" autocomplete="off">
            </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>
            <div class="layui-form-item" id='z4_craft'>
                <div class="layui-input-block">
                    印面工艺：
                    <input type="checkbox" name="craft" lay-filter="ui_craft" value="单面" title="单面" checked>
                </div>
                <div class="layui-input-block">
                    裁切工艺：
                    <input type="checkbox" name="craft" lay-filter="ui_craft" value="圆角" title="圆角" checked>
                    <input type="checkbox" name="craft" lay-filter="ui_craft" value="异形模切" title="异形模切">
                </div>

                <div class="layui-input-block">
                    锁边工艺：
                    <span>
                        <input type="checkbox" name="craft" lay-filter="ui_craft" value="锁边" title="锁边" checked>
                    </span>
                    <input type="checkbox" name="craft" lay-filter="ui_craft" value="不锁边" title="不锁边">
                </div>
            </div>
            <hr>
            <div class="layui-form-item">
                <button class="layui-btn" lay-submit="" lay-filter="acount_btn">计算</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>
            <div>
                <table class="layui-hide" id="priceTable" lay-filter="priceTable"></table>
            </div>
        </form>
    </div>
    <div class="right_div" style="margin-left:50px;">
        <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;


        //建造实例
        ins = carousel.render({});
        var html = " ";
        var remark = " ";
        const carft_list1 = ["单面", "双面"];

        const carft_list2 = ["圆角", "异形模切"];

        const carft_list3 = ["锁边", "不锁边"];

        // 清空轮播图
        $("#carousel").empty();
        $("#remark").empty();
        $(".ui_feb").show();
        $(".ui_feb select").attr("disabled", false);
        $.ajax({
            url: "${pageContext.request.contextPath}/getImgs",
            type: "GET",
            data: {
                proTypeValue: $("#proTypeValue").val(),
                kindValue: $('input[name="kindValue"]').val()
            },
            success: function (result) {
                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);
                }
                // 如果没有轮播图就隐藏
                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('checkbox(switchSize)', function (data) {
            $("#diySize").parent().hide()
            $("#size").parent().show()
            $("#size").attr("disabled", false);
            $("#diySize").attr("disabled", true);
            if (data.elem.checked) {
                $("#diySize").parent().show()
                $("#size").parent().hide()
                $("#size").attr("disabled", true);
                $("#diySize").attr("disabled", false);

            }
            form.render();
        })
        form.on('checkbox(ui_craft)', function (data) {
            let craft_list = [];
            craft_list.push($("select[name='craft'] option:selected").val());
            $("input[name='craft']:checked").each(function () {
                    if (!$(this).is(':disabled')) {
                        craft_list.push($(this).val());
                    }
                }
            );

            const carft1 = carft_list1.filter(craft => craft_list.includes(craft));
            if (carft1.length == 0) {
                $(data.elem).prop("checked", true);
                layer.msg("印面工艺不能取消", {offset: ['300px', '300px']}, function () {
                });
                form.render();
                return false;
            }
            const carft2 = carft_list2.filter(craft => craft_list.includes(craft));
            if (carft2.length > 1) {
                $(data.elem).prop("checked", false);
                layer.msg("裁切工艺不能同时选择", {offset: ['300px', '300px']}, function () {
                });
                form.render();
                return false;
            }
            const carft3 = carft_list3.filter(craft => craft_list.includes(craft));
            if (carft3.length > 1) {
                $(data.elem).prop("checked", false);
                layer.msg("锁边工艺不能同时选择", {offset: ['300px', '300px']}, function () {
                });
                form.render();
                return false;
            }
            $("input[name='craft'][value='锁边']").parent().show();
            $("input[name='craft'][value='锁边']").attr("disabled", false)
            if (craft_list.includes("异形模切")) {
                $("input[name='craft'][value='锁边']").prop("checked", false);
                $("input[name='craft'][value='不锁边']").prop("checked", true);
                $("input[name='craft'][value='锁边']").parent().hide();
                $("input[name='craft'][value='锁边']").attr("disabled", true)
            }
            form.render();
        })

        // 点击计算，计算价格
        form.on('submit(acount_btn)', function (data) {
            var number = $("#number").val();
            var size = $("#size option:selected").val();
            var count = $("#count").val();
            var kind = $("select[name='kind'] option:selected").text();
            var craft = [];
            if ($("input[name='switchSize']").is(":checked")) {
                size = $("#diySize").val();
            }
            if (size == "") {
                layer.msg('请填写尺寸！', {offset: ['300px', '300px']}, function () {
                });
                return false;
            }
            if ((size.split("*")[0] > 300 || size.split("*")[1] > 150) && (size.split("*")[1] > 300 || size.split("*")[0] > 150)) {
                layer.msg('最大尺寸300*150cm！', {offset: ['300px', '300px']}, function () {
                });
                return false;
            }
            if ((size.split("*")[0] < 10 || size.split("*")[1] < 10)) {
                layer.msg('最小尺寸10*10cm！', {offset: ['300px', '300px']}, function () {
                });
                return false;
            }
            if (Number.parseFloat(size.split("*")[0]) % 1 !== 0 || Number.parseFloat(size.split("*")[1]) % 1 !== 0) {
                layer.msg('尺寸不能存在小数点！', {offset: ['300px', '300px']}, function () {
                });
                return false;
            }
            $("input:checkbox[name='craft']:checked").each(function (i) {
                // 没有被禁用的工艺加到arr中
                if (!$(this).is(':disabled')) {
                    craft.push($(this).val());
                }
            });

            const carft1 = carft_list1.filter(item => craft.includes(item));
            if (carft1.length == 0) {
                layer.msg("请选择印面工艺", {offset: ['300px', '300px']}, function () {
                });
                return false;
            }
            const carft2 = carft_list2.filter(item => craft.includes(item));
            if (carft2.length == 0) {
                layer.msg("请选择裁切工艺", {offset: ['300px', '300px']}, function () {
                });
                return false;
            }
            const carft3 = carft_list3.filter(item => craft.includes(item));
            if (carft3.length == 0) {
                layer.msg("请选择锁边工艺", {offset: ['300px', '300px']}, function () {
                });
                return false;
            }
            $.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;
                    }
                    var data = result.data.proList;

                    var span_result = '鼠标垫 - ' + kind + ' - ' + size + ' CM (同款内容)\n';

                    span_result += `工艺：` + craft.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 += '包邮，免费设计呢~（偏远地区需补邮费）'
                    $("#span_result").val(span_result);

                    //计算完自动复制文本
                    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>