;
(function () {
    const apiService = require('app-service'),
        EventEmitter = require('myevents');
    angular.module('app.controllers')
        .controller('recycle_main_Controller', recycle_main_Controller);

    recycle_main_Controller.$inject = ['$scope', '$timeout', '$mdDialog', 'fileTool'];
    repeat_reductionCtr.$inject = ['$scope', '$mdDialog', 'same_file', 'file_ids', 'getFiles'];



    function recycle_main_Controller($scope, $timeout, $mdDialog, fileTool) {
        var vm = $scope;
        vm.ifDisabled = true;
        vm.ifRecycle = true;
        vm.pager = { page: 1 };
        vm.loading = true;
        //初始化按钮
        getFiles();
        vm.OnItemClick = function (num, files) {
            $timeout(() => {
                disableBtn(num === 0);
            });
            return false;
        };
        vm.on_pager = (idx = 1) => {
            vm.pager.page = idx;
            getFiles(vm.pager.page);
        }
        vm.OnCheckAll = function (is_all, files) {
            $timeout(() => {
                disableBtn(!is_all);
            });
            return false;
        }
        vm.OnContextmenu = function (ev, files) {
            $timeout(() => {
                contextmenu(ev, files);
                disableBtn(files.length == 0);
            });
            return false;
        }

        /*清空列表*/
        vm.empty_recycleBin = function () {
            fileTool.alert('确定要清空回收站吗？').then(ok => {
                if (ok) clearFile(true);
            });
        }
        /*恢复文件*/
        vm.reduction = function (files) {

            files || (files = getCheckFiles());
            fileTool.alert('确定要还原选中的文件吗？').then(ok => {
                if (!ok) return
                let file_ids = files.map(v => v._id);
                apiService
                    .check_recovery(file_ids)
                    .then(function (v) {
                        if (v.data && v.data.length == 0) {
                            apiService.recovery(file_ids, []).then(() => {
                                layer.msg('已恢复')
                                $mdDialog.cancel();
                                getFiles();
                            });
                            return;
                        }
                        var confirm = {
                            controller: repeat_reductionCtr,
                            templateUrl: 'folderRepeat.html',
                            parent: angular.element(document.body),
                            clickOutsideToClose: false,
                            fullscreen: false,
                            locals: {
                                same_file: v.data,
                                file_ids: file_ids,
                                getFiles: getFiles
                            }
                        }
                        return $mdDialog.show(confirm);
                    });

            });
        }
        vm.deleteFileRecycle = function () {
            fileTool.alert('确定要删除选中的文件吗？').then(ok => {
                if (ok) {
                    clearFile(getCheckFiles());
                    EventEmitter.emit('getUserSpace');
                }
            });
        }

        vm.menuBtn = [{
            id: 'deleteFile',
            events: () => {
                vm.deleteFileRecycle();
            },
            icon: 'icon-del',
            text: '删除',
            ifDisable: vm.ifDisabled
        }, {
            id: 'reduction',
            events: () => {
                vm.reduction();
            },
            icon: 'icon-recover',
            text: '还原',
            ifDisable: vm.ifDisabled
        }, {
            id: 'deleteAllFile',
            events: () => {
                vm.empty_recycleBin();
            },
            icon: 'icon-clear',
            text: '清空',
            ifDisable: false
        }, {
            id: 'refresh',
            events: () => {
                getFiles();
            },
            icon: 'icon-refresh',
            text: '刷新',
            ifDisable: false
        }];

        var contextmenu = (ev, files) => {
            function recycleMenuInit() {
                let recycleMenu = new nw.Menu();
                recycleMenu.append(new nw.MenuItem({
                    label: '        还原              ',
                    icon: './web_content/assets/imgs/recover.png',
                    click: function () {
                        vm.reduction(files);
                    }
                }));
                recycleMenu.append(new nw.MenuItem({
                    label: '        删除              ',
                    icon: './web_content/assets/imgs/clear.png',
                    click: function () {
                        vm.deleteFileRecycle(files);
                    }
                }));
                recycleMenu.popup(ev.clientX, ev.clientY);
            }

            recycleMenuInit();
        }



        function getFiles(page = 1) {
            apiService.recycle(page).then(function (d) {
                $timeout(() => {
                    vm.files = d.data;
                    disableBtn();
                });
            });
        }

        function disableBtn(bol = true) {
            vm.menuBtn[0].ifDisable = bol;
            vm.menuBtn[1].ifDisable = bol;
            if (!vm.files || vm.files.total === 0) {
                vm.menuBtn[2].ifDisable = true;
            }
        }

        function clearFile(files) {
            if (files === true) {
                vm.files = [];
                return apiService.recycleClear().then(() => {
                    getFiles();
                    EventEmitter.emit('getUserSpace');
                });
            }
            let file_ids = files.map(v => v._id);
            apiService.delete_item(file_ids, 0).then(() => {
                getFiles();
                EventEmitter.emit('getUserSpace');
            });
        }
        getFiles(vm.pager.page);
    }

    //文件冲突提示
    function repeat_reductionCtr($scope, $mdDialog, same_file, file_ids, getFiles) {
        var vm = $scope;
        vm.chk_all = false;
        let len = same_file.length - 1;
        vm.close = $mdDialog.cancel;

        vm.show_file = same_file[len];

        vm.jumpOver = function (operate_flag) {
            /*
            operate_flag=1(保留两个文件夹)
            operate_flag=0 (跳过)   
            */
            same_file[len].operate_flag = operate_flag;

            if (vm.chk_all) {
                for (; len >= 0; len--) {
                    same_file[len].operate_flag = operate_flag;
                }
            }

            if (len <= 0) {
                apiService.recovery(file_ids, same_file).then(d => {
                    layer.msg('已恢复')
                    $mdDialog.cancel();
                    EventEmitter.emit('getUserSpace');
                    getFiles();
                });
                vm.close();
            }
            vm.show_file = same_file[--len];
        };
    }





    function getCheckFiles() {

        let tbody = $('#J-tbody');
        let tr_file_active = [...tbody.find('.active')];

        let files = [];
        return tr_file_active.map((v) => {
            let data = $(v).data();
            return (data['$scope'].file);
        });
    }

})();
