/********************************************************
Copyright @ 苏州瑞泰信息技术有限公司 All rights reserved. 
 创建人   : joe Song
 创建时间 : 2016-03-10
说明     :  当没有数据时显示的空白图片
*********************************************************/

angular.module('xrmApp')
    .directive('rtEmpty', function () {
        return {
            restrict: 'EA',
            scope: {
                rtMessage: "=",
                data: "=rtData"
            },
            template: '<div ng-if="!data || data.length==0" style="margin-top: 20%;"><div class="row"><div class="col text-center"><img style="width:40%;height:auto;font-size: small;" src="./asset/img/empty2.png" />' +
            '</div></div><div class="row" style="padding-top: 0px;"><div class="col text-center"  style="padding-top: 0px;"><h4 style="color:#dadadf;margin-top: 0px;"><b>{{rtMessage}}</b></h4></div></div></div>'
        };
    });
/********************************************************
Copyright @ 苏州瑞泰信息技术有限公司 All rights reserved. 
 创建人   : East Lv
 创建时间 : 2016-03-10
 说明     : 高级查找的过滤视图
*********************************************************/
angular.module('xrmApp')
    .directive('rtFilterView', function () {
        return {
            restrict: 'E',
            replace: true,
            require: 'ngModel',
            transclude: true,
            scope: {
                visible: "=rtVisible",
                marginTop: "=rtMarginTop",
                conditions: "=rtConditions",
                onFilter: "&rtOnFilter"
            },
            template: '<div ng-show="visible === true" class="filter-container" style="top:{{ marginTop }}px;">' +
            '<div  class="filter-mask"></div>' +
            '<div class="filter-box"><div class="filter-conditions"><div ng-repeat="condition in conditions"                     ng-class="condition.selected ? \'filter-condition on\':\'filter-condition\'"                     ng-click="selectFilter(condition)">                    {{condition.displayName}}<div ng-if="condition.dataType===\'picklist\' && condition.selected" class="list"                         style="position:absolute;                                left:30%;top:0;bottom:0;right:0;                                overflow:auto;background-color: #f5f8fa;                                padding-left:10px;padding-right:1px;"><ion-checkbox ng-if="condition.multiple === true"                                      ng-repeat="option in condition.options"                                      ng-model="option.checked"                                      class="item-checkbox-right filter-condition-option">                            {{ option.text}}</ion-checkbox><ion-radio ng-if="condition.multiple === false"                                   ng-repeat="option in condition.options"                                   ng-value="option.value"                                   ng-model="condition.checkedValue"                                   class="item-checkbox-right filter-condition-option">                            {{ option.text}}</ion-radio></div></div></div><div class="filter-btns" style="border-top:1px solid #ccd6dd;"><button class="button button-clear"                        style="float:left;margin-left:10px;color:#469ae9;"                        ng-click="resetFilter()">重置</button><button class="button button-clear"                        style="float:right;margin-right:10px;color:#469ae9;"                        ng-click="confirmFilter()">确定</button></div></div>' +
            '</div>',
            link: function(scope, element, attrs, ngModelCtrl) {
                var conditions = scope.conditions;

                scope.selectFilter = function (filter) {
                    filter.selected = true;

                    for (var i = 0; i < conditions.length; i++) {
                        if (conditions[i].name === filter.name) {
                            continue;
                        }
                        if (conditions[i].selected) {
                            conditions[i].selected = false;
                        }
                    }
                };

                scope.checkOption = function (filter, option) {
                    option.checked = true;

                    for (var i = 0; i < filter.options.length; i++) {
                        if (filter.options[i].value === option.value) {
                            continue;
                        }

                        if (filter.options[i].checked) {
                            filter.options[i].checked = false;
                        }
                    }
                };

                scope.resetFilter = function () {

                    for (var i = 0; i < conditions.length; i++) {
                        if (conditions[i].multiple) {
                            var checkedOptions = _.filter(conditions[i].options, {"checked": true});
                            if (checkedOptions !== null && checkedOptions.length > 0) {

                                for (var j = 0; j < checkedOptions.length; j++) {
                                    checkedOptions[j].checked = false;
                                }
                            }
                        } else {
                            var selectedValue = conditions[i].checkedValue;
                            if (selectedValue !== undefined) {
                                conditions[i].checkedValue = undefined;
                            }
                        }
                    }
                };

                scope.confirmFilter = function () {
                    var result = [];

                    for (var i = 0; i < conditions.length; i++) {
                        if (conditions[i].multiple) {
                            var checkedOptions = _.filter(conditions[i].options, {"checked": true});
                            if (checkedOptions !== null && checkedOptions.length > 0) {

                                var values = [];
                                for (var j = 0; j < checkedOptions.length; j++) {
                                    values.push(checkedOptions[j].value);
                                }

                                result.push({"name": conditions[i].name, "values": values});
                            }
                        } else {
                            var selectedValue = conditions[i].checkedValue;
                            if (selectedValue !== undefined && selectedValue !== null) {
                                result.push({"name": conditions[i].name, "values": [selectedValue]});
                            }
                        }
                    }

                    scope.visible = false;

                    ngModelCtrl.$setViewValue(result);

                    if (scope.onFilter !== undefined) {
                        scope.onFilter();
                    }
                };
            }
        };
    });
