<%--
  Created by IntelliJ IDEA.
  User: njc
  Date: 2022/5/9
  Time: 19:12
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<script>
    var url;

    /**
     * "添加"按钮的事件
     */
    function openLinkAddDialog() {
        $("#l-dlg").dialog("open").dialog("setTitle", "添加友情链接信息");
        url = "${pageContext.request.contextPath}/admin/link/save.do";
    }

    /**
     * "修改"按钮的事件
     */
    function openLinkModifyDialog() {

        var selectedRows = $("#dg").datagrid("getSelections");
        if (selectedRows.length !== 1) {
            $.messager.alert("系统提示", "请选择一条要编辑的数据！")
            return;
        }
        var row = selectedRows[0];
        $("#l-dlg").dialog("open").dialog("setTitle", "修改友情链接信息");
        $("#l-fm").form("load", row);
        url = "${pageContext.request.contextPath}/admin/link/save.do?id=" + row.id;
    }

    /**
     * 删除按钮事件
     */
    function deleteLink() {
        var selectedRows = $("#dg").datagrid("getSelections");
        if (selectedRows.length === 0) {
            $.messager.alert("系统提示", "请至少选择一条要编辑的数据！");
            return;
        }
        var strIds = [];
        for (var i = 0; i < selectedRows.length; i++) {
            strIds.push(selectedRows[i].id);
        }
        var ids = strIds.join(",");
        $.messager.confirm("系统提示", "您确定要删除这<font color=red>" + selectedRows.length + "</font>条数据吗？", function (r) {
            if (r) {
                $.post("${pageContext.request.contextPath}/admin/link/delete.do", {ids: ids}, function (result) {
                    if (result.success) {
                        $.messager.alert("系统提示", "数据已经成功删除！");
                        $("#dg").datagrid("reload");
                    } else if (result.exists) {
                        $.messager.alert("系统提示", result.exists);
                    } else {
                        $.messager.alert("系统提示", "数据删除失败！");
                    }
                }, "json");
            }
        });
    }

    /**
     * "保存"按钮事件
     */
    function saveLink() {
        $("#l-fm").form("submit", {
            url: url,
            onSubmit: function () {
                return $(this).form("validate");
            },
            success: function (result) {
                var result = eval('(' + result + ')');
                if (result.success) {
                    $.messager.alert("系统提示", "保存成功！");
                    resetValue();
                    $("#l-dlg").dialog("close");
                    $("#dg").datagrid("reload");
                } else {
                    $.messager.alert("系统提示", "保存失败！");
                }
            }
        });
    }

    /**
     * "关闭"按钮事件
     */
    function closeLinkDialog() {
        $("#l-dlg").dialog("close");
        resetValue();
    }

    /**
     * 重置弹出对话框
     */
    function resetValue() {
        $("#linkName").val("");
        $("#linkUrl").val("");
        $("#orderNo").val("");
    }
</script>
<table id="dg" title="友情链接管理" class="easyui-datagrid" fitcolumns="true" pagination="true" rownumbers="true"
       url="${pageContext.request.contextPath}/admin/link/list.do" fit="true" toolbar="#l-tb">
    <thead>
    <tr>
        <th field="cb" checkbox="true" align="center"></th>
        <th field="id" width="30" align="center">编号</th>
        <th field="linkName" width="100" align="center">友情链接名称</th>
        <th field="linkUrl" width="100" align="center">友情链接地址</th>
        <th field="orderNo" width="100" align="center">排序序号</th>
    </tr>
    </thead>
</table>

<div id="l-tb">
    <a href="javascript:openLinkAddDialog()" class="easyui-linkbutton" iconcls="icon-add" plain="true">添加</a>
    <a href="javascript:openLinkModifyDialog()" class="easyui-linkbutton" iconcls="icon-edit" plain="true">修改</a>
    <a href="javascript:deleteLink()" class="easyui-linkbutton" iconcls="icon-remove" plain="true">删除</a>
</div>

<div id="l-dlg" class="easyui-dialog" style="width: 350px;height: 250px;padding: 10px 20px;" closed="true"
     buttons="l-dlg-buttons">
    <form id="l-fm" method="post">
        <table cellpadding="8px" style="font-size: 14px">
            <tr>
                <td>友情链接名称：</td>
                <td><input type="text" id="linkName" name="linkName" class="easyui-validatebox" required="true"/>
                </td>
            </tr>
            <tr>
                <td>友情链接地址：</td>
                <td><input type="text" id="linkUrl" name="linkUrl" class="easyui-validatebox" required="true"/>
                </td>
            </tr>
            <tr>
                <td>友情链接排序：</td>
                <td><input type="text" id="orderNo" name="orderNo" class="easyui-validatebox" required="true"/>
                </td>
            </tr>
            <tr>注意：类别根据排序序号从小到大排序</tr>
        </table>
    </form>
    <div id="l-dlg-buttons">
        <a href="javascript:saveLink();" class="easyui-linkbutton" iconcls="icon-ok" style="margin-left: 14%">保存</a>
        <a href="javascript:closeLinkDialog();" class="easyui-linkbutton" iconcls="icon-cancel"
           style="margin-left: 14%">取消</a>
    </div>
</div>


