<?php 
defined('IN_DESTOON') or exit('Access Denied');
login();
require DT_ROOT.'/module/'.$module.'/common.inc.php';
$MG['jiaose_limit'] > -1 or dalert(lang('message->without_permission_and_upgrade'), 'goback');
require DT_ROOT.'/include/post.func.php';
require MD_ROOT.'/jiaose.class.php';
$do = new jiaose();
switch($action) {
	case 'add':
		if($MG['jiaose_limit']) {
			$r = $db->get_one("SELECT COUNT(*) AS num FROM {$DT_PRE}game_jiaose WHERE username='$_username'");
			if($r['num'] >= $MG['jiaose_limit']) dalert(lang($L['limit_add'], array($MG['jiaose_limit'], $r['num'])), 'goback');
		}
		if($submit) {
			if($do->pass($post)) {
				$post['username'] = $_username;
				$do->add($post);
				dmsg($L['op_add_success'], $MOD['linkurl'].'juese.php');
			} else {
				message($do->errmsg);
			}
		} else {
			$head_title = $L['jiaose_title_add'];
		}
	break;
	case 'edit':
		$id or message();
		$do->id = $id;
		$r = $do->get_one();
		if(!$r || $r['username'] != $_username) message();
		if($submit) {
			if($do->pass($post)) {
				$post['username'] = $_username;
				$do->edit($post);
				dmsg($L['op_edit_success'], $forward);
			} else {
				message($do->errmsg);
			}
		} else {
			extract($r);
			$head_title = $L['jiaose_title_edit'];
		}
	break;
	case 'delete':
		$id or message($L['jiaose_msg_choose']);
		$do->id = $id;
		$r = $do->get_one();
		if(!$r || $r['username'] != $_username) message();
		$do->delete($id);
		dmsg($L['op_del_success'], $forward);
	break;
	default:
		$condition = "username='$_username'";
		if($keyword) $condition .= " AND game_jiaose LIKE '%$keyword%'";
		$lists = $do->get_list($condition);
		$head_title = $L['jiaose_title'];
	break;
}

$r = $db->get_one("SELECT COUNT(*) AS num FROM {$DT_PRE}game_jiaose WHERE username='$_username'");
$limit_used = $r['num'];

$limit_free = $MG['jiaose_limit'] && $MG['jiaose_limit'] > $limit_used ? $MG['jiaose_limit'] - $limit_used : 0;
include template('juese', $module);
?>