<?
$sub_menu = "400410";
include_once("./_common.php");
include_once(G4_LIB_PATH.'/thumbnail.lib.php');

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

$g4[title] = "주문개별관리";
include_once(G4_ADMIN_PATH."/admin.head.php");

$where = " where ";
$sql_search = "";
if ($search != "") {
    if ($sel_field == "c.ca_id") {
    	$sql_search .= " $where $sel_field like '$search%' ";
        $where = " and ";
    } else if ($sel_field != "") {
    	$sql_search .= " $where $sel_field like '%$search%' ";
        $where = " and ";
    }

    if ($save_search != $search)
        $page = 1;
}

if ($sel_field == "")  $sel_field = "od_id";
if ($sort1 == "") $sort1 = "od_id";
if ($sort2 == "") $sort2 = "desc";

$sql_common = " from {$g4['shop_order_table']} a
                left join {$g4['shop_cart_table']} b on (a.od_id = b.uq_id)
                left join {$g4['shop_item_table']} c on (b.it_id = c.it_id)
                $sql_search ";

// 테이블의 전체 레코드수만 얻음
$sql = " select count(*) as cnt " . $sql_common;
$row = sql_fetch($sql);
$total_count = $row[cnt];

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

$sql  = " select a.od_id,
                 a.mb_id,
                 a.od_name,
                 a.od_deposit_name,
                 a.od_time,
                 b.ct_option,
                 b.ct_status,
                 b.ct_qty,
                 b.it_amount,
                 b.ct_amount,
                 b.ct_point,
                 (b.ct_qty * (b.ct_amount+b.it_amount)) as ct_sub_amount,
                 (b.ct_qty * b.ct_point)  as ct_sub_point,
                 c.it_id,
                 c.it_name
           $sql_common
           order by $sort1 $sort2
           limit $from_record, $rows ";
$result = sql_query($sql);

$qstr1 = "sel_ca_id=$sel_ca_id&sel_field=$sel_field&search=$search&save_search=$search";
$qstr  = "$qstr1&sort1=$sort1&sort2=$sort2&page=$page";
?>

<form id="frmorderlist" name="frmorderlist">
<input type="hidden" id="doc" name="doc"   value="<? echo $doc ?>">
<input type="hidden" id="sort1" name="sort1" value="<? echo $sort1 ?>">
<input type="hidden" id="page" name="page"  value="<? echo $page ?>">
<table>
<tr>
    <td width=10%><a href='<?=$_SERVER[PHP_SELF]?>'>처음</a></td>
    <td%>
        <!-- <input type="button" value='주문' onclick="location.href='<?="$_SERVER[PHP_SELF]?sort1=$sort1&sort2=$sort2&sel_field=ct_status&search=주문"?>'">
        <input type="button" value='준비' onclick="location.href='<?="$_SERVER[PHP_SELF]?sort1=$sort1&sort2=$sort2&sel_field=ct_status&search=준비"?>'">
        <input type="button" value='배송' onclick="location.href='<?="$_SERVER[PHP_SELF]?sort1=$sort1&sort2=$sort2&sel_field=ct_status&search=배송"?>'">
        <input type="button" value='완료' onclick="location.href='<?="$_SERVER[PHP_SELF]?sort1=$sort1&sort2=$sort2&sel_field=ct_status&search=완료"?>'">
        <input type="button" value='취소' onclick="location.href='<?="$_SERVER[PHP_SELF]?sort1=$sort1&sort2=$sort2&sel_field=ct_status&search=취소"?>'">
        <input type="button" value='반품' onclick="location.href='<?="$_SERVER[PHP_SELF]?sort1=$sort1&sort2=$sort2&sel_field=ct_status&search=반품"?>'">
        <input type="button" value='품절' onclick="location.href='<?="$_SERVER[PHP_SELF]?sort1=$sort1&sort2=$sort2&sel_field=ct_status&search=품절"?>'"> -->
        <!-- utf-8 에서 처리되도록 변경 -->
        <input type="button" value='주문' onclick="location.href='<?="$_SERVER[PHP_SELF]?sort1=$sort1&sort2=$sort2&sel_field=ct_status&search=".urlencode('주문')?>'">
        <input type="button" value='준비' onclick="location.href='<?="$_SERVER[PHP_SELF]?sort1=$sort1&sort2=$sort2&sel_field=ct_status&search=".urlencode('준비')?>'">
        <input type="button" value='배송' onclick="location.href='<?="$_SERVER[PHP_SELF]?sort1=$sort1&sort2=$sort2&sel_field=ct_status&search=".urlencode('배송')?>'">
        <input type="button" value='완료' onclick="location.href='<?="$_SERVER[PHP_SELF]?sort1=$sort1&sort2=$sort2&sel_field=ct_status&search=".urlencode('완료')?>'">
        <input type="button" value='취소' onclick="location.href='<?="$_SERVER[PHP_SELF]?sort1=$sort1&sort2=$sort2&sel_field=ct_status&search=".urlencode('취소')?>'">
        <input type="button" value='반품' onclick="location.href='<?="$_SERVER[PHP_SELF]?sort1=$sort1&sort2=$sort2&sel_field=ct_status&search=".urlencode('반품')?>'">
        <input type="button" value='품절' onclick="location.href='<?="$_SERVER[PHP_SELF]?sort1=$sort1&sort2=$sort2&sel_field=ct_status&search=".urlencode('품절')?>'">
        &nbsp;
        <select id="sel_field" name="sel_field">
            <option value='od_id'>주문번호
            <option value='od_name'>주문자
            <option value='mb_id'>회원 ID
            <option value='od_deposit_name'>입금자
            <option value='c.it_id'>상품코드
            <option value='c.ca_id'>분류코드
            <option value='ct_status'>상태
        </select>
        <input type="hidden" id="save_search" name="save_search" value='<?=$search?>'>
        <input type="text" id="search" name="search" value='<? echo $search ?>' autocomplete="off">
        <input type="image" src='<?=$g4[admin_path]?>/img/btn_search.gif' align=absmiddle>
    </td>
    <td width=10%>건수 : <? echo $total_count ?>&nbsp;</td>
