;
(function () {
    'use strict';
    const apiService = require('app-service'),
          lodash = require('lodash'),
           EventEmitter = require('myevents'),
          appCache = require('app-cache');

    angular.module('app.controllers')
        .controller('m_newFileCtr', m_newFileCtr);
        m_newFileCtr.$inject = ['$scope','$timeout', '$mdDialog', 'fileTool']; 
    return;

    function m_newFileCtr($scope, $timeout, $mdDialog, fileTool) {
        var vm = $scope;
        vm.close = $mdDialog.cancel;

        vm.ok = function () {

            if (!vm.fileName) {
                layer.msg('请填写文件名')
            } 

            if(/[\\//:*?\"<>|]/.test(vm.fileName)){
                layer.msg('文件名不能包含\\//:*?\"<>|！');
                return;
            }

            let breadcrumb = lodash.last(appCache.breadcrumbs);
            var result = vm.fileName;

            let new_item = {
                name: result,
                pid: breadcrumb.id,
                user_id: '1',
                delflag: 0,
                isdir: 1
            };
            apiService.check_rename(new_item).then((d) => {
                return d.data;
            }).then((isexit) => {
                if (isexit) {
                    layer.msg('已存在同名文件夹！')
                    return;
                }
                return apiService.add_item({
                    name: result,
                    pid: breadcrumb.id,
                    user_id: '1',
                    delflag: 0,
                    isdir: 1
                }).then(function (d) {
                    if(!d.ret){
                        layer.msg(d.message)
                        return
                    }
                    EventEmitter.emit('fileListRefresh');
                });
            });

            $mdDialog.hide();



        }
    }
})();