/*
 * Activiti Modeler component part of the Activiti project
 * Copyright 2005-2014 Alfresco Software, Ltd. All rights reserved.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

/*
 * form-select
 */
var FormSelectWriteController = ['$scope', '$modal', function($scope, $modal) {
    $modal({
        template:  'editor-app/configuration/properties/form-select-template.html?version=' + Date.now(),
        backdrop: 'static',
        keyboard: false,
        scope: $scope
    });
}];

var FormSelectController = ['$scope', function($scope) {
    var initiator = $scope.selectedItem.properties[4];

    $scope.form = {};

    if($scope.property.value) {
        $scope.form.formId = $scope.property.value;
    }

    $scope.selectFormId = function() {
        top.dialog.dialogData.oldSelectedFormList = [{formId: $scope.form.formId}];
        top.dialog.open({
            url: 'route/form/list-select',
            title: '选择表单',
            width: '1000px',
            height: '500px',
            onClose: function() {
                var newSelectedFormList = top.dialog.dialogData.newSelectedFormList;
                if(newSelectedFormList.length != 0) {
                    $scope.form.formId = newSelectedFormList[0].formId;
                } else {
                    $scope.form.formId = '';
                }
                document.getElementById('formId').value = $scope.form.formId;
                top.dialog.dialogData.oldSelectedFormList = [];
            }
        });
    }

    $scope.save = function() {
        $scope.property.value = '';
        $scope.property.value = $scope.form.formId;
        $scope.updatePropertyInModel($scope.property);

        console.log(initiator);
        initiator.value = 'startAssigneeUser';
        $scope.updatePropertyInModel(initiator);
        $scope.close();
    };

    $scope.cancel = function() {
        $scope.$hide();
        $scope.property.mode = 'read';
    };

    // Close button handler
    $scope.close = function() {
        $scope.property.mode = 'read';
        $scope.$hide();
    };
}];