var colFieldIDs = [];
function init(colFields) {
    const colFieldData = JSON.parse(colFields);
    console.log(colFieldData);
    for (var i = 0; i < colFieldData.length; i++) {
        let colField = colFieldData[i];
        colFieldIDs.push(colField.name);
        if (colField.type == 526) {//单行文本
            let htmlRow = '<div class="awsui-row" ' + (colField.readonly == 1 ? 'style="pointer-events: none;"' : '') + '><div class="awsui-col-xs-2 awsui-col-sm-2 awsui-col-md-2 awsui-col-lg-2 awsui-text">' + colField.comment + '</div><div class="awsui-col-xs-10 awsui-col-sm-10 awsui-col-md-10 awsui-col-lg-10 awsui-form-parent"><span class=" ' + (colField.required == 1 ? 'required' : '') + '" style="width:100%;"><input type="text" class="awsui-input" id="' + colField.name + '" name="' + colField.name + '"></span></div></div>';
            $("#udfForm").append(htmlRow);
        } else if (colField.type == 527) {//多行文本
            let htmlRow = '<div class="awsui-row" ' + (colField.readonly == 1 ? 'style="pointer-events: none;"' : '') + '><div class="awsui-col-xs-2 awsui-col-sm-2 awsui-col-md-2 awsui-col-lg-2 awsui-textar">' + colField.comment + '</div><div class="awsui-col-xs-10 awsui-col-sm-10 awsui-col-md-10 awsui-col-lg-10 awsui-form-parent"><span class=" ' + (colField.required == 1 ? 'required' : '') + '"  style="width:100%;"><textarea type="text"  rows="3" class="awsui-input" id="' + colField.name + '" name="' + colField.name + '">' + colField.value + '</textarea></span></div></div>';
            $("#udfForm").append(htmlRow);
        } else if (colField.type == 528) {//日期
            let htmlRow = '<div class="awsui-row" ' + (colField.readonly == 1 ? 'style="pointer-events: none;"' : '') + '><div class="awsui-col-xs-2 awsui-col-sm-2 awsui-col-md-2 awsui-col-lg-2 awsui-text">' + colField.comment + '</div><div class="awsui-col-xs-10 awsui-col-sm-10 awsui-col-md-10 awsui-col-lg-10 awsui-form-parent"><span class=" ' + (colField.required == 1 ? 'required' : '') + '"  style="width:100%;"><input type="text" class="awsui-datepicker awsui-input" id="' + colField.name + '" name="' + colField.name + '"/></span></div></div>';
            $("#udfForm").append(htmlRow);
        } else if (colField.type == 529) {//数字
            let htmlRow = '<div class="awsui-row" ' + (colField.readonly == 1 ? 'style="pointer-events: none;"' : '') + '><div class="awsui-col-xs-2 awsui-col-sm-2 awsui-col-md-2 awsui-col-lg-2 awsui-text">' + colField.comment + '</div><div class="awsui-col-xs-10 awsui-col-sm-10 awsui-col-md-10 awsui-col-lg-10 awsui-form-parent"><span class=" ' + (colField.required == 1 ? 'required' : '') + '"  style="width:100%;"><input type="number" class="awsui-input" id="' + colField.name + '" name="' + colField.name + '"></span></div></div>';
            $("#udfForm").append(htmlRow);
        } else if (colField.type == 530) {//列表
            let options = '';
            for (var j = 0; j < colField.list.length; j++) {
                let option = colField.list[j];
                options += '<option value="' + option.VALUE + '">' + option.LABEL + '</option>';
            }
            //console.log("options",options);
            let htmlRow = '<div class="awsui-row" ' + (colField.readonly == 1 ? 'style="pointer-events: none;"' : '') + '><div class="awsui-col-xs-2 awsui-col-sm-2 awsui-col-md-2 awsui-col-lg-2 awsui-text">' + colField.comment + '</div><div class="awsui-col-xs-10 awsui-col-sm-10 awsui-col-md-10 awsui-col-lg-10 awsui-form-parent"><span class=" ' + (colField.required == 1 ? 'required' : '') + '"  style="width:100%;"><select id="' + colField.name + '" name="' + colField.name + '">' + options + '</select></span></div></div>';
            $("#udfForm").append(htmlRow);
        } else if (colField.type == 532) {//布尔
            let htmlRow = '<div class="awsui-row" ' + (colField.readonly == 1 ? 'style="pointer-events: none;"' : '') + '><div class="awsui-col-xs-2 awsui-col-sm-2 awsui-col-md-2 awsui-col-lg-2 awsui-text">' + colField.comment + '</div><div class="awsui-col-xs-10 awsui-col-sm-10 awsui-col-md-10 awsui-col-lg-10 awsui-form-parent"><span class=" ' + (colField.required == 1 ? 'required' : '') + '"  style="width:100%;"><input type="checkbox" id="' + colField.name + '" name="' + colField.name + '" ' + (colField.value == 1 ? 'checked' : '') + '></span></div></div>';
            $("#udfForm").append(htmlRow);
        }


        $("#" + colField.name).val(colField.value);
        if (colField.type == 528) {
            $("#" + colField.name).datepicker({
                format: "yyyy-mm-dd",
                autoclose: true
            });
        } else if (colField.type == 530) {
            $("#" + colField.name).select2();
            $(".select2-container").css("width", "100%");

            $("#" + colField.name).val([colField.value]).trigger("change"); //单选赋值

        } else if (colField.type == 532 && colField.value == 1) {
            $("#" + colField.name).switchButton();
        }
    }

   // $("#udfFormDiv").css("height", colFieldIDs.length*50+"px");
/*
    $(".required").find(".awsui-input").focus(function () {
        $.each($(".required").find(".awsui-input"), function (i, el) {
            $(el).css("background-color", "#fff");
        });
        $(this).css("background-color", "#FFFFCC");
    });
    $(".required").find(".awsui-input").blur(function () {
        var inputVal = $(this).val();
        if (inputVal == "") {
            $(this).tooltip({
                positon: "top",
                text: "不允许为空，请重新输入",
                bgcolor: "#f9f9f9",
                color: "red",
                borderColor: "red",
                close: false
            });
            return false;
        }
    });

 */

}

