<?php
include_once('./_common.php');

check_device('both');

if( $is_guest ) {
    alert("로그인 후 이용가능합니다.", G5_BBS_URL.'/login.php?url='.urlencode(LOTTO_URL.'/my_saved_number.php'));
}

$sql_search = " where mb_id = '{$member['mb_id']}' ";

if ($page < 1) { $page = 1; } // 페이지가 없으면 첫 페이지 (1 페이지)

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

$page_rows = 10;
$total_page  = ceil($total_count / $page_rows);  // 전체 페이지 계산
$from_record = ($page - 1) * $page_rows; // 시작 열을 구함

$sql_order = " order by pa_id desc ";
$sql = "select * from {$lo['payment_table']} {$sql_search} {$sql_order} limit {$from_record}, {$page_rows} ";
$result = sql_query($sql);

$list_num = $total_count - ($page - 1) * $page_rows;

$list = array();
for( $i=0; $row = sql_fetch_array($result); $i++ ) {
    $list[$i] = $row;
    $list[$i]['num'] = $list_num - $i;
    $list[$i]['name'] = "<strong class='". $lotto_text_color[$list[$i]['pa_name']] ."'>". $lotto_name_kr[$list[$i]['pa_name']] ."</strong>";

    $file_path = '/upload/'.$list[$i]['pa_file'];
    if( $list[$i]['pa_file'] && file_exists(LOTTO_DATA_PATH.$file_path) ) {
        $list[$i]['file_url'] = LOTTO_DATA_URL.$file_path;
    }

    $file_path_back = '/upload/'.$list[$i]['pa_file_back'];
    if( $list[$i]['pa_file_back'] && file_exists(LOTTO_DATA_PATH.$file_path_back) ) {
        $list[$i]['file_url_back'] = LOTTO_DATA_URL.$file_path_back;
    }

    $list[$i]['draw_date'] = $list[$i]['pa_draw_date'];
    $list[$i]['datetime'] = $list[$i]['pa_datetime'];
    if( $sdf == 'pa_datetime' ) {
        $list[$i]['datetime'] = preg_replace('/^([0-9]{4}-[0-9]{2}-[0-9]{2})/', "<span class=\"bg-orange-lt\">$1</span>", $list[$i]['datetime']);
    } else if( $sdf == 'pa_draw_date' ) {
        $list[$i]['draw_date'] = "<span class=\"bg-orange-lt\">{$list[$i]['draw_date']}</span>";
    }

    $sql = "select * from {$lo['order_table']} where pa_id = '{$list[$i]['pa_id']}' ";
    $order_result = sql_query($sql);
    while( $order = sql_fetch_array($order_result) ) {
        $list[$i]['order_list'][] = $order;
    }
}

$list_url = LOTTO_URL."/my_saved_number.php";

$write_pages = get_paging(G5_IS_MOBILE ? $config['cf_mobile_pages'] : $config['cf_write_pages'], $page, $total_page, $list_url."?".($qstr ? $qstr : "")."page=");

$colspan = 3;

$g5['title'] = "나의 스캔본보기";

$tmenu_ = '마이로또';

include_once(G5_PATH.'/_head.php');

// add_stylesheet('css 구문', 출력순서); 숫자가 작을 수록 먼저 출력됨
add_stylesheet('<link rel="stylesheet" href="'.LOTTO_CSS_URL.'/lotto.css">', 0);
add_stylesheet('<link rel="stylesheet" href="'.LOTTO_CSS_URL.'/board.css">', 0);
?>

<!-- 게시판 목록 시작 { -->
<div id="bo_list" style="width:100%">
    <!-- 게시판 페이지 정보 및 버튼 시작 { -->
    <div id="bo_btn_top">
        <div id="bo_list_total">
            <span>Total <?php echo number_format($total_count) ?>건</span>
            <?php echo $page ?> 페이지
        </div>
        <!-- <ul class="btn_bo_user">
            <li><strong class="user-text">보유캐시 : <?php echo number_format($member['mb_cash']); ?></strong></li>
        </ul> -->
    </div>
    <!-- } 게시판 페이지 정보 및 버튼 끝 -->

    <div class="tbl_head01 tbl_wrap">
        <table>
        <caption><?php echo $g5['title'] ?></caption>
        <thead>
        <tr>
            <th scope="col">No.</th>
            <th scope="col">복권명</th>
            <th scope="col" style="width: 40%;">나의복권번호</th>
            <th scope="col">스캔보기</th>
            <th scope="col">뒷면보기</th>
            <th scope="col">추첨일</th>
        </tr>
        </thead>
        <tbody>
            <?php for( $i=0; $i < count($list); $i++ ) { ?>
                <tr>
                    <td class="td_center"><?php echo $list[$i]['pa_id'] ?></td>
                    <td class="td_center"><?php echo $list[$i]['pa_name'] ?></td>
                    <td class="td_center">
                    <?php
                        $order_list = $list[$i]['order_list'];
                        for ($j=0; $j<count($order_list); $j++) {
                    ?>
                        <div style="display: flex;">
                        <div class="d-inline-block bg-blue rounded-circle p-0 custom_ballsize"><?php echo $order_list[$j]['od_main_set'] ?></div>
                            <div class="d-inline-block bg-red rounded-circle p-0 custom_ballsize">&nbsp;:&nbsp;<?php echo $order_list[$j]['od_bonus'] ?></div>
                        </div>
                    <?php
                        }
                    ?>
                    </td>
                    <td class="td_center"><?php if ( $list[$i]['file_url'] ) { ?><a href="<?php echo $list[$i]['file_url'] ?>" title="스캔"><img src="img/scan_front_.png"></a><?php } ?></td>
                    <td class="td_center"><?php if ( $list[$i]['file_url_back'] ) { ?><a href="<?php echo $list[$i]['file_url_back'] ?>" title="뒷면"><img src="img/scan_back_.png"></a><?php } ?></td>
                    <td class="td_center">
                        <?php echo conv_date_format('Y년 m월 d일 H시 i분', $list[$i]['draw_date']) ?>
                    </td>
                </tr>
            <?php } ?>
            <?php if (count($list) == 0) {?>
                <tr><td colspan="<?php echo $colspan ?>" class="empty_table">등록된 자료가 없습니다.</td></tr>
            <?php } ?>
            </tbody>
        </table>
    </div>

    <?php /* 게시판 페이지 */ ?>
    <?php echo $write_pages; ?>
</div>

<script>
$(function() {
    $('.popup-link').magnificPopup({
        type: 'image'
        // other options
    });
});    
</script>

<?php
include_once(G5_PATH.'/_tail.php');
