/**
 * Created by vian on 2017/3/27.
 */
var mainAjax = {
    //获取编办
    getCompilationList: function () {
        $.ajax({
            type: 'post',
            url: '/stdBillsEditor/getCompilationList',
            dataType: 'json',
            success: function (result) {
                //addoptions
                for(let i = 0; i < result.data.length; i++){
                    let $option =  $("<option >"+ result.data[i].name +"</option>");
                    $option.val( result.data[i]._id);
                    $('#compilationSels').append($option);//
                }
                $('#compilationSels').on("change", function () {
                });
            }
        });
    },
    getMaxNumber: function(billsLibId, field, callback){
        $.ajax({
            type: 'post',
            url: '/stdBillsEditor/getMaxNumber',
            data: {data: JSON.stringify({billsLibId: billsLibId, field: field})},
            dataType: 'json',
            success: function(result){
                if(!result.error){
                    if(callback){
                        callback(result.data);
                    }
                }
            }
        });
    },
    getABillsLib: function(billsLibId, callback){
        $.ajax({
            type: 'post',
            url: '/stdBillsEditor/getABillsLib',
            data: {data: JSON.stringify({billsLibId: billsLibId})},
            dataType: 'json',
            success: function(result){
                if(!result.error){
                    if(callback){
                        callback(result.data);
                    }
                }
            }
        });
    },
    getLibsHtml: function (data) {
        const libHtml = data.reduce((acc, libData) => {
            const id = libData.billsLibId;
            const billsLibName = libData.billsLibName;
            const createDateFmt = new Date(libData.createDate).format("yyyy-MM-dd");
            const html =
                `<tr id="${id}">
                    <td><a href="/stdBills?billsLibId=${id}&locked=true">${billsLibName}</a></td>
                    <td>${createDateFmt}</td>
                    <td>
                        <a class="lock-btn-control disabled" data-toggle="modal" data-target="#edit" href="javascript:void(0);" title="编辑"><i class="fa fa-pencil-square-o"></i></a>
                        <a class="text-danger lock-btn-control disabled" data-toggle="modal" data-target="#del" href="javascript:void(0);" title="删除"><i class="fa fa-remove"></i></a>
                        <a class="lock" data-locked="true" href="javascript:void(0);" title="解锁"><i class="fa fa-unlock-alt"></i></a>
                    </td>
                    <td>
                        <a class="btn btn-secondary btn-sm import-data lock-btn-control disabled" data-id="${id}" href="javascript:void(0);" title="导入数据"><i class="fa fa-sign-in fa-rotate-90"></i>导入</a>
                    </td>
                    <td>
                        <a class="btn btn-secondary btn-sm copy-data lock-btn-control disabled" data-id="${id}" href="javascript:void(0);" title="复制数据"><i class="fa fa-clone"></i>复制</a>
                    </td>
                 </tr>`;
            return acc += html;
        }, '');
        return libHtml;
    },
    getStdBillsLib: function(){
        const me = this;
        $.ajax({
            type: "post",
            url: "/stdBillsEditor/getStdBillsLib",
            dataType: "json",
            success: function(result){
                if(result.data){
                    $('#showArea').append(me.getLibsHtml(result.data));
                }
            }
        });
    },
    createStdBillsLib: function(userAccount, billsLibName){
        const me = this;
        $.ajax({
            type: "POST",
            url: "/stdBillsEditor/createStdBillsLib",
            data: {data: JSON.stringify({userAccount: userAccount, name: billsLibName}) },
            dataType: "json",
            success: function(result){
                if(!result.error){
                    $('#showArea').append(me.getLibsHtml(result.data));
                    $('#add').modal('hide');
                }
            }
        });
    },
    copyBillsLib: function (libId, libName) {
        const me = this;
        $.ajax({
            type: 'post',
            url: '/stdBillsEditor/copyStdBillsLib',
            data: {data: JSON.stringify({libId, libName})},
            dataType: 'json',
            timeout: 150000,
            beforeSend: function () {
                $('#copy').modal('hide');
                $.bootstrapLoading.start();
            },
            success: function (result) {
                if (!result.error) {
                    $('#showArea').append(me.getLibsHtml(result.data));
                }
            },
            complete: function () {
                $.bootstrapLoading.end();
            }
        });
    },
    deleteStdBillsLib: function(lastOperator, billsLibId){
        $.ajax({
            type: "POST",
            url: "/stdBillsEditor/deleteStdBillsLib",
            data: {data: JSON.stringify({lastOperator: lastOperator, billsLibId: billsLibId})},
            dataType: "json",
            success: function(result){
                if(!result.error){
                    var jqSel = "#"+billsLibId;
                    $(jqSel).remove();
                    $('#del').modal('hide');
                }
            }
        });
    },
    renameStdBillsLib: function(lastOperator, billsLibId, newName){
        $.ajax({
            type: "post",
            url: "/stdBillsEditor/renameStdbillsLib",
            data: {data: JSON.stringify({lastOperator: lastOperator, id: billsLibId, value: newName})},
            dataType: "json",
            success: function(result){
                if(!result.error){
                    var jqSel = "#" + billsLibId + " td:first" + " a";
                    $(jqSel).text(newName);
                }
            }
        });
    },
    getCurrentUniqId: function (callback) {
        $.ajax({
            type: 'post',
            url: 'stdBillsEditor/getCurrentUniqId',
            dataType: 'json',
            success: function(result){
                if(!result.error && callback){
                    callback(result.data);
                }
            }
        });
    }
}

