/**
 * 系统演示配置详情页面JS
 * Created by likui on 2020/2/5.
 */
var el = {
    form:'#form',
    id: '#id',
    remark: '#remark',
    configKey: '#configKey',
    configType: '#configType',

    text:'#text',
    div1:'#div1',
    div2:'#div2',
    div3:'#div3',
    div4:'#div4',

    fileUpload: '#fileUpload',
    helpFileUpload: '#helpFileUpload',
    attachmentUpload: '#attachmentUpload',
    helpAttachmentUpload: '#helpAttachmentUpload',
    fileInput: '#fileInput',
    helpFileInput: '#helpFileInput'
};


$(function () {
    var id = $(el.id).val();
    if (id) {
        $(el.configKey).textbox("readonly");
        $(el.configType).combobox("readonly");
    }
    $(el.configType).combobox({
        valueField: 'value',
        textField: 'label',
        data: [
            {label: '文本型', value: '1'},
            {label: '单选型', value: '2'},
            {label: '文件型', value: '3'},
            {label: '帮助文档', value: '4'}
        ],
        editable: false,
        prompt: '--- 请选择 ---',
        panelHeight: 'auto',
        onSelect:function (record) {
            console.log(record);
            $(el.div1).toggleClass("hidden",record.value !== '1');
            $(el.div2).toggleClass("hidden",record.value !== '2');
            $(el.div3).toggleClass("hidden",record.value !== '3');
            $(el.div4).toggleClass("hidden",record.value !== '4');

            if (record.value === '1') {
                $(el.text).textbox({cls: "text"});
            }

            if (record.value === '3') {
                $(el.fileUpload).textbox({cls: "text"});
            }
            if (record.value === '4') {
                $(el.helpFileUpload).textbox({cls: "text"});
            }
        }
    });
    var configType = $(el.configType).val();
    if (!configType) {
        $(el.configType).combobox("select","1");
    }


    $(el.fileUpload).textbox({
        editable: false,
        icons: [
            {
                iconCls: 'icon-add',
                handler: function () {
                    uploadAttachment();
                }
            }
        ]
    });
    $(el.fileUpload).textbox('textbox').css('cursor', 'pointer');
    $(el.fileUpload).textbox('textbox').bind('click', function () {
        uploadAttachment();
    });


    $(el.helpFileUpload).textbox({
        editable: false,
        icons: [
            {
                iconCls: 'icon-add',
                handler: function () {
                    uploadHelpAttachment();
                }
            }
        ]
    });
    $(el.helpFileUpload).textbox('textbox').css('cursor', 'pointer');
    $(el.helpFileUpload).textbox('textbox').bind('click', function () {
        uploadHelpAttachment();
    });


    $(el.fileInput).filebox({
        onChange: function (newValue, oldValue) {
            var files = $(el.fileInput).filebox('files');
            uploadAttachmentData(files);
        }
    });
    $(el.helpFileInput).filebox({
        onChange: function (newValue, oldValue) {
            var files = $(el.helpFileInput).filebox('files');
            uploadHelpAttachmentData(files);
        }
    });
});


/**
 * 浏览上传配置附件
 */
function uploadAttachment() {
    $(el.attachmentUpload).find('.filebox-label').click();
}

function uploadHelpAttachment() {
    $(el.helpAttachmentUpload).find('.filebox-label').click();
}



/**
 * 上传附件数据
 *
 * @param files 附件
 */
function uploadAttachmentData(files) {
    var formData = new FormData();
    for (var n = 0; n < files.length; n++) {
        formData.append('file', files[n]);
    }

    // loading
    var index = topWin.layer.load(1, {shade: [0.1, '#eee']});
    $.ajax({
        url: g.ctx + 'pc/sys/file/rest/upload/file',
        data: formData,
        type: 'POST',
        contentType: false,
        processData: false,
        success: function (data) {
            topWin.layer.close(index);
            $(el.fileUpload).textbox('setValue',  data.id);
        },
        error: function (data) {
            topWin.layer.alert(data.responseJSON.message, {icon: 2});
            topWin.layer.close(index);
        }
    });
}

/**
 * 上传附件数据
 *
 * @param files 附件
 */
function uploadHelpAttachmentData(files) {
    var formData = new FormData();
    for (var n = 0; n < files.length; n++) {
        formData.append('file', files[n]);
    }

    // loading
    var index = topWin.layer.load(1, {shade: [0.1, '#eee']});
    $.ajax({
        url: g.ctx + 'pc/sys/file/rest/upload/file',
        data: formData,
        type: 'POST',
        contentType: false,
        processData: false,
        success: function (data) {
            topWin.layer.close(index);
            $(el.helpFileUpload).textbox('setValue',  data.id);
        },
        error: function (data) {
            topWin.layer.alert(data.responseJSON.message, {icon: 2});
            topWin.layer.close(index);
        }
    });
}


/**
 * 数据保存
 *
 * @param callback 回调函数
 */
function saveConfigData(callback) {
    var validate = $(el.form).form('validate');
    if (!validate) {
        layer.msg(commonMsg.msg2);
        return false;
    }

    var config = {};
    config.id = $(el.id).val();
    config.remark = $(el.remark).val();
    config.configKey = $(el.configKey).val();
    config.configType = $(el.configType).val();
    switch (config.configType) {
        case "1":
            config.configValue = $(el.text).val();
            break;
        case "2":
            config.configValue = $("input[name=configValue]:checked").val();
            break;
        case "3":
            config.configValue = $(el.fileUpload).val();
            break;
        case "4":
            config.configValue = $(el.helpFileUpload).val();
            break;
    }
    if (!config.configValue) {
        topWin.layer.alert('配置内容请求完善', {icon: 2});
        return;
    }
    console.log(JSON.stringify(config));
    $.ajax({
        type: 'POST',
        url: g.ctx + 'pc/sys/sysConfig/rest/saveOrUpdate',
        data: config,
        success: function () {
            callback('');
        },
        error: function (data) {
            callback(data.responseJSON);
        }
    });
}