

$(document).ready(function(){

	//弹出层
	$("#btnAddCoupons").click(function(){
		$('#couponsPopupContact').popup({width:'80%',height:'80%',popupHead:'couponsPopupHead',btnClose:'couponsPopupContactClose'});
		if(couponsstatus == 0)
		{BindCoupons('PmtRule.aspx','type=ajax&Action=SearchCoupons');couponsstatus = 1;};
	});//如果是第一次弹出层,要获取商品列表,否则只需要显示层即可 
	
	if($.browser.mozilla){setInterval(setList,100);}
	$('.sub_container_content').height(500);
    $('input[@name=chkCouponsAll]').click(function(){$('input[@name=chkCouponsItem]').attr('checked',$(this).attr('checked'));});//全选
	
    
    //搜索
    $('#btnCouponsSearch').click(function(){
        keyword = $('#txtCouponsKeyword').val();
        searchType = $('#coupons_sel_searchType').val();
        search_urldata = 'type=ajax&Action=SearchCouponsAjax&keyword=' + escape(keyword);
        BindCoupons('PmtRule.aspx',search_urldata);
    });
    //--确认优惠券
    $('#btnCouponsSubmit').click(function(){
        var couponsIDs = '';
        var couponsNames = '';
        var selectedcouponsIDs = '';
        if($('#couponsIDs').val() != ''){
            selectedcouponsIDs = ','+$('#couponsIDs').val()+',';
        };
        $('input[@name=chkCouponsItem][@checked]').each(function(){
            var currobj = $(this);
            if(selectedcouponsIDs.indexOf(',' + currobj.val() + ',') == -1){
                var couponsName = currobj.parent().siblings('#couponsName').text();
                var row = $('<tr><td class="delete ccenter c7"><img src="../images/x-gray.gif" alt="删除" /><input type="hidden" name="couponsID" value=' + currobj.val() + '><input type="hidden" name="couponsName" value="' + couponsName + '"></td><td class="c8" style="overflow-x:hidden;">' + couponsName + '</td></tr>');
                row.find('.delete').css('cursor','pointer').click(function(){
                    $(this).parent().remove();
                    $('#couponscount').text(Number($('#couponscount').text())-1);
                    //--优惠券ID
                    var cids = '';
                    $('input[@name=couponsID]').each(function(){
                        cids += $(this).val() + ',';
                    });
                    if(cids != ''){cids = cids.substring(0,cids.length-1);};
                    $('#couponsIDs').val(cids);
                    
                    //--优惠券名称
                    var cNames = '';
                    $('input[@name=couponsName]').each(function(){
                        cNames += $(this).val() + ',';
                    });
                    if(cNames != ''){cNames = cNames.substring(0,cNames.length-1);};
                    $('#couponsNames').val(cNames);
                    
                }).find('img').mouseover(function(){
                    $(this).attr('src','../images/x-red.gif');
                }).mouseout(function(){
                    $(this).attr('src','../images/x-gray.gif');
                });
                $('#couponslistresult').append(row);
                couponsIDs += currobj.val() + ',';
                couponsNames += couponsName.replace(',',' ') + ',';
            };
        });
        if(couponsIDs != ""){
            couponsIDs = couponsIDs.substring(0,couponsIDs.length-1);
            couponsNames = couponsNames.substring(0,couponsNames.length-1);
            $('#couponsIDs').val($('#couponsIDs').val() + ($('#couponsIDs').val() != '' ? ',' : '') + couponsIDs);//隐藏控件记录已选择的商品ID(逗号分隔1,2,3)
            $('#couponsNames').val($('#couponsNames').val() + ($('#couponsNames').val() != '' ? ',' : '') + couponsNames);
        };
//        couponsdisablePopup();//关闭层
        $('#couponsPopupContactClose').click();
        $('#couponscount').text($('input[@name=couponsID]').length);
    });
    
    //--清空
    $('#btnClearCoupons').click(function(){
        $('#couponscount').text('0');
        $('tr:gt(0)','#couponslistresult').remove();
        $('#couponsIDs').val('');
        $('#couponsNames').val('');
    });  
});

function BindCoupons(weburl,urldata){
    $('#template').hide();//将第一行模板隐藏
    $.ajax({
        type:"get",
        dataType:"json",
        url:weburl+'?NoCopyRight=1',
        data:urldata,
        cache:false,
        success:function(msg){
            $('input[@name=chkAll]').attr('checked','');
            $('tr:gt(0)','#couponsdatas').remove();
            var data = msg.Coupons;
            $.each(data,function(i,n){
                var row = $('#couponstemplate').clone();
                row.show().mouseover(function(){$(this).addClass('on')}).mouseout(function(){$(this).removeClass('on')}); //将复制的行显示出来并添加样式
                row.find('#couponscheckbox').html('<input type="checkbox" name="chkCouponsItem" value="' + n.CouponsID + '" />');//checkbox
                row.find('#couponsName').html('<div style="overflow:hidden;width:220px;white-space:nowrap;">'+n.CouponsName+'</div>'); //商品名称
                row.find('#couponsPrefix').text(n.CouponsPrefix);   //编码
                row.find('#couponsGenQuantity').text(n.CouponsGenQuantity);
				row.find('#couponsStatus').text(n.Status);
				row.find('#couponsType').text(n.Type);
				row.attr("id","couponsready_" + n.CouponsID);//改变绑定好数据的行的id
				row.appendTo("#couponsdatas");//添加到模板的容器中
            });
            
        },
        error:function(XMLHttpRequest,textStatus){if(textStatus=='parsererror'){alert('身份已过期,请重新登陆!')}else{alert('error')};}
    });
}