var billsAjax = {
    getStdBillsLibName: function(billsLibId) {
        $.ajax({
            type: "post",
            url: "/stdBillsEditor/getStdBillsLibName",
            data: {data: JSON.stringify({billsLibId: billsLibId})},
            success: function(result){
                if(!result.error){
                    $(".navbar-text").append(
                    "<a href='stdBillsmain'>清单规则</a><i class='fa fa-angle-right fa-fw'></i>"+result.data[0].billsLibName
                    );
                }
            }
        });
    },
    getBills: function(billsLibId, callback){
        $.ajax({
            type: "post",
            url: "/stdBillsEditor/getBills",
            data: {data: JSON.stringify({billsLibId: billsLibId})},
            dataType: "json",
            success: function(result){
                if(!result.error){
                    if(callback) {
                        callback(result.data);
                    }
                }
            }
        });
    },
    createBills: function(lastOperator, billsLibId, newId, pid, nid, updatePreData, sectionInfo, callback){
        $.ajax({
            type: 'post',
            url: 'stdBillsEditor/createBills',
            data: {data: JSON.stringify({lastOperator: lastOperator, billsLibId: billsLibId, newId: newId, ParentID: pid, NextSiblingID: nid, updatePreData: updatePreData, sectionInfo: sectionInfo})},
            dataType: 'json',
            success: function(result){
                if(!result.error){
                    if(callback){
                        callback();
                    }
                }
            }
        });
    },
    upMove: function(lastOperator, billsLibId, updateDatas, callback){
        $.ajax({
            type: 'post',
            url: 'stdBillsEditor/upMove',
            data: {data: JSON.stringify({lastOperator: lastOperator, billsLibId: billsLibId, updateDatas: updateDatas})},
            dataType: 'json',
            success: function(result){
                if(!result.error && callback){
                    callback();
                }
            }
        });
    },
    downMove: function (lastOperator, billsLibId, updateDatas, callback) {
        $.ajax({
            type: 'post',
            url: 'stdBillsEditor/downMove',
            data: {data: JSON.stringify({lastOperator: lastOperator, billsLibId: billsLibId, updateDatas: updateDatas})},
            dataType: 'json',
            success: function(result){
                if(!result.error && callback){
                    callback();
                }
            }
        })
    },
    updatePNId: function(billsLibId, updateData, callback){
        $.ajax({
            type: 'post',
            url: 'stdBillsEditor/updatePNId',
            data: {data: JSON.stringify({billsLibId: billsLibId, updateData: updateData})},
            dataType: 'json',
            success: function(result){
                console.log(`entersFuc`);
                if(!result.error && callback){
                    console.log(`enterSc`);
                    callback();
                }
                else {
                    //提示窗口：更新失败
                }
            },
            error: function(){
                console.log(`error`);
            }
        });
    },
    upLevel: function(lastOperator, billsLibId, updateDatas, callback){
        $.ajax({
            type: 'post',
            url: 'stdBillsEditor/upLevel',
            data: {data: JSON.stringify({lastOperator: lastOperator, billsLibId: billsLibId, updateDatas: updateDatas})},
            dataType: 'json',
            success: function (result) {
                if(!result.error && callback){
                    callback();
                }
            }
        })
    },
    downLevel: function (lastOperator, billsLibId, updateDatas, callback) {
        $.ajax({
            type: 'post',
            url: 'stdBillsEditor/downLevel',
            data: {data: JSON.stringify({lastOperator: lastOperator, billsLibId: billsLibId, updateDatas: updateDatas})},
            dataType: 'json',
            success: function (result) {
                if(!result.error && callback){
                    callback();
                }
            }
        })
    },
    deleteBills: function(lastOperator, billsLibId, deleteIds, updateNode, callback){
        $.ajax({
            type: 'post',
            url: 'stdBillsEditor/deleteBills',
            data: {data: JSON.stringify({lastOperator: lastOperator, billsLibId: billsLibId, deleteIds: deleteIds, updateNode: updateNode})},
            dataType: 'json',
            success: function(result){
                if(!result.error){
                    console.log(result.message);
                    if(callback){
                        callback();
                    }
                }
            }

        });
    },
    updateBills: function(lastOperator, billsLibId, updateId, field, data){
        if (field === 'fixedFlag') {
            let findData = BillsFixedFlagList.find((x) => x.name === data);
            if (findData) {
                data = findData.value;
            }
        }
        $.ajax({
            type: 'post',
            url: 'stdBillsEditor/updateBills',
            data: {data: JSON.stringify({lastOperator: lastOperator, billsLibId: billsLibId, updateId: updateId, field: field, data: data})},
            dataType: 'json',
            success: function(result){

            }
        });
    },
    updateSectionInfo: function(data, callback){
        $.ajax({
            type: 'post',
            url: 'stdBillsEditor/updateSectionInfo',
            data: {data: JSON.stringify({updateData: data})},
            dataType: 'json',
            success: function(result){
                if(!result.error && callback){
                    callback();
                }
            }
        });
    },
    removeTotal: function (lastOperator, billsLibId, billsIds, delIds, field) {
        $.ajax({
            type: 'post',
            url: 'stdBillsEditor/removeTotal',
            data: {data: JSON.stringify({lastOperator: lastOperator, billsLibId: billsLibId, billsIds: billsIds, delIds: delIds, field: field})},
            dataType: 'json',
            success: function(result){

            }
        })
    },
    updateSerialNo: function (billsLibId, billsId, updateArr, field, callback) {
        $.ajax({
            type: 'post',
            url: 'stdBillsEditor/updateSerialNo',
            data: {data: JSON.stringify({lastOperator: userAccount, billsLibId: billsLibId, billsId: billsId, updateArr: updateArr, field: field})},
            dataType: 'json',
            success: function (result) {
                if(!result.error && callback){
                    callback();
                }
            }
        });
    },
    updateBillsArr: function(lastOperator, billsLibId, updateId, orgId, newId, type, classify, callback){
        $.ajax({
            type: 'post',
            url: 'stdBillsEditor/updateBillsArr',
            data: {data: JSON.stringify({lastOperator: lastOperator, billsLibId: billsLibId, updateId: updateId, orgId: orgId, newId: newId, type: type, classify: classify})},
            dataType: 'json',
            success: function(result){
                if(!result.error && callback){
                    callback();
                }
            }
        });
    },
    pasteBills: function(lastOperator, billsLibId, datas){
        for (let data of datas) {
            if (data.fixedFlag) {
                let findData = BillsFixedFlagList.find((x) => x.name === data.fixedFlag);
                if (findData) {
                    data.fixedFlag = findData.value;
                }
            }
        }
        $.ajax({
            type: 'post',
            url: 'stdBillsEditor/pasteBills',
            data: {data: JSON.stringify({lastOperator: lastOperator, billsLibId: billsLibId, datas: datas})},
            dataType: 'json',
            success: function(result){

            }
        });
    },
    updateRecharge: function(lastOperator, billsLibId, updateId, data){
        $.ajax({
            type: 'post',
            url: 'stdBillsEditor/updateRecharge',
            data: {data: JSON.stringify({lastOperator: lastOperator, billsLibId: billsLibId, updateId: updateId, data: data})},
            dataType: 'json',
            success: function(result){

            }
        });
    },
    pasteRel: function (lastOperator, billsLibId, updateDatas, createDatas, field, callback) {
        $.ajax({
            type: 'post',
            url: 'stdBillsEditor/pasteRel',
            data: {data:JSON.stringify({lastOperator: lastOperator, billsLibId: billsLibId, updateDatas: updateDatas, createDatas: createDatas, field: field})},
            dataType: 'json',
            success: function(result){
                if(!result.error && callback){
                    callback(result.data);
                }
            }
        });
    }
};

