<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>Title</title>
    <link rel="stylesheet" th:href="@{/lib/layui/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/css/public.css}" media="all">
    <link rel="stylesheet" th:href="@{/lib/font-awesome-4.7.0/css/font-awesome.min.css}" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<!--常用语开始-->
<form class="layui-form" action="" lay-filter="formDemo">
    <div class="layui-form-item layui-inline" style="width: 100%">
        <label class="layui-form-label" style="width: 85px">常用语</label>
        <div class="layuimini-container" style="width: 850px; margin-left: 113px">
            <div class="layui-btn-group" style="margin-bottom: -8px">
                <button type="button" class="layui-btn  layui-btn-primary layui-btn-sm" lay-filter="subAdd1"><i
                        class="layui-icon"></i>新建
                </button>
                <button type="button" class="layui-btn  layui-btn-primary layui-btn-sm" lay-filter="subEdit1"><i
                        class="layui-icon"></i>编辑
                </button>
                <button type="button" class="layui-btn  layui-btn-primary layui-btn-sm" lay-filter="subView1"><i
                        class="layui-icon">&#xe63c;</i>查看
                </button>
                <button type="button" class="layui-btn  layui-btn-primary layui-btn-sm" lay-filter="subDelete1"><i
                        class="layui-icon"></i>删除
                </button>
            </div>
            <table class="layui-hide" id="currentTableId1" lay-filter="currentTableFilter1"></table>
        </div>
    </div>

    <!-- 右侧悬浮按钮 -->
    <div class="layui-form layuimini-form" style="padding-bottom: 100px">
        <div class="right-bottom-btn">
            <!--                <button class="layui-btn" lay-filter="selectBtn">-->
            <!--                    <i class="layui-icon layui-icon-ok"></i>选择-->
            <!--                </button>-->
            <button class="layui-btn" lay-filter="cancleBtn">
                <i class="layui-icon layui-icon-close"></i>关闭
            </button>
        </div>
    </div>
