/**
 * 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("/mainQuantity/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": "挖土方量", "unit": "m2", "coe": 2.3},
            { "name": "填方量", "unit": "m2", "coe": 1.2},
            { "name": "连槽石基层", "unit": "t", "coe": 0.2},
            { "name": "砂砾石基层", "unit": "t", "coe": 0.6},
            { "name": "块片石", "unit":"m3", "coe": 0.8},
            { "name": "多渣","unit": "m3", "coe": ""},
            { "name": "砂", "unit":"t", "coe": ""}
        ];
        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;
}