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

if GetVarInt(mg.request_info.query_string,'json',0,1)==1 then
  ResponseJson(edcb.EnumAutoAdd(),mg.request_info.query_string)
else
  edcb.htmlEscape=15
  a=edcb.EnumAutoAdd()
  ct=CreateContentBuilder()
  ct:Append('<?xml version="1.0" encoding="UTF-8" ?><entry><total>'..#a..'</total><index>0</index><count>'..#a..'</count><items>\r\n')
  for i,v in ipairs(a) do
    ct:Append('<autoaddinfo><ID>'
      ..v.dataID..'</ID><addCount>'
      ..v.addCount..'</addCount><searchsetting><disableFlag>'
      ..(ParseAndKey(v.searchInfo.andKey).disableFlag and 1 or 0)..'</disableFlag><caseFlag>'
      ..(ParseAndKey(v.searchInfo.andKey).caseFlag and 1 or 0)..'</caseFlag><andKey>'
      ..ParseAndKey(v.searchInfo.andKey).andKey..'</andKey><notKey>'
      ..ParseNotKey(v.searchInfo.notKey).notKey..'</notKey><note>'
      ..ParseNotKey(v.searchInfo.notKey).note..'</note><regExpFlag>'
      ..(v.searchInfo.regExpFlag and 1 or 0)..'</regExpFlag><titleOnlyFlag>'
      ..(v.searchInfo.titleOnlyFlag and 1 or 0)..'</titleOnlyFlag><aimaiFlag>'
      ..(v.searchInfo.aimaiFlag and 1 or 0)..'</aimaiFlag><notContetFlag>'
      ..(v.searchInfo.notContetFlag and 1 or 0)..'</notContetFlag><notDateFlag>'
      ..(v.searchInfo.notDateFlag and 1 or 0)..'</notDateFlag><freeCAFlag>'
      ..v.searchInfo.freeCAFlag..'</freeCAFlag><chkRecEnd>'
      ..(v.searchInfo.chkRecEnd and 1 or 0)..'</chkRecEnd><chkRecDay>'
      ..v.searchInfo.chkRecDay..'</chkRecDay><chkRecNoService>'
      ..(v.searchInfo.chkRecNoService and 1 or 0)..'</chkRecNoService><chkDurationMin>'
      ..v.searchInfo.chkDurationMin..'</chkDurationMin><chkDurationMax>'
      ..v.searchInfo.chkDurationMax..'</chkDurationMax>')
    for j,w in ipairs(v.searchInfo.contentList) do
      ct:Append('<contentList><content_nibble>'
        ..w.content_nibble..'</content_nibble><user_nibble>'
        ..w.user_nibble..'</user_nibble></contentList>')
    end
    for j,w in ipairs(v.searchInfo.dateList) do
      ct:Append('<dateList><startDayOfWeek>'
        ..w.startDayOfWeek..'</startDayOfWeek><startHour>'
        ..w.startHour..'</startHour><startMin>'
        ..w.startMin..'</startMin><endDayOfWeek>'
        ..w.endDayOfWeek..'</endDayOfWeek><endHour>'
        ..w.endHour..'</endHour><endMin>'
        ..w.endMin..'</endMin></dateList>')
    end
    for j,w in ipairs(v.searchInfo.serviceList) do
      ct:Append('<serviceList><onid>'
        ..w.onid..'</onid><tsid>'
        ..w.tsid..'</tsid><sid>'
        ..w.sid..'</sid></serviceList>')
    end
    ct:Append('</searchsetting>'
      ..XmlRecSetting(v.recSetting)
      ..'</autoaddinfo>\r\n')
  end
  ct:Append('</items></entry>')
  ct:Finish()
  mg.write(ct:Pop(Response(200,'text/xml','utf-8',ct.len)..'\r\n'))
end
