/**
 * Created by zhang on 2018/9/3.
 */

$(document).ready(function () {
    const locked = lockUtil.getLocked();
    lockUtil.lockTools($(document.body), locked);
    try {
        let tem = sortJson(JSON.parse($("#originalInfo").val()), compareInfo);
        $("#editValue").val(JSON.stringify(tem,null,4));
    }catch (err){
        console.log(err);
    }

   $("#format").click( function() {
       try {
           let jsonText =  $("#editValue").val();
           $("#editValue").val(JSON.stringify(JSON.parse(jsonText),null,4));
       }catch (err){
           console.log(err);
           alert("输入的JSON格式有误，请重新输入！");
       }
   });
    $("#save").click(async function() {
        try {
            $.bootstrapLoading.start();
            let libID = $("#libID").val();
            let jsonText =  $("#editValue").val();
            if(jsonText.indexOf("'")!=-1){
                alert("输入的格式不能包含 ' 位于："+jsonText.substr(jsonText.indexOf("'")-15,18));
                return;
            }
            await ajaxPost("/economic/saveLib",{query:{ID:libID},data:{index:JSON.parse(jsonText)}});
            $.bootstrapLoading.end();
        }catch (err){
            $.bootstrapLoading.end();
            console.log(err);
            alert("保存失败，请查看输入数据");
        }
    });
    $("#createNormal").click(async function() {
        let normalInfo =  [
            { "name": "土石方工程", "value": ""},
            { "name": "路基处理",  "value": ""},
            { "name": "道路基层",  "value": ""},
            { "name": "道路面层",  "value": ""},
            { "name": "人行道及其他",  "value": ""},
            { "name": "交通管理设施", "value": ""},
            { "name": "其他工程", "value": ""}
        ];
        try {
            let jsonText =  JSON.stringify(normalInfo);
            $("#editValue").val(JSON.stringify(JSON.parse(jsonText),null,4));
        }catch (err){
            console.log(err);
        }

    })


});
function compareInfo(a, b) {
    let mapping = {
        dispName: 1,
        key: 2,
        cellType: 3,
        readOnly: 4,
        required: 5,
        value: 6,
        options: 7,
        items: 8
    };
    let aV = mapping[a] ? mapping[a] : 0,
        bV = mapping[b] ? mapping[b] : 0;
    return aV - bV;
}