<?php
if (!defined("_GNUBOARD_")) exit; // 개별 페이지 접근 불가

if (!defined("_ORDERINQUIRY_")) exit; // 개별 페이지 접근 불가

// 테마에 orderinquiry.sub.php 있으면 include
if(defined('G5_THEME_SHOP_PATH')) {
    $theme_inquiry_file = G5_THEME_MSHOP_PATH.'/orderinquiry.sub.php';
    if(is_file($theme_inquiry_file)) {
        include_once($theme_inquiry_file);
        return;
        unset($theme_inquiry_file);
    }
}
?>

<?php if (!$limit) { ?>총 <?php echo $cnt; ?> 건<?php } ?>

<style>
#sod_inquiry .idtime_link_wrap {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    flex-wrap: wrap;
}
#sod_inquiry .idtime_view_btn {
    padding: 0 6px;
    height: 20px;
    line-height: 18px;
    font-size: 11px;
    font-weight: normal;
    border-radius: 2px;
    background: #f3f4f6;
    border: 1px solid #d1d5db;
    color: #374151;
    text-decoration: none;
}
#sod_inquiry .idtime_view_btn:hover,
#sod_inquiry .idtime_view_btn:focus {
    background: #e5e7eb;
    color: #111827;
    text-decoration: none;
}
#sod_inquiry .idtime_ext {
    margin-top: 3px;
    font-size: 11px;
    line-height: 1.3;
    color: #0b6e4f;
    font-weight: 600;
}
</style>


<div id="sod_inquiry">
    <ul>
        <?php
        $member_mb_id_esc = sql_escape_string($member['mb_id']);
        $order_sub_where_sql = isset($order_sub_where_sql) ? $order_sub_where_sql : '';
        $sql = " select *,
                    (od_cart_coupon + od_coupon + od_send_coupon) as couponprice
                   from {$g5['g5_shop_order_table']}
              where mb_id = '{$member_mb_id_esc}' {$order_sub_where_sql}
                  order by od_id desc
                  $limit ";
        $result = sql_query($sql);
        for ($i=0; $row=sql_fetch_array($result); $i++)
        {
            // 주문상품
            $sql = " select it_name, ct_option
                        from {$g5['g5_shop_cart_table']}
                        where od_id = '{$row['od_id']}'
                        order by io_type, ct_id
                        limit 1 ";
            $ct = sql_fetch($sql);
            $ct_name = get_text($ct['it_name']).' '.get_text($ct['ct_option']);

            $sql = " select count(*) as cnt
                        from {$g5['g5_shop_cart_table']}
                        where od_id = '{$row['od_id']}' ";
            $ct2 = sql_fetch($sql);
            if($ct2['cnt'] > 1)
                $ct_name .= ' 외 '.($ct2['cnt'] - 1).'건';

            switch($row['od_status']) {
                case '주문':
                    $od_status = '<span class="status_01">입금확인중</span>';
                    break;
                case '입금':
                    $od_status = '<span class="status_02">입금완료</span>';
                    break;
                case '준비':
                    $od_status = '<span class="status_03">상품준비중</span>';
                    break;
                case '배송':
                    $od_status = '<span class="status_04">상품배송</span>';
                    break;
                case '완료':
                    $od_status = '<span class="status_05">배송완료</span>';
                    break;
                default:
                    $od_status = '<span class="status_06">주문취소</span>';
                    break;
            }

            $od_invoice = '';
            if($row['od_delivery_company'] && $row['od_invoice'])
                $od_invoice = '<span class="inv_inv"><i class="fa fa-truck" aria-hidden="true"></i> <strong>'.get_text($row['od_delivery_company']).'</strong> '.get_text($row['od_invoice']).'</span>';

            $uid = md5($row['od_id'].$row['od_time'].$row['od_ip']);

            $ext_paid_point = isset($row['od_receipt_point_for_extension_of_period']) ? (float)$row['od_receipt_point_for_extension_of_period'] : 0;
            $ext_order_count = 0;
            if ($ext_paid_point > 0) {
                $od_id_for_ext = sql_escape_string((string)$row['od_id']);
                $sql_ext = " select extension_of_period_price
                               from {$g5['g5_shop_cart_table']}
                              where od_id = '{$od_id_for_ext}' ";
                $result_ext = sql_query($sql_ext);
                while ($row_ext = sql_fetch_array($result_ext)) {
                    $ext_price_raw = isset($row_ext['extension_of_period_price']) ? trim((string)$row_ext['extension_of_period_price']) : '';
                    if ($ext_price_raw === '') {
                        continue;
                    }
                    $ext_price_lines = preg_split('/\r\n|\r|\n/', $ext_price_raw);
                    $ext_price_line_count = 0;
                    for ($e=0; $e < count($ext_price_lines); $e++) {
                        if (trim((string)$ext_price_lines[$e]) !== '') {
                            $ext_price_line_count++;
                        }
                    }
                    if ($ext_price_line_count > 1) {
                        $ext_order_count += ($ext_price_line_count - 1);
                    }
                }
                if ($ext_order_count < 1) {
                    $ext_order_count = 1;
                }
            }
            $extension_summary_text = ($ext_paid_point > 0)
                ? ('연장주문 '.number_format($ext_order_count).'건 / +'.number_format($ext_paid_point).' Point')
                : '';
        ?>

        <li>
            <div class="inquiry_idtime">
                <span class="idtime_link_wrap">
                    <a href="<?php echo G5_SHOP_URL; ?>/orderinquiryview.php?od_id=<?php echo $row['od_id']; ?>&amp;uid=<?php echo $uid; ?>" class="idtime_link"><?php echo $row['od_id']; ?></a>
                    <a href="<?php echo G5_SHOP_URL; ?>/orderinquiryview.php?od_id=<?php echo $row['od_id']; ?>&amp;uid=<?php echo $uid; ?>" class="idtime_view_btn">보기</a>
                </span>
                <span class="idtime_time"><?php echo substr($row['od_time'],2,8); ?></span>
            </div>
            <?php if ($extension_summary_text !== '') { ?>
            <div class="idtime_ext"><?php echo $extension_summary_text; ?></div>
            <?php } ?>
            <div class="inquiry_name">
                <?php echo $ct_name; ?>
            </div>
            <div class="inquiry_price">
                <?php echo display_price($row['od_receipt_price']); ?>
            </div>
            <div class="inquiry_inv">
                <?php echo $od_invoice; ?>
                <span class="inv_status"><?php echo $od_status; ?></span>
            </div>
        </li>

        <?php
        }

        if ($i == 0)
            echo '<li class="empty_list">주문 내역이 없습니다.</li>';
        ?>
    </ul>
</div>
