<?php
$sub_menu = '400450';
include_once('./_common.php');

auth_check($auth[$sub_menu], "r");

$sql_common = " from sp_rtpay ";

$sql_search = " where (1) ";
if ($stx) {
    $sql_search .= " and ( ";
    switch ($sfl) {
        case 'bname' :
            $sql_search .= " ({$sfl} = '{$stx}') ";
            break;
        case 'wdate' :
            $sql_search .= " (date_format({$sfl},'%Y-%m-%d') = '{$stx}') ";
            break;
        case 'od_id' :
            $sql_search .= " ({$sfl} = '{$stx}') ";
            break;
        default :
            $sql_search .= " ({$sfl} like '%{$stx}%') ";
            break;
    }
    $sql_search .= " ) ";
}

$sql_order = " order by bstate desc, rt_id desc ";

$sql = " select count(*) as cnt
            {$sql_common}
            {$sql_search}
            {$sql_order} ";
$row = sql_fetch($sql);
$total_count = $row['cnt'];

$rows = $config['cf_page_rows'];
$total_page  = ceil($total_count / $rows);  // 전체 페이지 계산
if ($page < 1) $page = 1; // 페이지가 없으면 첫 페이지 (1 페이지)
$from_record = ($page - 1) * $rows; // 시작 열을 구함

$sql = " select *
            {$sql_common}
            {$sql_search}
            {$sql_order}
            limit {$from_record}, {$rows} ";
$result = sql_query($sql);


$g5['title'] = '무통장입금로그 관리 [RTPay]';
include_once (G5_ADMIN_PATH.'/admin.head.php');

$colspan = 10;
?>




<div class="local_ov01 local_ov">
   <span class="btn_ov01"><span class="ov_txt">전체 </span><span class="ov_num"> <?php echo number_format($total_count) ?>건 </span></span>
</div>
	

<p>
		- 주문내역처리를 선택하시면 결제상태가 함께 입금으로 변경됩니다. 부분처리하시려면 각각 처리해주세요.<br>
		- 입금자명과 금액이 맞아야 자동매칭됩니다. 이름과 금액이 같은 주문이 2건 이상이면 비매칭 처리됩니다. 비매칭은 수동으로 매칭등록해주세요!<br>
		- 입금자명과 금액 추출에 문제점이 있을 경우 해당항목의 점검요청을 해주세요. 확인즉시 대응해 드립니다. <br>
		- 기타 요청및 의문사항들은 홈페이지를 통해 전달해주세요. <a href="http://rtpay.net" target="_blank">www.rtpay.net</a>
	</p>
<form name="fsearch" id="fsearch" class="local_sch01 local_sch" method="get" style="display:inline-block;zoom:1;*display:inline;width:50%;">
    <select name="sfl" title="검색대상">
        <option value="bname"<?php echo get_selected($_GET['sfl'], "bname"); ?>>입금자명</option>
        <option value="wdate"<?php echo get_selected($_GET['sfl'], "wdate"); ?>>입금일</option>
        <option value="od_id"<?php echo get_selected($_GET['sfl'], "od_id"); ?>>주문번호</option>
    </select>
    <label for="stx" class="sound_only">검색어<strong class="sound_only"> 필수</strong></label>
    <input type="text" name="stx" value="<?php echo $stx ?>" id="stx" required class="required frm_input">
    <input type="submit" class="btn_submit" value="검색">
</form><form name="upform" id="upform" method="post" action="./rtpayformupdate.php" onsubmit="return rtpayMatch(this);" style="vertical-align:middle;display:inline-block;zoom:1;*display:inline;width:50%;">
<input type="hidden" name="rtSid" id="rtSid" value="" />
<input type="hidden" name="token" value="">
	<div style="float:right;">
	<input type="checkbox" id="ordCK" name="ordCK" value="1" /><label for="ordCK">주문내역처리</label>
	<input type="text" name="odSid" value="" id="odSid" placeholder="주문번호입력" class="frm_input">
	<input type="submit" name="act_button" value="매칭등록" onclick="document.pressed=this.value" class="btn btn_02">
	</div>
	</form>



<form name="rtpaylist" id="rtpaylist" method="post" action="./rtpaylistdelete.php" onsubmit="return rtpaylist_submit(this);">
<input type="hidden" name="sst" value="<?php echo $sst; ?>">
<input type="hidden" name="sod" value="<?php echo $sod; ?>">
<input type="hidden" name="sfl" value="<?php echo $sfl; ?>">
<input type="hidden" name="stx" value="<?php echo $stx; ?>">
<input type="hidden" name="page" value="<?php echo $page; ?>">
<input type="hidden" name="token" value="">

