/**
 * 关联弹窗js
 */

//选择单个的回调事件
var singleCallback = function() {
    var iframe = this.iframe.contentWindow;
    if (!iframe.document.body) {
        alert('正在加载中...');
        return false;
    }
    //return false;
}

//选择多个的回调事件
var multiCallback = function() {
    var iframe = this.iframe.contentWindow;
    if (!iframe.document.body) {
        alert('正在加载中...');
        return false;
    }
    var checkeds = iframe.$("#main-form").find("[name='user[]']:checked");
    var inputArr = [];
    var valueArr = [];
    var haveValue = [];
    $.each(checkeds, function(index, checked) {
        var checkedValue = $(checked).val();
        //去除重复
        if ($.inArray(checkedValue, haveValue) == -1) {
            inputArr.push($(checked).data('name'));
            valueArr.push(checkedValue);
            haveValue.push(checkedValue);
        }
    });
    input = inputArr.join();
    value = valueArr.join();
    setValue(input, value);

    //return false;
}

//人物单选回调事件
var singleUserCallback = function() {
    var iframe = this.iframe.contentWindow;
    if (!iframe.document.body) {
        alert('正在加载中...');
        return false;
    }
    var checked = iframe.$("#main-form").find("[name='user']:checked");
    input = checked.data('name');
    value = checked.val();
    setValue(input, value);
}

jQuery(function($) {

    //弹出绑定	
    $(document).on('focus', ".select-project,.select-contract,.select-customer,.select-task,.select-application,.select-workflow,.select-opportunity", function() {
        selectDialog($(this), singleCallback);
    });

    $(document).on('focus', ".select-single-user", function() {
        var value = $("#" + $(this).data('hiddenId')).val();
        var url = $(this).data('url');
        $(this).data('url', url + (url.indexOf('?') != -1 ? '&' : '?') + 'value=' + value);
        selectDialog($(this), singleUserCallback);
    });

    //弹出绑定	
    $(document).on('focus', ".select-multi-user,.select-department", function() {
        //弹窗时把已选的值传给控制器
        var value = $("#" + $(this).data('hiddenId')).val();
        var url = $(this).data('url');
        $(this).data('url', url + (url.indexOf('?') != -1 ? '&' : '?') + 'value=' + value);
        selectDialog($(this), multiCallback);
    });

    //关联切换
    $(".link-select").on('change', function() {
        var option = $(this).find("option:selected");
        var input = $(this).next('input');
        input.attr('class', 'form-control ' + option.data('class'));
        input.data('url', option.data('url'));
        input.data('title', option.data('title'));
        if (input.data('switch') == 0) {
            input.data('switch', 1);
        }
        else {
            input.val('');
            input.trigger('focus');
        }
    });
    $(".link-select").trigger('change');

    //清空输入框
    $(document).on('click', '.clear-input', function() {
        var input = $(this).data('inputId');
        if (input != undefined) {
            $("#" + input).val('');
        }
        var hidden = $(this).data('hiddenId');
        var hiddenValue = $(this).data('hiddenValue');
        if (hidden != undefined) {
            hiddenValue = (hiddenValue == undefined) ? '' : hiddenValue;
            $("#" + hidden).val(hiddenValue);
        }
    });
});

/**
 * 弹出选择窗
 * 
 * @param object obj 触发事件的输入框对象
 * @param callback okCallback 确认的回调事件
 * @returns {undefined}
 */
function selectDialog(obj, okCallback) {
    var url = obj.data('url');
    var title = obj.data('title');
    var hiddenId = obj.data('hiddenId');
    var width = obj.data('width');
    var height = obj.data('height');
    width = width || 700;
    height = height || 420;
    var work_time = $("#blog-work_time").val();
    if (work_time != undefined && work_time.length > 0) {
        url = url + "&work_time=" + work_time;
    }
    art.dialog.data('inputObj', obj);
    art.dialog.data('hiddenObj', $("#" + hiddenId));
    art.dialog.open(url, {
        id: 'select-dialog',
        title: title,
        lock: true,
        width: width,
        height: height,
        ok: okCallback,
        cancel: true
    });
}

/**
 * 设置选择的值并关闭artdialog
 * 
 * @param string input 输入框的值
 * @param string value 隐藏域的值
 */
function setValue(input, value) {

    var inputObj = art.dialog.data('inputObj');
    var hiddenObj = art.dialog.data('hiddenObj');

    //如果触发input框有回调属性，则调用回调函数
    var selectCallback = inputObj.data('callback');
    if (selectCallback !== undefined) {
        var selectCallback = eval(selectCallback);
        selectCallback(input, value);
    }

    inputObj.val(input);
    hiddenObj.val(value);
    inputObj.trigger('blur');
    var list = art.dialog.list['select-dialog'].close();
}

