/// <reference path="Extjs_Intellisense.js" />
/// <reference path="src/data/XmlStore.js" />
/// <reference path="Base.js" />
/// <reference path="TreeFromXml.js" />


function GetTime(date,isStart)
{
    var now=date;
    var restime="";
    if(isStart)
    {
        restime=now.format('Y/m/d')+" 00:00:00";
    }else
    {
        restime=now.format('Y/m/d')+" 23:59:59";
    }
    return restime;
}



function DoSearch(evobj,htmlel,obj)
{
    var plattype=$("plattype").value;
    var devid=$("selectdevid").value;
    var sttime=$("startdate").value;
    var entime=$("enddate").value;
    var recordType=7;//RECORD_TYPE_SCHEDULE = 1,RECORD_TYPE_IO = 2,RECORD_TYPE_MOTION_DETECT = 4,RECORD_TYPE_ALL = 7,
    var streamType=3;//all STREAM_TYPE_CIF = 1, STREAM_TYPE_D1  =2,STREAM_TYPE_ALL = 3,
    var mediatype=7;//MEDIA_TYPE_AV = 1,MEDIA_TYPE_JEPG = 2,MEDIA_TYPE_VV = 4,MEDIA_TYPE_ALL = 7,
    var node=null;
    
    try
    {
        recordType=$("recordtype").value;
        streamType=$("streamType").value;
        mediatype=$("mediatype").value;
    }catch(eeee){}
    
    if(devid==null || devid=="")
    {
        oldAlert(GetDefaultLang("lang_selectdeverror8","请单击右边设备或输入设备ID"),GetDefaultLang("lang_inputerror","输入错误"),null,320);
        return false;
    }
    
    
    var realtree=GetRelaTimeTree();
    if(realtree && realtree.root)
    {
        node=realtree.root.findChild("id","rc"+devid,true);//$("selectdevid").value
    }
    if(node==null  || node.attributes==null || node.attributes["Id"]!=devid)
    {
        oldAlert(GetDefaultLang("lang_deviderror9","设备ID错误,未找到此设备"),GetDefaultLang("lang_inputerror","输入错误"));
        return false;   
    }
    
    var allowreplay=GetRightByDevId(devid,"replay");
    if(allowreplay!="")
    {
        oldAlert(allowreplay);
        //oldAlert("您无权对此设备进行回放视频查看!");
        return false;
    }
        
        
    if(sttime.length!=19)
    {
        oldAlert(GetDefaultLang("lang_timeerror10","开始时间长度应为19字节，年4位，月日时分秒各2位\r\n例如1970/01/01 00:00:00"),GetDefaultLang("lang_inputerror","输入错误"),null,300);
        return false;
    }
    var starttimeok=isDateTime(sttime);
    if(starttimeok==-103)
    {
        oldAlert(GetDefaultLang("lang_starttimeerror11","开始时间错误，时间必须大于1970/01/01 00:00:00"),GetDefaultLang("lang_inputerror","输入错误"),null,300);
        return false;
    }else if(starttimeok!=0)
    {
        oldAlert(GetDefaultLang("lang_starttimeerror12","开始时间格式错误"),GetDefaultLang("lang_inputerror","输入错误"),null,300);
        return false;
    }
    
    
    
    if(entime.length!=19)
    {
        oldAlert(GetDefaultLang("lang_timeerror13","结束时间长度应为19字节，年4位，月日时分秒各2位\r\n例如2036/12/30 23:59:59"),GetDefaultLang("lang_inputerror","输入错误"),null,300);
        return false;
    }
    
    var endtimeok=isDateTime(entime);
    if(endtimeok==-103)
    {
        oldAlert(GetDefaultLang("lang_timerror14","结束时间错误，时间必须小于2036/12/30 23:59:59"),GetDefaultLang("lang_inputerror","输入错误"),null,300);
        return false;
    }else if(endtimeok!=0)
    {
        oldAlert(GetDefaultLang("lang_timeerror15","结束时间格式错误"),GetDefaultLang("lang_inputerror","输入错误"),null,300);
        return false;
    }
    
    if(CompareDateTime(entime,sttime)!=1)
    {
        oldAlert(GetDefaultLang("lang_timeerror16","结束时间必须大于开始时间"),GetDefaultLang("lang_inputerror","输入错误"),null,320);
        return false;
    }
    
    
    var grid=GetSearchResultGrid();//grid.haveNextPage
    
    grid.DevId=lastSearchDeviceId=devid;
    grid.RecordType=lastSearchRecordTypeId=plattype;
    
    mediatype=$("mediatype").value;
    if(parseInt(plattype)!==0)
    {
        mediatype=$("mediatype").value;  
        recordType=$("recordType").value;  
    }
    var searhplan=GetTablPanel();
    searhplan.activate('showplayback');
    searhplan.selectPlayBackOjb=grid.SelectDevNode= node;
    $("selectcamera").innerText=GetDefaultLang("lang_selectdevmsg",",当前选择设备:{devname}").replace("{devname}",node.text);
    var filterstr="<Filter nCategory='"+plattype+"' szDevId='"+devid+"' ";
    filterstr+=" nPageNum='"+playbacksearchform.pageNum+"' ";
    filterstr+="  startTime='"+sttime+"' ";
    filterstr+="  mediaType='"+mediatype+"' ";
    filterstr+="  recordType='"+recordType+"' ";
    filterstr+="  streamType='"+streamType+"' ";
    filterstr+="  endTime='"+entime+"' />";
    WriteWebLog("搜索文件:"+filterstr);
    
    lastSearchPageNumber=playbacksearchform.pageNum;
    var ret=loginObj.QueryRecord(devid,filterstr);
    if(ret!=0)
    {//出错
        ShowSearchResultByXml("<data></data>");       
        switch(plattype)
        {
            case "0":
            {//平台录像
                oldAlert(GetDefaultLang("lang_searcherrorstr_0","搜索平台录像失败!"),GetDefaultLang("lang_searcherror","搜索失败"));
            }
            break;
            case "2":
            {//前端录像
                oldAlert(GetDefaultLang("lang_searcherrorstr_2","搜索平台录像失败!"),GetDefaultLang("lang_searcherror","搜索失败"));
            }
            break;
            case "1":
            default:
            {//前端上传录像
                oldAlert(GetDefaultLang("lang_searcherrorstr_1","搜索平台录像失败!"),GetDefaultLang("lang_searcherror","搜索失败"));
            }
            break;
        }
    }
} 

