/**
 * Created by Zhong on 2019/12/11.
 */

$(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("/overHeight/saveLib",{query:{ID:libID},data:{list:JSON.parse(jsonText)}});
            $.bootstrapLoading.end();
        }catch (err){
            $.bootstrapLoading.end();
            console.log(err);
            alert("保存失败，请查看输入数据");
        }
    });
    /*[{
        "name": "多层建筑物",
        "labourMachineName": "定额人工机械名称",
        "items": [{
            "code": "JZCG-1",
            "name": "20-40",
            "labourMachineRate": "0.0701"
        },{
            "code": "JZCG-2",
            "name": "40-60",
            "labourMachineRate": "0.1148"
        },{
            "code": "JZCG-3",
            "name": "60-80",
            "labourMachineRate": "0.1594"
        },{
            "code": "JZCG-4",
            "name": "80-100",
            "labourMachineRate": "0.2104"
        },{
            "code": "JZCG-5",
            "name": "100-120",
            "benchmark": "120",
            "labourMachineRate": "0.2614"
        },{
            "code": "JZCG-6",
            "name": "每增20m",
            "extra": "20",
            "labourMachineRate": "0.045"
        },{
            "code": "JZCG-5 + JZCG-6",
            "name": "120-140",
            "labourMachineRate": "0.3064"
        },{
            "code": "JZCG-5 + JZCG-6 * 2",
            "name": "140-160",
            "limit": "160",
            "labourMachineRate": "0.3514"
        }]
    }, {
        "name": "单层建筑物",
        "labourMachineName": "定额人工机械名称",
        "items": [{
            "code": "DCCG-1",
            "name": "30以内",
            "labourMachineRate": "0.025"
        },{
            "code": "DCCG-2",
            "name": "40以内",
            "labourMachineRate": "0.0374"
        },{
            "code": "DCCG-3",
            "name": "50以内",
            "labourMachineRate": "0.0544"
        }]
    }]*/
    $("#createNormal").click(async function() {
        let normalInfo = [  {
            "name": "不计算"
        },
        {
            "name": "高度增加20m"
        },{
            "code": "JZCG-1",
            "name": "高度增加30m",
            "unit": "元",
            "labourRate": "0.0124",
            "machineRate": "0.0313"
        },{
            "code": "JZCG-2",
            "name": "高度增加40m",
            "unit": "元",
            "labourRate": "0.023",
            "machineRate": "0.0513"
        },{
            "code": "JZCG-3",
            "name": "高度增加50m",
            "unit": "元",
            "labourRate": "0.0336",
            "machineRate": "0.0665"
        },{
            "code": "JZCG-4",
            "name": "高度增加60m",
            "unit": "元",
            "labourRate": "0.023",
            "machineRate": "0.0513"
        },{
            "code": "JZCG-5",
            "name": "高度增加70m",
            "unit": "元",
            "labourRate": "0.0664",
            "machineRate": "0.1104"
        },{
            "code": "JZCG-6",
            "name": "高度增加80m",
            "unit": "元",
            "labourRate": "0.0837",
            "machineRate": "0.1324"
        },{
            "code": "JZCG-7",
            "name": "高度增加90m",
            "unit": "元",
            "labourRate": "0.1012",
            "machineRate": "0.1528"
        },{
            "code": "JZCG-8",
            "name": "高度增加100m",
            "unit": "元",
            "benchmark": "100",
            "labourRate": "0.1309",
            "machineRate": "0.1881"
        },{
            "code": "JZCG-9",
            "name": "每增10m以内",
            "unit": "元",
            "extra": "10",
            "labourRate": "0.0206",
            "machineRate": "0.0249"
        },{
            "code": "JZCG-8 + JZCG-9",
            "name": "高度增加110m",
            "unit": "元",
            "labourRate": "0.1515",
            "machineRate": "0.213"
        },{
            "code": "JZCG-8 + JZCG-9 * 2",
            "name": "高度增加110m",
            "unit": "元",
            "limit": "110",
            "labourRate": "0.1721",
            "machineRate": "0.2379"
        }];
        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 = {
        name: 1,
        labourName: 2,
        machineName: 3,
        labourMachineName: 4,
        items: 5,
        code: 6,
        labourRate: 7,
        machineRate: 8,
        labourMachineRate: 9
    };
    let aV = mapping[a] ? mapping[a] : 0,
        bV = mapping[b] ? mapping[b] : 0;
    return aV - bV;
}
