<%@ Page Title="" Language="C#" MasterPageFile="~/whir_system/master/SystemMasterPage.master" AutoEventWireup="true" CodeFile="UploadFiles.aspx.cs" Inherits="Whir_System_Module_Extension_UploadFiles" %>


<%@ Import Namespace="Whir.Language" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
    <!--列表-->
    <link rel="stylesheet" href="<%= SysPath %>Res/assets/js/bootstrap-table/src/bootstrap-table.css" />
    <script src="<%= SysPath %>Res/assets/js/bootstrap-table/src/bootstrap-table.js"></script>
    <script src="<%= SysPath %>Res/assets/js/bootstrap-table/src/locale/bootstrap-table-zh-CN.js"></script>
    <script src="<%= SysPath %>Res/assets/js/bootstrap-table/src/extensions/export/bootstrap-table-export.js"></script>
    <script src="<%= SysPath %>Res/assets/js/bootstrap-table/src/extensions/column/bootstrap-table-column.js"></script>
    <script src="<%= SysPath %>Res/assets/js/bootstrap-table/src/extensions/print/bootstrap-table-print.js"></script>
    <!--树状结构-->
    <script src="<%=SysPath %>Res/assets/js/tree/bootstrap-treeview.js" type="text/javascript"></script>
    <!--上传控件-->
    <link href="<%= SysPath %>res/assets/js/fileinput/css/fileinput.min.css" rel="stylesheet" type="text/css" />
    <script src="<%= SysPath %>res/assets/js/fileinput/js/fileinput.min.js" type="text/javascript"></script>
    <script type="text/javascript" src="<%= SysPath %>res/assets/js/fileinput/locales/zh.js"></script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <form id="Form1" runat="server">
    <div class="content-wrap">
        <div class="space15">
        </div>
        <div class="panel">
            <div class="panel-body">
                <div class="row">
                    <div class="col-md-3">
                        <div class="panel panel-default">
                            <div class="panel-heading"><%="文件夹".ToLang()%></div>
                            <div class="panel-body">
                                <div class="row">
                                    <div class="site_select" id="folderList"></div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-9 ">
                        <div class="panel panel-default">
                            <div class="panel-heading"><%="文件".ToLang()%></div>
                            <div class="panel-body">
                                <div class="row">
                                     <div class="col-md-6">
                                        <div class="input-group">
                                            <%if (!IsCurrentRoleMenuRes("312"))
                                               { %> 
                                                <span class="input-group-addon btn btn-info" >
                                                <%="当前文件夹".ToLang()%>
                                            </span>
                                           <%} %>
                                            <span  class="input-group-addon" id="showFolder">
                                                <%=UploadFilePath %></span>
                                           <%if (IsCurrentRoleMenuRes("312"))
                                               { %> 
                                            <input type="text" id="folderName" name="folderName" class="form-control" required="true"
                                                maxlength="20" placeholder="<%="新建文件夹名称".ToLang()%> " />
                                            <a class="input-group-addon btn btn-white" onclick="addNewFolder();">
                                                <%="新建文件夹".ToLang()%>
                                            </a>
                                            <%}  %>
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="input-group">
                                              <%if (IsCurrentRoleMenuRes("313"))
                                                { %>
                                            <input type="text" class="form-control" id="keyword" placeholder="<%="搜索内容".ToLang()%> ">
                                            <a class="input-group-addon btn btn-white" onclick="searchFile();">
                                                <%="搜索".ToLang()%>
                                            </a>
                                                <%} %>
                                            <a class="input-group-addon btn btn-white" onclick="reload();">
                                                <%="刷新".ToLang()%>
                                            </a>
                                              <%if (IsCurrentRoleMenuRes("315"))
                                                   { %>
                                            <a class="input-group-addon btn text-danger border-normal" onclick="delAll();">
                                                <%="删除".ToLang() %>
                                            </a>
                                             <%} %>
                                        </div>
                                    </div>
                                </div>
                                <br />
                                <table id="Common_Table">
                                </table>
                                <br />
                                  <%if (IsCurrentRoleMenuRes("317"))
                                    { %>
                                 <div class="box_common" id="divUploadImg">
                                     <div class="panel panel-default">
                                        <div class="panel-heading">
                                            <%="上传文件".ToLang()%>
                                        </div>
                                        <div class="panel-body">  
                                         <div class="col-md-12">
                                            <div id="divUploadFile">
                                                <input id="txt_file" value="" name="txt_file" type="file" class="file-loading" multiple  accept="<%=AcceptType %>"/>
                                            </div>
                                         </div>
                                       </div>
                                    </div>  
                                </div>
                                <%} %>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <input type="hidden" id="txtPath" value="<%=UploadFilePath %>" />
    <input type="hidden" id="hidChoose" value="" />
    </form>
    <script type="text/javascript">
        //上传文件
        var currentpath = "<%= Server.UrlEncode(UploadFilePath) %>";
         
        //重新生成上传控件
        function RebuildUploadFile () {
            var html = '<input id="txt_file" value="" name="txt_file" type="file" class="file-loading" multiple />';
            $("#divUploadFile").html(html);
        }
 
        //注册上传控件
        function regeditUploadFile () {
            $("#txt_file")
                .fileinput({
                    language: 'zh',
                    uploadUrl: "<%= SysPath %>Ajax/Extension/uploadfiles.aspx?FormId=0&savePath=" + currentpath + "&fileName=",
                    previewFileType: "any",
                    allowedFileExtensions: [<%=AllowFileType %>],
                    initialCaption: '支持格式：<%=EnableExtensionNames %>',
                    previewClass: "bg-warning",
                    initialPreviewAsData: true,
                    initialPreviewFileType: 'other',
                    noPicker:true
                })
               .on("filebatchselected",
                            function (event, files) {
                                $(this).fileinput("upload");
                            })
                .on("fileuploaded",
                    function(event, data) {
                        if (data.response && data.response.Result == true) {
                            reload();
                        } else {
                            whir.toastr.error(data.response.Msg);
                        }
                    });
        }

        function getTreeJson () {
            whir.ajax.post('<%= SysPath%>Handler/Extension/UploadFiles.aspx', {
                data: {
                    _action: "GetFolderTree",
                    rootFolder: '<%=UploadFilePath %>'
                },
                success: function(result) {
                    if (result.Status) {
                        whir.loading.remove();
                        bindTree(result.Message);
                    } else {
                        whir.toastr.warning(result.Message);
                        whir.loading.remove();
                    }
                }
            });
             whir.loading.remove();
        }        

        var $table = $('#Common_Table'),_that;
       
        //绑定文件夹树
        function bindTree (treeJson) {
            var defaultData =eval(treeJson);
            $('#folderList').treeview1({
                data: defaultData,
                multiSelect: $('#chk-select-multi').is(':checked'),
                onNodeSelected: function(event, node) {                              //切换文件夹
                    var path = '<%=UploadFilePath %>' + node.href;
                    if (path.indexOf("//") > 0)                                  //防止根目录多了//
                        path = path.substring(0, path.length - 1);
                    $("#txtPath").val(encodeURIComponent(path));
                    currentpath =encodeURIComponent(path);                      //更改上传路径
                    RebuildUploadFile();                                        //重新生成上传控件
                    regeditUploadFile();                                           //注册上传控件
                    $table.bootstrapTable('refresh', { url: "<%= SysPath %>Handler/Extension/UploadFiles.aspx?_action=GetList&path=" + encodeURIComponent(path) });
                     $("#showFolder").attr("title", path);
                    if (path.length > 25) {                                 //目录太多太长，剪短点
                        path = path.substring(0, 10) + "..." + path.substring(path.length - 10, path.length);
                    }
                    $("#showFolder").html(path );
                }
            });
        }


        //列表
        function initTable() {
            $table.bootstrapTable({
                url:"<%= SysPath %>Handler/Extension/UploadFiles.aspx?_action=GetList&path=<%=UploadFilePath %>",
                dataType: "json",
                pagination: true, //分页
                sidePagination: "server", //服务端处理分页
                silent:true,
                clickToSelect: false,
                pageSize: 10,
                onLoadSuccess: function() {
                    //设置样式 后期需修改
                    SetTableStyleEvent();
                    whir.loading.remove();
                },
                onColumnSwitch: function() {
                    SetTableStyleEvent();
                },
                SelectColumnEvent: function() {
                    
                },
               onPageChange:function () {
                     var path = $("#txtPath").val();
                    $table.bootstrapTable('refreshOptions', { url: "<%= SysPath %>Handler/Extension/UploadFiles.aspx?_action=GetList&path=" + path });
                },
                columns: [
                    {title: '图标', type: 'Type',align: 'center',valign: 'middle',formatter: function(value, row, index) {return GetIcon(value, row, index);}},
                    {title: '文件名称', field: 'Name',align: 'left',valign: 'middle'},
                    {title: '物理名称', field: 'RealName',align: 'left',valign: 'middle'},
                    {title: '大小', field: 'Size',align: 'left',valign: 'middle'},
                    {title: '操作',field: 'Op',width:135, align: 'center',valign: 'middle',formatter: function(value, row, index) {return GetOperation(value, row, index);}},
                    {title: '<input type="checkbox" id="btSelectAll" />',field: 'Checkbox',width:40, align: 'center',valign: 'middle',formatter: function(value, row, index) {return GetCheckbox(value, row, index);}}
                ],
                onDblClickRow: function (row) {
                    openDir(row);
                }

            });
        }

        //双击打开文件夹
        function openDir(row) {
            if (row.Type != "File") {

                $("#txtPath").val($("#txtPath").val() + encodeURIComponent(row.RealName + "/"));        //防止中文文件夹 出现乱码 
                var path = decodeURIComponent($("#txtPath").val());
                currentpath = encodeURIComponent(path);            //更改上传路径
                RebuildUploadFile();                               //重新生成上传控件
                regeditUploadFile();                               //注册上传控件
                $table.bootstrapTable('refresh', { url: "<%= SysPath %>Handler/Extension/uploadfiles.aspx?_action=GetList&path=" + encodeURIComponent(path) });
                $("#showFolder").attr("title", path);
                if (path.length > 25) {                            //目录太多太长，剪短点
                    path = path.substring(0, 10) + "..." + path.substring(path.length - 10, path.length);
                }
                $("#showFolder").html(path);
            }
        }
        initTable(); 
         

        function SetTableStyleEvent() {
            whir.checkbox.destroy();
            whir.skin.radio();
            whir.skin.checkbox();
            //绑定全选按钮事件
            $("#btSelectAll").next().click(function() {
                if ($("#btSelectAll").is(':checked')) {
                    whir.checkbox.selectAll('lbDel', 'hidChoose', 'btSelectItem','*');
                } else {
                    whir.checkbox.cancelSelectAll('lbDel', 'hidChoose', 'btSelectItem');
                }

            });
             
            $("input[name=btSelectItem]").each(function() {
                $(this).next().click(function() {
                  $("#hidChoose").val(whir.checkbox.getSelect('btSelectItem', '*'));
                });
            });
        }

        $(document).ready(function() {
            //注册上传控件
            regeditUploadFile();
            //绑定文件夹树
            getTreeJson();
             //注册回车事件
            document.onkeydown = function(e) {
                var ev = document.all ? window.event : e;
                if (ev.keyCode == 13 && $("#keyword").is(":focus")) {
                    searchFile();
                } else if (ev.keyCode == 13 && $("#folderName").is(":focus")) {
                    addNewFolder();
                }
            };
        });
         
          <%--列表绑定 --%>
        //获取图标HTML
        function GetIcon(value, row, index) {
            var icon = "";
            if(row.Type!="File") {
                icon = "<img src='<%=SysPath %>res/images/ext/folder.gif'>";
            } else {
                icon = "<img src='<%=SysPath %>res/images/ext/"+row.Ext.substring(1,row.Ext.length)+".gif' onerror=\"this.src='<%=SysPath %>res/images/ext/unknow.gif'\">";
            }
            return icon;
        }
      

        //获取操作HTML
        function GetOperation(value, row, index) {
            var path = $("#txtPath").val();
            var html = "<div class='btn-group'>";
            if (row.Type == "File") {
                <%if (IsCurrentRoleMenuRes("316"))
            { %>
                html += '<a class="btn btn-xs btn-white" href="Download.aspx?path=<%=UploadFilePath %>&name=' + encodeURIComponent(row.FullName) + '" target="_blank">下载</a>';
                <%}%>
            }
             <%if (IsCurrentRoleMenuRes("315"))
            { %>
            html += '<a id="btnDel" class="btn btn-xs text-danger border-normal" value="' + path + row.RealName + '"  onclick="del(\'' + row.FullName + '\')" >删除</a>';
            <%}%>
            html += "</div>";
            return html;
        }
        //获取多选框 HTML
        function GetCheckbox(value, row, index) {
            if(row.Type=="File") {
                var html = '<input type="checkbox" name="btSelectItem" value="'+ row.FullName + '" />';
                return html;
            } else {
                return "";
            }
        }
       
        <%--按钮操作 --%>
        //刷新列表
        function reload() {
            var path=$("#txtPath").val();
            $table.bootstrapTable('refresh', { url: "<%= SysPath %>Handler/Extension/UploadFiles.aspx?_action=GetList&path=" + path });
            whir.loading.remove();
        } 
         
       
        //创建文件夹
        function addNewFolder() {
            whir.ajax.post('<%= SysPath%>Handler/Extension/UploadFiles.aspx', {
                data: {
                    _action: "AddNewFolder",
                    path: $("#txtPath").val(),
                    folderName: $("#folderName").val()
                },
                success: function (result) {
                    if (result.Status) {
                        // $("#folderName").val("");
                        whir.loading.remove();
                        getTreeJson();
                        reload();
                        whir.toastr.success(result.Message);
                    } else {
                        whir.toastr.warning(result.Message);
                    }
                }
            });
             whir.loading.remove();
        } 
        
        //搜索
        function searchFile() {
            var path=$("#txtPath").val();
            var keyword=$("#keyword").val();
            $table.bootstrapTable('refresh', { url: "<%= SysPath %>Handler/Extension/UploadFiles.aspx?_action=GetList&keyword="+keyword+"&path=" + path });
 
        } 

        //删除多条文件
        function delAll() {
            if($("#hidChoose").val()=="") {
                whir.toastr.warning("<%="请选择要删除的数据行！".ToLang() %>");
                return;
            }
            whir.dialog.confirm('<%="确定删除所选记录吗？".ToLang() %>', function() {
                whir.ajax.post('<%= SysPath%>Handler/Extension/UploadFiles.aspx', {
                    data: {
                        _action: "Delete",
                        commandName: "CheckDelete",
                        hidChoose: $("#hidChoose").val()
                    },
                    success: function(result) {
                        if (result.Status) {
                            whir.loading.remove();
                            whir.toastr.success(result.Message);
                            reload();

                        } else {
                            whir.loading.remove();
                            whir.toastr.warning(result.Message);

                        }
                    }
                });
                whir.dialog.remove();
                 whir.loading.remove();
            });
        }

        //删除单条文件
        function del(value) {
            whir.dialog.confirm('<%="确认删除该文件吗？".ToLang() %>', function() {
                whir.ajax.post('<%= SysPath%>Handler/Extension/UploadFiles.aspx', {
                    data: {
                        _action: "Delete",
                        file: value,
                        commandName: "Delete"
                    },
                    success: function(result) {
                        if (result.Status) {
                            whir.loading.remove();
                            whir.toastr.success(result.Message);
                            reload();
                            getTreeJson();
                        } else {
                            whir.loading.remove();
                            whir.toastr.warning(result.Message);

                        }

                    },
                    error: function(response) {
                        whir.loading.remove();
                        whir.toastr.error(response.Message);

                    }                    
                });
                whir.dialog.remove();
                 whir.loading.remove();
            });
        }
         
    </script>
</asp:Content>


