poconsole.controller('docsCtrl', function($scope, $route, Page, $log, Doc) {
    Page.setTitle('Documents');
    Page.setPage('docs');

    var file_types = ["_blank", "c", "dwg", "hpp", "key", "ods", "png", "rtf", "txt", "_page", "cpp", "dxf", "html", "less", "odt", "ppt", "sass", "wav", "aac", "css", "eps", "ics", "mid", "otp", "psd", "scss", "xls", "ai", "dat", "exe", "iso", "mp3", "ots", "py", "sql", "xlsx", "aiff", "dmg", "flv", "java", "mp4", "ott", "qt", "tga", "xml", "avi", "doc", "gif", "jpg", "mpg", "pdf", "rar", "tgz", "yml", "bmp", "dotx", "h", "js", "odf", "php", "rb", "tiff", "zip" ];

    $scope.loadDocs = function(){
        var loadDocs = Doc.getAllDocs();
        loadDocs.then(function(d){
            var imageBase = "/images/icons/";
            var imageExtension = ".png";
            $scope.docs = d.docs.map(function(doc){
                var spl = doc.name.split(".");
                var split_name = doc.name.split(".");
                var trunc_filebase = split_name[0];

                if (trunc_filebase.length > 12){
                    trunc_filebase = trunc_filebase.slice(0,5) + "..." + trunc_filebase.slice(-5);

                }
                doc.trunc_filename = trunc_filebase + "." + split_name[1];
                var ext = spl[spl.length - 1];
                if(file_types.indexOf(ext) > -1){
                    doc.img_url = imageBase + ext + imageExtension;
                } else {
                    doc.img_url = false;
                }
                return doc;
            });
        });
    };
    $scope.loadDocs();

    $scope.selectDelete = function(id){
        $scope.del_id = id;
        return;
    };

    $scope.deleteDoc = function(id){
        var del = Doc.deleteDoc(id);
        del.then(function(d){
            $scope.loadDocs();
        });
    };


});
