/**
 * Created by Tony on 2021/9/11.
 */

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

    $("#format").click( function() {
        try {
            let jsonText =  $("#featureList").val();
            $("#featureList").val(JSON.stringify(JSON.parse(jsonText),null,4));
        }catch (err){
            console.log(err);
            alert("输入的JSON格式有误，请重新输入！");
        }
    });
    $("#save").click(async function() {
        try {
            let libID = $("#libID").val();
            let jsonText =  $("#featureList").val();
            if(jsonText.indexOf("'")!=-1){
                alert("输入的格式不能包含 ' 位于："+jsonText.substr(jsonText.indexOf("'")-15,18));
                return;
            }
            let newFeature = await ajaxPost("/billsUnitPriceFeature/saveLib",{query:{ID:libID},data:{feature:JSON.parse(jsonText)}});
        }catch (err){
            console.log(err);
            alert("保存失败，请查看输入数据");
        }
    });
    $("#createNormal").click(async function() {
        let billsUnitpriceFeature = {
            "basicKeyOptions": [
                { "type": "bills", "key": "code", "dispName": "清单代号" },
                { "type": "bills", "key": "name", "dispName": "清单名称" },
                { "type": "bills", "key": "unit", "dispName": "清单单位" }
            ],
            "basicMappings": [
                {
                    parentBasicKeys: ["2010101", "路基保洁", "路基公里"],
                    subs: [
                        {
                            keys: [ { type: "prjFeature", key: "lanes", value: "双向四车道" } ],
                            basicValue: 539.00
                        },
                        {
                            keys: [ { type: "prjFeature", key: "lanes", value: "双向六车道" } ],
                            basicValue: 592.00
                        },
                        {
                            keys: [ { type: "prjFeature", key: "lanes", value: "双向八车道" } ],
                            basicValue: 618.00
                        }
                    ],
                    defaultBasicValue: 0.00
                }
            ],
            "factorMappings": [
                {
                    basicFactorKeys: ["2010101", "路基保洁", "路基公里"],
                    subs: [
                        {
                            keys: [{ type: "prjFeature", key: "trafficFlow", value: "2万以下" }],
                            basicValue: 0.970
                        }
                    ],
                    defaultFactorValue: 1
                }
            ]
        };
        try {
            let jsonText =  JSON.stringify(billsUnitpriceFeature);
            $("#featureList").val(JSON.stringify(JSON.parse(jsonText),null,4));
        }catch (err){
            console.log(err);
        }

    })


});
