/**
 * Created by vian on 2017/3/27.
 */
var mainAjax = {
    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);
                    }
                }
            }
        });
    },
    getStdBillsLib: function(userId){
        $.ajax({
            type: "post",
            url: "/stdBillsEditor/getStdBillsLib",
            data: {data: JSON.stringify({userId: userId})},
            dataType: "json",
            success: function(result){
                if(result.data){
                    for(var i=0; i<result.data.length; i++){
                        var id = result.data[i].billsLibId;
                        var billsLibName = result.data[i].billsLibName;
                        var createDate = result.data[i].createDate;
                        var createDateFmt = new Date(createDate).format("yyyy-MM-dd");
                        $("#showArea").append(
                            "<tr id='tempId'>" +
                            "<td><a href='stdBills'>"+billsLibName+"</a></td>" +
                            "<td>"+createDateFmt+" </td>" +
                            "<td><a href='javascript:void(0);' data-toggle='modal' data-target='#edit' title='编辑'>" +
                            "<i class='fa fa-pencil-square-o'></i></a> <a href='javascript:void(0);' data-toggle='modal' data-target='#del' class='text-danger' title='删除'>" +
                            "<i class='fa fa-remove'></i></a></td></tr>");
                        var newHref = "stdBills?billsLibId="+id;
                        $("#tempId td:first a").attr("href", newHref);
                        $("#tempId").attr("id", id);
                    }
                }

            }
        });
    },
    createStdBillsLib: function(userId, billsLibName){
        $.ajax({
            type: "POST",
            url: "/stdBillsEditor/createStdBillsLib",
            data: {data: JSON.stringify({userId: userId, name: billsLibName}) },
            dataType: "json",
            success: function(result){
                if(!result.error){
                    var id = result.data[0].billsLibId;
                    var createDate = result.data[0].createDate;
                    var createDateFmt = new Date(createDate).format("yyyy-MM-dd");
                    $("#showArea").append(
                        "<tr id='tempId'><td><a href='stdBills'>"+billsLibName+"</a></td><td>"+createDateFmt+" </td><td><a href='javascript:void(0);' data-toggle='modal' data-target='#edit' title='编辑'>" +
                        "<i class='fa fa-pencil-square-o'></i></a> <a href='javascript:void(0);' data-toggle='modal' data-target='#del' class='text-danger' title='删除'>" +
                        "<i class='fa fa-remove'></i></a></td></tr>"
                    );
                    var newHref = "stdBills?billsLibId="+id;
                    $("#tempId td:first a").attr("href", newHref);
                    $("#tempId").attr("id", id);
                }
            }
        });
    },
    deleteStdBillsLib: function(billsLibId){
        $.ajax({
            type: "POST",
            url: "/stdBillsEditor/deleteStdBillsLib",
            data: {data: JSON.stringify({billsLibId: billsLibId})},
            dataType: "json",
            success: function(result){
                if(!result.error){
                    var jqSel = "#"+billsLibId;
                    $(jqSel).remove();
                }
            }
        });
    },
    renameStdBillsLib: function(billsLibId, newName){
        $.ajax({
            type: "post",
            url: "/stdBillsEditor/renameStdbillsLib",
            data: {data: JSON.stringify({id: billsLibId, value: newName})},
            dataType: "json",
            success: function(result){
                if(!result.error){
                    var jqSel = "#" + billsLibId + " td:first" + " a";
                    $(jqSel).text(newName);
                }
            }
        });
    }
}

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(billsLibId, newId, pid, nid, callback){
        $.ajax({
            type: 'post',
            url: 'stdBillsEditor/createBills',
            data: {data: JSON.stringify({billsLibId: billsLibId, newId: newId, ParentID: pid, NextSiblingID: nid})},
            dataType: 'json',
            success: function(result){
                if(!result.error){
                    if(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){
                if(!result.error){
                    if(callback){
                        callback();
                    }
                }
            }
        });
    },
    deleteBills: function(billsLibId, deleteIds, callback){
        $.ajax({
            type: 'post',
            url: 'stdBillsEditor/deleteBills',
            data: {data: JSON.stringify({billsLibId: billsLibId, deleteIds: deleteIds})},
            dataType: 'json',
            success: function(result){
                if(!result.error){
                    if(callback){
                        callback();
                    }
                }
            }

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

            }
        });
    },
    updateBillsArr: function(billsLibId, updateId, orgId, newId, type, classify){
        $.ajax({
            type: 'post',
            url: 'stdBillsEditor/updateBillsArr',
            data: {data: JSON.stringify({billsLibId: billsLibId, updateId: updateId, orgId: orgId, newId: newId, type: type, classify: classify})},
            dataType: 'json',
            success: function(result){
            }
        });
    },
    pasteBills: function(datas){
        $.ajax({
            type: 'post',
            url: 'stdBillsEditor/pasteBills',
            data: {data: JSON.stringify({datas: datas})},
            dataType: 'json',
            success: function(result){

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

            }
        });
    }
}

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(billsLibId, field, data, serialNo, callback){
        $.ajax({
            type: 'post',
            url: 'stdBillsEditor/createJobContent',
            data: {data: JSON.stringify({billsLibId: billsLibId, field: field, data: data, serialNo: serialNo })},
            dataType: 'json',
            success: function(result){
                if(!result.error){
                    if(callback){
                        callback(result.data);
                    }
                }
            }
        });
    },
    updateJobContent: function(id, field, data){
        $.ajax({
            type: 'post',
            url: 'stdBillsEditor/updateJobContent',
            data: {data: JSON.stringify({updateId: id, field: field, data: data })},
            dataType: 'json',
            success: function(result){

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

            }
        });
    }
}

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(billsLibId, field, data, serialNo, callback){
        $.ajax({
            type: 'post',
            url: 'stdBillsEditor/createItemCharacter',
            data: {data: JSON.stringify({billsLibId: billsLibId, field: field, data: data, serialNo: serialNo})},
            dataType: 'json',
            success: function(result){
                if(!result.error){
                    if(callback){
                        callback(result.data);
                    }
                }
            }
        });
    },
    updateItemCharacter: function(id, field, data){
        $.ajax({
            type: 'post',
            url: 'stdBillsEditor/updateItemCharacter',
            data: {data: JSON.stringify({updateId: id, field: field, data: data })},
            dataType: 'json',
            success: function(result){

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

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

            }
        });
    }
}


