'use strict';

angular.module('app').controller('WebController', ['$scope', '$timeout', function($scope, $timeout) {
    //lxtalkClient.Invoke('{FB60F992-A0FD-47B3-AAA7-E80DF209C5A4}', '_Register', '', $scope);
    $scope.imgView = function(event) {
        angular.element(".onView").removeClass("onView");
        var img = $(event.target);
        img[0].className = "onView";
        if (img[0].naturalWidth > img[0].naturalHeight) {
            angular.element(".image-view").addClass("width-Img");
        } else {
            angular.element(".image-view").removeClass("width-Img");
        }
        $scope.imgUrl = img[0].src;
        $(".image-big").remove();
        $(".image-view").append('<img class="image-big" src="' + $scope.imgUrl + '" data-dismiss="modal">');
    };

    $scope.delSure = function() {
        $scope.sure = false;
        // var activeClick = $($event.target);
        // console.log(activeClick);
        // var imgUrl = activeClick[0].parentElement.previousElementSibling.firstElementChild.src;
        var imgs = $(".images");
        $(".image-big").remove();
        for (var i = 0, len = imgs.length; i < len; i++) {
            if ($(".images")[i].firstElementChild.className == "onView") {
                $(".images")[i].remove();
                var imgNum = $("#imgpreview").find('img').length;
                if (imgNum == 2) {
                    angular.element(".images").removeClass("three");
                    angular.element(".images").addClass("two");
                } else if (imgNum = 1) {
                    angular.element(".images").removeClass("two");
                    angular.element(".images").addClass("one");
                }
                return;
            }
        };
    };

    $scope.delImg = function() {
        $scope.sure = true;
    };

    $scope.actived = function($event) {
        var activeClick = $($event.target);
        if (activeClick[0].nodeName == "A") {
            angular.element(".Aa").removeClass("activeColor");
            angular.element(".glyphicon").removeClass("activeColor");
            activeClick.addClass("activeColor");
        } else if (activeClick[0].nodeName == "SPAN") {
            angular.element(".Aa").removeClass("activeColor");
            angular.element(".glyphicon").removeClass("activeColor");
            activeClick.addClass("activeColor");
            $(activeClick[0].parentElement).addClass("activeColor");
        }
    };

    $scope.footHide = function() {
        angular.element(".foot").addClass("hide");
    };
    $scope.footShow = function($event) {
        var activeClick = $($event.target);
        if (activeClick[0].className == "form-control input-sm" || activeClick[0].className == "text-message") {
            angular.element(".foot").addClass("hide");
        } else {
            $timeout(function() {
                angular.element(".foot").removeClass("hide");
            }, 400);
        }
    };

    $scope.imgPreview = function(fileDom) {
        //判断是否支持FileReader
        if (window.FileReader) {
            var reader = new FileReader();
        } else {
            alert("您的设备不支持图片预览功能，如需该功能请升级您的设备！");
        };

        //获取文件
        var file = fileDom.files[0];
        var imageType = /^image\//;
        //是否是图片
        if (!imageType.test(file.type)) {
            alert("请选择图片！");
            return;
        };
        $("#file")[0].value = "";
        //读取完成
        reader.onload = function(e) {
            // //获取图片dom
            // var img = document.getElementById("preview");
            // //图片路径设置为读取的图片
            // img.src = e.target.result;
            var img = new Image,
                width = 1080, //image resize
                quality = 0.9, //image quality
                canvas = document.createElement("canvas"),
                drawer = canvas.getContext("2d");
            img.src = this.result;
            if (img.src) {
                var imgNum = $("#imgpreview").find('img').length;
                canvas.width = width;
                canvas.height = width * (img.height / img.width);
                drawer.drawImage(img, 0, 0, canvas.width, canvas.height);
                img.src = canvas.toDataURL("image/jpeg", quality);
                if (imgNum == 0) {
                    $("#imgpreview").append('<a data-toggle="modal" data-target="#viewModal" class="thumbnail images"><img onClick="angular.element(this).scope().imgView(event)" src="' + img.src + '"></a>');
                    angular.element(".images").addClass("one");
                } else if (imgNum == 1) {
                    $("#imgpreview").append('<a data-toggle="modal" data-target="#viewModal" class="thumbnail images"><img onClick="angular.element(this).scope().imgView(event)" src="' + img.src + '"></a>');
                    angular.element(".images").removeClass("one");
                    angular.element(".images").addClass("two");
                } else if (imgNum >= 2) {
                    $("#imgpreview").append('<a data-toggle="modal" data-target="#viewModal" class="thumbnail images"><img onClick="angular.element(this).scope().imgView(event)" src="' + img.src + '"></a>');
                    angular.element(".images").removeClass("two");
                    angular.element(".images").addClass("three");
                }
            };
        };
        reader.readAsDataURL(file);
    };

}]);