/**
 * 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("/engineerInfo/saveLib",{query:{ID:libID},data:{info:JSON.parse(jsonText)}});
            $.bootstrapLoading.end();
        }catch (err){
            $.bootstrapLoading.end();
            console.log(err);
            alert("保存失败，请查看输入数据");
        }
    });
    $("#createNormal").click(async function() {
        let normalInfo =  [
            {dispName: '造价编制单位', key: 'establishmentUnit', value: ''},//只读，用户新建单位工程时选择的值
            {dispName: '造价审核单位', key: 'auditUnit', value: ''},
            {dispName: '项目负责人', key: 'projectLeader', value: ''},
            {dispName: '施工单位编制人员', key: 'buildingUnitAuthor', value: ''},
            {dispName: '编制人员', key: 'author', value: ''},
            {dispName: '审核人员', key: 'auditor', value: ''},
            {dispName: '开工日期', key: 'commencementDate', value: '',cellType:'date'},
            {dispName: '竣工日期', key: 'completionDate', value: '',cellType:'date'},
            {dispName: '工程地点', key: 'projectLocation', value: '',cellType:'comboBox',options : "渝中区@江北区@沙坪坝区@南岸区@九龙坡区@大渡口区@北碚区@渝北区@巴南区@万州区@涪陵区@万盛经开区@双桥经开区@黔江区@长寿区@江津区@合川区@永川区@南川区@綦江区@潼南县@铜梁区@大足区@荣昌县@璧山区@梁平县@城口县@丰都县@垫江县@武隆县@忠县@开县@云阳县@奉节县@巫山县@巫溪县@石柱土家族自治县@秀山土家族苗族自治县@酉阳土家族苗族自治县@彭水苗族土家族自治县"},
            {dispName: '工程类型', key: 'projectType', value: '',cellType:'number'},
            {dispName: '合同价类型', key: 'contractPriceType', value: '',cellType:'comboBox',"options" : "固定价@可调价@成本加酬金"},
            {dispName: '造价类型', key: 'costType', value: ''},
            {dispName: '计价方式及依据', key: 'valuationBase', value: ''},
            {dispName: '工程类别', key: 'engineerType', value: ''},
            {dispName: '编制日期', key: 'authorDate', value: '',cellType:'date'},
            {dispName: '审查日期', key: 'auditorDate', value: '',cellType:'date'}
        ];
        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;
}