starter.directive('itemWrite', [function () {
    return {
        restrict: "E",
        scope: {
            item: "=itemData"
        },
        templateUrl: 'templates/template-itemWrite.html',
        controller: function ($scope, $cordovaDatePicker) {

            $scope.selectedChange = function (value) {
                $scope.item.text = value;
            }

            $scope.chooseDate = function (datetime) {
                var options = {
                    mode: 'date',
                    date: new Date(),
                    androidTheme: 3
                };

                $cordovaDatePicker.show(options).then(function (date) {
                    if (date == undefined) return;
                    $scope.item.datetime_data = date;
                });
            }

            if ($scope.item.t__type in [1, 4]) {
                if ($scope.item.required) {
                    $scope.note = '输入内容';
                } else {
                    $scope.note = '输入内容(选填)';
                }
            } else if ($scope.item.t__type in [2, 3, 5]) {
                if ($scope.item.t__type == 5) {
                    if ($scope.item.select_data == undefined)
                        $scope.item.select_data = $scope.item.selecttext;
                    data = JSON.parse($scope.item.extra)
                    data = _.pluck(data, 'text')
                    $scope.selectData = data;
                }
                if (!$scope.item.required)
                    $scope.note = '(选填)';
            }
        }
    }
}])

.directive('setClassWhenAtTop', function ($window) {
    var $win = angular.element($window);
    return {
        restrict: 'A',
        link: function (scope, element, attrs) {
            var topClass = attrs.setClassWhenAtTop,
                offsetTop = element[0].offsetTop;

            $win.on('scroll', function (e) {
                if ($win.scrollTop() >= 44) {
                    element.addClass(topClass);
                } else {
                    element.removeClass(topClass);
                }
            });
        }
    };
})

.directive('daily', function () {
    return {
        restrict: "E",
        scope: {
            dailys: "=dailys",
            loading: "=loading"
        },
        templateUrl: 'templates/template-daily.html',
        controller: function ($scope, $state, Daily) {
            $scope.todetails = function (daily) {
                Daily.daily = _.clone(daily);
                $state.go('daily-details', {
                    'id': daily.id
                });
            }
        }
    }
})

.directive('dailyHeader', function () {
    return {
        restrict: "E",
        scope: {
            daily: "=daily"
        },
        template: '<div class="item item-avatar"><img ng-src="../../../img/panda.png" ng-click="showcardinfo(daily.create_user)"><span>{{daily.create_user_name}}</span><p>{{daily.create_date | date:"MM-dd HH:mm"}}</p><p class="time">日报({{daily.daily_dd | date:"M月d日"}})</p></div>'
    }
})

.directive('dailyContent', function () {
    return {
        restrict: "E",
        scope: {
            daily: "=daily"
        },
        template: '<div class="item item-body"><span ng-bind-html="itemContent | getHtmlContent" ng-repeat="itemContent in daily.daily_tfs"></span><div class="blob"><image-popover all-images="daily.files"></image-popover></div></div>'
    }
})

.filter('getHtmlContent', function () {
    return function (content) {
        if (!content.required)
            return null;
        fieldname = '';
        if (content.daily_template_item != 1)
            fieldname = content.name + '&nbsp:&nbsp';
        fielddata = content.text || '';
        var str = fieldname + fielddata + '<br/>';
        str = str.replace(/\n/g, '<br/>');
        return str;
    }
})