<?php
/*
* Copyright © 2010-2012 MLECMS. All Rights Reserved.
* @Multi-Language Enterprise Content Management System
* This Is Not a Freeware, Use Is Subject To License Terms.
*/
require_once(dirname(__FILE__).'/header.php');
$del_id = is_numeric($_GET['del']) ? array($_GET['del']) : $_POST['del'];
if(is_array($del_id)){
	if($db->delete('order',$del_id)){
		admin::logs(4,$language['page']['operation'][3].'，'.$language['common']['successful'].'(id:'.implode(",",$del_id).')');
		msgbox('',durl('','','del')); 
	} else { 
		admin::logs(4,$language['page']['operation'][3].'，'.$language['common']['failed'].'(id:'.implode(",",$del_id).')'); 
		msgbox($language['common']['failed']);	
	}
}
function order_list($sort = 0,$filter = 0,$keyword = ''){
	global $db,$_GET;
	$sql = "SELECT a.*,b.`username` FROM `{$db->prefix}order` a LEFT JOIN `{$db->prefix}members` b ON a.`uid` = b.`id` WHERE a.`lang` = '".LANG."' ";
	empty($keyword) or $sql .= " && (a.`oid` LIKE '%{$keyword}%' || a.`amount` LIKE '%{$keyword}%' || a.`waybill` LIKE '%{$keyword}%' || a.`consignee` LIKE '%{$keyword}%' || a.`email` LIKE '%{$keyword}%' || a.`address` LIKE '%{$keyword}%' || a.`tel` LIKE '%{$keyword}%' || a.`mobile` LIKE '%{$keyword}%' || b.`username` LIKE '%{$keyword}%') ";
	switch($filter){
		case 0 : break;
		case 1 : $sql .= "&& a.`status` = '0' "; break;
		case 2 : $sql .= "&& a.`status` = '1' "; break;
		case 3 : $sql .= "&& a.`status` = '2' "; break;
		case 4 : $sql .= "&& a.`status` = '3' "; break;
		case 5 : $sql .= "&& a.`status` = '4' "; break;
		case 6 : $sql .= "&& a.`status` = '-1' "; break;
		case 7 : $sql .= "&& a.`status` = '-2' "; break;
		case 8 : $sql .= "&& a.`status` = '-3' "; break;
		default: break;
	}
	$limit = $_SESSION['admin']['limit'];
	is_numeric($limit) or $limit = 20; 
	$total = $db->query(str_replace('a.*,b.`username`','count(*)',$sql),1,0);
	$total = $total[0];		
	$total_page = ceil($total / $limit);		
	$page = is_numeric($_GET['page']) ? $_GET['page'] : 1;
	$page > $total_page && $page = $total_page;	
	$page < 1 && $page = 1;
	$start = $limit * ($page - 1);		
	switch($sort){
		case 0 : $sql .= "ORDER BY a.`addtime` DESC,a.`id` DESC "; break;
		case 1 : $sql .= "ORDER BY a.`addtime` ASC "; break;
		case 2 : $sql .= "ORDER BY a.`oid` DESC,a.`id` DESC "; break;
		case 3 : $sql .= "ORDER BY a.`oid` ASC,a.`id` ASC "; break;
		case 4 : $sql .= "ORDER BY a.`amount` DESC,a.`id` DESC "; break;
		case 5 : $sql .= "ORDER BY a.`amount` ASC,a.`id` ASC "; break;
		default: $sql .= "ORDER BY a.`addtime` DESC,a.`id` DESC "; break;
	}
	$sql .= "LIMIT {$start},{$limit}";
	$data = $db->query($sql);
	$result = array(
		'page' => $page, 
		'limit' => $limit, 
		'total' => $total, 
		'total_page' => $total_page, 
		'data' => $data, 
	);
	return $result;
}
$list = order_list($_GET['sort'],$_GET['filter'],$_GET['word']);
$mle['list'] = $list['data']; 
$mle['page'] = admin::page($list['total'],$list['page'],$list['limit'],$list['total_page']);
$mle['sort_text'] = '';
$sort = is_numeric($_GET['sort']) ? $_GET['sort'] : 0;
for($i=0; $i<6; $i++){ 
	$mle['sort_text'] .= $sort == $i ? '<option selected="selected" value="">'.$language['page']['sort_text'][$i].'</option>' : '<option value="'.durl('sort',$i).'">'.$language['page']['sort_text'][$i].'</option>';
}
$mle['filter_text'] = '';
$filter = is_numeric($_GET['filter']) ? $_GET['filter'] : 0;
for($i=0; $i<9; $i++){ 
	$mle['filter_text'] .= $filter == $i ? '<option selected="selected" value="">'.$language['page']['filter_text'][$i].'</option>' : '<option value="'.durl('filter',$i).'">'.$language['page']['filter_text'][$i].'</option>';
}
require_once(ADMIN_PATH.'/footer.php');