<?php
include_once __DIR__ . '/_common.php';
include_once G5_EDITOR_LIB;

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

// 테이블이 없을 경우 생성
if (!sql_fetch_array(sql_query("DESC {$g5['font_table']}"))) {
  $sql = "CREATE TABLE IF NOT EXISTS {$g5['font_table']} (
              font_id INT(11) NOT NULL AUTO_INCREMENT,
              font_name VARCHAR(255) NOT NULL,
              font_family VARCHAR(255) NOT NULL,
              font_url TEXT NOT NULL,
              font_weight VARCHAR(50) NOT NULL DEFAULT 'normal',
              font_style VARCHAR(50) NOT NULL DEFAULT 'normal',
              PRIMARY KEY (font_id)
          ) ENGINE=MyISAM DEFAULT CHARSET=utf8mb3";
  sql_query($sql, false);

  sql_query("INSERT INTO `avo_editor_fonts` 
              (`font_name`, `font_family`, `font_url`, `font_weight`, `font_style`) 
              VALUES ('에스코어드림', 'S-CoreDream-3Light', 'url(\'https://cdn.jsdelivr.net/gh/projectnoonnu/noonfonts_six@1.2/S-CoreDream-3Light.woff\') format(\'woff\')', 'normal', 'normal');");
  sql_query("INSERT INTO `avo_editor_fonts` (`font_name`, `font_family`, `font_url`, `font_weight`, `font_style`) 
              VALUES ('평창평화체', 'PyeongChangPeace-Light', 'url(\'https://cdn.jsdelivr.net/gh/projectnoonnu/noonfonts_2206-02@1.0/PyeongChangPeace-Light.woff2\') format(\'woff2\')', '300', 'normal');");
}

// 목록 출력
$sql = "SELECT * FROM {$g5['font_table']} ORDER BY font_family ASC ";
$result = sql_query($sql);

$g5['title'] = "에디터 폰트 관리";

include_once __DIR__ . '/admin.head.php';

EventHandler::triggerEvent("amber.admin.editor_font_form_before");
?>
<style>
  th a {
    display: inline-block;
    vertical-align: middle;
    padding: 3px 10px;
    background: #ecc6c6;
    color: #fff;
    margin: 5px 0 0;
  }

  th a+a {
    margin-top: 3px;
  }
</style>
<section>
  <h2>폰트 등록</h2>
  <form name="ffontform" method="post" action="./editor_font_update.php" onsubmit="return ffontform_submit(this);">
    <input type="hidden" name="token" value="">
    <input type="hidden" name="w" value="">

    <div class="tbl_frm01 tbl_wrap">
      <table>
        <caption>폰트 입력</caption>
        <colgroup>
          <col class="grid_4" style="width: 140px">
          <col>
        </colgroup>
        <tbody>
          <tr>
            <th scope="row"><label for="font_name">폰트 이름<strong class="sound_only">필수</strong></label></th>
            <td><input type="text" name="font_name" value="" id="font_name" required class="required frm_input"
                size="50"></td>
          </tr>
          <tr>
            <th scope="row"><label for="font_css">@font-face CSS 또는 @import URL<strong class="sound_only">필수</strong>
                <a href="https://noonnu.cc/index" target="_blank">눈누 폰트</a>
                <a href="https://fonts.google.com/" target="_blank">구글 폰트</a>
              </label></th>
            <td>
              <span class="frm_info">@font-face CSS를 그대로 붙여넣거나, Google Fonts의 @import URL을 입력하세요.</span>
              <span class="frm_info"><strong>구글 폰트의 경우 여러 폰트가 한번에 선택되지 않도록 주의해 주시기 바랍니다.</strong></span>
              <textarea name="font_css" id="font_css" rows="8" required class="required frm_input" style="width:100%;"
                placeholder="@font-face {
  font-family: 'FontName';
  src: url('https://example.com/font.woff2') format('woff2');
  font-weight: normal;
  font-style: normal;
}

또는

@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap');"></textarea>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
    <div class="list_confirm">
      <input type="submit" value="등록" class="btn_submit" accesskey="s">
    </div>
  </form>
  <div class="local_ov01 local_ov">
    등록된 폰트 <?php echo number_format(sql_num_rows($result)) ?>개
  </div>
  <form name="ffontlist" id="ffontlist" method="post" action="./editor_font_update.php"
    onsubmit="return ffontlist_submit(this);">
    <input type="hidden" name="token" value="">
    <input type="hidden" name="w" value="u">

    <div class="tbl_head01 tbl_wrap">
      <table>
        <caption><?php echo $g5['title']; ?> 목록</caption>
        <thead>
          <tr>
            <th scope="col" style="width: 200px">폰트 이름</th>
            <th scope="col" style="width: 200px">Font Family</th>
            <th scope="col">Font URL</th>
            <th scope="col" style="width: 120px">Font Weight</th>
            <th scope="col" style="width: 120px">Font Style</th>
            <th scope="col" style="width: 80px">관리</th>
          </tr>
        </thead>
        <tbody>
          <?php
          for ($i = 0; $row = sql_fetch_array($result); $i++) {
            $font_id = $row['font_id'];
            ?>
            <tr>
              <td class="td_left"><?php echo get_text($row['font_name']); ?></td>
              <td class="td_left"><?php echo get_text($row['font_family']); ?></td>
              <td class="td_left"><?php echo get_text($row['font_url']); ?></td>
              <td class="td_left"><?php echo get_text($row['font_weight']); ?></td>
              <td class="td_left"><?php echo get_text($row['font_style']); ?></td>
              <td class="td_mng td_mng_l">
                <a href="./editor_font_update.php?w=d&amp;font_id=<?php echo $font_id ?>"
                  onclick="return delete_confirm(this);" class="btn btn_02">삭제</a>
              </td>
            </tr>
            <?php
          }
          if ($i == 0) {
            echo '<tr><td colspan="6" class="empty_table">등록된 폰트가 없습니다.</td></tr>';
          }
          ?>
        </tbody>
      </table>
    </div>
  </form>
</section>
<section>
  <?php echo help('폰트 출력을 미리 보기 위한 에디터입니다.') ?>
  <?php echo editor_html('preview_content', ""); ?>
</section>
<script>
  function ffontform_submit(f) {
    if (f.font_css.value.trim() === "") {
      alert("@font-face CSS 또는 @import URL을 입력해주세요.");
      f.font_css.focus();
      return false;
    }
    if (f.font_name.value.trim() === "") {
      alert("폰트 이름을 입력해주세요.");
      f.font_name.focus();
      return false;
    }
    return true;
  }

  function delete_confirm(el) {
    return confirm("선택한 폰트를 정말 삭제하시겠습니까?");
  }
</script>
<?php
EventHandler::triggerEvent("amber.admin.editor_font_form_after");
include_once __DIR__ . '/admin.tail.php';
