<?php
$sub_menu = '400300';
include_once('./_common.php');

auth_check($auth[$sub_menu], "r");

$g5['title'] = '상품관리';
include_once (G5_ADMIN_PATH.'/admin.head.php');

// 시중가 대비 가격변경 시작
if($frice_round) {
	if($sca) {
		$where = "where ca_id = '$sca'";
	}
	$sql2 = "update g5_shop_item set it_price=truncate(round(it_cust_price * '$frice_round'),0) $where";
	sql_query($sql2);
}
// 시중가 대비 가격변경 끝

// 시중가 대비 가격변경 시작
if($frice_round2) {
	if($sca) {
		$where = "where ca_id = '$sca'";
	}
	$sql2 = "update g5_shop_item set it_price=truncate(round(it_price * '$frice_round2'),0) $where";
	sql_query($sql2);
}
// 시중가 대비 가격변경 끝

// 시중가 대비 가격변경 시작
if($frice_round3) {
	if($sca) {
		$where = "where ca_id = '$sca'";
	}
	$sql2 = "update g5_shop_item set it_cust_price=truncate(round(it_price * '$frice_round3'),0) $where";
	sql_query($sql2);
}
// 시중가 대비 가격변경 끝

// 시중가 대비 가격변경 시작
if($frice_round4) {
	if($sca) {
		$where = "where ca_id = '$sca'";
	}
	$sql2 = "update g5_shop_item set it_price=truncate(round(it_price / '$frice_round4'),0) $where";
	sql_query($sql2);
}
// 시중가 대비 가격변경 끝

// 상품명 일괄 변경 시작
if($target_name) {
	if($sca) {
		$where = "where ca_id = '$sca'";
	}
	$sql2 = "update g5_shop_item set it_name=replace(it_name,'$source_name','$target_name') $where";
	sql_query($sql2);
}
// 상품명 일괄 변경 끝

// 상품명 기준 카테고리 일괄 변경 시작
// if($replace_caid) {
// 	if($sca) {
// 		$where = "where ca_id = '$sca'";
// 	}
	//분류 로드
// $ca_list  = '<option value="'.$replace_caid.'">변경할 분류 선택</option>'.PHP_EOL;
// $sql = " select * from {$g5['g5_shop_category_table']} ";
// if ($is_admin != 'super')
//     $sql .= " where ca_mb_id = '{$member['mb_id']}' ";
// $sql .= " order by ca_order, ca_id ";
// $result = sql_query($sql);
// for ($i=0; $row=sql_fetch_array($result); $i++)
// {
//     $len = strlen($row['ca_id']) / 2 - 1;
//     $nbsp = '';
//     for ($i=0; $i<$len; $i++) {
//         $nbsp .= '&nbsp;&nbsp;&nbsp;';
//     }
//     $ca_list .= '<option value="'.$row['ca_id'].'">'.$nbsp.$row['ca_name'].'</option>'.PHP_EOL;
// }
	//분류 로드 끝
	
// 	$sql2 = "update g5_shop_item set ca_id = $ca_list where it_name like '%$reptxt%' $where";
// 	
// 	sql_query($sql2);
// }
// 상품명 기준 카테고리 일괄 변경 끝

// 분류
$ca_list  = '<option value="">선택</option>'.PHP_EOL;
$sql = " select * from {$g5['g5_shop_category_table']} ";
if ($is_admin != 'super')
    $sql .= " where ca_mb_id = '{$member['mb_id']}' ";
$sql .= " order by ca_order, ca_id ";
$result = sql_query($sql);
for ($i=0; $row=sql_fetch_array($result); $i++)
{
    $len = strlen($row['ca_id']) / 2 - 1;
    $nbsp = '';
    for ($i=0; $i<$len; $i++) {
        $nbsp .= '&nbsp;&nbsp;&nbsp;';
    }
    $ca_list .= '<option value="'.$row['ca_id'].'">'.$nbsp.$row['ca_name'].'</option>'.PHP_EOL;
}

// 다중검색 시작

// 다중검색 끝

$where = " and ";
$sql_search = "";
if ($stx != "") {
    if ($sfl != "") {
        $sql_search .= " $where $sfl like '%$stx%' ";
        $where = " and ";
    }
    if ($save_stx != $stx)
        $page = 1;
}

