<?php

define("ADMIN_AUTHORIZATION",1);
include("../../../includes/common.php");

/*PAGE TITLE*/
$page_title = "ПИН-коды";

$header = "ПИН-коды";

$table = "pincodes";

if(isset($_GET["collection_id"]))
{
	$collection_id = $_GET["collection_id"];

	if($res=mysql_query("select name from pincode_collections where $collection_id"))
	{
		if($row=mysql_fetch_assoc($res))
		{
			$collection_name = $row["name"];
		}
	}
}

/* pager start */
$params['query']="SELECT pincodes.*, pincode_collections.name as 'collection_name'
FROM `pincodes`
JOIN `pincode_collections` WHERE pincode_collections.id=pincodes.collection_id"
. (isset($collection_id) ? " HAVING collection_id=$collection_id" : "");
$params['tables'] = $table;
$params['nocheckfield']=true;
$params['s_prefix'] = $table;
$pager = GetPagerParams($params);

/* pager end */

$group_funcs_options=("<option >Выберите действие<option value='./action.php?action=delete'> - Удалить</option>");

$table_headers = Array(
"itter"=>Array("№","width=20 align=center"),
"code"=>Array("<a href='".$pager['new_sort']."$table".".code".(isset($collection_id) ? "&collection_id=".$collection_id : "")."' title='Sort'>Код</a>","align=center"),
"collection_name"=>Array("<a href='".$pager['new_sort']."pincode_collections.name".(isset($collection_id) ? "&collection_id=".$collection_id : "")."' title='Sort'>Коллекция</a>","align=center"),
"status"=>Array("<a href='".$pager['new_sort']."$table".".status".(isset($collection_id) ? "&collection_id=".$collection_id : "")."' title='Sort'>Статус</a>","align=center"),
"funcs"=>Array("Операции","width=5% align=center"),
"group_funcs"=>Array("<select name=act_sel onChange='SubmitForm();'><option>Выберите действие<option value='./action.php?action=deletepins'>-Удалить<option value='./action.php?action=usedpins'>-Не действительны<option value='./action.php?action=validpins'>-Действительны</select>")
);

if(isset($collection_name))
{
	unset($table_headers["collection_name"]);
}

$query = "SELECT pincodes.*, pincode_collections.name as 'collection_name'
FROM `pincodes`
JOIN `pincode_collections` WHERE pincode_collections.id=pincodes.collection_id"
. (isset($collection_id) ? " HAVING collection_id=$collection_id" : "")
.($search_string?" WHERE $search_string":"")
." ORDER BY ".$pager['sort_by']." ".$pager['sort_ord']
." LIMIT ".$SETTINGS['admin_items_per_page']." OFFSET ".($pager['start_item']-1);

$res = mysql_query($query);
$table_items=Array();
$itter =$pager['start_item'];
$i=0;

if(mysql_num_rows($res) > 0)
{
	while ($row = mysql_fetch_object($res)){
		switch($row->status){
			case "used":
				$s= $ADMIN_ICONS[status_off];
				break;
			case "valid":
				$s= $ADMIN_ICONS[status_on];
				break;
		}
		$table_items[$itter] = Array(
		"itter"=>array($itter,"align=left"),
		"id"=>Array($row->id,"align=center"),
		"code"=>Array($row->code,"align=center"),
		"collection_name"=>array("<nobr><a href='./listcodes.php?collection_id=$row->collection_id' title='Список ПИН-кодов'>" . $row->collection_name . "</a>", "align=center"),
		"status"=>Array($s,"align=center"),
		"funcs"=>Array("<nobr><a href='./addpin.php?id=$row->id" .(isset($collection_id) ? "&collection_id=".$collection_id : "") . "' title='Редактировать'>$ADMIN_ICONS[edit]</a>&nbsp;<a href='#' onClick=\"if(confirm('Удалить??')){document.location.href='./action.php?action=deletepins&id=$row->id';}return false;\" title='Удалить'>$ADMIN_ICONS[del]</a></nobr>","align=center")
		);
		$itter++;
	}
}

$smarty->assign('page_title',"ПИН-коды" . (isset($collection_name) ? " (Коллекция: " . $collection_name . ")" : ""));

$smarty->assign('error',$error);

/* table template */
$smarty->assign('pager',$pager);
$smarty->assign('table_headers',$table_headers);
$smarty->assign('table_items',$table_items);
$smarty->assign('table_width',"100%");
$smarty->assign('top_menu_tpl',"admin_pincollections_menu.tpl");
$smarty->assign('main_tpl',"admin_list_table.tpl");
$smarty->assign('use_group_funcs',array_key_exists("group_funcs",$table_headers));

/* display content */
$smarty->display('admin_index.tpl');


?>