<%
__header("Content-Type","text/html; charset=gb2312");
var serr="{\"op\":\"op err\"}";
if (op=="")
  write(serr);
else
{
var sql;
var iupd = -1;
db_Open("/NandFlash/dbCfg.db3");
if (op=="qry")
  db_QryAdv("select * from tbSMDef");
else if (op=="del")
  iupd=db_UpdAdv("delete from tbSMDef where Type="+type+" and SID="+sid);
else if (op=="delt")
{
    if (db_QryInt("select count(*) from tbSM where Type="+type+") and State=0",1)==0)
	{
		iupd=db_UpdAdv("delete from tbSMDef where Type="+type);
		iupd=db_UpdAdv("delete from tbSMDefVal where Type="+type);
	}
}
else if (op=="delts")
{
    if (db_QryInt("select count(*) from tbSM where Type in (select Type from tbSM where SID="+sid+") and State=0",1)==0)
	{
		iupd=db_UpdAdv("delete from tbSMDef where Type in (select Type from tbSM where SID="+sid+")");
		iupd=db_UpdAdv("delete from tbSMDefVal where SID="+sid);
	}
}
else if (op=="addfld")
{
  if (db_QryInt("select count(SID) from tbSMDef where Type="+type+" and Fld="+fld,1)==0)
    iupd=db_UpdAdv("insert into tbSMDef values("+type+",0,"+fld+",'',1,"+Note+",0,"+vfactor+","+vmode+","+vref+","+vtip+","+vref2+","+vtip2+",0,NULL)", 1);
  else
    iupd=65535;
}
else if (op=="delfld")
  iupd=db_UpdAdv("delete from tbSMDef where Type="+type+" and Fld="+fld);
else if (op=="qryby")
  db_QryAdv("select * from tbSMDef where Type="+type+" and SID=0 order by FldDept,Fld", type);
else if (op=="qryval")
  db_QryAdv("select * from tbSMDefVal where Type="+type+" and SID="+sid+" order by Fld", sid);
else if (op=="upd")
{
  sql = "set vmode="+vmode+",vref="+vref+",vref2="+vref2+",ASubSys="+asubsys+",ANote='"+anote+"' where ";
  if (isdef==0)
  {
    if (db_QryInt("select count(SID) from tbSMDefVal where SID="+sid,1)==0)
      db_UpdAdv("insert into tbSMDefVal select Type,"+sid+",Fld,vmode,vref,vref2,ASubSys,'' from tbSMDef where Type="+type+" and Fld>0");
    sql = "update tbSMDefVal "+sql;
    sql = sql+"Type="+type+" and SID="+sid+" and Fld="+fld;
  }
  else
  {
    db_UpdAdv("delete from tbSMDefVal where SID="+sid);
    sql = "update tbSMDef "+sql;
    sql = sql+"Type="+type+" and SID=0 and Fld="+fld;
  }
  iupd=db_UpdAdv(sql,1);
}
else if (op=="upd2")
{
  sql = "update tbSMDef set "+setval+" where Type="+type+" and Fld="+fld;
  iupd=db_UpdAdv(sql,1);
}
else if (op=="updd")
{
  db_UpdAdv("delete from tbSMDefVal where SID="+sid,1);
  iupd=db_UpdAdv("update tbSMDef set "+setval+" where Type="+type+" and Fld="+fld,1);
}
else if (op=="chkx")
{
  iupd=db_QryInt("select count(*) from tbSMDefVal where SID="+sid,1);
  if (iupd==0)
    iupd=db_UpdAdv("insert into tbSMDefVal select Type,"+sid+",Fld,vmode,vref,vref2,0,'' from tbSMDef where Fld>0 and vmode>=0 and Type="+type,1);
}
else if (op=="updx")
{
  db_UpdAdv("delete from tbSMDefVal where SID="+sid+" and Fld="+fld,1);
  sql="insert into tbSMDefVal values("+setval+")";
  iupd=db_UpdAdv(sql,1);
}
if (op=="updalm")
{  
     sql = "update tbSMDefVal set vmode=10, vref=0, vref2=200, ASubSys=0, ANote='' where SID="+lsid+" and Fld="+lch;
     iupd=db_UpdAdv(sql,1);
     sql = "update tbSMDefVal set "+insval+" where SID="+nsid+" and Fld="+nch;
     iupd=db_UpdAdv(sql,1);
}
if (op=="updDef")
{  
  db_UpdAdv("delete from tbAICfg where Type="+AType+" and Xval="+Xvalue+" and Yval="+Yvalue,1);
  sql="insert into tbAICfg values("+insval2+","+AType+","+tag+","+Xvalue+","+Yvalue+","+other+")";
  iupd=db_UpdAdv(sql,1);
}
if (op=="updAIdel")
{  
  iupd=db_UpdAdv("delete from tbAICfg where Type="+AType+" and Xval="+Xvalue+" and Yval="+Yvalue,1); 
}
if (iupd<0 && iupd!=-1)
  write(serr);
else if (iupd==65535)
  write(serr);
else if (iupd>=0)
{
  write("{\"op\":\"ok\"}");
}
db_Close();
}
%>