/********************************************************
Copyright @ 苏州瑞泰信息技术有限公司 All rights reserved. 
 创建人   : joe Song
 创建时间 : 2016-03-10
 说明     : 自动设置焦点的Directive
*********************************************************/

angular.module('xrmApp')
    .directive('focusOn', function () {
        return function (scope, elem, attr) {
            scope.$on('focusOn', function (e, name) {
                if (name === attr.focusOn) {
                    elem[0].focus();
                }
            });
        };

    }).factory('focus', function ($rootScope, $timeout) {
        return function (name) {
            $timeout(function () {
                $rootScope.$broadcast('focusOn', name);
            });
        };
    });

/********************************************************
 Copyright @ 苏州瑞泰信息技术有限公司 All rights reserved.
 创建人   : MonaXia
 创建时间 : 2017-01-05
 说明     : 进度条
 *********************************************************/
/*global angular*/
angular.module('xrmApp')
    .directive('rtProcessSteps', ['rt', function(rt) {
        'use strict';
        return {
                  restrict: "E",
                   scope: {
                       rtModel: "="
                   },
                  replace: true,
                  template: function(element, attrs) {
                        return '<div class="rt-process-steps">' +
                                  '<div ng-repeat="data in rtModel" ng-class="{\'rt-content-last\': $last,\'rt-content-not-last\': !$last}">' +
                                      '<img ng-if="$first" src="./asset/img/now-in.png" class="rt-img">' +
                                      '<img ng-if="!$first" src="./asset/img/done.png" class="rt-img">' +
                                      '<div style="float: right;width: {{ScreenWidth - 40}}px;">' +
                                          '<span ng-if="$first" class="rt-content-first">{{data.title}}</span>' +
                                          '<span ng-if="!$first" class="rt-content-not-first">{{data.title}}</span>' +
                                          '<div class="rt-content-date">{{data.date | date:"yyyy-MM-dd HH:mm"}}</div>' +
                                          '<div class="rt-content-point">奖励积分：{{data.point}}</div>'+
                                      '</div>' +
                                  '</div>' +
                               '</div>'
                  },
                  controller: function($scope, $element) {
                    $scope.ScreenWidth = rt.getDocumentWidth();
                  }
            };
    }]);

/********************************************************
Copyright @ 苏州瑞泰信息技术有限公司 All rights reserved.
 创建人   : Chad Chen
 创建时间 : 2016-10-27
 说明     : 自动高度文本框
*********************************************************/

angular.module('xrmApp')
    .directive('rtTextareaAutosize', function () {
        return {
            template: `<textarea
                                style="overflow-y: hidden;border: 1px;"
                                onpropertychange="this.style.height = this.scrollHeight + 'px';"
                                oninput="this.style.height = this.scrollHeight + 'px';"
                                rows="{{rtRow}}"></textarea>
                `,
            restrict: 'E',
            transclude: true,
            replace: true,
            scope: {
                rtRow: '@'
            }
        };
    });
    
/********************************************************
 Copyright @ 苏州瑞泰信息技术有限公司 All rights reserved.
 创建人   : Chad Chen
 创建时间 : 2017-03-03
 说明     : 内容区域（解决iOS下弹出键盘界面有时会出现空白的问题，解决Android下页面滚动有时滚动不了的问题）
 *********************************************************/
/*global angular*/
angular.module('xrmApp')
/*-------   内容区域   -------*/
.directive('rtContent',function () {
    'use strict';
    return{
        restrict: 'E',           
        transclude: true,           
        template: function(element, attrs) {

            if (ionic.Platform.isIOS()) {
                return '<ion-scroll direction="y" class="'+ attrs.rtClass +'" overflow-scroll="true"style="right: 0;left: 0;position: absolute;"><rt-list ng-transclude></rt-list></ion-scroll>';
            }else{
                return '<ion-content overflow-scroll="true" class=" '+ attrs.rtClass +'"><rt-list ng-transclude></rt-list></ion-content>';
            }
        
        }
    };
});
