let saveFlag = {done: false, ok: false};
layui.use(['form'], function () {
    const form = layui.form;
    initDictStateCheckBox();

    /**
     * 初始化字典状态开关组件
     */
    function initDictStateCheckBox(){
        if(dict.dictState == null){
            dict.dictState = true;
        }
        $('#dictStateBox').attr('checked',dict.dictState)
        form.render();
        form.on('switch(dictStateBox)', function(data){
            form.val('dictForm', {dictState:data.elem.checked});
        });
    }

    /**
     * 表单提交事件监听
     */
    form.on('submit(dictForm)', function (elem) {
        let data = form.val('dictForm');
        $.post(ctx + "/sys/dict/save", data, function (data) {
            data.msg = data.flag ? '保存成功' : data.msg;
            let icon = data.flag ? 1 : 5;
            layer.msg(data.msg, {icon, time: 2000}, function () {
                saveFlag.done = true;
                saveFlag.ok = data.flag;
            });
        });
        return false; //阻止表单跳转
    });
});

/**
 * 提交保存
 */
function save() {
    $("#btn_submit").click();
    return getSaveStatus();
}

/**
 * 每隔100毫秒获取表单保存进度，如果保存请求完成则返回保存状态
 * @returns {Promise<boolean|boolean|*>}
 */
async function getSaveStatus() {
    await Util.sleep(100);
    if (saveFlag.done) {
        return saveFlag.ok;
    }
    return getSaveStatus();
}