var angular = require('angular');
require('jsUtil');
require('../modules');
require('../services/ArchivesInceptAuthService');
require('../services/ArchivesInceptService');
'use strict';

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

module.controller('ArchivesInceptDetailCtrl', function(
  $scope, $q, $stateParams, $toast,$ionicPopup,$sinoGoBackDelegate,
  ArchivesInceptService, ArchivesInceptAuthService) {
  var ctrl = this;

  ctrl.init = init;
  ctrl.save = save;
  ctrl.refreshWorkFlowInfo = refreshWorkFlowInfo;
  ctrl.signin = signin;
  ctrl.returnback = returnback;
  ctrl.insertSW = insertSW;
  ctrl.wanbi =  wanbi;

   /**
   * 初始化页面
   * @return
   */
   $scope.$on('$ionicView.enter',function(){
      ctrl.init();
   });

  /**
   * 初始化
   */
  function init() {
    var id = $stateParams.id ? $stateParams.id : null;
    ctrl.loadingState = 1;
    return ArchivesInceptService.get(id).then(function(data) {
      ctrl.ArchivesInceptData = data;
      ctrl.archivesincept = data.archivesincept;
      setParams(ctrl.archivesincept);
      ctrl.auth = new ArchivesInceptAuthService(ctrl.ArchivesInceptData);
    }).then(function() {
      ctrl.loadingState = 2;
    }, function() {
      ctrl.loadingState = 3;
    });
  };

  function setParams(archivesincept){
    var biaozhi =  archivesincept.deptflag;
    if (biaozhi == '0' || biaozhi === '部门发文') {
      biaozhi = '部门发文'
    } else if (biaozhi == '1' || biaozhi === '部门发文') {
      biaozhi = '局发文'
    }
    ctrl.biaozhi = biaozhi;

  };

  /*
  * 刷新方法
  */
  function refreshWorkFlowInfo() {
    var workflowInfoPromise = ArchivesInceptService.getWorkflowInfo(ctrl.archivesincept.id, ctrl.archivesincept.xiugai);
    var notionListPromise = ArchivesInceptService.getNotionList(ctrl.archivesincept.xiugai, ctrl.archivesincept.id);
    return $q.all([workflowInfoPromise, notionListPromise]).then(function(datas) {
      ctrl.ArchivesInceptData.workflow = datas[0];
      ctrl.ArchivesInceptData.notionList = datas[1];
    }).then(function() {
      ctrl.auth = new ArchivesInceptAuthService(ctrl.ArchivesInceptData);
    });
  };

  /**
   * 保存收文单数据。
   */
  function save(){
    return ArchivesInceptService.save(ctrl.archivesincept)
    .then(function(changeArchivesIncept) {
      ctrl.ArchivesInceptData.archivesincept = changeArchivesIncept;
      ctrl.archivesincept = changeArchivesIncept;
      ctrl.auth = new ArchivesInceptAuthService(ctrl.ArchivesInceptData);
    });
  };

  /**
  * 签收功能
  */
  function signin(){
    ArchivesInceptService.signin(ctrl.archivesincept)
    .then(function() {
      $toast.showLongCenter('签收成功！');
      $sinoGoBackDelegate.back();
     }),function(){
      $toast.showLongCenter('签收失败，请检查网络连接。');
    };
  };

  /**
  * 退文功能
  **/
  function returnback() {
    if (!ctrl.archivesincept.reasonDocbackCal) {
      $toast.showLongCenter('请填写退文原因！');
      return false;
    }
    ArchivesInceptService.returnback(ctrl.archivesincept)
    .then(function(){
      $toast.showLongCenter('退文成功！');
      $sinoGoBackDelegate.back();
    }),function(){
      $toast.showLongCenter('退文失败，请检查网络连接。');
    };
  };

  /**
  * 处理完毕按钮
  **/
  function wanbi() {
    ArchivesInceptService.wanbi(ctrl.archivesincept)
    .then(function(){
      $toast.showLongCenter('办毕完成！');
      $sinoGoBackDelegate.back();
    }),function(){
      $toast.showLongCenter('办毕失败，请检查网络连接。');
    };
  };

  /**
  * 生成收文单按钮
  **/
  function insertSW() {
    ArchivesInceptService.insertSW(ctrl.archivesincept)
    .then(function(){
      $toast.showLongCenter('生成收文单完成！');
      $sinoGoBackDelegate.back();
    }),function(){
      $toast.showLongCenter('生成收文单失败，请检查网络连接。');
    };
  };

});
