<?php
if($_POST){
  InitGP('action','G');
  InitGP('aid','P');
  switch($action){
    case'memory':
      if($_SESSION['aid']==$aid){
        $_SESSION['aid']='';
        echo'记忆已取消';
      }else{
        $_SESSION['aid']=$aid;
        echo'记忆成功';
      }
      break;
    case'paste':
      if(!$_SESSION['qid'])exit('请先记忆问题');
      DB()->replace('faq',array(
        'aid'=>$aid,
        'qid'=>$_SESSION['qid'],
      ));
      echo'添加成功';
      break;
    case'delete':
      DB()->delete('answer',"aid='$aid'");
      DB()->delete('faq',"aid='$aid'");
      echo'Y';
      break;
  }
  exit();
}
theme_header();
$ot->borderA('#content2','box2');
$ot->addhead('答案检索',array('editanswer'=>'添加新答案'));
function questionNum($rs){
  return DB()->rows('faq',"aid='$rs[aid]'").' <a href="javascript:viewquestion('.$rs['aid'].')">查看</a>';
}
function myBtns($rs){
  if($_SESSION['qid']){
    $co=DB()->select('question','content',"qid='$_SESSION[qid]'");
    $paste_question='<a href="javascript:paste('.$rs['aid'].')" style="display:block;color:#ffff00;background:#ff0000;padding:0 3px">将记忆的问题“'.$co.'”添加到这个答案下</a>';
  }
  if($_SESSION['aid']==$rs['aid']){
    $paste_btn='<a href="javascript:memory('.$rs['aid'].')" style="color:red">已记忆</a>';
  }else{
    $paste_btn='<a href="javascript:memory('.$rs['aid'].')" style="color:green">记忆答案</a>';
  }
  return<<<ONEZ
  $paste_btn
  $paste_question
  <a href="javascript:addquestion($rs[aid])">添加问题</a>
  <a href="javascript:edit($rs[aid])">编辑</a>
  <a href="javascript:del($rs[aid])">删除</a>
ONEZ;
}
InitGP('keyword','G');
$html=<<<ONEZ
<div style="top:0px;padding:5px 10px;text-align:right;position:relative">
<form action="$PHP_SELF">
<input name="keyword" type="text" class="text f-input" style="width:160px;height:21px" value="$keyword" />
<input type="submit" value="搜索" name="commit" id="submit" class="formbutton"/>
<input name="a" type="hidden" value="$a" />
<input name="b" type="hidden" value="$b" />
</form>
</div>
ONEZ;
$ot->addhtml($html);
$ot->setlist(
  DB()->page('answer','*',"content like '%$keyword%' order by aid desc",20,'?a='.$a.'&keyword='.$keyword.'&b='.$b),
  'aid',
  array(
    array(
      'label'=>'编号',
      'token'=>'aid',
    ),
    array(
      'label'=>'答案',
      'token'=>'content',
    ),
    array(
      'label'=>'自定义关键词',
      'token'=>'keywords',
    ),
    array(
      'label'=>'优先级',
      'token'=>'asknum',
    ),
    array(
      'label'=>'问题数量',
      'token'=>'questionNum',
      'type'=>'func',
    ),
    array(
      'label'=>'操作',
      'token'=>'myBtns',
      'type'=>'func',
    ),
  )
);
$ot->borderB(1);
$ot->flush();
echo<<<ONEZ
<script type="text/javascript">
function memory(aid){
  $.post('?a=$a&b=$b&action=memory',{aid:aid},function(s){
    location.reload();
  });
}
function paste(aid){
  $.post('?a=$a&b=$b&action=paste',{aid:aid},function(s){
    alert(s);
    location.reload();
  });
}
function addquestion(aid){
  location.href='?a=$a&b=editquestion&aid='+aid;
}
function viewquestion(aid){
  window.open('?a=$a&b=question2&aid='+aid,'','');
}
function edit(aid){
  location.href='?a=$a&b=editanswer&aid='+aid;
}
function del(aid){
  if(confirm('您确定要删除这条答案吗？')){
    $.post('?a=$a&b=$b&action=delete',{aid:aid},function(s){
      if(s=='Y'){
        location.reload();
      }else{
        alert(s);
      }
    });
  }
}
</script>
ONEZ;
theme_footer();
?>