/**
 * 合同添加编辑页面
 * 
 * @link      <http://www.u-bo.com>
 * @copyright 南京友博网络科技有限公司 
 * @license   <http://www.u-bo.com/license/> 
 * 
 * @author HuLimin <http://blog.u-bo.com>
 * @since 0.1
 */
jQuery(function($) {
    //正整型数据判断
    var intType = "^[0-9]*[1-9][0-9]*$";
    var floatType = "^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$";
    var intReg = new RegExp(intType);
    var floatReg = new RegExp(floatType);

    sumPercentage();


    /**
     * 实施范围新增行操作
     */
    $(document).on('click', '.add-payment-instruction', function() {
        var curTr = $('#payment_instruction-table').children('tbody').children('tr').eq(-2);
        var num = curTr.attr('class') * 1 + 1;
        var template;
        template = "<tr class='" + num + "'><td><input class='form-control sort'  name='instruction[" + num + "][sort]' value='" + num + "'/></td><td><input class='form-control percentage'  name='instruction[" + num + "][percentage]' value='0'/></td><td>%</td><td><input class='form-control sum'  name='instruction[" + num + "][sum]' readonly='true' value='0'/></td><td><input class='form-control remark'  name='instruction[" + num + "][remark]' /></td> <td><a href='javascript:;' class='delete-tr' >删除</a></td></tr>";
        curTr.after(template);
        sumPercentage();
        sort();
    });

    /**
     *付款比例变化 
     */
    $(document).on('change', '.percentage', function() {
        var sumObj = $(this).closest('tr').find('.sum');  //当前操作行的数量
        var percentage = $(this).val();
        var money = $("#contract-money").val();
        if (percentage.match(floatReg) == null) {//输入非数字，则重置为0
            $(this).val(0);
            sumObj.val(0);
        }
        sumPercentage();
        var totalPercentage = parseFloat($("#total-percentage").text());
        if (totalPercentage > 100) {
            art.dialog({
                title: "比例有误",
                time: 3,
                icon: 'error',
                content: "付款比例总和" + totalPercentage + "%,大于100%"
            });
            $(this).val(0);
        } else {
            sumObj.val(parseFloat(percentage) * money / 100);
        }
        sumPercentage();
    });

    /**
     *合同金额变化 
     */
    $(document).on('change', '#contract-money', function() {
        var money = $(this).val();
        var sumObj;
        if (money.match(floatReg) !== null) {//输入非数字，则重置为0
            var objPercentage = $("td input.percentage");
            objPercentage.each(function() {
                sumObj = $(this).closest('tr').find('.sum');  //当前操作行的数量
                sumObj.val(parseFloat(money) * $(this).val() / 100);
            });
        }
        sumPercentage();
    });
    /**
     *排序变化 
     */
    $(document).on('change', '.sort', function() {
        var sort = $(this).val();
        if (sort.match(intReg) == null) {
            $(this).val(0);
        }
    });

    /**
     * 删除行操作
     */
    $(document).on('click', '.delete-tr', function() {
        var objTr = $(this).closest('tr');
        objTr.remove();
        sumPercentage();
        sort();
    });

    /**
     * 维保时长变化修改维保日期
     */
    $(document).on('change', '#contract-maint_period', function() {
        changeBeginTime();
    });

});

/**
 * 计算总比例和金额
 */
function sumPercentage() {
    var objPercentage = $("td input.percentage");
    var objSum = $("td input.sum");
    var totalSum = 0;
    var totalPercentage = 0;
    var money = $("#contract-money").val();
    var sum = 0;
    objPercentage.each(function() {
        totalPercentage += parseFloat($(this).val());
    });
    objSum.each(function() {
        totalSum += parseFloat($(this).val());
    });
    $("#total-percentage").html(totalPercentage);
    $("#sumMoney").html(totalSum);
    $("#surplus").html(100 - totalPercentage);
}
/**
 * 自动计算维保日期
 */
function changeBeginTime() {
    var c = $dp.cal.newdate;
    var maint_period = $('#contract-maint_period').val();
    var dateTime = c.y + '-' + c.M + '-' + c.d;
    $.post(changeTimeUrl, {'acceptance_time': dateTime, 'maint_period': maint_period}, function(resp) {
        $("#contract-begin_time").val(resp.begin_time);
        $("#contract-end_time").val(resp.end_time);
        $dp.$('due-time').value = resp.end_time;//修改合同结束时间为维保结束时间
    });

}
function sort() {
    var len = $('#payment_instruction-table tr').length;
    for (var i = 1; i < len - 1; i++) {
        $('#payment_instruction-table tr:eq(' + i + ') .sort').val(i);
    }
}

function linkSelectCallback(input, value) {
    $.ajax({
        url: messageUrl,
        type: "post",
        data: "project_no=" + value,
        success: function(res) {
            if (res.status == 1) {
                $("#contract-customer_name").val(res.name);
                $("#customer-no").val(res.customer_no);
            }
        }
    });
}