if ($sca != "") {
    $sql_search .= " $where (a.ca_id like '$sca%' or a.ca_id2 like '$sca%' or a.ca_id3 like '$sca%') ";
}

if ($sfl == "")  $sfl = "it_name";

$sql_common = " from {$g5['g5_shop_item_table']} a ,
                     {$g5['g5_shop_category_table']} b
               where (a.ca_id = b.ca_id";
if ($is_admin != 'super')
    $sql_common .= " and b.ca_mb_id = '{$member['mb_id']}'";
$sql_common .= ") ";
$sql_common .= $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 < 1) { $page = 1; } // 페이지가 없으면 첫 페이지 (1 페이지)
$from_record = ($page - 1) * $rows; // 시작 열을 구함

if (!$sst) {
    $sst  = "it_id";
    $sod = "desc";
}
$sql_order = "order by $sst $sod";


$sql  = " select *
           $sql_common
           $sql_order
           limit $from_record, $rows ";
$result = sql_query($sql);

//$qstr  = $qstr.'&amp;sca='.$sca.'&amp;page='.$page;
$qstr  = $qstr.'&amp;sca='.$sca.'&amp;page='.$page.'&amp;save_stx='.$stx;

$listall = '<a href="'.$_SERVER['SCRIPT_NAME'].'" class="ov_listall">전체목록</a>';
?>

<div class="local_ov01 local_ov">
    <?php echo $listall; ?>
    등록된 상품 <?php echo $total_count; ?>건
</div>

<form name="flist" class="local_sch01 local_sch">
<input type="hidden" name="page" value="<?php echo $page; ?>">
<input type="hidden" name="save_stx" value="<?php echo $stx; ?>">

<label for="sca" class="sound_only">분류선택</label>
<select name="sca" id="sca" style="height:25px">
    <option value="">전체분류</option>
    <?php
    $sql1 = " select ca_id, ca_name from {$g5['g5_shop_category_table']} order by ca_order, ca_id ";
    $result1 = sql_query($sql1);
    for ($i=0; $row1=sql_fetch_array($result1); $i++) {
        $len = strlen($row1['ca_id']) / 2 - 1;
        $nbsp = '';
        for ($i=0; $i<$len; $i++) $nbsp .= '&nbsp;&nbsp;&nbsp;';
        echo '<option value="'.$row1['ca_id'].'" '.get_selected($sca, $row1['ca_id']).'>'.$nbsp.$row1['ca_name'].'</option>'.PHP_EOL;
    }
    ?>
</select>

<label for="sfl" class="sound_only">검색대상</label>
<select name="sfl" id="sfl" style="height:25px">
    <option value="it_name" <?php echo get_selected($sfl, 'it_name'); ?>>상품명</option>
    <option value="it_sell_email" <?php echo get_selected($sfl, 'it_sell_email'); ?>>쇼핑몰 관리코드</option>
	<option value="it_id" <?php echo get_selected($sfl, 'it_id'); ?>>상품코드</option>
    <option value="it_maker" <?php echo get_selected($sfl, 'it_maker'); ?>>제조사</option>
    <option value="it_origin" <?php echo get_selected($sfl, 'it_origin'); ?>>원산지</option>
</select>
<label for="stx" class="sound_only">검색어</label>
<input type="text" name="stx" value="<?php echo $stx; ?>" id="stx" class="frm_input" size="50">
<input type="submit" value="검색" class="btn_submit">

<!-- 가격 일괄변경 -->
<br><br>
<b><font color="red">[가격일괄변경]</font></b>
① 판매가 = 시중가격 <b>X</b> <input type="text" name="frice_round" id="stx" class="frm_input" value="<?php echo $frice_round; ?>">  　
② 판매가 = 판매가격 <b>X</b> <input type="text" name="frice_round2" id="stx" class="frm_input" value="<?php echo $frice_round2; ?>">  　
③ 시중가 = 판매가격 <b>X</b> <input type="text" name="frice_round3" id="stx" class="frm_input" value="<?php echo $frice_round3; ?>">
 <input type="submit" value="변경하기" class="btn_submit"><b><font color="red"> ※ 50%는 0.5 입력(%를 정수로 환산해서 입력)</font></b>
 <!-- 가격 일괄변경 -->
 <!-- 상품명 일괄변경 -->
 <br><br>
