<?php
defined('PHP168_PATH') or die();
//如没终审权限，检查初审
$this_controller->check_action($ACTION) or message('', $this_module->U_controller .'-verify_first',0);

if(REQUEST_METHOD == 'GET'){

$MODEL = '';

if(isset($_REQUEST['model'])) $this_system->init_model();

$MODEL && load_language($this_module, $MODEL);

$page_url = $this_router .'-'. $ACTION .'?';

$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
$page = max($page, 1);
$cid = isset($_GET['cid']) ? intval($_GET['cid']) : 0;
$keyword = isset($_GET['keyword']) ? ltrim($_GET['keyword']) : '';
$keyword = $keyword ? $keyword : (isset($_GET['word']) ? ltrim($_GET['word']) : '');
if(isset($_GET['verified'])){
	$verified = intval($_GET['verified']);
	$T = $verified == 1 ? $this_module->main_table : $this_module->unverified_table;

}else{
	$verified = 1;
	$T = $this_module->main_table;
}
//如果是初审，去掉终审
if($verified == 0) unset($this_module->CONFIG['verify_acl'][1]);

if(!in_array($verified,array(0,1,2,3,66,-99))) message('no_privilege');
$class = array(
    '0' => '',
    '1'=> '',
    '2' => '',
    '3' => '',
    '66' => '',
    '99' => '',
);
$class[abs($verified)]='class="over"';
$page_url .= '&verified='. $verified;
$page_url .= '&model='. $MODEL;
$page_url .= '&cid='. $cid;
$page_url .= '&word='. urlencode($keyword);
$page_url .= '&page=?page?';
$select = select();
$my_category_to_verify = $this_controller->get_acl('my_category_to_verify');
$my_category_to_verify_first = $this_controller->get_acl('my_category_to_verify_first');

$category = &$this_system->load_module('category');
$category->get_cache();

if($verified == 1){
	if($MODEL){
		$select->from($this_module->table .' AS i', 'i.*');
	}else{
		$select->from($this_module->main_table .' AS i', 'i.*');
	}
}else{
	$select->from($T .' AS i', 'i.*');
	if($verified==3)
		$select->in('i.verified', array(0,2));
	else
		$select->in('i.verified', $verified);
}
$verified = abs($verified);
//array_intersect the same id
$array_intersect = array();

$show[0]=$show[1]=$show[2]=$show[3]=$show[66]=$show[99]=true;
if(isset($my_category_to_verify[0])){
	if($IS_FOUNDER){
		if($cid)$select->in('i.cid',$cid);
	}else{
		if(isset($my_category_to_verify_first[0])){
			$show[0] = $show[2] = false;
		}elseif(!empty($my_category_to_verify_first)){
			$array_intersect = array_keys($my_category_to_verify_first);
			if($verified==3) $select->in('i.cid',array_keys($array_intersect));
			if($verified==2) $select->in('i.cid',array_keys($array_intersect),true);
			if($verified==0) $select->in('i.cid',array_keys($array_intersect),true);
		}else{
			$show[0] = $show[3] = false;
		}
	}
}elseif(!empty($my_category_to_verify)){
	if($IS_FOUNDER){
		if($cid)$select->in('i.cid',$cid);
	}else{
		if(isset($my_category_to_verify_first[0])){
			$array_intersect = array_keys($my_category_to_verify);
			if($verified == 0 ||  $verified == 2)$select->in('i.cid', $array_intersect,true);
			if(in_array($verified,array(1,3,66))) $select->in('i.cid', $array_intersect);
		}elseif(!empty($my_category_to_verify_first)){
			$array_intersect = array_intersect(array_keys($my_category_to_verify),array_keys($my_category_to_verify_first));
			$array_merge = array_unique(array_merge(array_keys($my_category_to_verify),array_keys($my_category_to_verify_first)));
			$array_diff_verify = array_diff(array_keys($my_category_to_verify),array_keys($my_category_to_verify_first));
			$array_diff_verify_first = array_diff(array_keys($my_category_to_verify_first),array_keys($my_category_to_verify));

			if(empty($array_intersect)){
				$show[3] = false;
				if($verified==0) $select->in('i.cid',array_keys($my_category_to_verify_first));
				if(in_array($verified,array(1,2,66))) $select->in('i.cid',array_keys($my_category_to_verify));
				if($verified==99) $select->in('i.cid',$array_merge);
			}else{
				if(in_array($verified,array(1,66,99))) $select->in('i.cid',array_keys($my_category_to_verify));
				if($verified==3) $select->in('i.cid',$array_intersect);
				if(empty($array_diff_verify_first)) $show[0] = false;
				if(empty($array_diff_verify)) $show[2] = false;
				if($verified==0 && !empty($array_diff_verify_first)) $select->in('i.cid',$array_diff_verify_first);
				if($verified==2 && !empty($array_diff_verify)) $select->in('i.cid',$array_diff_verify);
			}
		}else{
			$show[0] = $show[3] = false;
			$select->in('i.cid', array_keys($my_category_to_verify));
		}
	}
}else{
	if($IS_FOUNDER){
		if($cid)$select->in('i.cid',$cid);
	}else{
		$show[2] = $show[3] = false;
		if(isset($my_category_to_verify_first[0])){
			if($cid)$select->in('i.cid',$cid);
		}elseif(!empty($my_category_to_verify_first)){
			$select->in('i.cid', array_keys($my_category_to_verify_first));
		}else{
			message('no_privilege');
		}
	}
}

if($IS_FOUNDER)	$show[0] = $show[2] = $show[3] = $show[66] = true;
if(!$show[abs($verified)]) message('no_privilege');
/*
	if(isset($my_category_to_verify[0]) || $IS_FOUNDER){
		if($cid)$select->in('i.cid',$cid);
	}elseif(!empty($my_category_to_verify)){
		$my_category_to_verify =array_keys($my_category_to_verify);
		if($cid && in_array($cid, $my_category_to_verify)){
			$select->in('i.cid', $cid);
		}else{
			$select->in('i.cid', $my_category_to_verify);
		}
	}else{
			message('no_privilege');
	}
*/

$select->order('i.id DESC');

//所有模型
$models = $this_system->get_models();

$count = 0;
//echo $select->build_sql();
//取数据
$list = $core->list_item(
	$select,
	array(
		'page' => &$page,
		'count' => &$count,
		'page_size' => 20,
		'ms' => 'master'
	)
);
foreach($list as $k => $v){
		$list[$k]['title']=p8_cutstr($list[$k]['title'],66);
		$list[$k]['url'] = p8_url($this_module, $v, 'view');
		$list[$k]['url'] .= $verified == 1? '' : '?verified=0';
		if($verified != 1){
			$data = unserialize($v['data']);
			$list[$k]['verifier'] = $data['main']['verifier'];
		}
}
//分页
$pages = list_page(array(
	'count' => $count,
	'page' => $page,
	'page_size' => 20,
	'url' => $page_url
));

	include template($this_module, 'verify');

}else if(REQUEST_METHOD == 'POST'){
	//只提供AJAX调用
//id: id, value: value,verified:verified
//$_POST['id'] = 1254;
//$_POST['value'] = 2;
//$_POST['verified'] = 0;
	$id = isset($_POST['id']) ? $_POST['id'] : array();
	$value = isset($_POST['value']) ? intval($_POST['value']) : 0;
	$id or message('no_such_item');
	
	$id = filter_int($id);
	$id or exit('[]');
	
	$verified = isset($_POST['verified']) && $_POST['verified'] == 1 ? 1 : 0;
	//退稿理由
	$push_back_reason = isset($_POST['push_back_reason']) ? html_entities(from_utf8($_POST['push_back_reason'])) : '';
	
	$T = $value == 1 ? $this_module->unverified_table : $this_module->main_table;
	$T = $verified ? $this_module->main_table : $this_module->unverified_table;
	
	$cond = $T .'.id IN ('. implode(',', $id) .')';
	if($verified == 0){
        $ret = $this_controller->verify_first(array(
            'where' => $cond,
            'value' => $value,
            'verified' => $verified,
            'push_back_reason' => $push_back_reason
        ))or exit('[]');
    }else {
        $ret = $this_controller->verify(array(
            'where' => $cond,
            'value' => $value,
            'verified' => $verified,
            'push_back_reason' => $push_back_reason
        )) or exit('[]');
    }
	exit(jsonencode($ret));
}
