#include <WindowsAPI.js>

Screen=new dobject("TScreen");

function SetTitle(title)
{
  browser.SetTitle("做错之题管理 - " + title);
}

function Browser_OnNavigateComplete2(url)
{
  browser.InvokeFunction("ClearSubjects");
  DisplaySubjectItems();
}
browser=new dobject("MiniBrowser");//browser for google
browser.OnClose=function(){System.Exit();};
browser.OnNavigateComplete2=Browser_OnNavigateComplete2;

browser.SetPosition(Screen.Width+220,-220,10,10);
browser.SetNoMenu();
browser.Show();

w=820;
h=700;
//划定边界，去掉windows常规标题栏
hParentRgn=CreateRoundRectRgn(18,49,w-17,h-11,5,5);
SetWindowRgn(browser.GetHandle(),hParentRgn,true);
DeleteObject(hParentRgn);

browser.SetPosition((Screen.WorkAreaWidth-w)/2,(Screen.WorkAreaHeight-h)/2,w,h);

SetTitle("说明");

browser.Navigate2(System.GetWidgetPath()+"default.htm");

//引入数据库
System.ExecuteScriptFile(System.GetAppPath()+"SystemDB.php","PHP");

db2=new dobject("SystemDatabaseManager");
db2.open(System.GetDataPath()+"WrongSubject.db");
//craete tables
q="CREATE TABLE WrongSubject(idx int, title varchar(255),dossier_title varchar(255), subject_name varchar(50), xueli varchar(10), grade varchar(14), term varchar(12), test_type varchar(20), dt varchar(22), short_desc TEXT, wrong_desc TEXT, answer_desc TEXT, redo_record TEXT, finished_flag int, file_name varchar(255));";
db2.query(q);

var last_idx=-1;
var last_title="";
var last_dossier_title="";
var last_subject_name="";
var last_xueli="";
var last_grade="";
var last_term="";
var last_test_type="";
var last_dt="";
var last_short_desc="";
var last_wrong_desc="";
var last_answer_desc="";
var last_redo_record="";
var last_finished_flag="";

vcl=new dobject("vcl");

function getNumberString(num)
{
  num=Math.floor(num);
  if(num<10)
    return "0"+num;
  return ""+num;
}
function InsertRecord(fn,title)
{
 var idx=0;
 q="SELECT MAX(idx) as m FROM WrongSubject";
 db2.query(q);
 if(db2.fetch())
 {
   idx=db2.get_array_data("m");
 }
 idx++;
 
 dt = new Date();
 dts=dt.getFullYear()+"-"+getNumberString(dt.getMonth()+1)+"-"+getNumberString(dt.getDate());
 
 file_name=replaceAll(fn,"\\","/");

 q="INSERT INTO WrongSubject(idx,title,dossier_title,subject_name,xueli,grade,term,test_type,file_name,dt)VALUES("
 q+=idx+",'";
 q+=title+"','";
 q+=last_dossier_title+"','";
 q+=last_subject_name+"','";
 q+=last_xueli+"','";
 q+=last_grade+"','";
 q+=last_term+"','";
 q+=last_test_type+"','";
 q+=file_name+"','";
 q+=dts+"'";
 q+=")";

 db2.query(q);

 return idx;
}
function QueryRecord(idx)
{
  q="SELECT * FROM WrongSubject WHERE idx="+idx;
  db2.query(q);
  if(db2.fetch())
  {
    return true;
  }
  return false;
}
function GetRecordField(field_name)
{
  var v=db2.get_array_data(field_name);
  if(v==null)
    return "";
  return ""+v;//change to string
}
function DeleteRecord(idx)
{
  q="DELETE FROM WrongSubject WHERE idx="+idx;
  db2.query(q);
}
function OpenSubjectFile(idx)
{
  QueryRecord(idx);
  fn=GetRecordField("file_name");
  ShellExecute(browser.GetHandle(),"open",fn,null,null,SW_SHOWNORMAL);
}

function replaceAll(text, s, t) {
  var ss=s;
  var tt=t;
  var texttext=text;
  var ret;
  
  ret="";
  idx=texttext.indexOf(ss);
  while(idx>=0)
  {
    ret+=texttext.substring(0,idx);
    ret+=tt;
    texttext=texttext.substring(idx+ss.length,texttext.length);
    idx=texttext.indexOf(ss);
    
    if(idx%5==0)Sleep(0);//牺牲性能，却能防止90%以上的假死
  }
  ret+=texttext;
  Sleep(0);//牺牲性能，却能防止90%以上的假死
  return ret;
};