</tr>
</table>


<table cellpadding=0 cellspacing=0>
<colgroup>
<colgroup width=70>
<colgroup width=70>
<colgroup>
<colgroup>
<colgroup>
<colgroup width=30>
<colgroup width=70>
<colgroup>
<colgroup width=30>
<colgroup width=30>
<tr><td colspan=11 height=3 bgcolor=#0E87F9></td></tr>
<tr>
    <td><a href="<?=title_sort("od_id")."&$qstr1";?>">주문번호</a></td>
    <td><a href="<?=title_sort("od_name")."&$qstr1";?>">주문자</a></td>
    <td><a href="<?=title_sort("mb_id")."&$qstr1";?>">회원ID</a></td>
    <td></td>
    <td><a href="<?=title_sort("it_name")."&$qstr1";?>">상품명</a></td>
    <td><a href="<?=title_sort("ct_amount")."&$qstr1";?>">판매가</a></td>
    <td><a href="<?=title_sort("ct_qty")."&$qstr1";?>">수량</a></td>
    <td><a href="<?=title_sort("ct_sub_amount")."&$qstr1";?>">소계</a></td>
    <td><a href="<?=title_sort("ct_sub_point")."&$qstr1";?>">포인트</a></td>
    <td><a href="<?=title_sort("ct_status")."&$qstr1";?>">상태</a></td>
    <td>수정</td>
</tr>

<tr><td colspan=11 height=3 bgcolor=#F8F8F8></td></tr>

<?
for ($i=0; $row=sql_fetch_array($result); $i++) {

    $od_deposit_name = "";
    if ($row[od_deposit_name] != "")
        $od_deposit_name = "title='입금자 : $row[od_deposit_name]'";

    $href = "$_SERVER[PHP_SELF]?sort1=$sort1&sort2=$sort2&sel_field=c.it_id&search=$row[it_id]";
    $it_name = "<a href='$href'>".cut_str($row[it_name],35)."</a><br>";
    $it_name .= print_item_options($row[it_id], $row[it_opt1], $row[it_opt2], $row[it_opt3], $row[it_opt4], $row[it_opt5], $row[it_opt6]);

    $s_mod = icon("수정", "./orderform.php?od_id=$row[od_id]");

    $list = $i%2;

    echo "
    <tr class='list$list center'>
        <td title='주문일시 : $row[od_time]'><a href='$_SERVER[PHP_SELF]?sort1=$sort1&sort2=$sort2&sel_field=od_id&search=$row[od_id]'>$row[od_id]</a></td>
        <td $od_deposit_name><a href='$_SERVER[PHP_SELF]?sort1=$sort1&sort2=$sort2&sel_field=od_name&search=$row[od_name]'>".cut_str($row[od_name],10,"")."</a></td>
        <td><a href='$_SERVER[PHP_SELF]?sort1=$sort1&sort2=$sort2&sel_field=mb_id&search=$row[mb_id]'>$row[mb_id]</a></td>
        <td style='padding-top:5px; padding-bottom:5px;'><a href='$href'>".get_it_image($row['it_id'], 50, 50)."</a></td>
        <td align=left>$it_name</td>
        <td>".number_format($row[it_amount] + $row[ct_amount])."&nbsp;</td>
        <td>$row[ct_qty]</td>
        <td>".number_format($row[ct_sub_amount])."&nbsp;</td>
        <td>".number_format($row[ct_sub_point])."&nbsp;</td>
        <td><a href='$_SERVER[PHP_SELF]?sort1=$sort1&sort2=$sort2&sel_field=ct_status&search=$row[ct_status]'>$row[ct_status]</a></td>
        <td>$s_mod</td>
    </tr>";

    $tot_amount += $row[ct_amount];
    $tot_qty    += $row[ct_qty];
    $tot_sub_amount += $row[ct_sub_amount];
    $tot_sub_point  += $row[ct_sub_point];
}

if ($i == 0)
    echo "<tr><td colspan=11 height=100 bgcolor=#ffffff><span class=point>자료가 한건도 없습니다.</span></td></tr>\n";
?>

<tr>
    <td colspan=5>합 계&nbsp;</td>
    <td><?=number_format($tot_amount)?>&nbsp;</td>
    <td><?=number_format($tot_qty)?>&nbsp;</td>
    <td><?=number_format($tot_sub_amount)?>&nbsp;</td>
    <td><?=number_format($tot_sub_point)?>&nbsp;</td>
    <td colspan=2></td>
</tr>

</table>

<table>
<tr>
    <td>&nbsp;</td>
    <td><?=get_paging($config[cf_write_pages], $page, $total_page, "$_SERVER[PHP_SELF]?$qstr&page=");?></td>
</tr>
</table>
</form>


<script>
var f = document.frmorderlist;
f.sel_field.value  = '<? echo $sel_field ?>';
</script>

<?
include_once(G4_ADMIN_PATH."/admin.tail.php");
?>
