;
(function () {
    'use strict';

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

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

    /*页面控制层*/
    function myShareListController($scope, $timeout, $mdDialog, fileTool, files) {
        var vm = $scope;
        vm.num = 0;
        vm.files =
            vm.breadcrumbs = [{
                id: '',
                text: "我的分享"
            }];

        vm.$parent.share = true;
        vm.storage_share = 'storage_share';
        vm.$parent.allow_search = false;

        vm.files = [];

        vm.cancelShareFile = () => {
            vm.filesCheck = getCheckFiles();
            fileTool.cancelShareFile(vm.filesCheck).then(function (result) {
                $timeout(() => {
                    refreshList();
                    vm.num = 0;
                });
            });
        }

        vm.OnItemClick = function (num, files) {
            $timeout(() => {
                vm.num = num;
               // $scope.$emit("toFooter", files.length, true);
            });
            return false;
        };
        vm.OnCheckAll = function (is_all, files) {
            $timeout(() => {
                vm.num = files.length;
             //   $scope.$emit("toFooter", files.length, true);
            })

            return false;
        }
        vm.OnContextmenu = function (ev, files) {
            contextmenu(ev, files);
          //  $scope.$emit("toFooter", files.length, true);
            return false;
        }

        $scope.$on('$destroy', function () {
            vm.$parent.share = false;
        });

        refreshList();
        EventEmitter.on('shareListRefresh', refreshList);


        $scope.$parent.menuBtn.forEach(v => {
            v.ifDisable = v.id !== 'refresh';
        });

        $scope.itemClick = (share_file) => {
            $mdDialog.show({
                controller: 'share_detail_Controller',
                templateUrl: './app/share/detail.html',
                parent: angular.element(document.body),
                clickOutsideToClose: false,
                title: 'Attention',
                fullscreen: false,
                locals: {
                    share_file: share_file
                }
            })
        }

        function contextmenu(ev, files) {

            function myShareMenuInit() {
                let myShareMenu = new nw.Menu();
                myShareMenu.append(new nw.MenuItem({
                    label: ' 取消分享                ',
                    click: function () {
                        $scope.cancelShareFile(files);
                    }
                }));
                myShareMenu.append(new nw.MenuItem({
                    label: ' 详情              ',
                    click: () => {
                        $scope.itemClick(files[0]);
                    }
                }));
                myShareMenu.popup(ev.clientX, ev.clientY);
            }
            myShareMenuInit();

        }



        /*加载文件*/
        function refreshList() {
            apiService.user_share_files().then((d) => {
                $timeout(() => {
                    vm.files = d.data;
                    vm.num = 0;
                 //   $scope.$emit('toFooter', vm.files.length);
                }, 10);
            }); 
        }



        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);
            });
        }

      //   $scope.$emit('toFooter', vm.files.length);
        return;

    }
})();
