/**
 * 一个收入详情的字段
 */
//INCOME_DETAIL_ARR=["type","name","unit","amount","price","taxRate","totalTaxInclude","totalTaxExclude"];
INCOME_PLAN_DETAIL={
    "num":[false,"序号","string"],
    "receivePoint":[false,"收款节点","string"],
    "receiveTime":[false,"收款时间","string"],
    "receiveAmount":[false,"收款金额","number"]
};

$(function () {
    $("#income-plan-detail").click(function () {
        $('#my-prompt-income-plan-detail').modal({
            relatedTarget: this,
            closeOnConfirm:false,
            onConfirm: function(e) {
                //不能使用e.data，因为无法获取动态添加的
                var data = collectData("am-modal-prompt-input-income-plan");
                //data = prepareAjaxData(data, INCOME_DETAIL_ARR, $("#id").val(),false);
                data = prepareAjaxDataVerify(data, INCOME_PLAN_DETAIL, $("#id").val());
                if(data.details){
                    postAjax(base+"/project/budgetEditSaveIncomePlanDetail", data, updateIncomePlanData);
                }
            },
            onCancel: function(e) {
            }
        });
    });
});

//保存收款计划表
$(function () {
    $("#incomePlanTableSave").click(function () {
        var data = collectData("am-modal-prompt-input-income-plan");
        data = prepareAjaxDataVerify(data, INCOME_PLAN_DETAIL, $("#id").val());
        if(data.details){
            postAjax(base+"/project/budgetEditSaveIncomePlanDetail", data, updateIncomePlanData);
        }
    });
    $("#incomePlanAddBtn").click(function () {
        appendTrIncomePlan();
        $("#incomePlanAddBtn").blur();
    });
    $(".am-modal-line-delete").click(function () {
        //删除自己对应的tr
        $(this).parent().parent().remove();
        bindNum();
        updateTotal("input-changeable-receive-income-plan", "input-changeable-total-receive-income-plan");
    });
});


/**
 * 更新页面收入的数据【累加】
 */
function updateIncomePlanData(data,returnData) {
    layuiAlert("保存成功");
    $('#my-prompt-income-plan-detail').modal('close');
}




/**
 * 收入明细增加一行
 */
function appendTrIncomePlan() {
    var template = '<tr>\n' +
        '                                    <td><input type="text" class="am-modal-prompt-input am-modal-prompt-input-income-plan input-budget-num" readonly></td>\n' +
        '                                    <td><input type="text" class="am-modal-prompt-input am-modal-prompt-input-income-plan"></td>\n' +
        '                                    <td><input type="text" class="am-modal-prompt-input am-modal-prompt-input-income-plan incomePlanTime" autocomplete="off" data-am-datepicker></td>\n' +
        '                                    <td><input type="text" class="number am-modal-prompt-input am-modal-prompt-input-income-plan input-changeable-receive input-changeable-receive-income-plan"></td>\n' +
        '                                    <td><button type="button" style="margin-top: 10px" class="am-btn am-btn-danger am-btn-xs am-round am-modal-line-delete"><span class="am-icon-minus"></span></button></td>\n' +
        '                    </tr>';
    $("#incomePlanTotal").before(template);
    //重新绑定删除事件和input修改事件
    bindDeleteBtnIncomePlan();
    bindTime();
    bindNum();
    bindIncomePlanTotal();
    //绑定数字输入框保留两位小数
    bindNumberInput();

    if ($('#incomePlanTable').find('tbody') && $('#incomePlanTable').find('tbody')[0]) {
        $('#incomePlanTable').find('tbody')[0].scrollTop = $('#incomePlanTable').find('tbody')[0].scrollHeight
    }
}

/**
 * 绑定每一行的删除事件，删除当前的一行tr，修改总计值
 */
function bindDeleteBtnIncomePlan() {
    $(".am-modal-line-delete").click(function () {
        //删除自己对应的tr
        $(this).parent().parent().remove();
        bindNum();
        updateTotal("input-changeable-receive-income-plan", "input-changeable-total-receive-income-plan");
    });
}

/**
 * 绑定每个可改变的输入框，修改后改变对应输入框的值
 */
function bindIncomePlanTotal() {
    //收款金额变化
    $(".input-changeable-receive-income-plan").change(function () {
        updateTotal("input-changeable-receive-income-plan", "input-changeable-total-receive-income-plan");
    });
}

/**
 * 绑定时间选择器
 */
function bindTime() {
    $('[data-am-datepicker]').datepicker();
}