<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Index.Master" Inherits="System.Web.Mvc.ViewPage<DAL.^ReplaceClassCode^>" %>

<%@ Import Namespace="Common" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    ^ReplaceClassName^
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="HeadContent" runat="server">
    <script type="text/javascript">
        $(function () {

                $('#flexigridData').treegrid({
                    title: '^ReplaceClassName^',
                     iconCls: 'icon-site',
//                    url: '../^ReplaceClassCode^/GetAllMetadata',
                    idField: '^m_Id^',
                    treeField: '^m_Name^',
                    rownumbers: true,
                    toolbar: [
                    {
                        text: '详细',
                        iconCls: 'icon-search',
                        handler: function () {
                            return getView();
                        }
                    }, {
                        text: '创建',
                        iconCls: 'icon-add',
                        handler: function () {
                            return flexiCreate();
                        }
                    },  {
                        text: '删除',
                        iconCls: 'icon-remove',
                        handler: function () {
                            return flexiDelete();
                        }
                    }, {
                        text: '修改',
                        iconCls: 'icon-edit',
                        handler: function () {
                            return flexiModify();
                        }
                   
                    }],
                  
                    columns: [[ 
                    	{ field: '^m_Name^', title: '<%: Html.DisplayNameFor(model => model.^m_Name^) %>', width: 115 }
                        ^ReplaceAttribute^
				    ]]
                     ,
                    onBeforeLoad: function (row, param) {
                       
                        if (row) {
                            $(this).treegrid('options').url = '../^ReplaceClassCode^/GetAllMetadata/' + row.^m_Id^;
                        } else {
                            $(this).treegrid('options').url = '../^ReplaceClassCode^/GetAllMetadata';
                        }
                    }
                });
          
            var parent = window.dialogArguments; //获取父页面
            if (parent == "undefined" || parent == null) {
                //    不是在iframe中打开的
            } else {
                //隐藏所有的按钮和分隔符
                $(".l-btn.l-btn-plain").hide();
                $(".datagrid-btn-separator").hide();
                //添加选择按钮
                $('#flexigridData').datagrid("addToolbarItem", [{ "text": "选择", "iconCls": "icon-ok", handler: function () { flexiSelect(); } }]);
            }
         });

         function flexiSelect() {
            var node = $('#flexigridData').treegrid('getSelected');
            if (!node) {
                $.messager.alert('操作提示', '请选择一条数据!', 'warning');
               return false;
            }
            var arr = new Array(0);
            arr.push(node.^m_Id^);
            arr.push("^"); //主键列和显示列的分割符 ^ 
            arr.push(node.^m_Name^);
            //主键列和显示列之间用 ^ 分割   每一项用 , 分割
            if (arr.length == 3) {//一条数据和多于一条
                returnParent(arr.join("&")); //每一项用 & 分割
            }
             return false;
        }
        //导航到查看详细的按钮
        function getView() {

            var arr = $('#flexigridData').treegrid('getSelected');

            if (arr) {
                window.location.href = "../^ReplaceClassCode^/Details/" + arr.^m_Id^;
               
            } else {
                $.messager.alert('操作提示', '请选择一条数据!', 'warning');
            }
            return false;
        }
        //导航到创建的按钮
        function flexiCreate() {

            window.location.href = "../^ReplaceClassCode^/Create";
            return false;
        }
        //导航到修改的按钮
        function flexiModify() {

            var arr = $('#flexigridData').treegrid('getSelected');

            if (arr) {
                window.location.href = "../^ReplaceClassCode^/Edit/" + arr.^m_Id^;
               
            } else {
                $.messager.alert('操作提示', '请选择一条数据!', 'warning');
            }
             return false;
        };
        //删除的按钮
        function flexiDelete() {
          
            var node = $('#flexigridData').treegrid('getSelected');
            if (!node) {
                $.messager.alert('操作提示', '请选择数据!', 'warning');

            } else {
                $.messager.confirm('操作提示', "确认删除这1项吗？", function (r) {
                    if (r) {
                        $.post("../^ReplaceClassCode^/Delete", { query: node.^m_Id^ }, function (res) {
                            if (res == "OK") {
                                remove();
                                $.messager.alert('操作提示', '删除成功!', 'info');                  
                            }
                            else {
                                if (res == "") {
                                    $.messager.alert('操作提示', '删除失败!请查看该数据与其他模块下的信息的关联，或联系管理员。', 'info');
                                }
                                else {
                                    $.messager.alert('操作提示', res, 'info');
                                }
                            }             
                        }); 
                    }
                });
            }                  
            return false;
        };                
       
       
        function remove() {
            var node = $('#flexigridData').treegrid('getSelected');
            if (node) {
                $('#flexigridData').treegrid('remove', node.^m_Id^);
            }
        }
    </script>
</asp:Content>
