'use strict';

angular.module('appMessage').controller('SmsController', ['$scope', '$state', '$filter', function($scope, $state, $filter) {

    //lxtalkClient.Invoke('{FB60F992-A0FD-47B3-AAA7-E80DF209C5A4}', '_Register', '', $scope);
    $scope.now = new Date();
    $scope.nowDate = $filter("date")($scope.now, "yyyy/MM/dd HH:mm:ss");
    $scope.selectDate = $scope.nowDate;
    $scope.checkId = false;
    $scope.activeData = '按Ctrl+Enter键发送消息';

    $scope.disable = function($event) {
        var dataForm = $($event.currentTarget.previousElementSibling.children);
        if ($scope.checkId) {
            dataForm[0].disabled = false;
            dataForm.addClass("activeBackground");
        } else {
            dataForm[0].disabled = true;
            dataForm.removeClass("activeBackground");
        }
    };

    $scope.activeClick = function($event) {
        var activeClick = $($event.target.firstElementChild);
        $scope.activeData = $event.target.innerText;
        angular.element(".active").addClass("hid");
        activeClick.removeClass("hid");
    };

    $scope.sendKeyup = function(e) {
        var sendmsg = $(e.target);
        var mediamsg = $(e.target.parentElement.previousElementSibling);
        var msg = _.trim(sendmsg[0].textContent);
        msg = msg.replace(/ /g, "&nbsp");
        var myDate = new Date();
        var nowDate = $filter("date")(myDate.getTime(), "yyyy/MM/dd HH:mm:ss");
        var dateTime = $filter("date")(myDate.getTime(), "HH:mm:ss");
        var ts1 = (new Date(nowDate)).getTime();
        var ts2 = (new Date($scope.selectDate)).getTime();
        var keycode = window.event ? e.keyCode : e.which;
        console.log(sendmsg);
        console.log(keycode);
        console.log($scope.inputMob);
        if (msg.length !== 0)
            sendmsg[0].nextElementSibling.firstElementChild.textContent = "已输入" + msg.length * 2 + "个字符，本次将产生" + (Math.floor(msg.length / 70) + 1) + "条短信";
        else if (msg.length == 0)
            sendmsg[0].nextElementSibling.firstElementChild.textContent = "每条短信上限140字符(70个汉字)";
        if ($scope.inputMob) {
            if (!$scope.checkId)
                for (var i = 0; i < $scope.inputMob.length; i++) {
                    var html = '<div class="feed-element"><div class="media-body "><p><span class="glyphicon glyphicon-menu-right"><span>' + $scope.inputMob[i] + '</span>' + dateTime + '</span><span class="glyphicon glyphicon-ok"></span></p>' +
                        '<div class = "well m-l-xs m-t-xs fit-content" >' +
                        '<span class = "text-message">' + msg + '&nbsp&nbsp--丽水市人民政府办公室</span> </div></div></div>';
                    if (msg.length !== 0 && $scope.activeData == '按Enter键发送消息') {
                        if (!e.ctrlKey && e.which == 13) {
                            mediamsg.append(html);
                            sendmsg[0].textContent = "";
                            sendmsg[0].nextElementSibling.firstElementChild.textContent = "每条短信上限140字符(70个汉字)";
                        } else if (e.ctrlKey && e.which == 13) {
                            sendmsg.append('<br>');
                            e.keyCode = 13;
                        }
                    } else if (msg.length !== 0 && $scope.activeData == '按Ctrl+Enter键发送消息') {
                        if (e.ctrlKey && e.which == 13) {
                            mediamsg.append(html);
                            sendmsg[0].textContent = "";
                        }
                    }
                }
            else if ($scope.checkId && ts2 > ts1)
                for (var i = 0; i < $scope.inputMob.length; i++) {
                    var html = '<div class="feed-element"><div class="media-body "><p><span class="glyphicon glyphicon-menu-right"><span>' + $scope.inputMob[i] + '</span>' + dateTime + '</span><span class="glyphicon glyphicon-time"><span>( 定时于' + $scope.selectDate + '发送 )</span></span></p>' +
                        '<div class = "well m-l-xs m-t-xs fit-content" >' +
                        '<span class = "text-message">' + msg + '&nbsp&nbsp--丽水市人民政府办公室</span> </div></div></div>';
                    if (msg.length !== 0 && $scope.activeData == '按Enter键发送消息') {
                        if (!e.ctrlKey && e.which == 13) {
                            mediamsg.append(html);
                            sendmsg[0].textContent = "";
                            sendmsg[0].nextElementSibling.firstElementChild.textContent = "每条短信上限140字符(70个汉字)";
                        } else if (e.ctrlKey && e.which == 13) {
                            sendmsg.append('<br>');
                            e.keyCode = 13;
                        }
                    } else if (msg.length !== 0 && $scope.activeData == '按Ctrl+Enter键发送消息') {
                        if (e.ctrlKey && e.which == 13) {
                            mediamsg.append(html);
                            sendmsg[0].textContent = "";
                        }
                    }
                }
        }
    };

    $scope.sendMsg = function($event) {
        var sendmsg = $($event.target.parentElement.previousElementSibling);
        var mediamsg = $($event.target.parentElement.previousElementSibling.parentElement.previousElementSibling);
        var msg = _.trim(sendmsg[0].textContent);
        msg = msg.replace(/ /g, "&nbsp");
        var myDate = new Date();
        var nowDate = $filter("date")(myDate.getTime(), "yyyy/MM/dd HH:mm:ss");
        var dateTime = $filter("date")(myDate.getTime(), "HH:mm:ss");
        var ts1 = (new Date(nowDate)).getTime();
        var ts2 = (new Date($scope.selectDate)).getTime();
        if ($scope.inputMob == undefined)
            alert("请输入手机号码");
        else if (!$scope.checkId)
            for (var i = 0; i < $scope.inputMob.length; i++) {
                var html = '<div class="feed-element"><div class="media-body "><p><span class="glyphicon glyphicon-menu-right"><span>' + $scope.inputMob[i] + '</span>' + dateTime + '</span><span class="glyphicon glyphicon-ok"></span></p>' +
                    '<div class = "well m-l-xs m-t-xs fit-content" >' +
                    '<span class = "text-message">' + msg + '&nbsp&nbsp--丽水市人民政府办公室</span> </div></div></div>';
                if (msg.length !== 0) {
                    mediamsg.append(html);
                    sendmsg[0].textContent = "";
                    sendmsg[0].nextElementSibling.firstElementChild.textContent = "每条短信上限140字符(70个汉字)";
                }
            }
        else if ($scope.checkId && ts2 > ts1)
            for (var i = 0; i < $scope.inputMob.length; i++) {
                var html = '<div class="feed-element"><div class="media-body "><p><span class="glyphicon glyphicon-menu-right"><span>' + $scope.inputMob[i] + '</span>' + dateTime + '</span><span class="glyphicon glyphicon-time"><span>( 定时于' + $scope.selectDate + '发送 )</span></span></p>' +
                    '<div class = "well m-l-xs m-t-xs fit-content" >' +
                    '<span class = "text-message">' + msg + '&nbsp&nbsp--丽水市人民政府办公室</span> </div></div></div>';
                if (msg.length !== 0) {
                    mediamsg.append(html);
                    sendmsg[0].textContent = "";
                    sendmsg[0].nextElementSibling.firstElementChild.textContent = "每条短信上限140字符(70个汉字)";
                }
            }
    };
    // $scope.mobAdd = function() {
    //     var mobLen = $scope.inputMob.length;
    //     if (mobLen < 12)
    //         $scope.counts = 0;
    //     mobLen = mobLen - $scope.counts;
    //     console.log(mobLen);
    //     if (mobLen % 11 == 0 && mobLen !== 0) {
    //         $scope.inputMob = $scope.inputMob + ','
    //         $scope.counts = $scope.counts + 1;
    //            $scope.counts = Math.floor($scope.inputMob.length / 11);
    //     }
    // };
    $scope.mobReset = function() {
        if ($scope.inputMob) {
            if ($scope.inputMob instanceof Array)
                $scope.inputMob = $scope.inputMob.join(",");
            $scope.inputMob = $scope.inputMob.match(/\d{11}/ig);
        }
    };
}]);