<?php
	require("__include-common.php");
	require("../account/__logincheck.php");
	if ($_GET['id'].'' == '') {
		exit;
	}
	$my_id = $_GET['id'];
	if ($_GET['ud'].'' == 'd') {	// ↓
		$priority_where = "<=";
		$sql_order = "c_priority desc, id_todo asc";	// 自分より下を大きいものから順に
		$ud1 = -1;
		$ud2 = -2;
	} else {
		$priority_where = ">=";
		$sql_order = "c_priority asc, id_todo desc";	// 自分より上を小さいものから順に
		$ud1 = 1;
		$ud2 = 2;
	}
	$con = my_mysql_connect(_DB_SCHEMA);

	$sql = "select c_priority from m_todo where id_todo = " . $my_id;
	$sql .= " and id_account = " . $_SESSION['current_id'];
	$rs = my_mysql_query($sql);
	if (!($rec=mysql_fetch_array($rs))) {
		exit;
	}
	$my_priority = $rec['c_priority'];
	$sql = "select id_todo, c_priority from m_todo";
	$sql .= " where c_priority " . $priority_where . " " . $my_priority;
	$sql .= " and id_todo <> " . $my_id ;
	$sql .= " and id_account = " . $_SESSION['current_id'];
	$sql .= " and c_delete = 0 ";
	$sql .= " order by " . $sql_order;
	$rs = my_mysql_query($sql);
	$row = mysql_num_rows($rs);
	if ($row == 0) {
		exit;
	}
	if (!($rec=mysql_fetch_array($rs))) {	// 1つ上(下)
		exit;
	}
	$up_1_id = $rec['id_todo'];
	$up_1_priority = $rec['c_priority'];
	if ($row >= 2) {
		if (!($rec=mysql_fetch_array($rs))) {	// 2つ上(下)
			exit;
		}
		if (($_GET['ud'].'' == 'd' && ($rec['c_priority'] > ($up_1_priority-2))) or
		    ($_GET['ud'].'' == '' && ($rec['c_priority'] < ($up_1_priority+2)))) {
			$sql = "update m_todo set c_priority = c_priority + " . $ud2 . ",";
			$sql .= " c_updatetime = '" . date("Y/m/d H:i:s") . "'";
			$sql .= " where c_priority " . $priority_where . " " . $my_priority;
			$sql .= " and id_todo <> " . $my_id;
			$sql .= " and id_todo <> " . $up_1_id;
			$sql .= " and id_account = " . $_SESSION['current_id'];
			$sql .= " and c_delete = 0 ";
			my_mysql_query($sql);
		}
	}
	$sql = "update m_todo set c_priority = " . ($up_1_priority + $ud1) . ", c_updatetime = '" . date("Y/m/d H:i:s") . "'";
	$sql .= " where id_todo = " . $my_id . " and id_account = " . $_SESSION['current_id'];
	$sql .= " and c_delete = 0 ";
	my_mysql_query($sql);

	$con = mysql_close($con);
?>