</form>
<script th:src="@{/lib/jquery-3.4.1/jquery-3.4.1.min.js}" charset="utf-8"></script>
<script th:src="@{/lib/layui/layui.js}" charset="utf-8"></script>
<script th:src="@{/lib/coco-message/coco-message.js}" charset="utf-8"></script>
<script th:src="@{/lib/xm-select-v1.2.2/xm-select.js}" charset="utf-8"></script>
<script th:src="@{/js/lay-config.js}" charset="utf-8"></script>
<script type="text/javascript" th:inline="javascript">
    AjaxUtil.ctx = /*[[@{/}]]*/'';
    var cyyCompany = [];//当前常用语
    var updateflag = 0;//更新标志
    var project = null;
    layui.use(['form', 'table'], function () {
        var form = layui.form,
            table = layui.table,
            layer = layui.layer,
            $ = layui.$;

        // 用于存放表格数据
        var tableData = new Array();
        $.ajax({
            url: AjaxUtil.ctx + 'prjProject/cyyList',
            type: "post",
            async: false,
            dataType: "json",
            success: function (result) {
                tableData = result.data.rows;
            }
        });
        var currTable1 = table.render({
            elem: '#currentTableId1',
            data: tableData,
            cols: [
                [
                    {type: "checkbox"},
                    {field: 'id', title: '序号', width: '8%', align: 'center'},
                    {field: 'cyy', title: '常用语'}
                ]
            ],
            autoSort: true,
            initSort: {
                field: 'id', //排序字段
                type: 'asc' //排序方式:desc 降序,asc 升序
            }
        });

        // //监听行选中
        // table.on('row(currentTableFilter1)', function (obj) {
        //     project = obj.data;
        //     obj.tr.addClass('layui-bg-blue').siblings().removeClass('layui-bg-blue');
        //
        //     localStorage.setItem("xmlx", project.xmlx == null ? '' : project.xmlx);
        // });
        // //双击（常用语）
        // table.on('rowDouble(currentTableFilter1)', function (obj) {
        //     project = obj.data;
        //     obj.tr.addClass('layui-bg-blue').siblings().removeClass('layui-bg-blue');
        //     var data = obj.data;
        //     cyyCompany = {
        //         "id": data.id,
        //         "cyy": data.cyy
        //     };
        //     localStorage.setItem("xmlx", project.xmlx == null ? '' : project.xmlx);
        //     localStorage.setItem("updateflag", "1");
        //     parent.layer.close(parent.layer.getFrameIndex(window.name));// 关闭子窗口
        // });

        // 监听新建按钮（常用语）
        $("button[lay-filter='subAdd1']").click(function () {
            layer.open({
                title: '新建常用语',
                type: 2,
                shade: 0.2,
                area: [layerwidth + 'px', layerheight + 'px'],
                content: AjaxUtil.ctx + 'prjProject/forCyyAdd',
                end: function (index) {
                    if (updateflag == 1) {
                        updateflag = 0;
                        var oldData = table.cache["currentTableId1"];

                        var item = {
                            "id": cyyCompany.id,
                            "cyy": cyyCompany.cyy
                        };
                        //刷新表格
                        // oldData.push(item);
                        // table.reload('currentTableId1',{
                        //     data : oldData
                        // });

                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                    }
                }
            });
        });
        // 监听编辑按钮(常用语）
        $("button[lay-filter='subEdit1']").click(function () {
            var companys = table.cache["currentTableId1"];
            if (companys.length == 0)
                return;

            var checkStatus = table.checkStatus('currentTableId1')
                , data = checkStatus.data;
            if (data == null || data.length === 0) {
                Message.warning("请选择要编辑的常用语！", 1500);
            } else if (data.length > 1) {
                Message.warning("请选择一条常用语进行编辑操作！", 1500);
            } else {
                cyyCompany = {
                    "id": data[0].id,
                    "cyy": data[0].cyy
                };

                layer.open({
                    title: '编辑常用语',
                    type: 2,
                    // maxmin: true,
                    shade: 0.2,
                    area: [layerwidth + 'px', layerheight + 'px'],
                    content: AjaxUtil.ctx + 'prjProject/forUpdate',
                    end: function (index) {

                        if (updateflag == 1) {
                            updateflag = 0;
                            var oldData = table.cache["currentTableId1"];

                            var currCompany = {
                                "id": cyyCompany.id,
                                "cyy": cyyCompany.cyy
                            };

                            for (var i = 0; i < oldData.length; i++) {
                                var item = oldData[i];
                                if (item.LAY_CHECKED) {//条件：选中
                                    // oldData.splice(i, 1);//移除后后造成数组下标索引发生变化，所以下面需要i--
                                    oldData[i] = currCompany;
                                    break;
                                }
                            }
                            //刷新表格
                            var currData = oldData;
                            // currData.push(currCompany);
                            table.reload('currentTableId1', {
                                data: currData
                            });
                        }
                    }
                });
            }
        });
        // 监听查看按钮(常用语）
        $("button[lay-filter='subView1']").click(function () {
            var companys = table.cache["currentTableId1"];
            if (companys.length == 0)
                return;

            var checkStatus = table.checkStatus('currentTableId1')
                , data = checkStatus.data;
            if (data == null || data.length === 0) {
                Message.warning("请选择要浏览的常用语！", 1500);
            } else if (data.length > 1) {
                Message.warning("请选择一条常用语进行浏览！", 1500);
            } else {
                cyyCompany = {
                    "id": data[0].id,
                    "cyy": data[0].cyy
                };

                layer.open({
                    title: '浏览常用语',
                    type: 2,
                    // maxmin: true,
                    shade: 0.2,
                    area: [layerwidth + 'px', layerheight + 'px'],
                    content: AjaxUtil.ctx + 'prjProject/forCyyView',
                    end: function (index) {
                        layer.close(index);
                    }
                });
            }
        });
        // 监听删除按钮（常用语）
        $("button[lay-filter='subDelete1']").click(function () {
            var companys = table.cache["currentTableId1"];
            if (companys.length == 0)
                return;

            var checkStatus = table.checkStatus('currentTableId1')
                , data = checkStatus.data;
            if (data == null || data.length === 0) {
                Message.warning("请选择要删除的常用语！", 1000);
                return;
            } else if (data.length != 0) {
                AjaxUtil.get({
                    url: AjaxUtil.ctx + 'prjProject/forCyyDelete/' + data[0].id
                });
            }

            var oldData = table.cache["currentTableId1"];
            for (var i = 0; i < oldData.length; i++) {
                var item = oldData[i];
                if (item.LAY_CHECKED) {//条件：选中
                    oldData.splice(i, 1);//移除后后造成数组下标索引发生变化，所以下面需要i--
                    i--;
                }
            }
            var currData = oldData;
            table.reload('currentTableId1', {
                data: currData
            });
        });

        // // 监听选择按钮
        // $("button[lay-filter='selectBtn']").click(function(){
        //     if(project == null)
        //     {
        //         Message.warning("请选择项目", 1000);
        //         return;
        //     }
        //     localStorage.setItem("updateflag", "1");
        //
        //     parent.layer.close(parent.layer.getFrameIndex(window.name));// 关闭子窗口
        // });

        // 监听关闭按钮
        $("button[lay-filter='cancleBtn']").click(function () {
            localStorage.setItem("updateflag", "0");
            var iframeIndex = parent.layer.getFrameIndex(window.name);
            parent.layer.close(iframeIndex);
        });
    });

    function getCyyCompany() {
        return cyyCompany;
    }

    function setCyyCompany(expense) {
        cyyCompany = expense;
    }

    function setUpdateFlag() {
        updateflag = 1;
    }
</script>
</body>
</html>