<?php
$sub_menu = '100320';
include_once('./_common.php');
include_once(G5_EDITOR_LIB);

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

$g5['vpn_msg_table'] = 'strongswan_vpn_msg';

$msg_id = isset($_REQUEST['msg_id']) ? preg_replace('/[^0-9]/', '', $_REQUEST['msg_id']) : 0;

$html_title = "팝업레이어(VPN앱전용)";

if ($w == "u")
{
    $html_title .= " 수정";
    sql_query("set names utf8mb4");
    $sql = " select * from {$g5['vpn_msg_table']} where msg_id = '$msg_id' ";
    $msg = sql_fetch($sql);
    if (!$msg['msg_id']) alert("등록된 자료가 없습니다.");
}
else
{
    $html_title .= " 입력";
    $msg['msg_type'] = 'msg';
    $msg['msg_target'] = 'all';
    $msg['msg_lang'] = 'all';
    $msg['msg_output'] = 1;
    $msg['msg_begin_time'] = date("Y-m-d 00:00:00", G5_SERVER_TIME);
    $msg['msg_end_time'] = date("Y-m-d 23:59:59", G5_SERVER_TIME+(60*60*24*7));
    $msg['msg_order'] = 0;
}

// 언어 목록 스캔
$lang_list = array();
$lang_dir = G5_PATH . '/mylib/strongswan_profile_check_inc_language_free/';
if (is_dir($lang_dir)) {
    if ($dh = opendir($lang_dir)) {
        while (($file = readdir($dh)) !== false) {
            if (preg_match('/^strongswan_profile_check_inc_([a-zA-Z0-9-]+)\.php$/', $file, $matches)) {
                 $lang_code = $matches[1];
                 if($lang_code != 'common_lib') { // common lib 제외
                    $lang_list[] = $lang_code;
                 }
            }
        }
        closedir($dh);
    }
}
sort($lang_list);

// 언어 코드별 한국어 명칭 매핑
$lang_names = array(
    'ar' => '아랍어 (ar)',
    'bn' => '벵골어 (bn)',
    'de' => '독일어 (de)',
    'en' => '영어 (en)',
    'es-es' => '스페인어 (es-es)',
    'fa' => '페르시아어 (fa)',
    'fr' => '프랑스어 (fr)',
    'hi' => '힌디어 (hi)',
    'it' => '이탈리아어 (it)',
    'ja' => '일본어 (ja)',
    'ko' => '한국어 (ko)',
    'ms' => '말레이어 (ms)',
    'pl' => '폴란드어 (pl)',
    'pt-br' => '포르투갈어-브라질 (pt-br)',
    'pt-pt' => '포르투갈어-포르투갈 (pt-pt)',
    'ru' => '러시아어 (ru)',
    'tr' => '터키어 (tr)',
    'uk' => '우크라이나어 (uk)',
    'vi' => '베트남어 (vi)',
    'zh-cn' => '중국어 간체 (zh-cn)',
    'zh-tw' => '중국어 번체 (zh-tw)'
);

$g5['title'] = $html_title;
include_once (G5_ADMIN_PATH.'/admin.head.php');
?>

<form name="frmvpnmsg" action="./vpn_msg_form_update.php" onsubmit="return frmvpnmsg_check(this);" method="post">
<input type="hidden" name="w" value="<?php echo $w; ?>">
<input type="hidden" name="msg_id" value="<?php echo $msg_id; ?>">
<input type="hidden" name="token" value="">

<div class="local_desc01 local_desc">
    <p>VPN 앱에서 표시될 팝업 메시지를 설정합니다.</p>
</div>

