/**
 * 一个收入详情的字段
 */
//INCOME_DETAIL_ARR=["type","name","unit","amount","price","taxRate","totalTaxInclude","totalTaxExclude"];
INCOME_DETAIL={
    "num":[false,"序号","string"],
    "type":[true,"类别","string"],
    "name":[true,"名称","string"],
    "unit":[true,"单位","string"],
    "amount":[true,"数量","string"],
    "price":[true,"单价","price"],
    "taxRate":[true,"税率","number"],
    "totalTaxInclude":[true,"含税总金额","number"],
    "totalTaxExclude":[true,"不含税金额","number"]
};

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

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

//保存收入明细表
$(function () {
    $("#incomeTableSave").click(function () {
        var data = collectData("am-modal-prompt-input-income");
        data = prepareAjaxDataVerify(data, INCOME_DETAIL, $("#id").val());
        if(data.details){
            postAjax(base+"/project/budgetEditSaveIncomeDetail", data, updateIncomeData);
        }
    });
    $("#incomeAddBtn").click(function () {
        appendTrIncome();
    });
});


/**
 * 更新页面收入的数据【累加】
 */
function updateIncomeData(data,returnData) {
    var incomeDetails = data.details;
    var deviceTaxInclude = 0;
    var deviceTaxExclude = 0;
    var engineerTaxInclude = 0;
    var engineerTaxExclude = 0;
    var serviceTaxInclude = 0;
    var serviceTaxExclude = 0;
    incomeDetails.forEach(function (t, number, ts) {
        if(t["type"] == "1"){
            //设备类
            deviceTaxInclude += f2(t["totalTaxInclude"]);
            deviceTaxExclude += f2(t["totalTaxExclude"]);
        }else if(t["type"] == "2"){
            //工程类
            engineerTaxInclude += f2(t["totalTaxInclude"]);
            engineerTaxExclude += f2(t["totalTaxExclude"]);
        }else if(t["type"] == "3"){
            //服务类
            serviceTaxInclude += f2(t["totalTaxInclude"]);
            serviceTaxExclude += f2(t["totalTaxExclude"]);
        }
    });
    $("input[name='incomeDeviceTaxInclude']").val(f2Fixed(deviceTaxInclude));
    $("input[name='incomeDeviceTaxExclude']").val(f2Fixed(deviceTaxExclude));
    $("input[name='incomeEngineerTaxInclude']").val(f2Fixed(engineerTaxInclude));
    $("input[name='incomeEngineerTaxExclude']").val(f2Fixed(engineerTaxExclude));
    $("input[name='incomeServiceTaxInclude']").val(f2Fixed(serviceTaxInclude));
    $("input[name='incomeServiceTaxExclude']").val(f2Fixed(serviceTaxExclude));

    $("input[name='incomeTotalTaxInclude']").val(f2Fixed(deviceTaxInclude+engineerTaxInclude+serviceTaxInclude));
    $("input[name='incomeTotalTaxExclude']").val(f2Fixed(deviceTaxExclude+engineerTaxExclude+serviceTaxExclude));


    $(".input-total-title-sale-income-budget-plan").val($("input[name='incomeTotalTaxInclude']").val());
    $(".input-total-title-total-income-budget-plan").val(f2Fixed(f2($("input[name='incomeTotalTaxInclude']").val())
        +f2($(".input-total-title-earnest-money-income-budget-plan").val())));


    updateProjectContributionProfitRate();

    layuiAlert("保存成功");
    $('#my-prompt-income-detail').modal('close');
}




/**
 * 收入明细增加一行
 */
function appendTrIncome() {
    var template = '<tr>\n' +
        '                        <td><input type="text" class="am-modal-prompt-input am-modal-prompt-input-income input-budget-num" readonly></td>\n' +
        '                        <td>\n' +
        '                            <select style="width: 80px;float: left;" class="am-modal-prompt-input am-modal-prompt-input-income">\n' +
        '                                <option value="1">设备类</option>\n' +
        '                                <option value="2">工程类</option>\n' +
        '                                <option value="3">服务类</option>\n' +
        '                            </select>\n' +
        '                        </td>\n' +
        '                        <td><input type="text" class="am-modal-prompt-input am-modal-prompt-input-income"></td>\n' +
        '                        <td><input type="text" class="am-modal-prompt-input am-modal-prompt-input-income"></td>\n' +
        '                        <td><input type="number" min="0" max="99999999" step="1" maxlength="8" class="am-modal-prompt-input am-modal-prompt-input-income input-changeable-amount input-changeable-amount-income"></td>\n' +
        '                        <td><input type="text" min="0.00" max="9999999999.99" step="0.01" maxlength="19" class="price am-modal-prompt-input am-modal-prompt-input-income input-changeable-price input-changeable-price-income"></td>\n' +
        '                        <td><input type="text" min="0.00" max="99.99" step="0.01" maxlength="5" class="number am-modal-prompt-input am-modal-prompt-input-income input-changeable-tax-rate input-changeable-tax-rate-income"></td>\n' +
        '                        <td><input type="text" class="am-modal-prompt-input am-modal-prompt-input-income input-changeable-total-tax-include input-changeable-total-tax-include-income" readonly></td>\n' +
        '                        <td><input type="text" class="am-modal-prompt-input am-modal-prompt-input-income input-changeable-total-tax-exclude input-changeable-total-tax-exclude-income" readonly></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>';
    $("#incomeTotal").before(template);
    //重新绑定删除事件和input修改事件
    bindDeleteBtnIncome();
    bindNum();
    bindChangeableInput();
    bindIncomeTotal();
    //绑定数字输入框保留两位小数
    bindNumberInput();
}

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

/**
 * 绑定每个可改变的输入框，修改后改变对应输入框的值
 */
function bindIncomeTotal() {
    //数量改变
    $(".input-changeable-amount-income").change(function () {
        updateAmount("input-changeable-amount-income", "input-changeable-total-amount-income");
        updateTotal("input-changeable-total-tax-include-income", "input-changeable-total-total-tax-include-income");
        updateTotal("input-changeable-total-tax-exclude-income", "input-changeable-total-total-tax-exclude-income");
    });
    //单价改变
    $(".input-changeable-price-income").change(function () {
        updateTotal("input-changeable-total-tax-include-income", "input-changeable-total-total-tax-include-income");
        updateTotal("input-changeable-total-tax-exclude-income", "input-changeable-total-total-tax-exclude-income");
    });
    //税率改变
    $(".input-changeable-tax-rate-income").change(function () {
        updateTotal("input-changeable-total-tax-exclude-income", "input-changeable-total-total-tax-exclude-income");
    });
}