//document.write('<select id="plattype" onchange="ChangePlatType(this.value);" style="width:140px" name="plattype" >');
//document.write('<option value="0" >'+GetDefaultLang("lang_serachplatform","平台录像")+'</option>');
//document.write('<option value="2" >'+GetDefaultLang("lang_serachipcvideo","前端录像")+'</option>');
//document.write('<option value="1" >'+GetDefaultLang("lang_serachuploadp","前端上传录像")+'</option>');


//录像来源更改了
function ChangePlatType(val)
{
    var mediatype=$("mediatype");
    var recordtype=$("recordtype");
    var streamtype=$("streamType");
    var value=parseInt(val);
    switch(value)
    {
        case 0:
        {
            SetEnabled("mediatype,streamType",false);
            
            recordtype.length=0;
            recordtype.options[recordtype.length]=new Option(GetDefaultLang("lang_allrecord","全部"),7);
            recordtype.options[recordtype.length]=new Option(GetDefaultLang("lang_timerrecord","定时录像"),1);
            recordtype.options[recordtype.length]=new Option(GetDefaultLang("lang_alarmrecord","报警录像"),2);
            
            
            mediatype.length=0;
            mediatype.options[mediatype.length]=new Option(GetDefaultLang("lang_allrecord","全部"),7);
            mediatype.options[mediatype.length]=new Option(GetDefaultLang("lang_audioandvideo","音频和视频"),1);
            mediatype.options[mediatype.length]=new Option(GetDefaultLang("lang_video","视频"),4);
        }
        break;
        case 2:
        {
            SetEnabled("mediatype,streamType",true);
            
            
            recordtype.length=0;
            recordtype.options[recordtype.length]=new Option(GetDefaultLang("lang_allrecord","全部"),7);
            recordtype.options[recordtype.length]=new Option(GetDefaultLang("lang_timerrecord","定时录像"),1);
            recordtype.options[recordtype.length]=new Option(GetDefaultLang("lang_ioalarmrecord","IO报警"),2);
            recordtype.options[recordtype.length]=new Option(GetDefaultLang("lang_movealarmrecord","移动侦测"),4);
            
            
            mediatype.length=0;
            //mediatype.options[mediatype.length]=new Option(GetDefaultLang("lang_allmedia","全部"),7);
            mediatype.options[mediatype.length]=new Option(GetDefaultLang("lang_audioandvideo","音频和视频"),1);
            mediatype.options[mediatype.length]=new Option(GetDefaultLang("lang_videomedia","视频"),4);
            mediatype.options[mediatype.length]=new Option(GetDefaultLang("lang_picmedia","图片"),2);
            
        }
        break;
        case 1:
        {
            SetEnabled("mediatype,streamType",true);
            
            
            recordtype.length=0;
            recordtype.options[recordtype.length]=new Option(GetDefaultLang("lang_allrecord","全部"),7);
            recordtype.options[recordtype.length]=new Option(GetDefaultLang("lang_iorecord","IO报警"),2);
            recordtype.options[recordtype.length]=new Option(GetDefaultLang("lang_movealarmrecord","移动侦测"),4);
            
            
            
            mediatype.length=0;
            //mediatype.options[mediatype.length]=new Option(GetDefaultLang("lang_allmedia","全部"),7);
            mediatype.options[mediatype.length]=new Option(GetDefaultLang("lang_audioandvideo","音频和视频"),1);
            mediatype.options[mediatype.length]=new Option(GetDefaultLang("lang_videomedai","视频"),4);
            mediatype.options[mediatype.length]=new Option(GetDefaultLang("lang_picmedia","图片"),2);
        }
        break;    
    }
}