var jobsAjax = {
    getJobContent: function(billsLidId, callback){
        $.ajax({
            type: 'post',
            url: 'stdBillsEditor/getJobContent',
            data: {data: JSON.stringify({billsLibId: billsLidId})},
            dataType: 'json',
            success: function(result){
                if(!result.error && callback){
                    callback(result.data);
                }
            }
        });
    },
    getSomeJobs: function(billsLibId, ids, callback){
        $.ajax({
            type: 'post',
            url: 'stdBillsEditor/getSomeJobs',
            data: {data: JSON.stringify({billsLibId: billsLibId, ids: ids})},
            dataType: 'json',
            success: function(result){
                if(!result.error && callback){
                    callback(result.data);
                }
            }
        });
    },
    createJobContent: function(lastOperator, billsLibId, data, serialNo, callback){
        $.ajax({
            type: 'post',
            url: 'stdBillsEditor/createJobContent',
            data: {data: JSON.stringify({lastOperator: lastOperator, billsLibId: billsLibId, data: data, serialNo: serialNo})},
            dataType: 'json',
            success: function(result){
                if(!result.error && callback){
                    callback(result.data);
                }
            }
        });
    },
    updateJobContent: function(lastOperator, billsLibId, id, field, data){
        $.ajax({
            type: 'post',
            url: 'stdBillsEditor/updateJobContent',
            data: {data: JSON.stringify({lastOperator: lastOperator, billsLibId: billsLibId, updateId: id, field: field, data: data })},
            dataType: 'json',
            success: function(result){

            }
        });
    },
    deleteJobContent: function(lastOperator, billsLibId, ids){
        $.ajax({
            type: 'post',
            url: 'stdBillsEditor/deleteJobContent',
            data: {data: JSON.stringify({lastOperator: lastOperator, billsLibId: billsLibId, ids: ids})},
            dataType: 'json',
            success: function(result){

            }
        });
    },
    pasteJobs: function(lastOperator, billsLibId, pasteDatas, callback){
        $.ajax({
            type: 'post',
            url: 'stdBillsEditor/pasteJobs',
            data: {data: JSON.stringify({lastOperator: lastOperator, billsLibId: billsLibId, pasteDatas: pasteDatas})},
            dataType: 'json',
            success: function(result){
                if(!result.error && callback){
                    callback(result.data);
                }
            }

        });
    },
    edCreateJob: function(lastOperator, billsLibId, billsId, data, code, serialNo, callback){
        $.ajax({
            type: 'post',
            url: 'stdBillsEditor/edCreateJob',
            data: {data: JSON.stringify({lastOperator: lastOperator, billsLibId: billsLibId, billsId:billsId, data: data, code: code, serialNo: serialNo})},
            dataType: 'json',
            success: function(result){
                if(!result.error && callback){
                    callback(result.data);
                }
            }
        });
    },
    edUpdateJob: function(lastOperator, billsLibId, billsId, content, code, orgJobId, callback){
        $.ajax({
            type: 'post',
            url: 'stdBillsEditor/edUpdateJob',
            data: {data: JSON.stringify({lastOperator: lastOperator, billsLibId: billsLibId, billsId:billsId, content: content, code: code, orgJobId: orgJobId})},
            dataType: 'json',
            success: function(result){
                if(!result.error && callback){
                    callback(result.data);
                }
            }
        });
    }
};

