;
(function () {

    const event = require('myevents')
        , path = require('path')
        , child_process = require('child_process')
        , dbServer = require('db-server')
        , nedbCollection = require('nedbCollection');

    angular.module('app.controllers')
        .controller('task_complete_Controller', task_complete_Controller)
        ;

    task_complete_Controller.$inject = ['$scope', '$mdDialog', '$rootScope', 'fileTool'];
    return;

    function task_complete_Controller($scope, $mdDialog, $rootScope, fileTool) {

        let vm = $scope;

        vm.open = (file, folder) => {
            if (!folder) {
                return child_process.exec(`explorer.exe ${file.path}`);
            }
            let file_path = path.parse(file.path);
            child_process.exec(`explorer.exe ${file_path.dir}`);
        };

        vm.delete = (file, idx) => {
            vm.file_list.splice(idx, 1);
            dbServer.loacl.remove(file._id).then(() => {
                // on_task_complete_ctr();
            });
        };

        vm.empty_recode = function () {
            fileTool.alert('确定要清空全部上传和下载记录吗？').then(ok => {
                if (ok) {
                    dbServer.loacl.clear().then(() => {
                        on_task_complete_ctr();
                    });
                }
            });
        };

        $scope.$on('$destroy', () => {
            event.removeAllListeners('task_complete_ctr');
        });

        on_task_complete_ctr();

        event.on('task_complete_ctr', on_task_complete_ctr);

        function on_task_complete_ctr() {
            get_all().then((d) => {
                $scope.$apply(() => {
                    if (vm && vm.$parent && vm.$parent.updataTaskNum)
                        vm.$parent.updataTaskNum();
                    $scope.file_list = d;
                });
            });
        }
    }

    //  查找所有任务
    function get_all() {
        return dbServer.loacl.find({}).then((doc) => {
            return doc;
        });
    }

})();