function ChangeMediaType(obj,objvalue)
{
    var streamtype=$("streamType");
    if(objvalue==2 || objvalue=="2")
    {
        streamtype.selectedIndex=0;
        streamtype.disabled=true;
    }else
    {
        if(streamtype.disabled)
        {
            streamtype.disabled=false;
        }
    }
}

 
////<Filter nCategory=\"%d\" szDevId=\"%s\" nPageNum=\"%s\" startTime=\"%s\" endTime=\"%s\"/> //nCategory 0平台，1报警，2 ipc
function GetSearchPlayBackForm()
{
    if(playbacksearchform==null)
    {
      playbacksearchform = new Ext.FormPanel({
            labelWidth: 60, // label settings here cascade unless overridden
            url:'save-form.php',
            frame:true,
            title: GetDefaultLang("lang_videosearchtitle",'录像回放搜索')+'<div id="selectcamera" style="display:inline;" ></div>',
            bodyStyle:'padding:5px 5px 0',   
            layout:'form',
            region:'center',
            pageNum:0,
            szDevId:'',
            lastFilter:'',
            haveNextPage:false,
            items:
            [ 
                {
                    contentEl:'playbackobjlist'
                }
            ],
            buttons: [
            {
                xtype:'label',
                html:'<div  style="display:inline;"  >'+GetDefaultLang("lang_downloadsavepath","下载保存路径:")+'</div><div style="display:inline;overflow:hidden;width:400px;text-align:left;"  title="'+downloadfiledir+'" id="downloadpathdiv" >' + downloadfiledir + '</div>'
            },
            main_text_showpage=new Ext.form.Label({
                text:GetDefaultLang("lang_pageitemmsg",'第{idx}页,共{all}页').replace("{idx}","0").replace("{all}","0"),
                id:'showpagemsglabel',
                xtype:'label'
            })
            ,{
                text:GetDefaultLang("lang_changesavepath2",'更改目录')
                ,handler:function(evobj,htmlel,obj)
                {
                    var obj=$("replay1");
                    try
                    {
                        obj.SetCaptureDir(1,"");
                        downloadfiledir=obj.GetCaptureDir(1);
                        if(	downloadfiledir!=undefined && downloadfiledir!="undefined" && downloadfiledir!="" )
						{
							downloadfiledir=downloadfiledir;
						}else
						{
							downloadfiledir="c:\\";
						}
                    }catch(eee)
                    {
                        downloadfiledir="c:\\";
                    }
                    var showobj=$("downloadpathdiv");
                    showobj.innerText=downloadfiledir;
                    showobj.title=downloadfiledir;
                }
            }
            ,
            {
                text:GetDefaultLang("lang_searchitem", '搜索')
                ,handler:function(evobj,htmlel,obj)
                {
                    var playform=GetPlayBackForm();
                    if(playform.isVisible())
                    {
                        oldAlert(GetDefaultLang("lang_closeerror20","请先关闭[录像文件回放]窗口再进行搜索"),GetDefaultLang("lang_searcherror21","错误操作"));
                        return ;
                    }
                    playbacksearchform.pageNum=0;
                    DoSearch(evobj,htmlel,obj);
                }
            },{
                id:'SearchFilePrev',
                text:GetDefaultLang("lang_prepage", '上一页')
                ,handler:function(evobj,htmlel,obj)
                {
                    var playform=GetPlayBackForm();
                    if(playform.isVisible())
                    {
                        oldAlert(GetDefaultLang("lang_closeerror20","请先关闭[录像文件回放]窗口再进行搜索"),GetDefaultLang("lang_searcherror21","错误操作"));
                        return ;
                    }
                    if(playbacksearchform.pageNum>0)
                    {
                        playbacksearchform.pageNum--;
                        DoSearch(evobj,htmlel,obj);
                    }else
                    {
                        oldAlert(GetDefaultLang("lang_isfirstpageerror","当前页为第一页！"),GetDefaultLang("lang_error22","错误"));
                    }
                }
            },{
                id:'SearchFileNext',
                text: GetDefaultLang("lang_nextpage",'下一页')
                ,handler:function(evobj,htmlel,obj)
                {//grid.haveNextPage           
                    var playform=GetPlayBackForm();
                    if(playform.isVisible())
                    {
                        oldAlert(GetDefaultLang("lang_closeerror20","请先关闭[录像文件回放]窗口再进行搜索"),GetDefaultLang("lang_searcherror21","错误操作"));
                        return ;
                    }         
                    if(playbacksearchform.haveNextPage)
                    {
                        playbacksearchform.pageNum++;
                        DoSearch(evobj,htmlel,obj);                     
                    }
                    else
                    {
                        oldAlert(GetDefaultLang("lang_islastpageerror","当前页为最后一页！"),GetDefaultLang("lang_;lang_error23","错误"));
                    }
                }
            }
            ]
        });
    }
   return playbacksearchform;
   
}








