/** 意见按钮 */


var angular = require('angular');
require('../modules');
require('../../flow/services/NotionService');
require('../../../commons/services/CommonService');
require('../../flow/services/CommonNotionService');
require('../../linker/services/LinkerService');
require('../services/OpenQzNotionService');

var module = angular.module('flowmodulebutton.directives');

/**
 <sino-flow-notion-button
  flow-type=""
  record-id=""
  table-name="" 表名 archive_out
  table-id="" 表主键名 id
  notions="" 意见列表 Array<String>非必填
  name="" 名称
 ></sino-flow-notion-button>
 */
module.directive('sinoFlowNotionButton', function(NotionService, $state, CommonService,
  $ionicActionSheet, $toast, CommonNotionService, LinkerService, OpenQzNotionService, DefaultWorkflowParam) {

  return {
    restrict: 'E',
    require: ['^sinoFlowButtonBar', 'sinoFlowNotionButton'],
    controller: 'SinoFlowNotionButtonController',
    link: function(scope, element, attrs, ctrls) {
      var buttonBarCtrl = ctrls[0],
        notionButtonCtrl = ctrls[1],
        newScope;

      buttonBarCtrl.openNotion = openNotion;
      DefaultWorkflowParam.get().notions = scope.$eval(attrs.notions);
      DefaultWorkflowParam.get().tempNotions = notionButtonCtrl.getAllTempNotion();


      (function() {
        setScope();

        buttonBarCtrl.setTempNotions(notionButtonCtrl.getAllTempNotion());

        buttonBarCtrl.button(function() {
          return {
            id: 'notion',
            name: attrs.name,
            show: attrs.show ? scope.$eval(attrs.show) : true,
            onClick: openNotion
          };
        });
      })();

      function setScope() {
        newScope = scope.$new();
        newScope.closeNotion = function() {
          if (newScope.modal) {
            newScope.modal.remove();
            newScope.modal = null;
          }
        }
        newScope.$on('$destroy', function() {
          newScope.closeNotion();
        });
      }

      function openNotion() {
        DefaultWorkflowParam.get().okButton = '保存';
        DefaultWorkflowParam.get().successFn = function() {
          $toast.showShortCenter('保存意见成功!');
        };
        var notionSelects = notionButtonCtrl.getButtonsText();
        if(notionSelects.length === 1 && notionSelects[0].text.indexOf('查看意见') != -1) {
          openNotionList();
        } else {
          $ionicActionSheet.show({
            buttons: notionButtonCtrl.getButtonsText(),
            titleText: '请选择操作',
            cancelText: '取消',
            cancel: function() {
              return true;
            },
            buttonClicked: function(index, text) {
              if(index === 0){
                openNotionList();
                return true;
              } else {
                OpenQzNotionService.openWriteNotion(text.text);
                return true;
              }
            }
           });
        }
      }

      function openNotionList() {
        if (!newScope.modal) {
          CommonService.createModal('views/flow/notionList.tpl.html', newScope).then(function(modal) {
            newScope.modal = modal;
            newScope.modal.show();
          });
        } else {
          newScope.modal.show();
        }
      }
    }
  };
})
.controller('SinoFlowNotionButtonController', function() {
  var self = this, tempNotions = [];

  self.resetTempNotion = resetTempNotion;
  self.addTempNotion = addTempNotion;
  self.getAllTempNotion = getAllTempNotion;
  self.getButtonsText = getButtonsText;

  function resetTempNotion() {
    tempNotions = [];
  }

  function addTempNotion(tempNotion) {
    tempNotions.push(tempNotion);
  }

  function getAllTempNotion() {
    return tempNotions;
  }

  function getButtonsText() {
    var buttons = [{ text: '<i class="icon ion-android-list sino-view-notions-icon"></i>查看意见' }];
    tempNotions.forEach(function(tempNotion, index) {
      if(tempNotion.show) {
        buttons.push({ text: '<i class="icon ion-android-create sino-edit-notions-icon"></i>' + '填写' +tempNotion.title });
      }
    });
    return buttons;
  }



});
