<!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-6">
                            <p class="form-control-static" th:attr="style='color:#333'" th:text="'模型名称 ：' + ${modelProject.modelName}"></p>
                            <p class="form-control-static" th:attr="style='color:#333'" th:text="'模型描述 ：' + ${modelProject.modelRemark}"></p>
                        </div>
                        <div class="col-sm-6" th:if="${modelEntity.entityName != null && modelEntity.entityName != ''}">
                            <p class="form-control-static" th:attr="style='color:#333'" th:text="'表名称 ：' + ${modelEntity.entityName} + '[' + ${modelEntity.entityCode} + ']'"></p>
                            <p class="form-control-static" th:attr="style='color:#333'" th:text="'表描述 ：' + ${modelEntity.remark==null?' ':modelEntity.remark}"></p>
                        </div>
                    </div>
                </div>
                <form id="form-add" class="form-horizontal">
                    <input type="hidden" id="projectId" name="projectId" th:value="${modelProject.id}">
                    <input type="hidden" id="entityId" name="entityId" th:value="${modelEntity.id}">
                    <input type="hidden" id="versionId" name="versionId" th:value="0">
                </form>
            </div>
            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="add()" shiro:hasPermission="model:modelField:add">
                    <i class="fa fa-plus"></i> 添加
                </a>
                <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="model:modelField:edit">
                    <i class="fa fa-edit"></i> 修改
                </a>
                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="model:modelField:remove">
                    <i class="fa fa-remove"></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>


     <div th:include="include :: footer"></div>
     <th:block th:include="include :: bootstrap-table-reorder-rows-js" />

     <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('model:modelField:edit')}]];
        var removeFlag = [[${@permission.hasPermi('model:modelField:remove')}]];

        var dataType = [[${dataTypeList}]];

        var prefix = ctx + "model/modelField";

        $(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                modalName: "数据字段",
                pagination: true,
                showSearch: false,
                showRefresh: true,
                showToggle: true,
                showColumns: true,
                // sidePagination: "client",
                sortName: "fieldSort",
                sortOrder: "asc",
                onReorderRow: function (data) {
                    saveSorted(JSON.stringify(data))
                    return true;
                },
                columns: [{
                    checkbox: true
                },
                    {
                        field: 'id',
                        title: '主键',
                        visible: false
                    },
                    {
                        field: 'fieldName',
                        title: '字段名'
                    },
                    {
                        field: 'fieldCode',
                        title: '逻辑名'
                    },
                    {
                        field: 'fieldType',
                        title: '类型',
                        formatter: function(value, row, index) {
                            return fieldType(value, row, index);
                        }
                    },
                    {
                        field: 'fieldPk',
                        title: '主键',
                        align: 'center',
                        formatter: function(value, row, index) {
                            if (value == 'Y') {
                                return '<i class=\"fa fa-toggle-on text-info fa-2x\" onclick="disablePK(\'' + row.id + '\')"></i> ';
                            } else {
                                return '<i class=\"fa fa-toggle-off text-info fa-2x\" onclick="enablePK(\'' + row.id + '\')"></i> ';
                            }
                        }
                    },
                    {
                        field: 'notNull',
                        title: '非空',
                        align: 'center',
                        formatter: function(value, row, index) {
                            if (value == 'Y') {
                                return '<i class=\"fa fa-toggle-on text-info fa-2x\" onclick="disableNull(\'' + row.id + '\')"></i> ';
                            } else {
                                return '<i class=\"fa fa-toggle-off text-info fa-2x\" onclick="enableNull(\'' + row.id + '\')"></i> ';
                            }
                        }
                    },
                    {
                        field: 'autoIncrement',
                        title: '自增',
                        align: 'center',
                        formatter: function(value, row, index) {
                            if (value == 'Y') {
                                return '<i class=\"fa fa-toggle-on text-info fa-2x\" onclick="disableIncrement(\'' + row.id + '\')"></i> ';
                            } else {
                                return '<i class=\"fa fa-toggle-off text-info fa-2x\" onclick="enableIncrement(\'' + row.id + '\')"></i> ';
                            }
                        }
                    },
                    {
                        field: 'defaultValue',
                        title: '默认值'
                    },
                    {
                        field: 'remark',
                        title: '备注'
                    },
                    {
                        title: '操作',
                        align: 'left',
                        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>');
                            return actions.join('');
                        }
                    }
                ]
            };
            $.table.init(options);
        });

        /* 用户状态显示 */
        function fieldType(value, row, index) {
            for(var i=0; i< dataType.length; i++){
                if(dataType[i].dataTypeCode == value) {
                    return dataType[i].dataTypeName;
                }
            }
            return value;
        }

        /** 新增字段 */
        function add() {
            var projectId = $("#projectId").val();
            var entityId = $("#entityId").val();
            var url = ctx + "model/modelField/add?projectId=" + projectId + "&entityId=" + entityId + "&versionId=0";
            $.modal.open("新增字段", url);
        }

        /* 主键-停用 */
        function disablePK(id) {
            $.operate.post(prefix + "/changeStatus", { "id": id, "fieldPk": "N" });
        }

        /* 主键-启用 */
        function enablePK(id) {
            $.operate.post(prefix + "/changeStatus", { "id": id, "fieldPk": "Y" });
        }

        /* 非空-停用 */
        function disableNull(id) {
            $.operate.post(prefix + "/changeStatus", { "id": id, "notNull": "N" });
        }

        /* 非空-启用 */
        function enableNull(id) {
            $.operate.post(prefix + "/changeStatus", { "id": id, "notNull": "Y" });
        }

        /* 自增-停用 */
        function disableIncrement(id) {
            $.operate.post(prefix + "/changeStatus", { "id": id, "autoIncrement": "N" });
        }

        /* 自增-启用 */
        function enableIncrement(id) {
            $.operate.post(prefix + "/changeStatus", { "id": id, "autoIncrement": "Y" });
        }

        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>