/**
 * 项目
 * 
 * @link      http://www.u-bo.com
 * @copyright 南京友博网络科技有限公司 
 * @license   http://www.u-bo.com/license/ 
 * 
 * @author Strief http://blog.u-bo.com
 * @since 0.1
 */
jQuery(function($) {
    //正整型数据判断
    var type = "^[0-9]*[1-9][0-9]*$";
    var reg = new RegExp(type);

    var oldScopeVersion = 0; //更改前的实施范围型号
    var oldScopePerformance = 0; //更改前的实施范围绩效

    var oldResourcePrice = 0; //更改前的资源信息单价

    /*********************实施范围***********************/

    /**
     * 实施范围新增行操作
     */
    $(document).on('click', '.add-scope-implement', function() {
        var curTr = $('.scope-table').children('tbody').children('tr').eq(-2);
        var id = curTr.attr('id') * 1 + 1;
        $.post(projectParams.getScopeCategory, {'id': id}, function(resp) {
            if (resp.status == 1) {
                curTr.after(resp.data);
            }
        });
    });

    /**
     * 实施范围类别列表选择操作
     */
    $(document).on('change', '#scope-category-id', function() {
        var curTd = $(this).closest('tr').find('#scope-implement-name');
        var curSelect = $(this).val(); //当前操作行所选择的实施范围类别
        var scopeNum = $(this).closest('tr').find('#scope-num');  //当前操作行的数量
        var scopeVersion = $(this).closest('tr').find('#scope-version');  //当前操作行的型号
        var scopePerformance = $(this).closest('tr').find('#scope-performance');  //当前操作行的绩效
        //清空实施范围下拉框中的内容
        curTd.empty();
        $.post(projectParams.getScopeImplement, {'id': curSelect}, function(resp) {
            if (resp.status == 1) {
                //解锁实施范围下拉框
                curTd.removeAttr('disabled');
                //向实施范围下拉框中添加新数据
                curTd.html(resp.data);
                changeData(0, 0, 'delete', scopeVersion.val(), scopePerformance.val());
            } else {
                //若当前选择的实施范围类别下没有内容则除了类别以外的数据清空，并锁定不允许编辑
                curTd.attr('disabled', 'disabled');
                if (scopeVersion.val() != '' || scopePerformance.val() != '') {
                    //若清空时数量或绩效有数据，则需要更新总计天数和总绩效
                    changeData(0, 0, 'old', scopeVersion.val(), scopePerformance.val());
                }

            }
            scopeNum.val(null);
            scopeVersion.html('');
            scopePerformance.val('');
            scopeNum.attr('disabled', 'disabled');
            scopeVersion.attr('disabled', 'disabled');
            scopePerformance.attr('disabled', 'disabled');
        });
    });

    /**
     * 已存在的实施范围数量变更操作
     */
    $(document).on('change', '#scope-num-old', function() {
        $(this).closest('tr').find('.redstar-scope-performance').remove();
        var scopeNum = $(this);
        var scopeCountOld = $(this).closest('tr').find('#scope-count-old').val(); //更改前的数量
        var scopeVersionOld = $(this).closest('tr').find('#scope-version-old').val(); //更改前的型号
        var scopePerformanceOld = $(this).closest('tr').find('#scope-performance-old').val(); //更改前的单位工作量任务绩效
        var scopeCountPerformanceOld = $(this).closest('tr').find('#scope-count-performance-old').val(); //更改前的总绩效

        var scopeVersion = $(this).closest('tr').find('#scope-version'); //当前行的型号所在元素
        var scopePerformance = $(this).closest('tr').find('#scope-performance'); //当前行所在的绩效所在元素
        //若输入的数量不是数字，则将此行信息还原为最初的数据
        if (scopeNum.val().match(reg) == null) {
            changeData(scopeVersionOld, scopeCountPerformanceOld, 'old', scopeVersion.val(), scopePerformance.val());
            scopeNum.val(scopeCountOld);
            scopeVersion.val(scopeVersionOld);
            scopePerformance.val(scopeCountPerformanceOld);
        } else {
            var version = scopeVersionOld / scopeCountOld * scopeNum.val();
            var performance = scopePerformanceOld * scopeNum.val();
            changeData(version, performance, 'old', scopeVersion.val(), scopePerformance.val());
            //scopeVersion.val(version);
            scopePerformance.val(performance + '.00');
        }
        //判断总 是否超出，若超出则出现提示
        if (scopePerformance.val() > 99999999.99) {
            scopePerformance.after("<span class='redstar redstar-scope-performance' style='font-size:10px;'>总 超出</span>");
        }
    });

    /**
     * 实施范围名称与数量变更操作
     */
    $(document).on('change', '#scope-implement-name, #scope-num', function() {
        var scopeNum = $(this).closest('tr').find('#scope-num');  //当前操作行的数量
        var scopeVersion = $(this).closest('tr').find('#scope-version');  //当前操作行的型号
        var scopePerformance = $(this).closest('tr').find('#scope-performance');  //当前操作行的绩效
        var curSelect = $(this).closest('tr').find('#scope-implement-name').val();  //当前操作行所选择的实施范围
        var IdName = this.id;
        var curScopeAllVersion = $('#all-scope-implement').find('#scope-all-version');  //获取总计天数所在处
        var curScopeAllPerformance = $('#all-scope-implement').find('#scope-all-performance');  //获取总绩效所在处
        var allVersion = curScopeAllVersion.text();
        var allPerformance = curScopeAllPerformance.text();
        $(this).closest('tr').find('.redstar-scope-performance').remove();
        $.post(projectParams.getScope, {'id': curSelect}, function(resp) {
            //若成功取得实施范围数据并且当前已选择实施范围则进行相应操作
            if (resp.status == 1 && curSelect != null) {
                if (IdName !== 'scope-num') {
                    scopeVersion.html(resp.version);
                }
                //判断是否是更改数量操作
                if (scopeNum.val() != '') {

                    //若数量格式错误，则重设数量为1
                    if (scopeNum.val().match(reg) == null) {
                        scopeNum.val(1);
                    }
                    var version = resp.version;
                    var performance = scopeNum.val() * resp.performance;
                    changeData(version, performance, 'old', scopeVersion.val(), scopePerformance.val());
                    //修改数量时更改型号和绩效显示
//                    scopeVersion.html(version);
                    scopePerformance.val(performance + '.00');
                } else {

                    //新增时增加数量以及型号和绩效显示，数量默认为1
                    scopeNum.val('1');
                    scopeVersion.html(resp.version);
                    scopePerformance.val(resp.performance);
                    scopeNum.removeAttr('disabled');
                    scopeVersion.removeAttr('disabled');
                    scopePerformance.removeAttr('disabled');

                    changeData(resp.version, resp.performance, 'new');
                }
                if (scopePerformance.val() > 99999999.99) {
                    scopePerformance.after("<span class='redstar redstar-scope-performance' style='font-size:10px;'>总 超出</span>");
                }
            } else {
                //若当前未选择实施范围则清空右侧数据并锁定不允许编辑
                if (scopeVersion.val() != '' || scopePerformance.val() != '') {
                    //若清空时数量或绩效有数据，则需要更新总计天数和总绩效
                    changeData(0, 0, 'old', scopeVersion.val(), scopePerformance.val());
                }
                scopeNum.val(null);
                scopeVersion.html(null);
                scopePerformance.val(null);
                scopeNum.attr('disabled', 'disabled');
                scopeVersion.attr('disabled', 'disabled');
                scopePerformance.attr('disabled', 'disabled');
            }
        });
    });

    /**
     * 更改型号或绩效 前先获取旧数值
     */
    $(document).on('focus', '#scope-performance', function() {
        if ($(this).attr('id') == 'scope-version') {
            oldScopeVersion = $(this).val();
        } else {
            oldScopePerformance = $(this).val();
        }
    });

    /**
     * 型号或绩效更改操作
     */
    $(document).on('change', '#scope-performance', function() {
        if ($(this).attr('id') == 'scope-version') { //型号更改操作
            $(this).closest('tr').find('.redstar-scope-version').remove();
            //若型号格式错误，则还原数据
            if ($(this).val().match(reg) == null) {
                $(this).val(oldScopeVersion);
            } else {
                changeData($(this).val(), 0, 'old', oldScopeVersion, 0);
            }
            //若型号超出则显示错误信息
            if ($(this).val() > 32767) {
                $(this).after("<span class='redstar redstar-scope-version' style='font-size:10px;'>型号长度超出</span>");
            }
        } else { // 更改操作
            $(this).closest('tr').find('.redstar-scope-performance').remove();
            //若 格式错误，则还原数据
            if ($(this).val().match(reg) == null) {
                $(this).val(oldScopePerformance);
            } else {
                changeData(0, $(this).val(), 'old', 0, oldScopePerformance);
            }
            //若 超出则显示错误信息
            if ($(this).val() > 99999999.99) {
                $(this).after("<span class='redstar redstar-scope-performance' style='font-size:10px;'>总 超出</span>");
            }
        }

    });

    /**
     * 删除行操作
     */
    $(document).on('click', '.delete-scope', function() {
        var curTr = $(this).closest('tr');
        var scopeVersion = curTr.find('#scope-version');
        var scopePerformance = curTr.find('#scope-performance');
        changeData(0, 0, 'delete', scopeVersion.val(), scopePerformance.val())
        if ($(this).attr('id') == 'new') {
            //若该行信息为新增信息，则直接删除此行
            curTr.remove();
        } else {
            //若该行信息属于已关联信息，则隐藏此行并将此行信息归为删除
            var id = curTr.find('#scope-implement-name');
            id.attr('name', 'scope_delete[' + id.val() + ']');
            curTr.find('#scope-num-old').removeAttr('name');
            curTr.find('#scope-version').removeAttr('name');
            curTr.find('#scope-performance').removeAttr('name');
            curTr.find('#scope-remark').removeAttr('name');
            curTr.hide();
        }
    });

    /**
     * 更改总计天数和总绩效显示
     * 
     * @param scopeVersion 更改后的型号
     * @param scopePerformance    更改后的绩效
     * @param type          操作类型(old:更改信息 new:新增信息 delete:删除信息)
     * @param oldVersion   更改前的型号
     * @param oldPerformance      更改前的绩效
     */
    function changeData(scopeVersion, scopePerformance, type, oldVersion, oldPerformance) {
        var curScopeAllVersion = $('#all-scope-implement').find('#scope-all-version');  //获取总计天数所在处
        var curScopeAllPerformance = $('#all-scope-implement').find('#scope-all-performance');  //获取总绩效所在处
        var allVersion = curScopeAllVersion.text();
        var allPerformance = curScopeAllPerformance.text();

        //若是更改天数则总计天数和总绩效需先扣除之前的份额
        if (type == 'old' || type == 'delete') {
            allVersion = allVersion * 1 - oldVersion * 1;
            allPerformance = allPerformance * 1 - oldPerformance * 1 + '.00';
        }

        //将新增或更改的天数以及 加入总计天数和总绩效
        if (type == 'old' || type == 'new') {
            allVersion = allVersion * 1 + scopeVersion * 1;
            allPerformance = allPerformance * 1 + scopePerformance * 1 + '.00';
        }

        //更改总计天数和总绩效显示
        curScopeAllVersion.text(allVersion);
        curScopeAllPerformance.text(allPerformance);
    }



    /*********************资源信息***********************/

    /**
     * 新增行操作
     */
    $(document).on('click', '.add-resource', function() {
        var curTr = $('.resource-table').children('tbody').children('tr').last();
        var id = curTr.attr('id') * 1 + 1;
        $.post(projectParams.getResourceCategory, {'id': id}, function(resp) {
            if (resp.status == 1) {
                curTr.after(resp.data);
            }
        });
    });

    /**
     * 资源信息类别列表选择操作
     */
    $(document).on('change', '#resource-category-id', function() {
        var curTd = $(this).closest('tr').find('#resource-id');
        var curSelect = $(this).val(); //当前操作行所选择的资源信息类别
        var resourceModel = $(this).closest('tr').find('#resource-model');  //当前操作行的型号
        var resourceNum = $(this).closest('tr').find('#resource-num');  //当前操作行的数量
        var resourcePrice = $(this).closest('tr').find('#resource-price');  //当前操作行的单价
        //清空资源信息下拉框中的内容
        curTd.empty();
        $.post(projectParams.getResourceList, {'id': curSelect}, function(resp) {
            if (resp.status == 1) {
                //解锁资源信息下拉框
                curTd.removeAttr('disabled');
                //向资源信息下拉框中添加新数据
                curTd.append(resp.data);
            } else {
                //若当前选择的资源信息类别下没有内容则除了类别以外的数据清空，并锁定不允许编辑
                curTd.attr('disabled', 'disabled');
                resourceModel.html('');
                resourceNum.val(null);
                resourcePrice.val(null);
                resourceModel.attr('disabled', 'disabled');
                resourceNum.attr('disabled', 'disabled');
                resourcePrice.attr('disabled', 'disabled');
            }
        });
    });

    /**
     * 资源信息列表选择更改操作
     */
    $(document).on('change', '#resource-id', function() {
        var resourceModel = $(this).closest('tr').find('#resource-model');  //当前操作行的型号
        var resourceNum = $(this).closest('tr').find('#resource-num');  //当前操作行的数量
        var resourcePrice = $(this).closest('tr').find('#resource-price');  //当前操作行的单价
        var curSelect = $(this).closest('tr').find('#resource-id').val();  //当前操作行所选择的资源信息
        $.post(projectParams.getResource, {'id': curSelect}, function(resp) {
            if (resp.status == 1) {
                //判断是否是更改数量操作
                if (resourceNum.val() != '') {

                } else {
                    resourceNum.val('1');
                    resourceModel.removeAttr('disabled');
                    resourceNum.removeAttr('disabled');
                    resourcePrice.removeAttr('disabled');
                }
                resourceModel.html(resp.model);
                resourcePrice.val(resp.price);
            } else {
                resourceModel.html('');
                resourceNum.val(null);
                resourcePrice.val(null);
                resourceModel.attr('disabled', 'disabled');
                resourceNum.attr('disabled', 'disabled');
                resourcePrice.attr('disabled', 'disabled');
            }
        });
    });

    /**
     * 更改 前先获取旧数值
     */
    $(document).on('focus', '#resource-price', function() {
        oldResourcePrice = $(this).val();
    });

    /**
     * 数量或 更改操作
     */
    $(document).on('change', '#resource-num,#resource-price', function() {
        $(this).closest('tr').find('.redstar-resource-price').remove();
        if ($(this).attr('id') == 'resource-num') { //数量更改操作
            //若数量格式错误，则重设数据
            if ($(this).val().match(reg) == null) {
                $(this).val(1);
            } else {
                var resourcePrice = $(this).closest('tr').find('#resource-price');
            }
            if ($(this).val() * resourcePrice.val() > 99999999.99) {
                resourcePrice.after("<span class='redstar redstar-resource-price' style='font-size:10px;'>总 超出</span>");
            }
        } else { // 更改操作
            //若 格式错误，则还原数据
            if ($(this).val().match(reg) == null) {
                $(this).val(oldResourcePrice);
            } else {
                var resourceNum = $(this).closest('tr').find('#resource-num').val();
            }
            if ($(this).val() * resourceNum > 99999999.99) {
                $(this).after("<span class='redstar redstar-resource-price' style='font-size:10px;'>总绩效超出</span>");
            }
        }
    });

    /**
     * 删除行操作
     */
    $(document).on('click', '.delete-resource', function() {
        var curTr = $(this).closest('tr');
        if ($(this).attr('id') == 'new') {
            //若该行信息为新增信息，则直接删除此行
            curTr.remove();
        } else {
            //若该行信息属于已关联信息，则隐藏此行并将此行信息归为删除
            var id = curTr.find('#resource-id');
            id.attr('name', 'resource_delete[' + id.val() + ']');
            curTr.find('#resource-model').removeAttr('name');
            curTr.find('#resource-num').removeAttr('name');
            curTr.find('#resource-price').removeAttr('name');
            curTr.find('#resource-remark').removeAttr('name');
            curTr.hide();
        }
    });

    /**
     * 若出现错误则禁止表单提交
     */
    $("#main-form").submit(function() {
        if ($('.redstar-scope-performance').text() != '' || $('.redstar-scope-version').text() != '' || $('.redstar-resource-price').text() != '') {
            return false;
        } else {
            return true;
        }
    });

});

