var angular = require('angular');
var _ = require('jsUtil');
require('../modules');
require('../services/mulJobsService');

'use strict';

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

module.controller('SelectJobCtrl', function($scope, mulJobsService) {
    $scope.disableJob = true;
    $scope.hasSend = false;
    $scope.result = {};
    init();
    function init() {
        $scope.jobs = mulJobsService.getMulJobs();
    }

    function getSelectDeptId() {
        return $scope.result.selectJob ? $scope.result.selectJob : "";
    }

    $scope.$watch(function() {
        $scope.disableJob = getSelectDeptId().length <= 0;
        return $scope.disableJob;
    });

    $scope.submit = function() {
        $scope.disable = true;
        $scope.hasSend = true;
        var selectDeptId = getSelectDeptId();
        $scope.data.deptid = selectDeptId ? selectDeptId : scope.data.deptid;
        _.each($scope.jobs, function(job) {
            if(job.deptId == $scope.data.deptid) {
                $scope.data.deptName = job.deptName;
            }
        });
        $scope.close(false);
    }

    $scope.close = function(cancel) {
        $scope.onSelected(cancel);
    };
    
});