<div class="tbl_head01 tbl_wrap">
    <table>
    <caption><?php echo $g5['title']; ?> 목록</caption>
    <thead>
    <tr>
        <th scope="col">
            <label for="chkall" class="sound_only">개인결제 전체</label>
            <input type="checkbox" name="chkall" value="1" id="chkall" onclick="check_all(this.form)">
        </th>
        <th scope="col">주문번호</th>
        <th scope="col">은행명</th>
        <th scope="col">입금자명</th>
        <th scope="col">입금금액</th>
        <th scope="col">전송전문</th>
        <th scope="col">매칭여부</th>
        <th scope="col">입금일</th>
        <th scope="col">점검요청</th>
    </tr>
    </thead>
    <tbody>
    <?php
    for ($i=0; $row=sql_fetch_array($result); $i++) {
        if($row['od_id'])
            $od_id = '<a href="./orderform.php?od_id='.$row['od_id'].'" target="_blank">'.$row['od_id'].'</a>';
        else
            $od_id = '&nbsp;';

        $bg = 'bg'.($i%2);
    ?>

    <tr class="<?php echo $bg; ?>">
        <td class="td_chk">
            <input type="hidden" id="rt_id<?php echo $i; ?>" name="rt_id[<?php echo $i; ?>]" value="<?php echo $row['rt_id']; ?>">
            <input type="checkbox" id="chk_<?php echo $i; ?>" name="chk[]" value="<?php echo $i; ?>" title="내역선택">
        </td>
        <td class="td_odrnum3"><?php echo $od_id; ?></td>
        <td class="td_mng"><?php echo get_text($row['bank']); ?></td>
        <td class="td_mng"><?php echo $row['bname']; ?></td>
        <td class="td_payby"><?php echo number_format($row['bpay']); ?></td>
        <td class="td_name"><?php echo $row['bcontent']; ?></td>
        <td class="td_mng"><?php echo $row['bstate']>0 ? '매칭' : '<font color=red>비매칭</font>'; ?></td>
        <td class="td_date"><?php echo $row['wdate']; ?></td>
        <td class="td_mng td_mng_m">
            <?php if (($row['bstate']>0)||($row['pp_id']>0)) {?>
			&nbsp;
			<?php } else { ?>
			<a href="javascript:;" onClick="sendREQ('<?php echo $row['bcontent']; ?>','<?php echo $row['rt_id']; ?>');" class="btn btn_02">요청</a>
			<?php }?>
        </td>
    </tr>

    <?php
    }

    if ($i == 0)
        echo '<tr><td colspan="'.$colspan.'" class="empty_table">자료가 없습니다.</td></tr>';
    ?>
    </tbody>
    </table>
</div>

<div class="btn_fixed_top">
    <input type="submit" name="act_button" value="선택삭제" onclick="document.pressed=this.value" class="btn btn_02">
</div>
</form>

<?php echo get_paging(G5_IS_MOBILE ? $config['cf_mobile_pages'] : $config['cf_write_pages'], $page, $total_page, "{$_SERVER['SCRIPT_NAME']}?$qstr&amp;page="); ?>

<script>
$(function() {
    $(".personalpaycopy").on("click", function() {
        var href = this.href;
        window.open(href, "copywin", "left=100, top=100, width=600, height=300, scrollbars=0");
        return false;
    });
});

function rtpaylist_submit(f)
{
    if (!is_checked("chk[]")) {
        alert(document.pressed+" 하실 항목을 하나 이상 선택하세요.");
        return false;
    }

    if(document.pressed == "선택삭제") {
        if(!confirm("선택한 자료를 정말 삭제하시겠습니까?")) {
            return false;
        }
    }

    return true;
}

function rtpayMatch(f) {
	var rtform=document.rtpaylist;

	var rtid=RTcheckval(rtform, "chk[]");
	
    if (rtid=="") return false;

	if (!f.odSid.value) {alert("매칭등록하실 항목의 주문번호를 입력해주세요.");f.odSid.focus();return false;}

	$("#rtSid").val($("#rt_id"+rtid).val());

	return true;
}


function RTcheckval(form,name)
{
	var chk=0;
	var gid;

	for(i=0;i<form.elements.length;i++)
	{
		if(form.elements[i].name==name)
		{
			if(form.elements[i].checked) {
				chk++;
				gid=form.elements[i].value;
			}
		}
	}

	if (chk > 1) {
		alert('매칭등록 하실 항목을 하나만 선택해주세요.');
		return "";
	} else if (chk == 0) {
		alert('매칭등록 하실 항목을 선택해 주세요.');
		return "";
	} else {
		return gid;
	}
}

function sendREQ(cval,rtid) {
	var ProcUrl="./rtpayrequest.php";
	var data = {amsg : cval, rtid : rtid};
	$.ajax({
		url: ProcUrl,
		type: "POST",
		data: data,
		dataType: 'json',
		success: function (response) {
			alert(response.retMag);
			location.reload();
		},
        error: function(xhr, status, error) {
            alert(status);
        } 
	});
}
</script>

<?php
include_once (G5_ADMIN_PATH.'/admin.tail.php');
?>