<?php
define('G5_IS_ADMIN', true);
include_once ('../common.php');

if (!$is_demo && $is_admin != 'super') {
    alert_close("최고관리자만 가능합니다.");
}

include_once(G5_ADMIN_PATH.'/apms_admin/apms.admin.lib.php');
include_once(G5_LIB_PATH.'/apms.widget.lib.php');

if($skin == 'group') { //보드그룹
	$skin_pc = $group['as_main'];
	$skin_mobile = $group['as_mobile_main'];
	$group_skin_path = G5_SKIN_PATH.'/group/'.$group['as_'.MOBILE_.'main'];
	$group_skin_url = G5_SKIN_URL.'/group/'.$group['as_'.MOBILE_.'main'];
	$skin_path = $group_skin_path;
	$skin_url = $group_skin_url;
	$title = $group['gr_subject'].' 보드그룹';
} else if($skin == 'connect') { //현재접속자
	$skin_pc = $config['cf_connect_skin'];
	$skin_mobile = $config['cf_mobile_connect_skin'];
	$skin_path = $connect_skin_path;
	$skin_url = $connect_skin_url;
	$title = '현재접속자';
} else if($skin == 'faq') { //faq
	$skin_pc = $config['cf_faq_skin'];
	$skin_mobile = $config['cf_mobile_faq_skin'];
	$skin_path = $faq_skin_path;
	$skin_url = $faq_skin_url;
	$title = 'FAQ';
} else if($skin == 'member') { //회원스킨
	$skin_pc = $config['cf_member_skin'];
	$skin_mobile = $config['cf_mobile_member_skin'];
	$skin_path = $member_skin_path;
	$skin_url = $member_skin_url;
	$title = '회원스킨';
} else if($skin == 'new') { //새글
	$skin_pc = $config['cf_new_skin'];
	$skin_mobile = $config['cf_mobile_new_skin'];
	$skin_path = $new_skin_path;
	$skin_url = $new_skin_url;
	$title = '새글모음';
} else if($skin == 'search') { //게시물검색
	$skin_pc = $config['cf_search_skin'];
	$skin_mobile = $config['cf_mobile_search_skin'];
	$skin_path = $search_skin_url;
	$skin_url = $search_skin_path;
	$title = '게시물검색';
} else if($skin == 'tag') { //태그박스
	$skin_pc = $config['cf_mobile_search_skin'];
	$skin_mobile = $config['cf_mobile_search_skin'];
	$skin_path = get_skin_path('tag', (G5_IS_MOBILE ? $config['as_mobile_tag_skin'] : $config['as_tag_skin']));
	$skin_url = get_skin_url('tag', (G5_IS_MOBILE ? $config['as_mobile_tag_skin'] : $config['as_tag_skin']));
	$title = '태그박스';
} else if($skin == 'qa') { //1:1문의
	$qaconfig = get_qa_config();
	$skin_pc = $qaconfig['qa_skin'];
	$skin_mobile = $qaconfig['qa_mobile_skin'];
	$skin_path = get_skin_path('qa', (G5_IS_MOBILE ? $qaconfig['qa_mobile_skin'] : $qaconfig['qa_skin']));
	$skin_url = get_skin_url('qa', (G5_IS_MOBILE ? $qaconfig['qa_mobile_skin'] : $qaconfig['qa_skin']));
	$title = '1:1문의';
} else {
   alert_close('값이 넘어오지 않았습니다.');
}

$skin_file = $skin_path.'/setup.skin.php';

if(!is_file($skin_file)) {
    alert_close('설정을 할 수 없는 스킨입니다.');
}

if(isset($mode) && $mode == "save") {

	if ($is_admin != 'super') {
		alert("최고관리자만 가능합니다.");
	}

	$wset = apms_pack($_POST['wset']);

	$sql_group = $ins_group = "";
	if($gr_id) {
		$sql_group = "and data_1 = '{$gr_id}'";
		$ins_group = ", data_1 = '{$gr_id}'";
	}

	// 등록여부 체크
	if(G5_IS_MOBILE) {
		$data = sql_fetch(" select * from {$g5['apms_data']} where type = '30' and data_q = '{$skin}_mobile' $sql_group ");
		if($data['id']) {
			sql_query(" update {$g5['apms_data']} set data_set = '{$wset}' where type = '30' and data_q = '{$skin}_mobile' $sql_group ");
		} else {
			sql_query(" insert {$g5['apms_data']} set type = '30', data_q = '{$skin}_mobile', data_set = '{$wset}' $ins_group ");
		}
	} else {
		$data = sql_fetch(" select * from {$g5['apms_data']} where type = '30' and data_q = '{$skin}' $sql_group ");
		if($data['id']) {
			sql_query(" update {$g5['apms_data']} set data_set = '{$wset}' where type = '30' and data_q = '{$skin}' $sql_group ");
		} else {
			sql_query(" insert {$g5['apms_data']} set type = '30', data_q = '{$skin}', data_set = '{$wset}' $ins_group ");
		}
	}

	if($skin_pc == $skin_mobile) { //스킨이 같으면 동일값 적용
		if(G5_IS_MOBILE) { //모바일은 PC값
			$data = sql_fetch(" select * from {$g5['apms_data']} where type = '30' and data_q = '{$skin}' $sql_group ");
			if($data['id']) {
				sql_query(" update {$g5['apms_data']} set data_set = '{$wset}' where type = '30' and data_q = '{$skin}' $sql_group ");
			} else {
				sql_query(" insert {$g5['apms_data']} set type = '30', data_q = '{$skin}', data_set = '{$wset}' $ins_group ");
			}
		} else { //PC는 모바일값
			$data = sql_fetch(" select * from {$g5['apms_data']} where type = '30' and data_q = '{$skin}_mobile' $sql_group ");
			if($data['id']) {
				sql_query(" update {$g5['apms_data']} set data_set = '{$wset}' where type = '30' and data_q = '{$skin}_mobile' $sql_group ");
			} else {
				sql_query(" insert {$g5['apms_data']} set type = '30', data_q = '{$skin}_mobile', data_set = '{$wset}' $ins_group ");
			}
		}
	}

	$goto_url = './skin.setup.php?skin='.urlencode($skin);
	if($gr_id)
		$goto_url .= '&amp;gr_id='.$gr_id;

	goto_url($goto_url);
}

$wset = (G5_IS_MOBILE) ? apms_skin_set($skin.'_mobile', $gr_id) : apms_skin_set($skin, $gr_id);

$g5['title'] = $title.' 스킨설정';
include_once(G5_PATH.'/head.sub.php');
?>
<div id="sch_skin_frm" class="new_win bsp_new_win">
    <h1><?php echo $g5['title'];?></h1>
	<form id="fsetup" name="fsetup" method="post">
	<input type="hidden" name="mode" value="save">
	<input type="hidden" name="skin" value="<?php echo $skin;?>">
	<input type="hidden" name="gr_id" value="<?php echo (isset($gr_id) && $gr_id) ? $gr_id : '';?>">

	<?php include_once($skin_file); ?>

    <div class="btn_confirm01 btn_confirm">
		<input type="submit" value="확인" class="btn_submit" accesskey="s">
		<button type="button" onclick="window.close();">닫기</button>
    </div>
	</form>
	<br>
</div>
<script>
var win_h = parseInt($('#sch_skin_frm').height()) + 80;
if(win_h > screen.height) {
    win_h = screen.height - 40;
}

window.moveTo(0, 0);
window.resizeTo(640, win_h);
</script>
<?php include_once(G5_PATH.'/tail.sub.php'); ?>