function WriteSearchTable()
{
        document.write('<table style="width:100%" >');
        document.write('<tr>'); 
        document.write('<td style="text-align:center;" >');
        document.write('<table style="text-align:center;width:100%;" >');
        document.write('<tr>');
        document.write('<td style="text-align:center;" ><object classid="clsid:'+__playbackclassid+'" codebase="'+__playerbackclasspath+'"  width="430px" height="340px" id="replay1" ></object></td>');
        document.write('<td style="width:5px" ></td>');
        document.write('<td style="text-align:center;" ><object classid="clsid:'+__playbackclassid+'" codebase="'+__playerbackclasspath+'"  width="430px" height="340px" id="replay2" ></object></td>');
        document.write('</tr>');
        document.write('<tr style="height:20px" >');
        document.write('<td style="text-align:left;" ><div  style="width:420px;overflow:hidden;display:inline;"  ><div  id="playfile_1" style="display:inline;width:9999px;overflow:hidden;" >&nbsp;</div></div></td>');
        document.write('<td style="width:5px" ></td>');
        document.write('<td style="text-align:left;" ><div style="text-align:left;width:420px;overflow:hidden;display:inline;"  ><div  id="playfile_2" style="text-align:left;display:inline;width:9999px;overflow:hidden;" >&nbsp;</div></div></td>');
        document.write('</tr>');
        document.write('</table>');
        document.write('</td>');
        document.write('</tr><tr><td style="text-align:center;" align="center"  >');
        document.write('<table><tr style="height:24px" >');
        document.write('<td style="width:80px;" >'+GetDefaultLang("lang_searchdevid","&nbsp;设备ID&nbsp;")+'</td><td  style="width:140px" >');
        document.write('<input maxlength="33"  style="width:140px"  id="selectdevid" autocomplete="off" class="input" name="selectdevid" /></td>');
        document.write('<td style="width:80px;" >'+GetDefaultLang("lang_serachvideosrc","录像来源")+'</td><td  style="width:140px" ><div class="select" style="display:inline" >');
        document.write('<div  style="display:inline" > ');
        document.write('<select id="plattype" onchange="ChangePlatType(this.value);" style="width:140px" name="plattype" >');
        document.write('<option value="0" >'+GetDefaultLang("lang_serachplatform","平台录像")+'</option>');
        document.write('<option value="2" >'+GetDefaultLang("lang_serachipcvideo","前端录像")+'</option>');
        document.write('<option value="1" >'+GetDefaultLang("lang_serachuploadp","前端上传录像")+'</option>');
        document.write('</select></div></div></td>');
        document.write('<td style="width:60px;" >'+GetDefaultLang("lang_serachstarttime","开始时间")+'</td><td  style="width:340px" >');
        document.write('<input  maxlength="21" style="width:140px" onclick="popUpCalendar(this,this,\'yyyy/mm/dd\');" value="'+GetTime(new Date(),true)+'" ');
        document.write(' id="startdate" autocomplete="off" class="input" name="startdate" />~~<input style="width:140px"  maxlength="21"');
        document.write(' onclick="popUpCalendar(this,this,\'yyyy/mm/dd\');"  value="'+GetTime(new Date(),false)+'" ');
        document.write('  id="enddate" autocomplete="off" class="input" name="enddate" /></td>');
        document.write('</tr>');
        document.write('<tr style="height:24px" >');
        document.write('<td style="width:80px;" >'+GetDefaultLang("lang_serachmediatype","录像类型")+'</td><td  style="width:140px" ><div class="select" style="display:inline" ><div  style="display:inline" >');
        document.write(' <select  id="recordtype" style="width:140px" name="recordtype" >');
        document.write('<option value="7" >'+GetDefaultLang("lang_serachall","全部")+'</option>');
        document.write('<option value="1" >'+GetDefaultLang("lang_serachtimerrecord","定时录像")+'</option>');
        document.write('<option value="2" >'+GetDefaultLang("lang_serachio","IO报警")+'</option>');
        document.write('</select></div></div></td>');
        document.write('<td style="width:80px;" >'+GetDefaultLang("lang_mediatype","媒体类型")+'</td><td  style="width:140px" ><div class="select" style="display:inline" >');
        document.write('<div  style="display:inline" > <select  disabled="true"  id="mediatype" onchange="ChangeMediaType(this,this.value);" ');
        document.write('style="width:140px" name="mediatype" >');
        //document.write('<option value="7" >'+GetDefaultLang("lang_allmedia","全部")+'</option>');
        document.write('<option value="1" >'+GetDefaultLang("lang_videoandaudio","音频和视频")+'</option>');
        document.write('<option value="4" >'+GetDefaultLang("lang_srcvideo","视频")+'</option>');
        document.write('<option value="2" >'+GetDefaultLang("lang_srcpic","图片")+'</option>');
        document.write('</select></div></div></td>');
        document.write('<td style="width:80px;" >'+GetDefaultLang("lang_diorcif","&nbsp;分辨率&nbsp;")+'</td><td  style="width:140px" >');
        document.write('<div class="select" style="display:inline" ><div  style="display:inline" > ');
        document.write('<select  disabled="true" id="streamType" style="width:140px" name="streamType" >');
        document.write('<option value="3" >'+GetDefaultLang("lang_allvideo","全部")+'</option>');
        document.write('<option value="1" >'+GetDefaultLang("lang_d1video","低分辨率")+'</option>');
        document.write('<option value="2" >'+GetDefaultLang("lang_cifvido","高分辨率")+'</option>');
        document.write('</select></div></div></td>');
        document.write('</tr>');
        document.write('</table> </td></tr></table>');
        document.write("<script>try{ChangePlatType($('plattype').value);}catch(eeee){}</" + "script>");
}