<!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">
                <div class="select-list">
                    <div class="form-group">
                        <div class="col-sm-12">
                            <p class="form-control-static" th:attr="style='color:#333'" th:text="'模型名称 ：' + ${modelProject.modelName}"></p>
                        </div>
                        <div class="col-sm-12">
                            <p class="form-control-static" th:attr="style='color:#333'" th:text="'模型描述 ：' + ${modelProject.modelRemark}"></p>
                        </div>
                    </div>
                </div>
            </div>

            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <input type="hidden" id="projectId" name="projectId" th:value="${modelProject.id}">
                    <input type="hidden" id="versionId" name="versionId" value="0">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>表名称：</label>
                                <input type="text" name="entityName"/>
                            </li>
                            <li>
                                <label>表逻辑名：</label>
                                <input type="text" name="entityCode"/>
                            </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="add()" shiro:hasPermission="model:modelEntity:add">
                    <i class="fa fa-plus"></i> 添加
                </a>
                <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="model:modelEntity:edit">
                    <i class="fa fa-edit"></i> 修改
                </a>
                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="model:modelEntity:remove">
                    <i class="fa fa-remove"></i> 删除
                </a>
<!--                <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="model:modelEntity:export">-->
<!--                    <i class="fa fa-download"></i> 导出-->
<!--                </a>-->
                <a class="btn btn-warning" onclick="importTable()" shiro:hasPermission="model:modelEntity:import">
                    <i class="fa fa-upload"></i> 从数据库导入
                </a>
                <a class="btn btn-primary" onclick="$.table.importExcel()" shiro:hasPermission="model:modelEntity:import">
                    <i class="fa fa-edit"></i> 从文件导入
                </a>
                <a class="btn btn-danger" onclick="closeItem()">
                    <i class="fa fa-reply-all"></i> 关闭
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"
                       data-use-row-attr-func="true"
                       data-reorderable-rows="true"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: bootstrap-table-reorder-rows-js" />
     <!-- 导入区域 -->
     <script id="importTpl" type="text/template">
         <form enctype="multipart/form-data" class="mt20 mb10">
             <div class="col-xs-offset-1">
                 <input type="hidden" id="prjId" name="prjId" th:value="${modelProject.id}">
                 <input type="file" id="file" name="file"/>
                 <div class="mt10 pt5">
                     点击这里<a onclick="$.table.importTemplate()" class="btn btn-default btn-xs"><i class="fa fa-file-excel-o"></i> 下载模板</a>文件
                 </div>
                 <p class="pull-left mt10 text-danger">
                     提示：仅允许导入“xls”或“xlsx”格式文件！
                 </p>
             </div>
         </form>
     </script>
    <script th:inline="javascript">
        var addFlag = [[${@permission.hasPermi('model:modelEntity:add')}]];
        var editFlag = [[${@permission.hasPermi('model:modelEntity:edit')}]];
        var removeFlag = [[${@permission.hasPermi('model:modelEntity:remove')}]];
        var modelFieldFlag = [[${@permission.hasPermi('model:modelField:view')}]];

        var prefix = ctx + "model/modelEntity";

        $(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add/{id}",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                importUrl: prefix + "/importExpress",
                importTemplateUrl: prefix + "/importTemplate",
                modalName: "数据",
                pagination: true,
                showSearch: true,
                showRefresh: true,
                showToggle: true,
                showColumns: true,
                uniqueId: "id",
                sortName: "entitySort",
                sortOrder: "asc",
                onReorderRow: function (data) {
                    saveSorted(JSON.stringify(data))
                    return true;
                },
                // sidePagination: "client",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'id',
                    title: '主键',
                    visible: false
                },
                {
                    field: 'projectId',
                    title: '模型对象ID',
                    visible: false
                },
                {
                    field: 'versionId',
                    title: '版本id',
                    visible: false
                },
                {
                    field: 'entityName',
                    title: '表名称'
                },
                {
                    field: 'entityCode',
                    title: '表逻辑名'
                },
                {
                    field: 'remark',
                    title: '描述'
                },
                {
                    field: 'parentVersionId',
                    title: '基版本id',
                    visible: false
                },
                {
                    title: '操作',
                    align: 'left',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-warning btn-xs ' + modelFieldFlag + '" href="javascript:void(0)" onclick="modelField(\'' + row.projectId + '\',\'' + row.id + '\')"><i class="fa fa-list"></i> 字段列表 </a> ');
                        actions.push('<a class="btn btn-success btn-xs ' + addFlag + '" href="javascript:void(0)" onclick="copy(\'' + row.id + '\',\'' + row.entityName + '\')"><i class="fa fa-edit"></i>复制</a> ');
                        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>');
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        });

        /*新增数据*/
        function add() {
            var projectId = $("#projectId").val();
            $.operate.add(projectId);
        }

        /*复制表结构*/
        function copy(id,entityName) {
            $.modal.open("复制" + entityName, prefix + "/copy/"+ id);
        }

        /** 字段列表 */
        function modelField(projectId, entityId) {
            var data = $("#bootstrap-table").bootstrapTable('getRowByUniqueId', entityId);
            var url = ctx + "model/modelField?projectId=" + projectId + "&entityId=" + entityId + "&versionId=0";
            $.modal.openTab(data.entityCode+"["+data.entityName+"]", url);
        }

        // 从数据库导入
        function importTable() {
            var projectId = $("#projectId").val();
            var checkFlag = false;
            var config = {
                url: prefix + "/importTableCheck",
                type: "post",
                dataType: "json",
                data: {
                    "projectId": projectId
                },
                success: function(result) {
                    if (result.code == web_status.SUCCESS) {
                        openImportTable();
                    }  else if (result.code == web_status.WARNING) {
                        $.modal.alertWarning(result.msg);
                    }  else {
                        $.modal.alertError(result.msg);
                    }
                }
            };
            $.ajax(config);
        }

        function openImportTable() {
            var projectId = $("#projectId").val();
            var importTableUrl = prefix + "/importTable/"+projectId;
            $.modal.open("从数据库导入", importTableUrl);
        }

        function saveSorted(data) {
            var table = $("#bootstrap-table").bootstrapTable('getOptions');
            var pageSize = table.pageSize;
            var pageNumber = table.pageNumber;
            var sortParams = {
                "pageSize": pageSize,
                "pageNumber": pageNumber,
                "sortData": data
            };

            $.ajax({
                url: prefix + "/sort",
                type: "post",
                dataType: "json",
                data: {
                    params: sortParams
                },
                beforeSend: function () {
                    $.modal.loading("正在处理中，请稍后...");
                    $.modal.disable();
                },
                success: function(result) {
                    if (result.code == web_status.SUCCESS) {
                        $.table.search();
                        $.modal.msgSuccess(result.msg)
                    } else if (result.code == web_status.WARNING) {
                        $.modal.alertWarning(result.msg)
                    } else {
                        $.modal.alertError(result.msg);
                    }
                    $.modal.closeLoading();
                    $.modal.enable();
                }
            })
        }
    </script>
</body>
</html>