var angular = require('angular');
var _ = require('jsUtil');
require('../services/CommonNotionService');
require('../../../commons/directives/$toast');
require('../../linker/services/LinkerService');
require('../services/NotionService');
require('../../flowmodulebutton/services/DefaultWorkflowService');

var module = angular.module('flow');

module.config(function($stateProvider) {
  $stateProvider.state('flowQzNotion', {
    url: '/qzflowNotion',
    controller: 'QzNotionCtrl',
    templateUrl: 'views/flow/qzNotion.tpl.html'
  });
});
module.controller('QzNotionCtrl', function($scope, $state, $toast,
  CommonNotionService, LinkerService, NotionService, DefaultWorkflowService, DefaultWorkflowParam) {

  var notionConfig = DefaultWorkflowParam.get();
  $scope.okButton = notionConfig.okButton;
  $scope.tempNotion = notionConfig.tempNotion;
  $scope.stashNotion = {};
  $scope.stashNotion.notion = $scope.tempNotion.content || '';
  $scope.stashNotion.notion = $scope.stashNotion.notion === 'TRUE' ? '' : $scope.stashNotion.notion;


  $scope.setCommonNotion = setCommonNotion;//添加意见
  $scope.saveNotion = saveNotion;//保存意见
  $scope.saveCommonNotion = saveCommonNotion;// 保存为常用批示语
  $scope.closeNotionModal = closeNotionModal;
  $scope.nextOperate = nextOperate;

  function nextOperate() {
    saveNotion().then(function() {
      if (notionConfig.successFn) {
        notionConfig.successFn();
      } else {
        saveSuccess();
      }
    }, function() {
      $toast.showShortCenter('意见保存失败!');
    });
  }

  //流程操作
  function flowOperate() {
    DefaultWorkflowService.send(DefaultWorkflowParam.get())
      .then(function(data) {
      if (data !== 'cancel') {
        ctrl.updateSubflag(scope, attrs, '1');
        deferred.resolve(ctrl.successFn());
      } else {
        deferred.resolve();
      }
    });
  }


  function setCommonNotion(notion) {
    $scope.stashNotion.notion = $scope.stashNotion.notion + notion;
  };

  //保存意见
  function saveNotion() {
    if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard.isVisible) {
      window.cordova.plugins.Keyboard.close();
    }
    var stashNotion = $scope.stashNotion.notion.replace(/<[^>]*>/g, '');
    var opinion = {
      'recordId': notionConfig.recordId,
      'flowTypeId': notionConfig.flowType,
      'notionName': $scope.tempNotion.type,
      'notion': _.isEmpty(stashNotion) ? 'TRUE' : stashNotion,
      'tableName': notionConfig.tableName,
      'signFieldName': '',
      'idFieldName': notionConfig.tableId/*,
      'generateSign': !!notionConfig.tempNotion.generateSign*/
    };
    opinion.signTime = _.formatDate(new Date(), 'Y-m-d H:i:s');
    return NotionService.saveTempNotion(opinion).then(function() {
      $scope.closeNotionModal();
      $scope.tempNotion.content = $scope.stashNotion.notion;
      DefaultWorkflowParam.setTempNotion($scope.tempNotion.type, _.isEmpty(stashNotion) ? 'TRUE' : stashNotion);
      if(notionConfig.tempNotion.generateSign) {
        $scope.tempNotion.tempSign = true;
      }
    });
  };

  function saveSuccess() {
    $toast.showShortCenter('保存意见成功!');
  }


  function saveCommonNotion() {
    if (!$scope.stashNotion.notion) {
      $toast.showShortCenter('添加的意见不能为空!');
      return;
    }
    return LinkerService.getCurrentUser().then(function(loginUser) {
      return CommonNotionService.saveCommonNotion(loginUser.userId, $scope.stashNotion.notion)
      .then(function(data) {
        if (data) {
          $toast.showShortCenter('添加成功!');
        }
        $scope.$broadcast('scroll.infiniteScrollComplete');
        $scope.$broadcast('scroll.resize');
      });
    });
  };

  function closeNotionModal() {
    if ($scope.modal) {
      $scope.modal.hide();
      $scope.closeNotion();
    }
  };

});
