//流程模块【contract_list.合同管理】下录入页面自定义js页面,初始函数
function initbodys(){

    //如果是编辑 则初始化账号信息
    if(mid > 0){
        js.ajax(geturlact('getbankcodebymid'),{mid:mid},function(ret){
            if(ret.data && ret.data.length > 0){
                $(form('bankcode')).find("option").remove();
                js.setselectdata(form('bankcode'),ret.data,'value');

                $("select[name='bankcode']").find('option').each(function (){
                    if($(this).val() == ret.bank){
                        $(this).attr('selected','selected');
                    }
                })
            }
        },'post,json');
    }

    $(form("finance_type")).change(function(){
        getsubtypes()
    })

    $(form("receive")).change(function(){
        $(form('bankcode')).find("option").remove()
    })

    c.onselectdata['part_c']=function(d){
        form('partc_name').value = d.value;
    };

    c.onselectdatabefore=function(fid,zb){
        var receive = $("select[name='receive'] option:selected").val();
        var financetype = $("select[name='finance_type'] option:selected").val();
        if(fid == 'part_b' || fid == 'part_a') {
            if(financetype == '') {
                return '请选择类型';
            }
            if (receive != 1 && receive != 2) {
                return '请选择收款方,否则无法获取银行账号信息';
            }
        }
        return {'receive':receive}
    }


    c.onselectdata['part_a']=function(d){
        var receive = $("select[name='receive'] option:selected").val();
        form('parta_name').value = d.value;
        if (receive == 1) {
            $(form('bankcode')).find("option").remove()

            js.ajax(geturlact('getbankcode'),{numb:d.value,receive:receive},function(ret){
                if(ret.length == 0) {
                    $(form('bankcode')).find("option").remove()
                    return '未找到银行信息,请到供应商/客户 补充银行信息';
                }
                $(form('bankcode')).find("option").remove()
                js.setselectdata(form('bankcode'),ret,'value')
            },'post,json');
        }
    };

    c.onselectdata['part_b']=function(d){
        var receive = $("select[name='receive'] option:selected").val();
        form('partb_name').value = d.value;
        if (receive == 2) {
            $(form('bankcode')).find("option").remove()

            js.ajax(geturlact('getbankcode'),{numb:d.value,receive:receive},function(ret){
                if(ret.length == 0) {
                    $(form('bankcode')).find("option").remove()
                    return '未找到银行信息,请到供应商/客户 补充银行信息';
                }
                js.setselectdata(form('bankcode'),ret,'value')
            },'post,json');
        }
    };
    bindeventchange()
}

function eventaddsubrows(xu){
    bindeventchange()
    if(xu == 0) getsubtypes()
}

//动态绑定change事件
function bindeventchange(){
    $("table.tablesub").find("input[name~='amount']").each(function(){
        $(this).change(function(){
            filteramount(this)
        })
    })
}

//过滤金额
function filteramount(obj) {
    js.setmsg('');
    //如果不符合规则则提示数值不合法
    var val = obj.value;
    if(val != ''){
        val = parseFloat(obj.value);
        var reg = new RegExp(/^\d+\.?\d*$/);
        var res = reg.test(val);

        if(!res) js.setmsg('金额只能填写数字或小数')
        else obj.value = val.toFixed(2);
    }
}

function getsubtypes(){

    var val =  $(form("finance_type")).val();
    var pid = 0;

    if(val == 797) pid = 676 ;//成本
    if(val == 798) pid = 955 ; //收入

    js.ajax(geturlact('gettypes'),{pid:pid},function(ret){
        if(ret.length == 0) {
            return '没有款项信息';
        }
        var $objs = $("#tablesub0").find("select[name~='title0_']");
        $objs.each(function(index,el){
            if(index == ($objs.length - 1)){
                $(this).find("option:not(:first)").remove()
                js.setselectdata(this,ret,'value')
            }
        })
    },'post,json');
}
