Init();
function Init()
{
	//check xem da khoi tao chua
	var page_initflag=document.getElementById("page_initflag");
	if(page_initflag.value.length>0)
		return;
	page_initflag.value="1";
	
	showWaitModal();
	g_ShowStoreInSelect("select_store_id");
	var iddate=document.getElementById("iddate");
	iddate.value=getCurDate().toString();
	showSummary();
}
function showSummaryDetails(view,date_view,store_id,iddiv)
{
	var divview=document.getElementById(iddiv);
	if(divview.innerHTML.length>1)
		return;
	showWaitModal();
	var url = "?page=bc_tonghoptrongngay&store_id="+store_id+"&view="+view+"&date="+date_view;
	
	var dataString = "";
	jQuery.ajax({
			url: url,
			data: dataString,
			type: "GET",
			success: function(data)
			{
				divview.innerHTML=data;
				hideModal();
				
			},
			error: function (){
				hideModal();
				
			}
	});		
}
function showSummary(store_id,date_view)
{
	var url = "?page=bc_tonghoptrongngay&date="+date_view+"&view=sum&store_id="+store_id;
	
	var dataString = "";
	jQuery.ajax({
        url: url,
        data: dataString,
		async: false,
        type: "GET",
        success: function(data)
		{
			
			var arstore=data.split(g_split_row);
			var contents="";
			var dSExport=0,dSExportDebit=0,dSExportDiscount=0,dSImport=0,dSImportDebit=0,dSImportDiscount=0,dCash=0;
			for(i=1;i<arstore.length;i++)
			{
				var stores=arstore[i].split(g_split_val);
				if(stores.length==8)
				{
					dSExport+=parseNumber(stores[2]);dSExportDebit+=parseNumber(stores[3]);dSExportDiscount+=parseNumber(stores[4]);
					dSImport+=parseNumber(stores[5]);dSImportDebit+=parseNumber(stores[6]);dSImportDiscount+=parseNumber(stores[7]);	
					var idxuat="idxuat"+stores[0];var idnhap="idnhap"+stores[0];
					contents+="<div class='collapsibleheader active' onclick='divToggleNext(this)'>"+stores[1]+"</div>";
					contents+="<div class='collapsiblecontentexpand'>";			
						contents+="<div class='collapsibleheader sub' onclick='divToggleNext(this,showSummaryDetails(\"export\",\""+date_view+"\","+stores[0]+",\""+idxuat+"\"));'>";
						contents+="<span style='display: inline-block;width:80px'>Xuất</span><span  style='display: inline-block;width:80px'>"+stores[2]+"</span><span  style='display: inline-block;width:80px'>"+stores[3]+"</span><span  style='display: inline-block;width:80px'>"+stores[4]+"</span>";	
						contents+="</div>";
						contents+="<div class='collapsiblecontent' id='"+idxuat+"'>";
						contents+="</div>";	
						contents+="<div style='width:100%;height:1px'></div>";	

						contents+="<div class='collapsibleheader sub' onclick='divToggleNext(this,showSummaryDetails(\"import\",\""+date_view+"\","+stores[0]+",\""+idnhap+"\"))'>";
						contents+="<span style='display: inline-block;width:80px'>Nhập</span><span  style='display: inline-block;width:80px'>"+stores[5]+"</span><span  style='display: inline-block;width:80px'>"+stores[6]+"</span><span  style='display: inline-block;width:80px'>"+stores[7]+"</span>";	
						contents+="</div>";
						contents+="<div class='collapsiblecontent' id='"+idnhap+"'>";
						contents+="</div>";	
					contents+="</div>";
					contents+="<div style='width:100%;height:1px'></div>";
				}
			}
			
			var stores=arstore[0].split(g_split_val);
			dCash=dSExport-dSExportDebit-dSImport+dSImportDebit+parseNumber(stores[0])-parseNumber(stores[1]);
			var contents_head="";
		
			contents_head+="<div class='sumfix'>";
			contents_head+="<div style='display: table-cell;width:80px'></div><div style='display: table-cell;width:80px'>Tổng</div><div style='display: table-cell;width:80px'>Nợ</div><div style='display: table-cell;width:80px'>Giảm</div>";
			contents_head+="</div>";

			contents_head+="<div class='sumfix'>";
			contents_head+="<span style='display: inline-block;width:80px;'>Xuất</span><span style='display: inline-block;width:80px;'>"+formatNumber(dSExport)+"</span>";	
			contents_head+="<span style='display: inline-block;width:80px;'>"+formatNumber(dSExportDebit)+"</span><span style='display: inline-block;width:80px;'>"+dSExportDiscount+"</span>";	
			contents_head+="</div>";
			contents_head+="<div class='sumfix'>";
			contents_head+="<span style='display: inline-block;width:80px;'>Nhập</span><span style='display: inline-block;width:80px;'>"+formatNumber(dSImport)+"</span>";	
			contents_head+="<span style='display: inline-block;width:80px;'>"+formatNumber(dSImportDebit)+"</span><span style='display: inline-block;width:80px;'>"+formatNumber(dSImportDiscount)+"</span>";	
			contents_head+="</div>";

			contents_head+="<div class='collapsibleheader sub' onclick='divToggleNext(this,showSummaryDetails(\"receipt\",\""+date_view+"\",0,\"idthu\"))'>";
			contents_head+="<span style='display: inline-block;width:80px'>Thu</span><span style='display: inline-block;width:80px'>"+(stores[0])+"</span>";	
			contents_head+="</div>";
			contents_head+="<div class='collapsiblecontent' id='idthu'>";
			contents_head+="</div>";	
			contents_head+="<div style='width:100%;height:2px'></div>";

			contents_head+="<div class='collapsibleheader sub' onclick='divToggleNext(this,showSummaryDetails(\"pay\",\""+date_view+"\",0,\"idchi\"))'>";
			contents_head+="<span style='display: inline-block;width:80px'>Chi</span><span style='display: inline-block;width:80px'>"+(stores[1])+"</span>";	
			contents_head+="</div>";
			contents_head+="<div class='collapsiblecontent' id='idchi'>";
			contents_head+="</div>";	
			contents_head+="<div style='width:100%;height:2px'></div>";

			contents_head+="<div class='sumfix'>";
			contents_head+="<span style='display: inline-block;width:80px;font-weight:bold'>Tiền mặt</span><span style='display: inline-block;width:80px;color:red'>"+formatNumber(dCash)+"</span>";	
			contents_head+="</div>";

			var idcontent=document.getElementById("idcontent");
			idcontent.innerHTML=contents_head+contents;
			hideModal();
		},
        error: function (){
			showMsg("Thất bại","DANH SÁCH KHO");
		}
	});
}
function xem()
{
	var idcontent=document.getElementById("idcontent");
	idcontent.innerHTML="";
	showWaitModal();
	var iddate=document.getElementById("iddate");
	var select_store_id=document.getElementById("select_store_id");
	showSummary(select_store_id.value,iddate.value);
}