var mobileClickTimes = 0;
var tr_index = -1;

$(function(){
	// 封装小区名称和id
    $("#communityName_s").val($("#currentManageCommunityName").val());
    $("#communityId_s").val($("#currentManageCommunityId").val());

	/**
	 * 搜索按钮 多项搜索
	 */
	$('.btn_search_more').click(function(){
		refreshList();
	});



	$('#communityId_s').on('change', function () {
		$('#buildId_s').val("");
		$('#buildName_s').val("");
	});

	$("form.form-search").submit(function () {
		return false;
	}).find(":input[name=name]").keydown(function () {
		if (event.keyCode === 13) {
			refreshList();
		}
	});

	//table添加按钮
	$('#add_btn').click(function(){
		var data = {};
		doDataPost(data,false,ctx+"/adm/door/add/form_ui",function(htmlText){
			$("div.popup_window").remove();
			addForm(htmlText);
			openWin('door_add_div');

		});
	});
	//table删除按钮
	$('#remove_btn').click(function(){
		//询问框，确认是否删除数据
		var x = ($(window).width() - 260 + $('div.viewFramework-sidebar').width())/2;
		var y = ($(window).height() / 2 - 73);
		var flag=false;
		$('table > tbody > tr > td > input[type=checkbox]').each(function(){
			var input = $(this);
			if(input.is(':checked')){
				flag=true;
			}
		});
		if(!flag){
			layer.alert('请选择要删除的楼栋。', {
				  icon: 0,
				  title:'提示',
				  area: ['260px', '146px'],
				  offset: [y+'px', x+'px']
				});
			return;
		};
		layer.confirm('确定要删除？', {
			icon: 3, 
			title:'提示' ,
			btn: ['确定','取消'], //按钮
		  area: ['260px', '146px'],
		  offset: [y+'px', x+'px']
		}, function(index){
			// remove(ctx+"/adm/door/del");
            removeAndRefresh(ctx + "/adm/door/del",function () {
                refreshList();
            });
		    layer.close(index);
		});
	});



	//实现分页条件查询
	$('#doorListTableDiv div.my-pagination > div > a').click(function(){

		
		var inputs = $("form[name=door_searcg_from] :input");
		var data = {};
		$(inputs).each(function(){
			var input = $(this);
			data[input.attr('name')]=input.val();
		});
		var href = $(this).attr('href');
		//截取href中#之后的值
		var str = href.substring(1,href.Length);
		data.page=parseInt(str);
		getPageData(data,ctx+"/adm/door/list");
	});
	$('#doorListTableDiv div.my-pagination .page-current').change(function(){
	
		var inputs = $("form[name=door_searcg_from] :input");
		var data = {};
		$(inputs).each(function(){
			var input = $(this);
			data[input.attr('name')]=input.val();
		});
		var page = $(this).val();
		data.page=parseInt(page);
		getPageData(data,ctx+"/adm/door/list");
	});
	adjustTableCellWidth('doorListTableDiv');
});

/**
 * 打开楼栋回填
 * */
$('#buildName_s').click(function () {
    var communityId_s = $('#communityId_s').val();
    if (communityId_s == null || communityId_s.length <= 0) {
        alert("请先选择小区");
        return;
    }
    var data = {"communityId": communityId_s};
    buildList.data = data;
    buildList.popBuildList(data);
    buildList.backfillFun = function (data) {
        $('#buildId_s').val(data.id);
        $('#buildName_s').val(data.name);
        $('#buildId_s').change();
    }
});

/**
 * 打开修改窗口
 * */
$('table > tbody > tr').click(function () {
    var this_index = $(this).index();
    if (this_index === tr_index) {
        if (mobileClickTimes > 0) {
            dblclickF(this);
            mobileClickTimes = 0;
            tr_index = -1;
        }
    } else {
        tr_index = this_index;
        setTimeout(function () {
            mobileClickTimes = 0;
            tr_index = -1;
        }, 400);
    }
    mobileClickTimes++;
});

/**
 * 打开修改窗口时回填修改的门信息
 * */
function dblclickF(obj){
    var id = $(obj).find(':checkbox:eq(0)').val();
    var data = {};
    data["id"] = id;
    doDataPost(data,false,ctx+"/adm/door/edit/form_ui",function(htmlText){
        $("div.popup_window").remove();
        addForm(htmlText);

        openWin('door_edit_div');

    });
}

/**
 * 刷新列表页面（附搜索条件）
 */
function refreshList(){

	
	doFormPost(
			"door_searcg_from",
			ctx+"/adm/door/list",
			function(msg){
				addPage(msg);
			});
}
