var angular = require('angular');
require('../modules');
var module = angular.module('commons.directives');
module.directive('sinoClosableFormItemDivider', function($timeout) {
  return {
    require: ['?^$ionicScroll'],
    template: '<div class="item item-divider item-icon-right">' +
        '{{title}}（{{tip}}）' +
        '<i class="icon" ' +
        ' ng-class="{\'ion-arrow-down-b\': openStatus, \'ion-arrow-up-b\': !openStatus}"></i>' +
      '</div>',
    scope: {
      title: '@',
      openStatus: '='
    },
    link: function($scope, $element, $attr, ctrls) {
      var ionicScrollCtrl = ctrls[0];
      $element[0].addEventListener('click', function() {
        $scope.$evalAsync(function() {
          $scope.openStatus = !$scope.openStatus;
          updateTip();
          $timeout(function() {
            if (ionicScrollCtrl) {
              var scrollView = ionicScrollCtrl.scrollView;
              scrollView.resize();
              scrollView.scrollTo(scrollView.__scrollLeft, scrollView.__scrollTop + ($scope.openStatus? 400 : 0), false);
            }
          }, 50);
        });
      });

      function updateTip() {
        if ($scope.openStatus) {
          $scope.tip = '点击关闭';
        } else {
          $scope.tip = '点击打开';
        }
      }

      updateTip();
    }
  };
});