<b><font color="red">[상품명 일괄변경]</font></b>
 상품명 <input type="text" name="source_name" id="stx" class="frm_input" value="<?php echo $source_name; ?>">을(를) <input type="text" name="target_name" id="stx" class="frm_input" value="<?php echo $target_name; ?>">으로 
 <input type="submit" value="변경하기" class="btn_submit">
 <!-- 상품명 일괄변경 -->
<!-- 가격 일괄변경 -->
<br><br>
<b><font color="red">[할인후 가격복구]</font></b>
② 판매가 = 판매가격 <b>÷</b> <input type="text" name="frice_round4" id="stx" class="frm_input" value="<?php echo $frice_round4; ?>">  　
 <input type="submit" value="변경하기" class="btn_submit"><b><font color="red"> ※ 50%는 0.5 입력(%를 정수로 환산해서 입력) / 30% 할인 후 정상가 환원할때는 70%값 입력 ex) 0.7 </font></b>
 <!-- 가격 일괄변경 -->
</form>

<div class="btn_add01 btn_add">
    <a href="./itemform.php">상품등록</a>
    <a href="./itemexcel.php" onclick="return excelform(this.href);" target="_blank">상품일괄등록</a>
	<!--<a href="./add_itemoptionexcel.php" onclick="return excelform(this.href);" target="_blank">상품옵션일괄등록</a>-->
</div>

<form name="fitemlistupdate" method="post" action="./itemlistupdate.php" onsubmit="return fitemlist_submit(this);" autocomplete="off">
<input type="hidden" name="sca" value="<?php echo $sca; ?>">
<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; ?>">

