var form, formdata;

function setFormData() {
    form = document.querySelector("#formId");
    formdata = new FormData(form);
}

function renderForm() {
    layui.use('form', function () {
        var form = layui.form;
        form.render();
    });
}


function closeLayer() {
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
}

if (typeof FileReader == 'undefined') {
    document.getElementById("xmTanDiv").InnerHTML = "<h1>当前浏览器不支持FileReader接口</h1>";
    //使选择控件不可操作
    document.getElementById("xdaTanFileImg").setAttribute("disabled", "disabled");
}

function xmTanUploadImg(obj) {
    var file = obj.files[0];
    var reader = new FileReader();
    //读取文件过程方法
    reader.onloadstart = function (e) {
        console.log("开始读取....");
    }
    reader.onprogress = function (e) {
        console.log("正在读取中....");
    }
    reader.onabort = function (e) {
        console.log("中断读取....");
    }
    reader.onerror = function (e) {
        console.log("读取异常....");
    }
    reader.onload = function (e) {
        console.log("成功读取....");
        var img = document.getElementById("pic");
        img.src = e.target.result;
        $("#img").val(e.target.result);
    }
    reader.readAsDataURL(file);
}

function chioceImage() {
    $('#file').trigger("click");
}

function save(fd) {
    $.ajax({
        url: "save",
        data: fd,
        type: "POST",
        dataType: "json",
        processData: false,
        contentType: false,
        success: function (result) {
            if (result.success) {
                layer.msg(result.msg, {icon: 6, time: 1000}, function () {
                    closeLayer();
                });
            } else {
                layer.msg(result.msg, {icon: 5});
            }
        }
    });
}

function setObj(obj) {
    for (var key in obj) {
        if ($("#formId [name='" + key + "']").length == 1) {
            $("#formId [name='" + key + "']").val(obj[key]);
        }
    }
}

function getObjById(id) {

    if (id == 0) {
        return false;
    }
    $.ajax({
        url: "getObjById",
        data: {
            id: id
        },
        type: "POST",
        dataType: "json",
        success: function (result) {
            if (result.success) {
                var obj = result.obj;
                setObj(obj);
                setOtherObj(obj);
                renderForm();
            } else {
                layer.msg(result.msg, {icon: 5});
            }
        }
    });
}

function setOtherObj(obj) {

}