-- vim:set ft=lua:
dofile(mg.script_name:gsub('[^\\/]*$','')..'util.lua')

ct=CreateContentBuilder(GZIP_THRESHOLD_BYTE)
ct:Append(DOCTYPE_HTML4_STRICT..[=[
<html lang="ja">
<head>
]=]..DefaultHeadContents()..[=[
<title>番組詳細 - EDCB</title>
</head>
<body>
<h1>番組詳細</h1>
<div id="main">
  <dl>
]=])

onid,tsid,sid,startTime=GetVarPastEventID(mg.request_info.query_string,'id')

edcb.htmlEscape=15
epgDate=0
et=edcb.EnumEventInfoArchive({{onid=onid,tsid=tsid,sid=sid}}, {startTime=os.date('!*t',startTime), durationSecond=1})
if et and #et~=0 then
  ct:Append('    <dt id="pginfo-term">番組情報</dt><dd id="pginfo-desc">'..DecorateProgramText(ConvertProgramText(et[1]))..'</dd>\n')
  --番組表のdateクエリは今日(深夜4時区切り)を基準(=0)とする
  epgDate=math.floor((startTime-4*3600)/(24*3600))-math.floor((os.time()+(9-4)*3600)/(24*3600))
end

ct:Append([=[
  </dl>
</div>
<div id="footer">
  <a href="epg.html?date=]=]..epgDate..[=[">番組表</a> <a href="epglist.html?id=]=]..onid..'-'..tsid..'-'..sid..[=[#now">リスト番組表</a>
</div>
<script type="text/javascript">
(function(){
  var desc=document.getElementById("pginfo-desc");
  if(desc&&desc.textContent){
    var a=document.createElement("a");
    a.download="a.program.txt";
    a.href=URL.createObjectURL(new Blob(["\ufeff",desc.textContent],{endings:"native",type:"text/plain"}));
    a.innerText="(DL)";
    document.getElementById("pginfo-term").appendChild(a);
  }
})();
</script>
</body>
</html>
]=])
ct:Finish()
mg.write(ct:Pop(Response(200,'text/html','utf-8',ct.len,ct.gzip)..'\r\n'))