<div class="tbl_head02 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"><?php echo subject_sort_link('it_id', 'sca='.$sca); ?>상품코드</a></th>
		<th scope="col" id="th_img"><?php echo subject_sort_link('it_sell_email', 'sca='.$sca); ?>쇼핑몰</p>관리코드</a></th>
        <th scope="col">분류</th>
		<th scope="col" id="th_img">이미지</th>
		<th scope="col" id="th_pc_title"><?php echo subject_sort_link('it_name', 'sca='.$sca); ?>상품명</a></th>
		<th scope="col" id="th_amt"><?php echo subject_sort_link('it_price', 'sca='.$sca); ?>판매가격</a></th>
        <th scope="col" id="th_camt"><?php echo subject_sort_link('it_cust_price', 'sca='.$sca); ?>시중가격</a></th>
		<th scope="col" id="th_pt"><?php echo subject_sort_link('it_point', 'sca='.$sca); ?>포인트</a></th>
        <th scope="col" id="th_qty"><?php echo subject_sort_link('it_stock_qty', 'sca='.$sca); ?>재고</a></th>
		<th scope="col" id="th_skin">PC스킨</th>
		<th scope="col" id="th_mskin">모바일스킨</th>
        <th scope="col"><?php echo subject_sort_link('it_order', 'sca='.$sca); ?>순서</a></th>
        <th scope="col">
		<?php echo subject_sort_link('it_use', 'sca='.$sca, 1); ?>판매</a><br>
		<input type="checkbox" id="pdcheck" value="모두선택"/>
		</th>
		<script type="text/javascript">
			$(document).ready(function(){
				//체크박스 모두선택
				$("#pdcheck").click(function(){
					if($("#pdcheck").prop("checked")){
						$("input[class=chkpd]").prop("checked",true);
					}
					else {
						$("input[class=chkpd]").prop("checked",false);
					}
				});
		});
		</script>
        <th scope="col"><?php echo subject_sort_link('it_soldout', 'sca='.$sca, 1); ?>품절</a></th>
        <th scope="col"><?php echo subject_sort_link('it_hit', 'sca='.$sca, 1); ?>조회</a></th>
        <th scope="col">관리</th>
    </tr>
    </thead>
    <tbody>
    <?php
    for ($i=0; $row=sql_fetch_array($result); $i++)
    {
        $href = G5_SHOP_URL.'/item.php?it_id='.$row['it_id'];
        $bg = 'bg'.($i%2);

        $it_point = $row['it_point'];
        if($row['it_point_type'])
            $it_point .= '%';
    ?>
    <tr class="<?php echo $bg; ?>">
        <td class="td_chk" style="width:10px">
            <label for="chk_<?php echo $i; ?>" class="sound_only"><?php echo get_text($row['it_name']); ?></label>
            <input type="checkbox" name="chk[]" class="all" value="<?php echo $i ?>" id="chk_<?php echo $i; ?>">
        </td>
        <td class="td_num">
            <input type="hidden" name="it_id[<?php echo $i; ?>]" value="<?php echo $row['it_id']; ?>">
            <?php echo $row['it_id']; ?>
        </td>
		<td class="td_num">
            <label for="sell_email_<?php echo $i; ?>" class="sound_only">관리코드</label>
            <input type="text" name="it_sell_email[<?php echo $i; ?>]" value="<?php echo htmlspecialchars2(cut_str($row['it_sell_email'],250, "")); ?>" id="sell_email_<?php echo $i; ?>" size="5">
        </td>
        <td style="width:340px">
            <label for="ca_id_<?php echo $i; ?>" class="sound_only"><?php echo get_text($row['it_name']); ?> 기본분류</label>
            <select name="ca_id[<?php echo $i; ?>]" id="ca_id_<?php echo $i; ?>" style="width:110px">
                <?php echo conv_selected_option($ca_list, $row['ca_id']); ?>
            </select>
            <label for="ca_id2_<?php echo $i; ?>" class="sound_only"><?php echo get_text($row['it_name']); ?> 2차분류</label>
            <select name="ca_id2[<?php echo $i; ?>]" id="ca_id2_<?php echo $i; ?>" style="width:110px">
                <?php echo conv_selected_option($ca_list, $row['ca_id2']); ?>
            </select>
            <label for="ca_id3_<?php echo $i; ?>" class="sound_only"><?php echo get_text($row['it_name']); ?> 3차분류</label>
            <select name="ca_id3[<?php echo $i; ?>]" id="ca_id3_<?php echo $i; ?>" style="width:110px">
                <?php echo conv_selected_option($ca_list, $row['ca_id3']); ?>
            </select>
        </td>
		<td class="td_img"><a href="<?php echo $href; ?>"><?php echo get_it_image($row['it_id'], 35, 35); ?></a></td>
		<td headers="th_pc_title" class="td_input">
            <label for="name_<?php echo $i; ?>" class="sound_only">상품명</label>
            <input type="text" name="it_name[<?php echo $i; ?>]" value="<?php echo htmlspecialchars2(cut_str($row['it_name'],250, "")); ?>" id="name_<?php echo $i; ?>" required class="frm_input required" size="30">
        </td>
        <td headers="th_amt" class="td_numbig td_input" style="width:65px">
            <label for="price_<?php echo $i; ?>" class="sound_only">판매가격</label>
            <input type="text" name="it_price[<?php echo $i; ?>]" value="<?php echo $row['it_price']; ?>" id="price_<?php echo $i; ?>" class="frm_input sit_amt" size="4" style="width:60px">
        </td>
        <td headers="th_camt" class="td_numbig td_input" style="width:65px">
            <label for="cust_price_<?php echo $i; ?>" class="sound_only">시중가격</label>
            <input type="text" name="it_cust_price[<?php echo $i; ?>]" value="<?php echo $row['it_cust_price']; ?>" id="cust_price_<?php echo $i; ?>" class="frm_input sit_camt" size="4" style="width:60px">
        </td>
		<td headers="th_pt" class="td_numbig td_input" style="width:40px"><?php echo $it_point; ?></td>
        <td headers="th_qty" class="td_numbig td_input" style="width:50px">
            <label for="stock_qty_<?php echo $i; ?>" class="sound_only">재고</label>
            <input type="text" name="it_stock_qty[<?php echo $i; ?>]" value="<?php echo $row['it_stock_qty']; ?>" id="stock_qty_<?php echo $i; ?>" class="frm_input sit_qty" size="7" style="width:45px">
        </td>
		<td headers="th_skin" class="td_numbig td_input">
            <label for="it_skin_<?php echo $i; ?>" class="sound_only">PC 스킨</label>
            <?php echo get_skin_select('shop', 'it_skin_'.$i, 'it_skin['.$i.']', $row['it_skin']); ?>
        </td>
        
        <td headers="th_mskin" class="td_numbig td_input">
            <label for="it_mobile_skin_<?php echo $i; ?>" class="sound_only">모바일 스킨</label>
            <?php echo get_mobile_skin_select('shop', 'it_mobile_skin_'.$i, 'it_mobile_skin['.$i.']', $row['it_mobile_skin']); ?>
        </td>
        <td class="td_mngsmall" style="width:30px">
            <label for="order_<?php echo $i; ?>" class="sound_only">순서</label>
            <input type="text" name="it_order[<?php echo $i; ?>]" value="<?php echo $row['it_order']; ?>" id="order_<?php echo $i; ?>" class="frm_input" size="3">
        </td>
        <td class="td_chk">
            <label for="use_<?php echo $i; ?>" class="sound_only">판매여부</label>
            <input type="checkbox" class="chkpd" name="it_use[<?php echo $i; ?>]" <?php echo ($row['it_use'] ? 'checked' : ''); ?> value="1" id="use_<?php echo $i; ?>">
        </td>
        <td class="td_chk">
            <label for="soldout_<?php echo $i; ?>" class="sound_only">품절</label>
            <input type="checkbox" name="it_soldout[<?php echo $i; ?>]" <?php echo ($row['it_soldout'] ? 'checked' : ''); ?> value="1" id="soldout_<?php echo $i; ?>">
        </td>
        <td class="td_num" style="width:30px"><?php echo $row['it_hit']; ?></td>
        <td class="td_mng">
            <a href="./itemform.php?w=u&amp;it_id=<?php echo $row['it_id']; ?>&amp;ca_id=<?php echo $row['ca_id']; ?>&amp;<?php echo $qstr; ?>"><span class="sound_only"><?php echo htmlspecialchars2(cut_str($row['it_name'],250, "")); ?> </span>수정</a>
            <a href="./itemcopy.php?it_id=<?php echo $row['it_id']; ?>&amp;ca_id=<?php echo $row['ca_id']; ?>" class="itemcopy" target="_blank"><span class="sound_only"><?php echo htmlspecialchars2(cut_str($row['it_name'],250, "")); ?> </span>복사</a>
            <a href="<?php echo $href; ?>"><span class="sound_only"><?php echo htmlspecialchars2(cut_str($row['it_name'],250, "")); ?> </span>보기</a>
        </td>
    </tr>
    <?php
    }
    if ($i == 0)
        echo '<tr><td colspan="12" class="empty_table">자료가 한건도 없습니다.</td></tr>';
    ?>
    </tbody>
    </table>
