<?php
session_start();

if( isset($_SESSION['init_info']) )
{
	if( ( $str_temp = trim($_SESSION['init_info']) ) != '' )
		$str_init_info = $str_temp;

	unset($_SESSION['init_info']);
}

include('includes/check_login.php');

if( !isset($_REQUEST['news_id']) || ( $static_uint_news_id = (int)$_REQUEST['news_id'] ) <= 0 )
{
	$_SESSION['init_info'] = '輸入資料不足！';
	exit('<script language="javascript">history.back();</script>');
}

require_once('includes/common.php');

require_once('includes/admin.php');
$obj_class_admin = new class_admin();

if( !( $arr_news = $obj_class_admin->get_news_by_news_id($static_uint_news_id) ) )
{
	$_SESSION['init_info'] = '指定的新聞不存在！';
	exit('<script language="javascript">history.back();</script>');
}
else
	$_SESSION['upload_news_id'] = $static_uint_news_id;

$arr_news_pictures = $obj_class_admin->get_news_pictures_by_news_id($static_uint_news_id);

$uint_now = time();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="utf-8" lang="utf-8">
<head>
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
	<meta http-equiv="content-language" content="zh-tw" />
	<meta name="robots" content="none" />
	<link rel="stylesheet" type="text/css" href="admin.css" />
	<link rel="stylesheet" type="text/css" href="uploadify/uploadifive.css">
	<title>管理新聞照片</title>
	<script type="text/javascript" src="scripts/jquery.min.js"></script>
	<script type="text/javascript" src="uploadify/jquery.uploadifive.js"></script>
	<script type="text/javascript">
<!--
$(function() {
	$('#file_upload').uploadifive({
		'auto'             : false,
		'checkScript'      : 'do_check_exists.php',
		'formData'         : {
							   'timestamp' : '<?=$uint_now?>',
							   'token'     : '<?=md5('unique_salt' . $uint_now)?>'
		                     },
		'queueID'          : 'queue',
		'uploadScript'     : 'do_add_news_picture.php',
		'onUploadComplete' : function(file, data) { console.log(data); }
	});
});

var global_bool_processing = false;

function do_update_news_picture_subjects()
{
	if( global_bool_processing )
		return false;

	var obj_form_data = new FormData(document.update_news_picture_subjects);

	global_bool_processing = true;

	$.ajax(
	{
		url: 'ajax_do_update_news_picture_subjects.php',
		type: 'POST',
		data: obj_form_data,
		cache: false,
		processData: false,
		contentType: false,
		dataType: 'json',
		success: function(jdata)
		{
			if( jdata['RedirectURL'] )
				location.replace(jdata['RedirectURL']);
			else if( jdata['ErrMessage'] != '' )
				alert(jdata['ErrMessage']);
			else if( jdata['ErrCode'] != '1' )
				alert('Error:' + jdata['ErrCode']);

			if( jdata['ErrCode'] == '1' )
				location.reload();

			global_bool_processing = false;
		}
	});

	return false;
}

function do_set_news_picture_is_cover(static_uint_picture_id)
{
	if( global_bool_processing )
		return false;

	global_bool_processing = true;

	$.ajax(
	{
		url: 'ajax_do_set_news_picture_is_cover.php',
		type: 'POST',
		data: 'picture_id=' + static_uint_picture_id,
		dataType: 'json',
		success: function(jdata)
		{
			if( jdata['RedirectURL'] )
				location.replace(jdata['RedirectURL']);
			else if( jdata['ErrMessage'] != '' )
				alert(jdata['ErrMessage']);
			else if( jdata['ErrCode'] != '1' )
				alert('Error:' + jdata['ErrCode']);

			if( jdata['ErrCode'] == '1' )
				location.reload();

			global_bool_processing = false;
		}
	});

	return true;
}

function do_delete_news_picture(static_uint_picture_id, static_bool_reload)
{
	if( global_bool_processing )
		return false;

	if( !confirm('確定要刪除此張照片？') )
		return false;

	global_bool_processing = true;

	$.ajax(
	{
		url: 'ajax_do_delete_news_picture.php',
		type: 'POST',
		data: 'picture_id=' + static_uint_picture_id,
		dataType: 'json',
		success: function(jdata)
		{
			if( jdata['RedirectURL'] )
				location.replace(jdata['RedirectURL']);
			else if( jdata['ErrMessage'] != '' )
				alert(jdata['ErrMessage']);
			else if( jdata['ErrCode'] != '1' )
				alert('Error:' + jdata['ErrCode']);

			if( jdata['ErrCode'] == '1' )
			{
				if( static_bool_reload )
					location.reload();
				else
					$('#picture_' + static_uint_picture_id).remove();
			}

			global_bool_processing = false;
		}
	});

	return true;
}
//-->
	</script>
</head>

<body<?=( isset($str_init_info) ? ' onLoad="javascript:alert(\'' . $str_init_info . '\');"' : '' )?>>
<? include('_title.php'); ?>
<? include('_menu.php'); ?>
<div id="content">
<h2>新聞照片管理：<?=$arr_news[0]['Subject']?></h2>
<form>
	<div id="queue" style="margin-bottom:10px"></div>
	<input id="file_upload" name="file_upload" type="file" multiple="true">
	<input class="uploadifive-button" type="button" value="進行上傳" onclick="javascript:$('#file_upload').uploadifive('upload')" style="width:105px;height:34px;margin:-47px 0 0 115px" />
</form>
<?
if( !$arr_news_pictures )
	echo("<p>目前並無任何照片</p>\n");
else
{
?>
<ul id="news_imgs"><form name="update_news_picture_subjects">
<?
	foreach( $arr_news_pictures as $arr_row )
	{
		echo('<li id="picture_' . $arr_row['PictureId'] . '">');
		echo('<p class="image"><img src="../images/news/' . $arr_news[0]['NewsId'] . '/' . $arr_row['PictureId'] . '.jpg" /></p><p class="subject"><input type="text" name="picture_subject_' . $arr_row['PictureId'] . '" value="' . $arr_row['Subject'] . '" maxlength="255" /></p><p class="function">');

		if( $arr_row['IsCover'] )
			echo('【封面】');
		else
			echo('<input type="button" value="設為封面" onclick="javascript:do_set_news_picture_is_cover(' . $arr_row['PictureId'] . ');" />');

		echo('<input type="button" name="button_submit" value="刪除圖片" onclick="javascript:do_delete_news_picture(' . $arr_row['PictureId'] . ', ' . ( $arr_row['IsCover'] ? 'true' : 'false' ) . ');" /></p>');
		echo("</li>\n");
	}
?>
</form></ul>
<?
}
?>
</div>
<? include('_footer.php'); ?>
</body>

</html>