<?
require_once('include/global.php');
require_once("include/mail_lib.php");
require_once('include/pager.php');
require_once('conf.inc.php');


// set
	$CFG['TITLE'] = '<b>수신확인</b>';


// data

	// search
		$q_where = '';
		if ($_GET['s_word'])
		{
			set_q_search($_GET['s_word']);
			$q_where = " AND ( mrc_to LIKE '%" . $_GET['s_word'] . "%' OR mrc_subject LIKE '%" . $_GET['s_word'] . "%' ) ";
			$v_s_word = htmlspecialchars_fix(stripslashes($_GET['s_word']));
		}	// if()

		// [2012-09-27] 예약발송
			if ($G_SYS['MAIL_SEND_RESERVE_ISUSE'])
			{
				if ($_GET['s_mrc_reserve_status'] != '')
				{
					$q_where .= " AND mrc_reserve_status = '".$_GET['s_mrc_reserve_status']."' ";
				}	// if()
			}	// if()

	// sort
		$CFG['MAIL_RECEIVE_CHECK_FIELD'] = array(
				'mrc_to' => '받은사람',
				'mrc_subject' => '제 목',
				'mrc_senddate' => '보낸날짜',
				'mrc_readdate' => '읽은(취소)날짜',
				'mrc_status' => '상태'
				);
		// [2012-09-27] 예약발송
		//$order_set = get_order(array_keys($CFG['MAIL_RECEIVE_CHECK_FIELD']), 'mrc_no', $_GET['s_key'], $_GET['s_key2']);
		$order_set = get_order(array_keys($CFG['MAIL_RECEIVE_CHECK_FIELD']), 'mrc_senddate', $_GET['s_key'], $_GET['s_key2']);
		$order_img = get_order_img(array_keys($CFG['MAIL_RECEIVE_CHECK_FIELD']), $_GET['s_key'], $_GET['s_key2'], false, 'common_skin');		// array

	// db - pager
		$DEBUG['db_cnt']['start'] = getmicrotime();
		$pager_params['totalItems'] = db_get_one("SELECT COUNT(*) FROM " . $TB['MAIL_RECEIVE_CHECK'] . " WHERE m_no='" . $AUTH['auth_m_no'] . "'" . $q_where);
		// [2007-05-29]
		//$pager_params['perPage'] = 20;
		if (!is_numeric($_GET['page_size']) || $_GET['page_size'] > 100)
		{
			$dfcon = get_default_config($AUTH['auth_m_no']);
			$_GET['page_size'] = $dfcon['m_cfg_mail_list_cnt'];
		}	// if()
		$pager_params['perPage'] = $_GET['page_size'];

		$DEBUG['db_cnt']['exec'] += getmicrotime() - $DEBUG['db_cnt']['start'];

		$pager = &new Pager_Sliding($pager_params);
		$pager_links = $pager->getLinks();
		$pager_limit = get_pager_limit($pager->getCurrentPageID(), $pager_params['perPage']);

	// db - select
		$DEBUG['db_select']['start'] = getmicrotime();
		$sql = "
			SELECT
				*
			FROM
				" . $TB['MAIL_RECEIVE_CHECK'] . "
			WHERE
				m_no='". $AUTH['auth_m_no'] . "'
				" . $q_where . "
			ORDER BY
				" . $order_set['query'] . "
			";
		$rows = db_get_rows($sql, $pager_limit['from'], $pager_limit['count']);
		$DEBUG['db_select']['exec'] += getmicrotime() - $DEBUG['db_select']['start'];

	// process data
		$DEBUG['db_fetch_all']['start'] = getmicrotime();

		$view = array();
		for ($i=0; $i<sizeof($rows); $i++)
		{
			$view['mrc_no'][$i] = $rows[$i]['mrc_no'];
			$view['mrc_to'][$i] = address_view($rows[$i]['mrc_to']);
			$_ap = mime_address_parse($rows[$i]['mrc_to']);
			$view['mrc_to_view'][$i] = $_ap['display'];
			$view['mrc_subject'][$i] = (trim($rows[$i]['mrc_subject']) == NULL) ? '__제목없음__' : htmlspecialchars_fix(han_substr($rows[$i]['mrc_subject'], 90));
			if ($rows[$i]['mf_no'])
			{
				$view['mrc_subject'][$i] = '<a href="'.$nmpath.'/mail_read.php?mb_id=sent&mf_no='.$rows[$i]['mf_no'].'&back_url='.urlencode($_SERVER['REQUEST_URI']).'">'.$view['mrc_subject'][$i].'</a>';
			} else 
			{
				$view['mrc_subject'][$i] = '<a href="JavaScript:alert(\'보낸편지함에 저장하지 않은 메일입니다.\');">'.$view['mrc_subject'][$i].'</a>';
			}	// if()
			// [2006-01-05][custmize] 보낸편지함/수신확인 보관기능.
			$view['is_mrcext'][$i] = ($rows[$i]['mrcext_m_id']) ? true : false;
			$view['mrc_senddate'][$i] = date('y-m-d H:i', strtotime($rows[$i]['mrc_senddate']));
			$view['mrc_readdate'][$i] = ($rows[$i]['mrc_readdate'] == '') ? '-' : date('y-m-d H:i', strtotime($rows[$i]['mrc_readdate']));

			// [2007-05-28] 발송취소 기능추가
			switch ($rows[$i]['mrc_status'])
			{
				case 'N':
					$view['mrc_status'][$i] = '<b>읽지않음</b>';
					break;
			
				case 'R':
					$view['mrc_status'][$i] = '읽음';
					break;

				case 'T':
					$view['mrc_status'][$i] = '수신확인안함';
					break;

				case 'C':
					$view['mrc_status'][$i] = '<font color=red><b>발송취소</b></font>';
					break;
			}	// switch()

			// [2012-09-27] 예약발송
				if ($G_SYS['MAIL_SEND_RESERVE_ISUSE'])
				{
					if ($rows[$i]['mrc_reserve_status'] == 'W') {
						$view['mrc_status'][$i] = '<font color=blue><b>예약</b></font>';
					}	// if()
				}	// if()

			$view['cancel_enable'][$i] = false;
			// [2008-07-09] 발송취소 여부를 목록에서 확인시 부하가 많이 걸리므로 실제 클릭시 확인하도록 변경
			if ($G_SYS['MAIL_CANCEL_LIST_CHECK'])
			{
				if (in_array($rows[$i]['mrc_status'], array('N', 'T')) && send_mail_exists($rows[$i]['mrc_to'], $rows[$i]['mrc_senddate'], $rows[$i]['mrc_subject']))
					$view['cancel_enable'][$i] = true;
			} else		// 기본값.  내부회원인지만 체크함.
			{
				if (in_array($rows[$i]['mrc_status'], array('N', 'T')) && send_mail_exists($rows[$i]['mrc_to'], $rows[$i]['mrc_senddate'], $rows[$i]['mrc_subject'], false, true))
					$view['cancel_enable'][$i] = true;
			}	// if()
		}	// for()
		$view_cnt = sizeof($rows);

		$DEBUG['db_fetch_all']['exec'] += getmicrotime() - $DEBUG['db_fetch_all']['start'];

	// 메일수
		$DEBUG['db_cnt_unread']['start'] = getmicrotime();
		$mail_all_cnt = number_format($pager_params['totalItems']);
		$mail_unread_cnt = db_get_one("SELECT COUNT(*) FROM " . $TB['MAIL_RECEIVE_CHECK'] . " WHERE m_no='". $AUTH['auth_m_no'] . "' AND mrc_readdate IS NULL" . $q_where);
		$mail_unread_cnt = number_format($mail_unread_cnt);

		$DEBUG['db_cnt_unread']['exec'] += getmicrotime() - $DEBUG['db_cnt_unread']['start'];


	// [2012-09-27] 예약발송
		if ($G_SYS['MAIL_SEND_RESERVE_ISUSE'])
		{
			$q = "SELECT COUNT(*) FROM ".$TB['MAIL_RECEIVE_CHECK']." WHERE mrc_reserve_status='W' AND m_no='".$AUTH['auth_m_no']."'";
			$mf_reserve_count = db_get_one($q);
		}	// if()


// skin view
	nskinset('webmail', 'mail_receive_check.html');
	include($NSKIN['view_layout']);


require_once('include/global_end.php');
?>