
// 播出媒体
var firstIndSelect = $('select[name="mediaName"]').select2({
	placeholder: "请输入播出媒体",
	allowClear: true,
	width:'120px',
	ajax:{
		url: _PUBLIC_ +'ad-media/select/list.do',
		dataType:'json',
		quietMillis:100,
		data:function(params){
			return {
				name : params.term,
				pageNo : params.page || 1,
				pageSize : 20
			}
		},
		processResults: function (data, params) {
			params.page = params.page || 1;
			return {
		        results: data.results,
		        pagination: {
		          more: (params.page * 20) < data.total
		        }
		    };
		},
		cache: true
	}
});

// 获取表格数据
function getList(page){
	page = page || 1;
	var pageSize = 10, data = {
		"type" : $("#type").val(),
		"productName" : $("#productName").val(),
		"brandName" : $("#brandName").val(),
		"advMaster" : $("#advMaster").val(),
		"brandName" : $("#brandName").val(),
		"mediaName" : $("#mediaName").val(),
		"showEnv" : $("#showEnv").val(),
		"tvName" : $("#tvName").val(),
		"showTimeStart" : $("#showTimeStart").val(),
		"showTimeEnd" : $("#showTimeEnd").val(),
		"showDateStart" : $("#showDateStart").val(),
		"showDateEnd" : $("#showDateEnd").val(),
		"showDurationMin" : $("#showDurationMin").val(),
		"showDurationMax" : $("#showDurationMax").val(),
		"priceMin" : $("#priceMin").val(),
		"priceMax" : $("#priceMax").val(),
		"phone" : $("#phone").val(),
		"pageNo" : page,
	    "pageSize" : pageSize
	}, html = "";
		
	$("#page, #list").empty();
	ajax("ajax_list.do", data, function(res){
		if (res.code == 1 && res.list.records.length > 0){
			$.each(res.list.records, function(index, item){
				html += '<tr>'+
					'<td><input type="checkbox" class="checkbox {click:itemCheck}" name="chk" value="'+item.id+'" /><label></label></td>'+
					'<td>'+item.productName.toNull()+'</td>'+
					'<td>'+item.brandName+'</td>'+
					'<td>'+item.advMaster.toNull()+'</td>'+
					'<td>'+item.mediaName.toNull()+'</td>'+
					'<td>'+item.showDate.toNull()+'</td>'+
					'<td>'+item.showTime.toNull()+'</td>'+
					'<td>'+item.showEnv.toNull()+'</td>'+
					'<td>'+item.tvName.toNull()+'</td>'+
					'<td>'+item.phone+'</td>'+
					'<td>'+item.showDuration+'</td>'+
					'<td>'+item.price+'</td>'+
					'<td class="edit">'+
    					(update_flag == 1 ? '<a href="javascript:platformModfiy(\'edit\',\'品牌型号信息\','+item.id+');">编辑</a><br />' : '') +
    					(delete_flag == 1 ? '<a href="javascript:delRecord('+item.id+');">删除</a>' : '' ) +
					'</td>'+
				'</tr>';
			});
			$("#list").html(html);
			$.checkbox();
			$("#count").text(res.list.total);
			var newpage = new createPage(res.list.total, page, pageSize, 3);
			$("#page").html(newpage.pageHtml);
		} else {
			$("#list").info("暂无数据。");
		}
	});
}
pageGo = function(page){
	resetCheckAll();
	getList(page);
}

// 删除单个
function delRecord(id){
	confirm("确定删除该条记录吗？", function(){
        ajax("del.do", {"id" : id}, function(res){
            if (res.code == 1){
    			alert("删除成功。", 1, function(){
    				window.location.href = "index.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.do", {"ids" : ids.substring(1,ids.length)}, function(res){
            if (res.code == 1){
    			alert("删除成功。", 1, function(){
    				window.location.href = "index.do";
    			});
            } else {
            	alert(res.msg);
            }
        });
	});
}

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

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

// 保存品牌型号信息
function save(){
	// 得到数据
    var data = $('#dataForm').serializeJSON();
    data.id = $.getParam("id");
    console.log(data);
    // 数据是否完整
    if (data['productName'].isEmpty()){
    	top.alert("请填写产品名称", 0, function(){
            $("input[name='productName']").focus();
        });
        return false;
    }
    if (data['mediaName'].isEmpty()){
    	top.alert("请填写媒体名称");
        return false;
    }
    if (data['showEnv'].isEmpty()){
    	top.alert("请选择播出环境");
        return false;
    }
    if (data['tvName'].isEmpty()){
    	top.alert("请选择播出节目");
        return false;
    }
    if (data['showDate'].isEmpty()){
    	top.alert("请选择播出日期");
        return false;
    }
    if (data['showTime'].isEmpty()){
    	top.alert("请选择播出时间");
        return false;
    }
    // 提交数据
    top.ajax("save.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, "detail.do?id="+id, 800, 400, true);
}