</div>

<div class="btn_list01 btn_list" style="position:fixed;left:0;bottom:0;background:#000;border:2px solid #000">
    <input type="submit" name="act_button" value="선택수정" onclick="document.pressed=this.value">
    <?php if ($is_admin == 'super') { ?>
    <input type="submit" name="act_button" value="선택삭제" onclick="document.pressed=this.value">
    <?php } ?>
</div>
<!-- <div class="btn_confirm01 btn_confirm">
    <input type="submit" value="일괄수정" class="btn_submit" accesskey="s">
</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 fitemlist_submit(f)
{
    if (!is_checked("chk[]")) {
        alert(document.pressed+" 하실 항목을 하나 이상 선택하세요.");
        return false;
    }

    if(document.pressed == "선택삭제") {
        if(!confirm("선택한 자료를 정말 삭제하시겠습니까?")) {
            return false;
        }
    }

    return true;
}

$(function() {
    $(".itemcopy").click(function() {
        var href = $(this).attr("href");
        window.open(href, "copywin", "left=100, top=100, width=300, height=200, scrollbars=0");
        return false;
    });
});

function excelform(url)
{
    var opt = "width=600,height=450,left=10,top=10";
    window.open(url, "win_excel", opt);
    return false;
}
</script>

<?php
include_once (G5_ADMIN_PATH.'/admin.tail.php');
?>
