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

   $("#format").click( function() {
       try {
           let jsonText =  $("#basicInfoList").val();
           $("#basicInfoList").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 =  $("#basicInfoList").val();
            if(jsonText.indexOf("'")!=-1){
                alert("输入的格式不能包含 ' 位于："+jsonText.substr(jsonText.indexOf("'")-15,18));
                return;
            }
            await ajaxPost("/basicInfo/saveLib",{query:{ID:libID},data:{info:JSON.parse(jsonText)}});
            $.bootstrapLoading.end();
        }catch (err){
            $.bootstrapLoading.end();
            console.log(err);
            alert("保存失败，请查看输入数据");
        }
    });
    $("#createNormal").click(async function() {
        let basicInfo = [
            {dispName: '基本信息', key: 'basicInfo', readOnly: true, items: [
                {dispName: '合同号', key: 'contractNum', value: ''},
                {dispName: '建设地点', key: 'constructionPlace', value: ''},
                {dispName: '质量标准', key: 'qualityStandard', value: ''},
                {dispName: '工程类别', key: 'projectCategory', value: ''},
                {dispName: '建设日期', key: 'constructionDate', cellType: 'date', value: ''},
                {dispName: '工程规模', key: 'projectScale', value: ''},
                {dispName: '总建筑面积(m2)', key: 'grossArea', cellType: 'number', value: ''},
                {dispName: '建设单位', key: 'constructionUnit', value: ''},
                {dispName: '设计单位', key: 'designUnit', value: ''},
                {dispName: '施工单位', key: 'buildingUnit', value: ''},
                {dispName: '监理单位', key: 'supervisingUnit', value: ''},
                {dispName: '审核单位', key: 'auditUnit', value: ''},
                {dispName: '建设单位审核人', key: 'constructionUnitAudit', value: ''},
                {dispName: '设计单位负责人', key: 'designUnitPrincipal', value: ''},
                {dispName: '施工单位编制人', key: 'buildingUnitAuthor', value: ''},
                {dispName: '监理单位审核人', key: 'supervisingUnitAuditor', value: ''},
                {dispName: '审核单位审核人', key: 'auditUnitAuditor', value: ''},
                {dispName: '编制时间', key: 'establishDate', cellType: 'date', value: ''}
            ]},
            {dispName: '招标信息', key: 'biddingInfo', readOnly: true, items: [
                {dispName: '招标人', key: 'bidInviter', value: ''},
                {dispName: '法定代表人或其他授权人', key: 'tenderRepresentative', value: ''},
                {dispName: '造价工程师', key: 'tenderCostEngineer', value: ''}
            ]},
            {dispName: '投标信息', key: 'bidInfo', readOnly: true, items: [
                {dispName: '投标人', key: 'bidder', value: ''},
                {dispName: '法定代表人或其授权人', key: 'representative', value: ''},
                {dispName: '造价工程师', key: 'bidCostEngineer', value: ''}
            ]},
            {dispName: '工程造价咨询信息', key: 'engineeringCostConsultationInfo', readOnly: true, items: [
                {dispName: '工程造价咨询人', key: 'consultRepresentative', value: ''},
                {dispName: '法定代表人或其授权人', key: 'representative', value: ''},
                {dispName: '造价工程师', key: 'consultCostEngineer', value: ''}
            ]}
        ];
        try {
            let jsonText =  JSON.stringify(basicInfo);
            $("#basicInfoList").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;
}