<?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>');
}

$arr_news_videos = $obj_class_admin->get_news_videos_by_news_id($static_uint_news_id);
?>
<!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" />
	<title>管理新聞影片</title>
	<script type="text/javascript" src="scripts/jquery.js"></script>
	<script type="text/javascript">
<!--
var global_bool_processing = false;

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

	if( document.add_news_video.video_no.value == '' )
	{
		alert('請輸入影片youtube編號');
		return false;
	}

	var obj_form_data = new FormData(document.add_news_video);

	global_bool_processing = true;

	$.ajax(
	{
		url: 'ajax_do_add_news_video.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_update_news_videos_orders()
{
	if( global_bool_processing )
		return false;

	var regexp_element_name = /^order_(\d+)$/;

	for( var i=0; i<document.update_news_videos_orders.length; i++ )
	{
		if( document.update_news_videos_orders.elements[i].type == 'text' && ( arr_matches = regexp_element_name.exec(document.update_news_videos_orders.elements[i].name) ) )
		{
			if( document.update_news_videos_orders.elements[i].value == '' )
			{
				alert('請輸入類別編號[' + arr_matches[1] + ']之顯示順序');
				return false;
			}
			else if( isNaN(document.update_news_videos_orders.elements[i].value) || document.update_news_videos_orders.elements[i].value < 0 || document.update_news_videos_orders.elements[i].value > 255 )
			{
				alert('類別編號[' + arr_matches[1] + ']之顯示順序必須在0-255之間');
				return false;
			}
		}
	}

	var obj_form_data = new FormData(document.update_news_videos_orders);

	global_bool_processing = true;

	$.ajax(
	{
		url: 'ajax_do_update_news_videos_orders.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 edit_news_video(static_uint_video_id)
{
	if( !global_bool_processing )
		window.location = 'edit_news_video.php?video_id=' + static_uint_video_id;

	return false;
}

function do_delete_news_video(static_uint_video_id)
{
	if( global_bool_processing )
		return false;

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

	global_bool_processing = true;

	$.ajax(
	{
		url: 'ajax_do_delete_news_video.php',
		type: 'POST',
		data: 'video_id=' + static_uint_video_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;
}
//-->
	</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 name="add_news_video">
<input class="content" type="hidden" name="news_id" value="<?=$arr_news[0]['NewsId']?>" />
<fieldset class="search">
<legend>新增影片</legend>
影片網址：https://www.youtube.com/watch?v=<input class="video_no" type="text" name="video_no" maxlength="11" />標題：<input class="video_subject" type="text" name="subject" maxlength="255" /><input class="button" type="button" value="確定新增影片" onclick="do_add_news_video();" /><input class="button" type="button" value="更新優先順序" onclick="do_update_news_videos_orders();" />
</fieldset>
</form>
<?
if( !$arr_news_videos )
	echo("<p>目前並無任何資料</p>\n");
else
{
?>
<form name="update_news_videos_orders">
<input type="hidden" name="news_id" value="<?=$static_uint_news_id?>" />
<table class="data_list"><tbody>
<tr><th>影片網址</th><th>影片標題</th><th>優先順序</th><th>管理</th></tr>
<?
	foreach( $arr_news_videos as $arr_row )
		echo('<tr><td class="video_url"><a href="https://www.youtube.com/watch?v=' . $arr_row['VideoNo'] . '" target="_blank">https://www.youtube.com/watch?v=' . $arr_row['VideoNo'] . '</a></td><td><a href="edit_news_video.php?video_id=' . $arr_row['VideoId'] . '">' . $arr_row['Subject'] . '</a></td><td class="order"><input type="text" name="order_' . $arr_row['VideoId'] . '" value="' . $arr_row['Order'] . '" size="4" maxlength="3" /></td><td class="function"><input type="button" name="button_submit" value="編輯" onClick="edit_news_video(' . $arr_row['VideoId'] . ');" />&nbsp;<input type="button" name="button_submit" value="刪除" onClick="javascript:do_delete_news_video(' . $arr_row['VideoId'] . ');" /></td></tr>' . "\n");
?>
</table>
</form>
<?
}
?>
</div>
<? include('_footer.php'); ?>
</body>

</html>