<?php
if($_POST){
  InitGP('action','G');
  InitGP('qid','P');
  switch($action){
    case'default':
      DB()->update('question',array('isdefault'=>'1'),"qid='$qid'");
      echo'Y';
      break;
    case'cancel':
      DB()->update('question',array('isdefault'=>'0'),"qid='$qid'");
      echo'Y';
      break;
    case'memory':
      if($_SESSION['qid']==$qid){
        $_SESSION['qid']='';
        echo'记忆已取消';
      }else{
        $_SESSION['qid']=$qid;
        echo'记忆成功';
      }
      break;
    case'paste':
      if(!$_SESSION['aid'])exit('请先记忆答案');
      DB()->replace('faq',array(
        'qid'=>$qid,
        'aid'=>$_SESSION['aid'],
      ));
      echo'添加成功';
      break;
    case'delete':
      DB()->delete('question',"qid='$qid'");
      DB()->delete('faq',"qid='$qid'");
      echo'Y';
      break;
  }
  exit();
}
theme_header();
$ot->borderA('#content2','box2');
$ot->addhead('问题检索',array('editquestion'=>'添加新问题'));
function answerNum($rs){
  return DB()->rows('faq',"qid='$rs[qid]'").' <a href="javascript:viewanswer('.$rs['qid'].')">查看</a>';
}
function myBtns($rs){
  if($_SESSION['aid']){
    $co=DB()->select('answer','content',"aid='$_SESSION[aid]'");
    $paste_answer='<a href="javascript:paste('.$rs['qid'].')" style="display:inline-block;color:#ffff00;background:#ff0000;padding:0 3px">将记忆的答案“'.$co.'”添加到这个问题下</a>';
  }
  if($_SESSION['qid']==$rs['qid']){
    $paste_btn='<a href="javascript:memory('.$rs['qid'].')" style="color:red">已记忆</a>';
  }else{
    $paste_btn='<a href="javascript:memory('.$rs['qid'].')" style="color:green">记忆问题</a>';
  }
  if($rs['isdefault']){
    $btn_default='<a href="javascript:setDefault('.$rs['qid'].')">取消默认</a>';
  }else{
    $btn_default='<a href="javascript:setDefault('.$rs['qid'].')">设为默认</a>';
  }
  return<<<ONEZ
  $paste_btn
  $paste_answer
  $btn_default
  <a href="javascript:addanswer($rs[qid])">添加答案</a>
  <a href="javascript:edit($rs[qid])">编辑</a>
  <a href="javascript:del($rs[qid])">删除</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('question','*',"content like '%$keyword%' order by qid desc",20,'?a='.$a.'&keyword='.$keyword.'&b='.$b),
  'qid',
  array(
    array(
      'label'=>'编号',
      'token'=>'qid',
    ),
    array(
      'label'=>'问题',
      'token'=>'content',
    ),
    array(
      'label'=>'自定义关键词',
      'token'=>'keywords',
    ),
    array(
      'label'=>'优先级',
      'token'=>'asknum',
    ),
    array(
      'label'=>'答案数量',
      'token'=>'answerNum',
      'type'=>'func',
    ),
    array(
      'label'=>'操作',
      'token'=>'myBtns',
      'type'=>'func',
    ),
  )
);
$ot->borderB(1);
$ot->flush();
echo<<<ONEZ
<script type="text/javascript">
function setDefault(qid){
  $.post('?a=$a&b=$b&action=default',{qid:qid},function(s){
    if(s=='Y'){
      location.reload();
    }else{
      alert(s);
    }
  });
}
function cancel(qid){
  $.post('?a=$a&b=$b&action=cancel',{qid:qid},function(s){
    if(s=='Y'){
      location.reload();
    }else{
      alert(s);
    }
  });
}
function memory(qid){
  $.post('?a=$a&b=$b&action=memory',{qid:qid},function(s){
    location.reload();
  });
}
function paste(qid){
  $.post('?a=$a&b=$b&action=paste',{qid:qid},function(s){
    alert(s);
    location.reload();
  });
}
function addanswer(qid){
  location.href='?a=$a&b=editanswer&qid='+qid;
}
function viewanswer(qid){
  window.open('?a=$a&b=answer2&qid='+qid,'','');
}
function edit(qid){
  location.href='?a=$a&b=editquestion&qid='+qid;
}
function del(qid){
  if(confirm('您确定要删除这条问题吗？')){
    $.post('?a=$a&b=$b&action=delete',{qid:qid},function(s){
      if(s=='Y'){
        location.reload();
      }else{
        alert(s);
      }
    });
  }
}
</script>
ONEZ;
theme_footer();
?>