/**
 * 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("/engineerFeature/saveLib",{query:{ID:libID},data:{features:JSON.parse(jsonText)}});
            $.bootstrapLoading.end();
        }catch (err){
            $.bootstrapLoading.end();
            console.log(err);
            alert("保存失败，请查看输入数据");
        }
    });
    $("#createNormal").click(async function() {
        let normalInfo =  [
            { "ID": 1, "ParentID": -1, "name": "建筑特征", "value": ""},
            { "ID": 101, "ParentID": 1, "name": "建筑面积", "value": ""},
            { "ID": 10101, "ParentID": 101, "name": "总建筑面积*", "value": "","required":true,cellType:'number',index:true,indexUnit:"100M2",coe:100},
            { "ID": 10102, "ParentID": 101, "name": "标准层面积", "value": "","cellType":'number'},//cellType:'date'
            { "ID": 102, "ParentID": 1, "name": "层数", "value": ""},
            { "ID": 10201, "ParentID": 102, "name": "总层数*", "value": "","required":true},
            { "ID": 10202, "ParentID": 102, "name": "标准层层数", "value": ""},
            { "ID": 2, "ParentID": -1, "name": "结构特征", "value": ""},
            { "ID": 201, "ParentID": 2, "name": "结构类型", "value": ""},
            { "ID": 202, "ParentID": 2, "name": "基础类型", "value": ""},
            { "ID": 203, "ParentID": 2, "name": "混凝土强度等级", "value": ""},
            { "ID": 20301, "ParentID": 203, "name": "基础", "value": ""},
            { "ID": 20302, "ParentID": 203, "name": "柱", "value": ""},
            { "ID": 3, "ParentID": -1, "name": "装饰特征", "value": ""},
            { "ID": 301, "ParentID": 3, "name": "楼地面", "value": ""},
            { "ID": 30101, "ParentID": 301, "name": "整体面层", "value": ""},
            { "ID": 3010101, "ParentID": 30101, "name": "整体面层1", "value": ""},
            { "ID": 3010102, "ParentID": 30101, "name": "整体面层2", "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;
}