// 获取表格数据
function getList(page){
	page = page || 1;
	var firstInd = $("#firstInd option:selected").text();
	var pageSize = 10, data = {
		"brand" : $("#brand").val(),
		"model" : $("#model").val(),
		"advertiser" : $("#advertiser").val(),
		"firstInd" : ( firstInd == "一级行业" ? "" : firstInd),
		"secondInd" : $("#secondInd option:selected").val(),
		"pageNo" : page,
	    "pageSize" : pageSize
	}, html = "";
		
	$("#page, #list").empty();
	ajax("ajax_brand_list.do", data, function(res){
		if (res.code == 1 && res.list.length > 0){
			$.each(res.list, function(index, item){
				html += '<tr>'+
					'<td><input type="checkbox" class="checkbox {click:itemCheck}" name="chk" value="'+item.id+'" /><label></label></td>'+
					'<td>'+item.model.toNull()+'</td>'+
    				'<td class="nop">'+(item.modelImgUrl ? '<img src="'+item.modelImgUrl.toNull()+'" width="98" height="58" valign="middle" onerror="this.src=\'static/img/noimg.jpg\'"/>':'&nbsp;')+'</td>'+
					'<td>'+item.brand.toNull()+'</td>'+
    				'<td class="nop">'+(item.brandImgUrl ? '<img src="'+item.brandImgUrl.toNull()+'" width="98" height="58" valign="middle" onerror="this.src=\'static/img/noimg.jpg\'"/>':'&nbsp;')+'</td>'+
					'<td>'+item.advertiser.toNull()+'</td>'+
					'<td>'+item.firstInd.toNull()+'</td>'+
					'<td>'+item.secondInd.toNull()+'</td>'+
					'<td class="edit">'+
    					(update_flag == 1 ? '<a href="javascript:platformModfiy(\'edit\',\'品牌型号信息\','+item.id+');">编辑</a>&nbsp;&nbsp;' : '') +
    					(delete_flag == 1 ? '<a href="javascript:delRecord('+item.id+');">删除</a>' : '' ) +
					'</td>'+
				'</tr>';
			});
			$("#list").html(html);
			$.checkbox();
			$("#count").text(res.count);
			var newpage = new createPage(res.count, page, pageSize, 3);
			$("#page").html(newpage.pageHtml);
		} else {
			$("#list").info("暂无数据。");
		}
	});
}
pageGo = function(page){
	resetCheckAll();
	getList(page);
}

// 删除单个
function delRecord(id){
	confirm("确定删除该条记录吗？", function(){
        ajax("del_brand.do", {"id" : id}, function(res){
            if (res.code == 1){
    			alert("删除成功。", 1, function(){
    				window.location.href = "brand_list.do";
    			});
            } else {
            	alert(res.msg);
            }
        });
	});
}

// 删除多个
function delRecordBatch(){
	var arr = document.getElementsByName("chk");
	var ids = "";
	for (i=0; i<arr.length; i++){
		if (arr[i].checked){
			ids += ("," + arr[i].value);
		}
	}
	if (ids.isEmpty()){
		alert("请至少选择一条记录");
		return false;
	}
	confirm("确定删除这些记录吗？", function(){
        ajax("batch_del_for_brand.do", {"ids" : ids.substring(1,ids.length)}, function(res){
            if (res.code == 1){
    			alert("删除成功。", 1, function(){
    				window.location.href = "brand_list.do";
    			});
            } else {
            	alert(res.msg);
            }
        });
	});
}

// 导入表格数据
function importExcel(obj){
    $("#fileForm").submit();
}

// 导入结果
function excelCallback(code, msg) {
    if (code == 1) {
        // 导入成功
    	alert("导入成功。", 1, function(){
			window.location.href = "brand_list.do";
		});
    } else {
    	// 导入失败
    	alert(msg, 0, function(){
        	location.reload();
		});
    }
}

// 保存品牌型号信息
function save(){
	// 得到数据
    var data = $('#dataForm').serializeJSON();
    data.id = $.getParam("id");
    data.firstInd = $("select[name='firstInd']>option:selected").text();
    data.modelImgUrl = $("#modelImgUrl")[0].src;
    data.brandImgUrl = $("#brandImgUrl")[0].src;
    console.log(data);
    // 数据是否完整
    if (data['model'].isEmpty()){
    	top.alert("请填写产品型号", 0, function(){
            $("input[name='model']").focus();
        });
        return false;
    }
    if (data['modelImgUrl'].isEmpty() || data['modelImgUrl'].indexOf("ylcm_sys") > 1){
    	top.alert("请上传产品LOGO");
        return false;
    }
    if (data['brand'].isEmpty()){
    	top.alert("请填写所属品牌", 0, function(){
            $("input[name='brand']").focus();
        });
        return false;
    }
    if (data['brandImgUrl'].isEmpty() || data['brandImgUrl'].indexOf("ylcm_sys") > 1){
    	top.alert("请上传品牌LOGO");
        return false;
    }
    if (data['advertiser'].isEmpty()){
    	top.alert("请填写广告主", 0, function(){
            $("input[name='advertiser']").focus();
        });
        return false;
    }
    if (data['firstInd'].isEmpty() || data['firstInd'] == "下拉选择"){
    	top.alert("请选择一级行业");
        return false;
    }
    if (data['secondInd'].isEmpty()){
    	top.alert("请选择二级行业");
        return false;
    }
    // 提交数据
    top.ajax("save_brand_info.do", data, function(res){
        if (res.code == 1){
            top.alert("保存成功。", 1, function(){
            	top.getList(1);
            	top.$.closeWindow();
            });
        } else {
            top.alert(res.msg);
        }
    });
}

// 弹窗
function platformModfiy(act, title, id){
	title = title || "";
	id = id || "";
	$.openWindow(act.decode("add","新增","edit","修改","")+title, "brand_info.do?id="+id, 500, 470, true);
}
// 切换，一级行业
function onchangeByFirstInd(secondIndName){
	var html = "", number = $("select[name='firstInd']>option:selected").val();
	if(number.isEmpty()){
		html = '<option value="">二级行业</option>';
		$("select[name='secondInd']").html(html);
		$(".select").uiSelect();
		return false;
	}
	ajax({url:"ajax_brand_type.do",data:{"number":number},callBack:function(res){
		if(res.code == 1 && res.list.length > 0){
			$.each(res.list,function(index, item){
				html+='<option value="'+item.name+'" '+(item.name == secondIndName ? " selected":"")+'>'+item.name+'</option>';
			});
			$("select[name='secondInd']").html(html);
			$(".select").uiSelect();
		}
	},load:false});
}
