<?php

namespace amJL_class;

use amJL_class\CheckStatus;

class TextFormForAdmin {

    // normal text form
    public function single_form($label, $name, $place, $isNeed = false){
        $value = get_option($name);
        ?>
        <div class='amJL_normal_form'>
            <label class='amJL_form_label'><?php echo esc_html($label) ?></label>
            <div class='amJL_input_form'>
                <input type='text' name='<?php echo esc_attr($name) ?>' value='<?php echo esc_attr($value) ?>' placeholder='<?php echo esc_attr($place) ?>' <?php if($isNeed) echo ' required' ?>>
            </div>
        </div>
        <?php
    }

    // normal form with URL check
    public function single_form_w_check($label, $name, $place, $isNeed = false){
        $value = get_option($name);
        ?>
        <div class='amJL_normal_form'>
            <label class='amJL_form_label'><?php echo esc_html($label) ?></label>
            <div class='amJL_input_form_wcheck'>
                <input type='text' name='<?php echo esc_attr($name) ?>' value='<?php echo esc_attr($value) ?>' placeholder='<?php echo esc_attr($place) ?>' <?php if($isNeed) echo ' required' ?>>
            </div>
        </div>
        <?php
        if($value){
            $url_check = new CheckStatus($value);
            if(!$url_check->check_url_status()){
                echo '<span class="amJL_url_check">&#x26a0;このURLは無効です</span>';
                unset($url_check);
            }
        }
    }

    // category thumbnail URL setting form
    public function category_thumb(){
        $categories = get_categories();
        $catNo = 1;
        foreach ($categories as $cat){
            $ID = "amJL_cat_ID_{$catNo}";
            $thumb = "amJL_thumb_url_{$catNo}";
            $thumb_url = get_option($thumb);
            ?>
            <div class='amJL_normal_form'>
                <div class='amJL_cat_name'>
                    <label><?php echo esc_html($cat->name) ?></label>
                </div>
                <div class='amJL_cat_no'>
                    <input type="text" size=10 name="<?php echo esc_attr($ID) ?>" value="<?php echo esc_attr($cat->cat_ID) ?>" readonly>
                </div>
                <div class='amJL_thumb_url'>
                    <input type='text' name='<?php echo esc_attr($thumb) ?>' value='<?php echo esc_attr($thumb_url) ?>' placeholder='※必須' required>
                </div>
            </div>
            <?php
            if($thumb_url){
                $url_check = new CheckStatus($thumb_url);
                if(!$url_check->check_url_status()){
                    echo '<span class="amJL_url_check">&#x26a0;このURLは無効です</span>';
                    unset($url_check);
                }
            }
            $catNo++;
        }
    }

    // private SNS URL setting form
    public function URL_forms(){
        $arr_label = array(
            'プロフィールページのURL(※必須)',
            'Facebook',
            'X (旧Twitter)',
            'Instagram',
            'TikTok',
            '楽天ROOM',
            'Pinterest',
            'Github',
            'YouTube',
            'Amazon欲しいものリスト',
            'その他'
        );
        $place = 'URLを入力';

        for((int)$i = 0; $i < count($arr_label); $i++){
            $count = $i + 1;
            $label = $arr_label[$i];
            $name = "amJL_url{$count}";
            $value = get_option($name);
            ?>
            <div class='amJL_normal_form'>
                <label class='amJL_form_label'><?php echo esc_html($label) ?></label>
                <div class='amJL_input_form_wcheck'>
                    <input type='text' name='<?php echo esc_attr($name) ?>' value='<?php echo esc_attr($value) ?>' placeholder='<?php if($i === 0) echo '※必須'; else echo esc_attr($place); ?>' <?php if($i === 0) echo ' required'; ?>>
                </div>
            </div>
            <?php 
        }
    }

    private function echo_checked($val, $isAll){
        if ($isAll === 100) return -1;
        if ($isAll === 1 || $val === 1) echo " checked='checked'";
    }

    private function checkbox($name){
        $value = get_option($name) ? (int)get_option($name) : 0;
        $isAll = ($name === 'amJL_all_view') ? 100 : (int)get_option('amJL_all_view');
        ?>
            <div>
                <input type='hidden' name='<?php echo esc_attr($name) ?>' value='0'>
                <input type='checkbox' name='<?php echo esc_attr($name) ?>' value='1' <?php $this->echo_checked($value, $isAll) ?>>
            </div>
        <?php
    }

    public function view_setting(){
        ?>
        <div class='amJL_normal_form'>
            <label class='amJL_form_label'><b>すべて表示する</b></label>
            <div class='amJL_checkbox'><?php $this->checkbox('amJL_all_view') ?></div>
        </div>
        <div class='amJL_normal_form'>
            <label class='amJL_form_label'>パンくずリスト</label>
            <div class='amJL_checkbox'><?php $this->checkbox('amJL_breadcrumb_check') ?></div>
        </div>
        <div class='amJL_normal_form'>
            <label class='amJL_form_label'>Article(記事)</label>
            <div class='amJL_checkbox'><?php $this->checkbox('amJL_article_check') ?></div>
        </div>
        <div class='amJL_normal_form'>
            <label class='amJL_form_label'>画像メタデータ</label>
            <div class='amJL_checkbox'><?php $this->checkbox('amJL_image_check') ?></div>
        </div>
        <div class='amJL_normal_form'>
            <label class='amJL_form_label'>Logo(ロゴ)</label>
            <div class='amJL_checkbox'><?php $this->checkbox('amJL_logo_check') ?></div>
        </div>
        <div class='amJL_normal_form'>
            <label class='amJL_form_label'>サイトリンク検索ボックス</label>
            <div class='amJL_checkbox'><?php $this->checkbox('amJL_sitelink_check') ?></div>
        </div>
        <div class='amJL_normal_form'>
            <label class='amJL_form_label'>レシピ(Recipe)</label>
            <div class='amJL_checkbox'><?php $this->checkbox('amJL_recipe_check') ?></div>
        </div>
        <div class='amJL_normal_form'>
            <label class='amJL_form_label'>Video</label>
            <div class='amJL_checkbox'><?php $this->checkbox('amJL_video_check') ?></div>
        </div>
        <div class='amJL_normal_form'>
            <label class='amJL_form_label'>よくある質問</label>
            <div class='amJL_checkbox'><?php $this->checkbox('amJL_faq_check') ?></div>
        </div>
        <div class='amJL_normal_form'>
            <label class='amJL_form_label'>ハウツー(HowTo)</label>
            <div class='amJL_checkbox'><?php $this->checkbox('amJL_howto_check') ?></div>
        </div>
        <div class='amJL_normal_form'>
            <label class='amJL_form_label'>商品レビュー</label>
            <div class='amJL_checkbox'><?php $this->checkbox('amJL_review_check') ?></div>
        </div>
        <div class='amJL_normal_form'>
            <label class='amJL_form_label'>プロフィール</label>
            <div class='amJL_checkbox'><?php $this->checkbox('amJL_profile_check') ?></div>
        </div>
        <div class='amJL_normal_form'>
            <label class='amJL_form_label'>組織</label>
            <div class='amJL_checkbox'><?php $this->checkbox('amJL_organization_check') ?></div>
        </div>
        <?php
    }

}