/**
 * 保存用户定义字段(UDF)的数据
 * 此函数负责收集所有必需的用户定义字段数据，并通过AJAX请求将其保存到服务器
 * @param {string} objectId - 关联的对象ID，用于标识特定的记录或实体
 */
function getUDFColumnData(objectId) {
    let udfForm = {};
    // 遍历收集所有用户定义字段的值
    for (var i = 0; i < colFieldIDs.length; i++) {
        let id = colFieldIDs[i];
        // 将每个字段的值存储到udfForm对象中
        udfForm[id] = $("#" + id).val().toString();
    }
    return  udfForm;
}

function saveUDF(objectId) {
    // 必填校验
    // 遍历所有标记为必填的输入字段，检查是否有空值
    $(".required").find(".awsui-input").each(function () {
        if ($(this).val() == "") {
            // 如果发现空值，通常会在这里添加一些提示用户输入的逻辑
        }
    });

    // 创建一个对象来存储所有用户定义字段的数据
    let udfForm = {};
    // 遍历收集所有用户定义字段的值
    for (var i = 0; i < colFieldIDs.length; i++) {
        let id = colFieldIDs[i];
        // 将每个字段的值存储到udfForm对象中
        udfForm[id] = $("#" + id).val().toString();
    }

    // 使用AWSUI框架的AJAX请求方法，向服务器发送POST请求以保存数据
    awsui.ajax.request({
        type: "POST",
        // 定义请求的URL，包含命令和会话ID
        url: "./jd?cmd=com.awspaas.user.apps.donenow_sys_udf_save&sid=" + sid,
        // 将类别ID、对象ID和用户定义字段数据作为请求数据发送
        data: "cateId="+cateId+"&objectId="+objectId+"&data="+JSON.stringify(udfForm),
        alert: false,
        // 定义请求成功时的回调函数
        success: function (res) {
            // 在控制台输出服务器返回的响应数据，通常在这里处理成功后的逻辑
            console.log(res);
        }
    });
}




