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

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

module.config(function($stateProvider) {
  $stateProvider.state('flowNotion', {
    url: '/flowNotion',
    controller: 'NotionCtrl',
    templateUrl: 'views/flow/notion.tpl.html'
  });
});

/**
* notionConfig : {
*   tableName : 业务表
*   tableId: 业务主键ID
*   recordId: 主键ID
*   moduleName: 模块名称，例如局发文
*   flowType: 流程类型
*   backUrl: 返回地址
*   backUrlParam: 返回地址参数
*   notions: 意见信息列表 [notion]
*   tempNotions: [{
*       type: 'notionType',
*       title: 'notionName',
*       content: 'notionContent',
*       show: 展示权限  ture,false
*     }]
* }
*/
module.controller('NotionCtrl', function($scope, $state, $toast, $ionicPopup, $ionicScrollDelegate, $q,
  NotionService, CommonNotionService, CommonService, LinkerService, $ionicListDelegate, DefaultWorkflowParam) {

  (function() {
    $scope.delegate = $ionicScrollDelegate.$getByHandle('mainScroll');
    $scope.notionConfig = DefaultWorkflowParam.get();
    initNotion();
    initTempNotion();
    initAuth();
  })();

  function initNotion() {
    $scope.notions = $scope.notionConfig.notions || null;
  }

  function initAuth() {
    $scope.hasNotion = _.some($scope.notions, function(value) {
      return !!value;
    }) || _.some($scope.tempNotions, function(value) {
      return value.show;
    });
  }

  function initTempNotion() {
    $scope.tempNotions = $scope.notionConfig.tempNotions;
  }

  $scope.setTempNotion = function(notion, type) {
    $scope.tempNotions.forEach(function(tempNotion) {
      if(tempNotion.type === type) {
        tempNotion.content = notion;
      }
    });
    $scope.delegate.scrollBottom();
  }

  $scope.deleteNotion = function(index, notionType, event) {
    $ionicPopup.confirm({
      title: '删除',
      content: '确定要删除签字意见吗？',
      okText: '确定',
      cancelText: '取消'
    }).then(function(res) {
      if (res) {
        var opinion = {
          'recordId': $scope.notionConfig.recordId,
          'flowTypeId': $scope.notionConfig.flowType,
          'notionName': notionType,
          'notion': '',
          'tableName': $scope.notionConfig.tableName,
          'signFieldName': '',
          'idFieldName': $scope.notionConfig.tableId
        };
        opinion.signTime = _.formatDate(new Date(), 'Y-m-d H:i:s');
        return NotionService.saveTempNotion(opinion).then(function() {
          $toast.showShortCenter('删除意见成功!');
          $ionicListDelegate.closeOptionButtons()
          $scope.tempNotions[index].content = '';
        }, function() {
          $toast.showShortCenter('删除意见失败!');
        });
      }
    });
  };

  $scope.orderBy = function(order) {
     $scope.order = order;
  };

  /**
   * 关闭下拉列表
   */
  $scope.closeDropDown = function() {
    if ($scope.dropdown) {
      $scope.dropdown = !$scope.dropdown;
    }
  };

  $scope.changeDropDown = function() {
    $scope.dropdown = !$scope.dropdown;
  };
});