function xmlencode(string) {
  var str=string;
  str=replaceAll(str,'&','&amp;');
  str=replaceAll(str,'<','&lt;');
  str=replaceAll(str,'>','&gt;');
  str=replaceAll(str,'\'','&apos;');
  str=replaceAll(str,'"','&quot;');
  return str;
}
function xmldencode(string) {
  var str=string;
  str=replaceAll(str,'&amp;','&');
  str=replaceAll(str,'&lt;','<');
  str=replaceAll(str,'&gt;','>');
  str=replaceAll(str,'&apos;','\'');
  str=replaceAll(str,'&quot;','"');
  return str;
}
function UpdateRecord(idx,title,dossier_title,subject_name,xueli,grade,term,test_type,dt,short_desc,wrong_desc,answer_desc,redo_record,finished_flag)
{
  title=xmlencode(title);
  dossier_title=xmlencode(dossier_title);
  subject_name=xmlencode(subject_name);
  xueli=xmlencode(xueli);
  grade=xmlencode(grade);
  term=xmlencode(term);
  test_type=xmlencode(test_type);
  dt=xmlencode(dt);
  short_desc=xmlencode(short_desc);
  wrong_desc=xmlencode(wrong_desc);
  answer_desc=xmlencode(answer_desc);
  redo_record=xmlencode(redo_record);

  q="UPDATE WrongSubject SET ";
  q+="title='"+title+"',";
  q+="dossier_title='"+dossier_title+"',";
  q+="subject_name='"+subject_name+"',";
  q+="xueli='"+xueli+"',";
  q+="grade='"+grade+"',";
  q+="term='"+term+"',";
  q+="test_type='"+test_type+"',";
  q+="dt='"+dt+"',";
  q+="short_desc='"+short_desc+"',";
  q+="wrong_desc='"+wrong_desc+"',";
  q+="answer_desc='"+answer_desc+"',";
  q+="redo_record='"+redo_record+"',";
  q+="finished_flag="+finished_flag+"";
  q+=" WHERE idx="+idx;
  db2.query(q);

  last_idx=idx;
  last_title=title;
  last_dossier_title=dossier_title;
  last_subject_name=subject_name;
  last_xueli=xueli;
  last_grade=grade;
  last_term=term;
  last_test_type=test_type;
  last_dt=dt;
  last_short_desc=short_desc;
  last_wrong_desc=wrong_desc;
  last_answer_desc=answer_desc;
  last_redo_record=redo_record;
  last_finished_flag=finished_flag;
}

function DisplaySubjectItems()
{
  q="SELECT * FROM WrongSubject ORDER BY dt DESC,idx DESC";
  db2.query(q);
  var first_idx=-1;
  while(db2.fetch())
  {
    idx=db2.get_array_data("idx");
    if(first_idx<0)
    {
      first_idx=idx;
      last_idx=db2.get_array_data("idx");
      last_title=db2.get_array_data("title");
      last_dossier_title=db2.get_array_data("dossier_title");if(last_dossier_title==null)last_dossier_title="";
      last_subject_name=db2.get_array_data("subject_name");if(last_subject_name==null)last_subject_name="";
      last_xueli=db2.get_array_data("xueli");if(last_xueli==null)last_xueli="";
      last_grade=db2.get_array_data("grade");if(last_grade==null)last_grade="";
      last_term=db2.get_array_data("term");if(last_term==null)last_term="";
      last_test_type=db2.get_array_data("test_type");if(last_test_type==null)last_test_type="";
      last_dt=db2.get_array_data("dt");if(last_dt==null)last_dt="";
      last_short_desc=db2.get_array_data("short_desc");if(last_short_desc==null)last_short_desc="";
      last_wrong_desc=db2.get_array_data("wrong_desc");if(last_wrong_desc==null)last_wrong_desc="";
      last_answer_desc=db2.get_array_data("answer_desc");if(last_answer_desc==null)last_answer_desc="";
      last_redo_record=db2.get_array_data("redo_record");if(last_redo_record==null)last_redo_record="";
      last_finished_flag=db2.get_array_data("finished_flag");
    }
    title=db2.get_array_data("title");
    browser.InvokeFunction("AddSubject",idx,title);
  }
  
  if(first_idx>0)
  {
    browser.InvokeFunction("HilightSubject",first_idx);//焦点在第一条
  }
}
function DragTitle()
{
  ReleaseCapture();
  PostMessage(browser.GetHandle(),WM_SYSCOMMAND,SC_MOVE+HTCAPTION,0);
}
