<?php
if (!defined('MOD')) die("access die");
if (AJAX) {
	$pageno = intval($_GET['pageno']) > 0 ? intval($_GET['pageno']) : 1;
	$pagesize = intval($_GET['pagesize']) > 0 ? intval($_GET['pagesize']) : 15;
	$keyword = trim(urldecode($_GET['keyword']));
	$where = '';
	if($keyword){
		if(intval($keyword) > 0){
			$id = intval($keyword);
			$where = " and a.id = '{$id}' ";
		}else{
			$kid = qdb::queryAlone("select kid from " . TABLEPRE . "tag where name = '{$keyword}' ");
			$where = " and a.kid = '{$kid}' ";
		}
	}
	$sql = "select count(*) from " . TABLEPRE . "tag_mapping as a left join " . TABLEPRE . "tag as t on a.kid=t.kid where a.channelid = '{$channelid}' {$where}";
	$total = qdb :: QueryAlone($sql);
	$pages = ceil($total / $pagesize) == 0 ? 1 : ceil($total / $pagesize);
	if ($pageno > $pages) $pageno = $pages ;
	$startid = ($pageno-1) * $pagesize;
	$mod = $_MODULE[$channelselect]['table_name'];
	$sql = "select a.tid, a.id, a.kid, t.name, t.rank, d.subject from " . TABLEPRE . "tag_mapping as a left join " . TABLEPRE . "tag as t on a.kid=t.kid left join " . TABLEPRE . "{$mod}_{$channelid} as d on a.id = d.id where a.channelid = '{$channelid}'  {$where}";
	$list = qdb :: fetchAll("{$sql} limit {$startid}, {$pagesize}");
	if (!empty($list)) {
		$data = array('status' => 200 , 'total' => $total, 'pagesize' => $pagesize, 'message' => $list);
	} else {
		$data = array('status' => 404 , 'message' => array(), 'total' => 0, 'pagesize' => $pagesize);
	} 
	echo qbase :: xmlPrint(qbase :: jsonencode($data));
	exit();
} 
?>