/**
 * 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($("#originalVVTax").val()), compareVVTax);
        $("#vvTaxList").val(JSON.stringify(tem,null,4));
    }catch (err){
        console.log(err);
    }

   $("#format").click( function() {
       try {
           let jsonText =  $("#vvTaxList").val();
           $("#vvTaxList").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 =  $("#vvTaxList").val();
            if(jsonText.indexOf("'")!=-1){
                alert("输入的格式不能包含 ' 位于："+jsonText.substr(jsonText.indexOf("'")-15,18));
                return;
            }
            await ajaxPost("/vvTax/saveLib",{edit:true,query:{ID:libID},data:{data:JSON.parse(jsonText)}});
            $.bootstrapLoading.end();
        }catch (err){
            $.bootstrapLoading.end();
            console.log(err);
            alert("保存失败，请查看输入数据");
        }
    });
    $("#createNormal").click(async function() {
        let vvTax = [
            {
                code: '0101001',
                name: '名称x',
                spec: '规格x',
                unit: '单位x',
                vehicleVesselTax: 0.1,
                useTax: 0.1,
                useTaxMeasure: 10,
                annualMonth: 3,
                annualMachine: 3
            }
        ];
        try {
            let jsonText =  JSON.stringify(vvTax);
            $("#vvTaxList").val(JSON.stringify(JSON.parse(jsonText),null,4));
        }catch (err){
            console.log(err);
        }

    })
});

function compareVVTax(a, b) {
    let mapping = {
        code: 1,
        name: 2,
        spec: 3,
        unit: 4,
        vehicleVesselTax: 5,
        useTax: 6,
        useTaxMeasure: 7,
        annualMonth: 8,
        annualMachine: 9
    };
    let aV = mapping[a] ? mapping[a] : 0,
        bV = mapping[b] ? mapping[b] : 0;
    return aV - bV;
}