var itemsAjax = {
    getItemCharacter: function(billsLibId, callback){
        $.ajax({
            type: 'post',
            url: 'stdBillsEditor/getItemCharacter',
            data: {data: JSON.stringify({billsLibId: billsLibId})},
            dataType: 'json',
            success: function(result){
                if(!result.error){
                    if(callback){
                        callback(result.data);
                    }
                }
            }
        });
    },
    createItemCharacter: function(lastOperator, billsLibId,  data, code, callback){
        $.ajax({
            type: 'post',
            url: 'stdBillsEditor/createItemCharacter',
            data: {data: JSON.stringify({lastOperator: lastOperator, billsLibId: billsLibId, data: data, code: code})},
            dataType: 'json',
            success: function(result){
                if(!result.error && callback){
                    callback(result.data);
                }
            }
        });
    },
    updateItemCharacter: function(lastOperator, billsLibId, id, field, data){
        $.ajax({
            type: 'post',
            url: 'stdBillsEditor/updateItemCharacter',
            data: {data: JSON.stringify({lastOperator: lastOperator, billsLibId: billsLibId, updateId: id, field: field, data: data })},
            dataType: 'json',
            success: function(result){

            }
        });
    },
    updateValue: function(lastOperator, billsLibId, id, data, deleteCodes, type){
        $.ajax({
            type: 'post',
            url: 'stdBillsEditor/updateValue',
            data: {data: JSON.stringify({lastOperator: lastOperator, billsLibId: billsLibId, updateId: id, data: data, type: type, deleteCodes: deleteCodes})},
            dataType: 'json',
            success: function(reslut){

            }
        });
    },
    deleteItemCharacter: function(lastOperator, billsLibId, ids){
        $.ajax({
            type: 'post',
            url: 'stdBillsEditor/deleteItemCharacter',
            data: {data: JSON.stringify({lastOperator: lastOperator, billsLibId: billsLibId, ids: ids})},
            dataType: 'json',
            success: function(result){

            }
        });
    },
    pasteItems: function(lastOperator, billsLibId, pasteDatas, callback){
        $.ajax({
            type: 'post',
            url: 'stdBillsEditor/pasteItems',
            data: {data: JSON.stringify({lastOperator: lastOperator, billsLibId: billsLibId, pasteDatas: pasteDatas})},
            dataType: 'json',
            success: function(result){
                console.log(result);
                if(!result.error){
                    console.log(result.data);
                    callback(result.data);
                }
            }

        });
    },
    pasteValues: function(lastOperator, billsLibId, pasteDatas, callback){
        $.ajax({
            type: 'post',
            url: 'stdBillsEditor/pasteValues',
            data: {data: JSON.stringify({lastOperator: lastOperator, billsLibId: billsLibId, pasteDatas: pasteDatas})},
            dataType: 'json',
            success: function(result){
                if(!result.error && callback){
                    callback(result.data);
                }
            }
        });
    },
    edCreateItem: function(lastOperator, billsLibId, billsId, data, code, serialNo, callback){
        $.ajax({
            type: 'post',
            url: 'stdBillsEditor/edCreateItem',
            data: {data: JSON.stringify({lastOperator: lastOperator, billsLibId: billsLibId, billsId:billsId, data: data, code: code, serialNo: serialNo})},
            dataType: 'json',
            success: function(result){
                if(!result.error && callback){
                    callback(result.data);
                }
            }
        });
    },
    edUpdateItem: function(lastOperator, billsLibId, billsId, content, code, orgItemId, callback){
        $.ajax({
            type: 'post',
            url: 'stdBillsEditor/edUpdateItem',
            data: {data: JSON.stringify({lastOperator: lastOperator, billsLibId: billsLibId, billsId:billsId, content: content, code: code, orgItemId: orgItemId})},
            dataType: 'json',
            success: function(result){
                if(!result.error && callback){
                    callback(result.data);
                }
            }
        });
    }
}


