<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('专业术语列表')" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>词条：</label>
                                <input type="text" name="wrongWord"/>
                            </li>

                            <li>
                                <label>错误类型：</label>
                                <div class="error-type-dropdown dropdown-container">
                                    <button type="button" class="dropdown-toggle form-control" id="errorTypeDropdown">
                                        <span id="selectedErrorType"  style="font-size: 13px">所有</span>
                                        <span class="caret"></span>
                                    </button>
                                    <input type="hidden" name="errorType" id="errorTypeValue" value="">
                                    <ul class="dropdown-menu error-type-menu" id="errorTypeMenu" style="display:none; width: 360px;">
                                        <div class="error-type-column" id="errorTypeMain"></div>
                                        <div class="error-type-column" id="errorTypeSub"></div>
                                    </ul>
                                </div>
                            </li>

                            <style>
                                /* 仅限错误类型联动选择器样式，避免影响其他 */
                                .error-type-dropdown {
                                    position: relative;
                                    display: inline-block;
                                    width: 230px;
                                }
                                .error-type-dropdown .dropdown-toggle {
                                    text-align: left;
                                    /*padding: 6px 12px;*/
                                    width: 100%;
                                    height: 30px;
                                    border: 1px solid #ccc;
                                    border-radius: 4px;
                                    background: #fff;
                                    cursor: pointer;
                                    position: relative;
                                }

                                .error-type-dropdown .dropdown-toggle .caret {
                                    position: absolute;
                                    right: 10px;
                                    top: 50%;
                                    transform: translateY(-50%);
                                }
                                .error-type-dropdown .error-type-menu {
                                    position: absolute;
                                    top: 100%;
                                    left: 0;
                                    background: #fff;
                                    border: 1px solid #ccc;
                                    border-radius: 4px;
                                    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
                                    width: 400px;
                                    display: flex;
                                    z-index: 1000;
                                    max-height: 300px;
                                    overflow: hidden;
                                }
                                .error-type-dropdown .error-type-column {
                                    width: 50%;
                                    overflow-y: auto;
                                    border-right: 1px solid #eee;
                                }
                                .error-type-dropdown .error-type-column:last-child {
                                    border-right: none;
                                }
                                .error-type-dropdown .cascader-item {
                                    padding: 8px 12px;
                                    cursor: pointer;
                                }
                                .error-type-dropdown .cascader-item:hover {
                                    background-color: #f5f5f5;
                                }
                            </style>

                            <script th:inline="javascript">
                                /*<![CDATA[*/
                                var errorTypeDict = /*[[${@dict.getType('trs_error_type')}]]*/ [];
                                /*]]>*/
                            </script>

                            <script th:inline="javascript">
                                /*<![CDATA[*/
                                document.addEventListener('DOMContentLoaded', function () {
                                    var cascaderData = {};
                                    errorTypeDict.forEach(function(item) {
                                        if (!item.dictLabel || !item.dictValue) return;
                                        var parts = item.dictLabel.split("-");
                                        if(parts.length < 2) return;
                                        var main = parts[0];
                                        var subLabel = parts.slice(1).join("-");
                                        if (!cascaderData[main]) cascaderData[main] = [];
                                        cascaderData[main].push({ label: item.dictLabel, value: item.dictValue });
                                    });

                                    var $dropdown = document.getElementById('errorTypeDropdown');
                                    var $menu = document.getElementById('errorTypeMenu');
                                    var $main = document.getElementById('errorTypeMain');
                                    var $sub = document.getElementById('errorTypeSub');
                                    var $selected = document.getElementById('selectedErrorType');
                                    var $hidden = document.getElementById('errorTypeValue');

                                    // 填充主菜单
                                    Object.keys(cascaderData).forEach(function(mainKey) {
                                        var div = document.createElement('div');
                                        div.className = 'cascader-item';
                                        div.textContent = mainKey;
                                        div.dataset.main = mainKey;
                                        $main.appendChild(div);

                                        div.addEventListener('mouseover', function () {
                                            $sub.innerHTML = '';
                                            cascaderData[this.dataset.main].forEach(function(subItem) {
                                                var subDiv = document.createElement('div');
                                                subDiv.className = 'cascader-item';
                                                subDiv.textContent = subItem.label;
                                                subDiv.dataset.value = subItem.value;
                                                $sub.appendChild(subDiv);

                                                subDiv.addEventListener('click', function(e) {
                                                    e.stopPropagation();
                                                    $selected.textContent = this.textContent;
                                                    $hidden.value = this.dataset.value;
                                                    $menu.style.display = 'none';
                                                });
                                            });
                                        });
                                    });

                                    $dropdown.addEventListener('click', function(e) {
                                        e.stopPropagation();
                                        $menu.style.display = ($menu.style.display === 'flex') ? 'none' : 'flex';
                                    });

                                    document.addEventListener('click', function() {
                                        $menu.style.display = 'none';
                                    });

                                    // URL回填
                                    var urlParams = new URLSearchParams(window.location.search);
                                    var errorTypeVal = urlParams.get('errorType');
                                    if (errorTypeVal) {
                                        var found = false;
                                        errorTypeDict.forEach(function(item){
                                            if(item.dictValue === errorTypeVal){
                                                $selected.textContent = item.dictLabel;
                                                $hidden.value = item.dictValue;
                                                found = true;
                                            }
                                        });
                                        if (!found) {
                                            $selected.textContent = '所有';
                                            $hidden.value = '';
                                        }
                                    } else {
                                        $selected.textContent = '所有';
                                        $hidden.value = '';
                                    }

                                    // 重置按钮处理
                                    document.getElementById('formId').addEventListener('reset', function () {
                                        setTimeout(function () {
                                            document.getElementById('selectedErrorType').textContent = '所有';
                                            document.getElementById('errorTypeValue').value = '';
                                        }, 0);
                                    });


                                });
                                /*]]>*/
                            </script>
                            <!--<li>
                                <label>错误类型：</label>
                                <select name="errorType" th:with="type=${@dict.getType('trs_error_type')}">
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                </select>
                            </li>-->
                            <li>
                                <label>审核状态：</label>
                                <select name="status" th:with="type=${@dict.getType('trs_dict_status')}">
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li class="select-time">
                                <label>时间：</label>
                                <input type="text" class="time-input" id="startTime" placeholder="开始时间" name="params[beginTime]"/>
                                <span>-</span>
                                <input type="text" class="time-input" id="endTime" placeholder="结束时间" name="params[endTime]"/>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="project:technical:add">
                    <i class="fa fa-plus"></i> 添加
                </a>
                <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="project:technical:edit">
                    <i class="fa fa-edit"></i> 修改
                </a>
                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="project:technical:remove">
                    <i class="fa fa-remove"></i> 删除
                </a>
                <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="project:technical:export">
                    <i class="fa fa-download"></i> 导出
                </a>
                <a class="btn btn-success" onclick="$.table.importExcel()" shiro:hasPermission="project:technical:import">
                    <i class="fa fa-plus"></i> 导入
                </a>
                <a class="btn btn-info" onclick="confirmstatus()" shiro:hasPermission="project:technical:edit">
                    <i class="fa fa-chevron-circle-right"></i> 批量审批
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('project:technical:edit')}]];
        var removeFlag = [[${@permission.hasPermi('project:technical:remove')}]];
        var errorTypeDatas = [[${@dict.getType('trs_error_type')}]];
        var dictTypeDatas = [[${@dict.getType('trs_dict_type')}]];
        var dictStatusDatas = [[${@dict.getType('trs_dict_status')}]];
        var prefix = ctx + "project/technical";

        $(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                importUrl: prefix + "/importData",
                importTemplateUrl: prefix + "/importTemplate",
                modalName: "专业术语",
                showPageGo: true,          // 启用跳转到指定页码功能
                columns: [{
                    checkbox: true
                },
                {
                    field: 'id',
                    title: '主键',
                    visible: false
                },
                {
                    field: 'wrongWord',
                    title: '词条'
                },
                {
                    field: 'errorType',
                    title: '错误类型',
                    formatter: function(value, row, index) {
                       return $.table.selectDictLabel(errorTypeDatas, value);
                    }
                },
                {
                    field: 'status',
                    title: '审核状态',
                    formatter: function(value, row, index) {
                        return $.table.selectDictLabel(dictStatusDatas, value);
                    }
                },
                {
                    field: 'time',
                    width: '100px',
                    title: '时间'
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');

                        var more = [];
                        more.push("<a class='btn btn-default btn-xs " + editFlag + "' href='javascript:void(0)' onclick='checkstatus(" + row.id + ",1)'><i class='fa fa-support'></i>通过</a> ");
                        more.push("<a class='btn btn-default btn-xs " + editFlag + "' href='javascript:void(0)' onclick='checkstatus(" + row.id + ",2)'><i class='fa fa-remove'></i>拒绝</a>");
                        actions.push('<a tabindex="0" class="btn btn-info btn-xs" role="button" data-container="body" data-placement="left" data-toggle="popover" data-html="true" data-trigger="hover" data-content="' + more.join('') + '"><i class="fa fa-chevron-circle-right"></i>审批</a>');

                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        });
        function checkstatus(id,status) {
            if (status == 1) {
                $.modal.confirm("是否确认审批字词词条？", function() {
                    $.operate.post(prefix + "/editstatus", { "ids": id, "status": "已通过" });
                })
            } else if (status == 2) {
                $.modal.confirm("是否确认审批字词词条？", function() {
                    $.operate.post(prefix + "/editstatus", { "ids": id, "status": "已拒绝" });
                })
            }
        }
        function confirmstatus() {
            table.set();
            var rows = $.common.isEmpty(table.options.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns(table.options.uniqueId);
            if (rows.length == 0) {
                $.modal.alertWarning("请至少选择一条记录");
                return;
            }
            $.modal.confirm("确认要审核通过选中的" + rows.length + "条数据吗?", function() {
                var data = { "ids": rows.join() };
                $.operate.post(prefix + "/editstatus", { "ids": rows.join(), "status": "已通过" });
            });
        }
        $(function() {
            $('#formId').on('keydown', function(e) {
                if (e.key === 'Enter') {
                    e.preventDefault();
                    $.table.search();
                }
            });
        });
    </script>
     <!-- 导入区域 -->
     <script id="importTpl" type="text/template">
         <form enctype="multipart/form-data" class="mt20 mb10">
             <div class="col-xs-offset-1">
                 <input type="file" id="file" name="file"/>
                 <div class="mt10 pt5">
                     <input type="checkbox" checked="checked" id="updateSupport" name="updateSupport" title="如果数据已经存在，更新这条数据。"> 是否更新已经存在的数据
                     &nbsp;	<a onclick="$.table.importTemplate()" class="btn btn-default btn-xs"><i class="fa fa-file-excel-o"></i> 下载模板</a>
                 </div>
                 <font color="red" class="pull-left mt10">
                     提示：仅允许导入“xls”或“xlsx”格式文件！
                 </font>
             </div>
         </form>
     </script>
</body>
</html>