<div class="tbl_frm01 tbl_wrap">
    <table>
    <caption><?php echo $g5['title']; ?></caption>
    <colgroup>
        <col class="grid_4">
        <col>
    </colgroup>
    <tbody>
    <tr>
        <th scope="row"><label for="msg_type">Type</label></th>
        <td>
             <input type="text" name="msg_type" value="<?php echo $msg['msg_type']; ?>" id="msg_type" required class="frm_input required" size="20">
             <?php echo help("기본값 msg"); ?>
        </td>
    </tr>
    <tr>
        <th scope="row"><label for="msg_target">대상 고객</label></th>
        <td>
            <select name="msg_target" id="msg_target">
                <option value="all"<?php echo get_selected('all', $msg['msg_target']); ?>>전체</option>
                <option value="free"<?php echo get_selected('free', $msg['msg_target']); ?>>무료 VPN 사용자</option>
                <option value="paid"<?php echo get_selected('paid', $msg['msg_target']); ?>>유료 VPN 사용자</option>
            </select>
        </td>
    </tr>
    <tr>
        <th scope="row"><label for="msg_lang">앱 언어</label></th>
        <td>
            <select name="msg_lang" id="msg_lang">
                <option value="all"<?php echo get_selected('all', $msg['msg_lang']); ?>>전체 언어</option>
                <?php foreach($lang_list as $lang) {
                    $lang_text = isset($lang_names[$lang]) ? $lang_names[$lang] : $lang;
                ?>
                <option value="<?php echo $lang; ?>"<?php echo get_selected($lang, $msg['msg_lang']); ?>><?php echo $lang_text; ?></option>
                <?php } ?>
            </select>
        </td>
    </tr>
    <tr>
        <th scope="row"><label for="msg_title">제목<strong class="sound_only"> 필수</strong></label></th>
        <td>
            <input type="text" name="msg_title" value="<?php echo $msg['msg_title']; ?>" id="msg_title" required class="frm_input required" size="80">
        </td>
    </tr>
    <tr>
        <th scope="row"><label for="msg_video_url">동영상 URL</label></th>
        <td>
            <input type="text" name="msg_video_url" value="<?php echo isset($msg['msg_video_url']) ? $msg['msg_video_url'] : ''; ?>" id="msg_video_url" class="frm_input" size="80">
            <?php echo help("YouTube 영상 주소 또는 동영상 파일(mp4 등) URL을 입력하세요. 입력 시 팝업에 동영상이 표시됩니다."); ?>
        </td>
    </tr>
    <tr>
        <th scope="row"><label for="msg_link_url">바로가기 링크 URL</label></th>
        <td>
            <input type="text" name="msg_link_url" value="<?php echo isset($msg['msg_link_url']) ? $msg['msg_link_url'] : ''; ?>" id="msg_link_url" class="frm_input" size="80">
            <?php echo help("입력 시 팝업의 텍스트나 이미지를 클릭하면 해당 URL로 이동합니다."); ?>
        </td>
    </tr>
    <tr>
        <th scope="row"><label for="msg_begin_time">시작일시<strong class="sound_only"> 필수</strong></label></th>
        <td>
            <input type="text" name="msg_begin_time" value="<?php echo $msg['msg_begin_time']; ?>" id="msg_begin_time" required class="frm_input required" size="21" maxlength="19">
            <input type="checkbox" name="msg_begin_chk" value="<?php echo date("Y-m-d 00:00:00", G5_SERVER_TIME); ?>" id="msg_begin_chk" onclick="if (this.checked == true) this.form.msg_begin_time.value=this.form.msg_begin_chk.value; else this.form.msg_begin_time.value = this.form.msg_begin_time.defaultValue;">
            <label for="msg_begin_chk">시작일시를 오늘로</label>
        </td>
    </tr>
    <tr>
        <th scope="row"><label for="msg_end_time">종료일시<strong class="sound_only"> 필수</strong></label></th>
        <td>
            <input type="text" name="msg_end_time" value="<?php echo $msg['msg_end_time']; ?>" id="msg_end_time" required class="frm_input required" size="21" maxlength="19">
            <input type="checkbox" name="msg_end_chk" value="<?php echo date("Y-m-d 23:59:59", G5_SERVER_TIME+(60*60*24*7)); ?>" id="msg_end_chk" onclick="if (this.checked == true) this.form.msg_end_time.value=this.form.msg_end_chk.value; else this.form.msg_end_time.value = this.form.msg_end_time.defaultValue;">
            <label for="msg_end_chk">종료일시를 오늘로부터 7일 후로</label>
        </td>
    </tr>
    <tr>
        <th scope="row"><label for="msg_order">출력순서</label></th>
        <td>
            <input type="text" name="msg_order" value="<?php echo $msg['msg_order']; ?>" id="msg_order" class="frm_input" size="5">
        </td>
    </tr>
    <tr>
        <th scope="row"><label for="msg_output">출력여부</label></th>
        <td>
            <input type="checkbox" name="msg_output" value="1" id="msg_output" <?php echo ($msg['msg_output'] ? "checked" : ""); ?>>
            <label for="msg_output">사용</label>
        </td>
    </tr>
    <tr>
        <th scope="row">내용</th>
        <td>
            <?php echo editor_html('msg_content', get_text($msg['msg_content'], 0)); ?>
        </td>
    </tr>
    </tbody>
    </table>
</div>

<div class="btn_confirm01 btn_confirm">
    <input type="submit" value="확인" class="btn_submit" accesskey="s">
    <a href="./vpn_msg_list.php">목록</a>
</div>

</form>

<script>
function frmvpnmsg_check(f)
{
    <?php echo get_editor_js('msg_content'); ?>

    return true;
}
</script>

<?php
include_once (G5_ADMIN_PATH.'/admin.tail.php');
?>