/**
 * 客户选择弹窗回调函数
 */
function customerSelectCallback(input, value) {
    $.post(projectParams.getCustomer, {'id': value}, function(resp) {
        if (resp.status == 1) {
            //显示客户地址
            $('.customer_address').val(resp.data);
            $('#customer_remark').val(resp.remark);
            $('.customer_organization_no').val(resp.organizationNo);
            var curA = $('.select-customer').closest('tr').find('a');
            //设置客户详情链接地址
            curA.attr('href', projectParams.customerUrl + '&id=' + resp.id);
        }
    });
}

/**
 * 商务联系人选择弹窗回调函数
 */
function businessSelectCallback(input, value) {
    $.post(projectParams.getBusiness, {'id': value}, function(resp) {
        if (resp.status == 1) {
            $('input[name="Project[business_tel]"]').val(resp.data);
        }
    });
}

function contractSelectCallback(input, value) {
    $.post(projectParams.getCustomerByContract, {'contract_no': value}, function(resp) {
        if (resp.status == 1) {
            $('#project-customer_name').val(resp.name);
            $('#customer-id').val(resp.no);
            $('.customer_address').val(resp.address);
            $('#customer_remark').val(resp.remark);
            $('.customer_organization_no').val(resp.organizationNo);
            var curA = $('.select-customer').closest('tr').find('a');
            //设置客户详情链接地址
            curA.attr('href', projectParams.customerUrl + '&id=' + resp.id);
        }
    });
}