<?php
defined('ABSPATH') || exit;
if (!class_exists('yascode_settings')) :
    require_once(THEME_TEMPLATE_URL_PATH . '/app/settings/setting_options.php');
    class yascode_settings
    {
        //init before run
        private static $instance;
        public static function get_instance()
        {
            if (!self::$instance) {
                self::$instance = new self();
            }
            return self::$instance;
        }

        public function __construct()
        {
            add_action('admin_menu', array($this, 'create_settings_menu_actual'));
            add_action('admin_head', array($this, 'save_admin_settings_data'));
        }

        public function save_admin_settings_data()
        {
            $page = (!empty($_GET['page'])) ? $_GET['page'] : '';
            if ($page == 'yascode_settings') {
                if (isset($_POST['save-data'])) {
                    if (!empty(TEMPLATE_SETTINGS) && is_array(TEMPLATE_SETTINGS)) {
                        foreach (TEMPLATE_SETTINGS as $setting) {
                            if (!empty($setting['items']) && is_array($setting['items'])) {
                                foreach ($setting['items'] as $item) {
                                    if (!empty($item['id'])) {
                                        if (!empty($_POST[$item['id']])) {
                                            if ($item['type'] == 'listitem') $_POST[$item['id']] = array_values($_POST[$item['id']]);
                                            update_option($item['id'], $_POST[$item['id']]);
                                        } else delete_option($item['id']);
                                    }
                                }
                            }
                        }
                    }
                } elseif (isset($_POST['remove-data'])) {
                    if (!empty(TEMPLATE_SETTINGS) && is_array(TEMPLATE_SETTINGS)) {
                        foreach (TEMPLATE_SETTINGS as $setting) {
                            if (!empty($setting['items']) && is_array($setting['items'])) {
                                foreach ($setting['items'] as $item) {
                                    if (!empty($item['id'])) {
                                        delete_option($item['id']);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        public function create_settings_menu_actual()
        {
            add_menu_page(
                'تنظیمات قالب',
                'تنظیمات قالب',
                'administrator',
                'yascode_settings',
                array($this, 'yascode_create_settings_page'),
                get_template_directory_uri() . '/assets/admin/img/admin-icon.png',
                60
            );
        }

        public function yascode_create_settings_page()
        { ?>
            <div class="wrap yc-settings-page">
                <hr class="wp-header-end">
                <div class="msg-body"></div>

                <section class="main-settings-panel-section">
                    <div class="head-panel-settings">
                        <div class="top-info">
                            <div class="main-title">
                                <h1>قالب وردپرس <?php echo FA_THEME_NAME; ?></h1>
                                <span class="version"><?php echo THEME_VER; ?></span>
                            </div>
                            <a href="https://yascode.ir/" target="_blank" rel="follow" class="copyright">یاس کد</a>
                        </div>
                        <div class="bottom-info">
                            <p>پنل اختصاصی مدیریت تنظیمات</p>
                        </div>
                    </div>

                    <div class="panel-settings-body">
                        <div class="right-sidebar">
                            <ul class="list-menu">
                                <?php if (!empty(TEMPLATE_SETTINGS) && is_array(TEMPLATE_SETTINGS)) :
                                    foreach (TEMPLATE_SETTINGS as $key => $setting) :
                                        if ($setting['type'] == 'section') : ?>
                                            <li class="<?php echo ($key == 0) ? 'active' : ''; ?>" data-tabid="<?php echo $setting['id']; ?>"><?php echo $setting['label']; ?></li>
                                <?php endif;
                                    endforeach;
                                endif; ?>
                            </ul>
                        </div>

                        <form action="<?php echo admin_url('/admin.php?page=yascode_settings'); ?>" class="main-panel-body" method="post">
                            <?php if (!empty(TEMPLATE_SETTINGS) && is_array(TEMPLATE_SETTINGS)) : ?>
                                <div class="headline-title">
                                    <span>تنظیمات</span>
                                    <div class="on-icon"></div>
                                    <span class="title-info"><?php if (!empty(TEMPLATE_SETTINGS[0]['label'])) : ?><?php echo TEMPLATE_SETTINGS[0]['label']; ?><?php endif; ?></span>
                                </div>
                            <?php endif; ?>
                            <div class="sec-body-info">
                                <?php if (!empty(TEMPLATE_SETTINGS) && is_array(TEMPLATE_SETTINGS)) :
                                    foreach (TEMPLATE_SETTINGS as $key => $setting) :
                                        if ($setting['type'] == 'section') : ?>
                                            <section class="sect-box <?php echo ($key != 0) ? 'none' : ''; ?>" data-secid="<?php echo $setting['id']; ?>">

                                                <?php if (!empty($setting['items'])) :
                                                    foreach ($setting['items'] as $item) :
                                                        $option_value = yc_get_option($item['id']);
                                                        switch ($item['type']):
                                                            case 'notification': ?>
                                                                <div class="notif-item"><?php echo $item['label']; ?></div>
                                                            <?php break;
                                                            case 'button': ?>
                                                                <button class="option-btn button button-primary" id="<?php echo $item['id']; ?>" data-active="true"><?php echo $item['label']; ?></button>
                                                            <?php break;
                                                            case 'text': ?>
                                                                <div class="data-box input-input">
                                                                    <label class="title" for="<?php echo $item['id']; ?>"><?php echo $item['label']; ?>
                                                                        <?php if (!empty($item['description'])) : ?>
                                                                            <span class="ycd-tooltip">?<p><?php echo $item['description']; ?></p></span>
                                                                        <?php endif; ?>
                                                                    </label>
                                                                    <div class="input-box-info">
                                                                        <input type="text" name="<?php echo $item['id']; ?>" id="<?php echo $item['id']; ?>" value="<?php echo (!empty($option_value)) ? $option_value : ''; ?>">
                                                                        <?php if (!empty($item['excerpt'])) : ?>
                                                                            <p class="excerpt"><?php echo $item['excerpt']; ?></p>
                                                                        <?php endif; ?>
                                                                    </div>
                                                                </div>
                                                            <?php break;
                                                            case 'textarea': ?>
                                                                <div class="data-box input-input">
                                                                    <label class="title" for="<?php echo $item['id']; ?>"><?php echo $item['label']; ?>
                                                                        <?php if (!empty($item['description'])) : ?>
                                                                            <span class="ycd-tooltip">?<p><?php echo $item['description']; ?></p></span>
                                                                        <?php endif; ?>
                                                                    </label>
                                                                    <div class="input-box-info">
                                                                        <textarea name="<?php echo $item['id']; ?>" id="<?php echo $item['id']; ?>"><?php echo (!empty($option_value)) ? $option_value : ''; ?></textarea>
                                                                        <?php if (!empty($item['excerpt'])) : ?>
                                                                            <p class="excerpt"><?php echo $item['excerpt']; ?></p>
                                                                        <?php endif; ?>
                                                                    </div>
                                                                </div>
                                                            <?php break;
                                                            case 'checkbox': ?>
                                                                <div class="data-box input-checkbox">
                                                                    <label class="title" for="<?php echo $item['id']; ?>"><?php echo $item['label']; ?>
                                                                        <?php if (!empty($item['description'])) : ?>
                                                                            <span class="ycd-tooltip">?<p><?php echo $item['description']; ?></p></span>
                                                                        <?php endif; ?>
                                                                    </label>
                                                                    <div class="check-box-info">
                                                                        <div class="checkbox-radio">
                                                                            <input type="checkbox" name="<?php echo $item['id']; ?>" id="<?php echo $item['id']; ?>" <?php echo (!empty($option_value)) ? 'checked' : ''; ?> />
                                                                            <div class="check-slider"></div>
                                                                        </div>
                                                                        <?php if (!empty($item['excerpt'])) : ?>
                                                                            <p class="excerpt"><?php echo $item['excerpt']; ?></p>
                                                                        <?php endif; ?>
                                                                    </div>
                                                                </div>
                                                            <?php break;
                                                            case 'colorpick': ?>
                                                                <div class="data-box input-color">
                                                                    <label class="title" for="<?php echo $item['id']; ?>"><?php echo $item['label']; ?>
                                                                        <?php if (!empty($item['description'])) : ?>
                                                                            <span class="ycd-tooltip">?<p><?php echo $item['description']; ?></p></span>
                                                                        <?php endif; ?>
                                                                    </label>
                                                                    <div class="input-box-info">
                                                                        <input type="text" name="<?php echo $item['id']; ?>" id="<?php echo $item['id']; ?>" value="<?php echo (!empty($option_value)) ? $option_value : ''; ?>">
                                                                        <input type="color" value="<?php echo (!empty($option_value)) ? $option_value : ''; ?>" />
                                                                    </div>
                                                                </div>
                                                            <?php break;
                                                            case 'select': ?>
                                                                <div class="data-box input-input">
                                                                    <label class="title" for="<?php echo $item['id']; ?>"><?php echo $item['label']; ?>
                                                                        <?php if (!empty($item['description'])) : ?>
                                                                            <span class="ycd-tooltip">?<p><?php echo $item['description']; ?></p></span>
                                                                        <?php endif; ?>
                                                                    </label>
                                                                    <div class="input-box-info">
                                                                        <?php if (!empty($item['options']) && is_array($item['options'])) : ?>
                                                                            <div class="select-box">
                                                                                <select name="<?php echo $item['id']; ?>" id="<?php echo $item['id']; ?>">
                                                                                    <?php foreach ($item['options'] as $key => $option) : ?>
                                                                                        <option value="<?php echo $key; ?>" <?php echo (!empty($option_value) && $option_value == $key) ? 'selected' : ''; ?>><?php echo $option; ?></option>
                                                                                    <?php endforeach; ?>
                                                                                </select>
                                                                            </div>
                                                                        <?php endif; ?>
                                                                    </div>
                                                                </div>
                                                            <?php break;
                                                            case 'upload': ?>
                                                                <div class="data-box input-upload">
                                                                    <label class="title" for="<?php echo $item['id']; ?>"><?php echo $item['label']; ?>
                                                                        <?php if (!empty($item['description'])) : ?>
                                                                            <span class="ycd-tooltip">?<p><?php echo $item['description']; ?></p></span>
                                                                        <?php endif; ?>
                                                                    </label>
                                                                    <div class="upload-box-info">
                                                                        <input type="text" name="<?php echo $item['id']; ?>" id="<?php echo $item['id']; ?>" value="<?php echo (!empty($option_value)) ? $option_value : ''; ?>" />
                                                                        <span class="modual-selectfile">انتخاب تصویر</span>
                                                                        <span class="delete-file">حذف</span>
                                                                    </div>
                                                                </div>
                                                            <?php break;
                                                            case 'multiple_select': ?>
                                                                <div class="data-box multiple-input">
                                                                    <label class="title" for="<?php echo $item['id']; ?>"><?php echo $item['label']; ?>
                                                                        <?php if (!empty($item['description'])) : ?>
                                                                            <span class="ycd-tooltip">?<p><?php echo $item['description']; ?></p></span>
                                                                        <?php endif; ?>
                                                                    </label>
                                                                    <div class="box-multiple">
                                                                        <?php if (!empty($item['options'])) :
                                                                            foreach ($item['options'] as $option_key => $option_name) : ?>
                                                                                <div class="item-type">
                                                                                    <label for="<?php echo $item['id'] . '[' . $option_key . ']'; ?>"><?php echo $option_name; ?></label>
                                                                                    <input type="checkbox" name="<?php echo $item['id'] . '[' . $option_key . ']'; ?>" id="<?php echo $item['id'] . '[' . $option_key . ']'; ?>" <?php echo (!empty($option_value) && is_array($option_value) && array_key_exists($option_key, $option_value)) ? 'checked' : ''; ?>>
                                                                                </div>
                                                                        <?php endforeach;
                                                                        endif; ?>
                                                                    </div>
                                                                </div>
                                                            <?php break;
                                                            case 'listitem': ?>
                                                                <div class="input-listitem">
                                                                    <label class="title" for="<?php echo $item['id']; ?>"><?php echo $item['label']; ?>
                                                                        <?php if (!empty($item['description'])) : ?>
                                                                            <span class="ycd-tooltip">?<p><?php echo $item['description']; ?></p></span>
                                                                        <?php endif; ?>
                                                                    </label>
                                                                    <span class="add-item">افزودن</span>

                                                                    <ul class="listitem-list">
                                                                        <li class="list-item example-item" data-index="0">
                                                                            <div class="head-item">
                                                                                <span class="title">مدیریت آیتم</span>
                                                                                <div class="manage-btns">
                                                                                    <samp class="dashicons dashicons-trash delete"></samp>
                                                                                    <samp class="dashicons dashicons-edit edit"></samp>
                                                                                </div>
                                                                            </div>

                                                                            <div class="inner-item">

                                                                                <div class="main-content">
                                                                                    <?php if (!empty($item['list_items']) && is_array($item['list_items'])) :
                                                                                        foreach ($item['list_items'] as $item_id) :
                                                                                            switch ($item_id['type']):
                                                                                                case 'text': ?>
                                                                                                    <div class="input-box text-input">
                                                                                                        <span class="info-title"><?php echo (!empty($item_id['label'])) ? $item_id['label'] : ''; ?></span>
                                                                                                        <input type="text" class="<?php echo ($item_id['id'] == 'title') ? 'autoComplete' : ''; ?>" data-name="<?php echo $item['id'] ?>[index_id][<?php echo $item_id['id'] ?>]">
                                                                                                    </div>
                                                                                                <?php break;
                                                                                                case 'upload': ?>
                                                                                                    <div class="input-box upload-input">
                                                                                                        <span class="info-title"><?php echo (!empty($item_id['label'])) ? $item_id['label'] : ''; ?></span>
                                                                                                        <div class="upload-box-info">
                                                                                                            <input type="text" data-name="<?php echo $item['id'] ?>[index_id][<?php echo $item_id['id'] ?>]" />
                                                                                                            <span class="modual-selectfile">انتخاب تصویر</span>
                                                                                                            <span class="delete-file">حذف</span>
                                                                                                        </div>
                                                                                                    </div>
                                                                                                <?php break;
                                                                                                case 'checkbox': ?>
                                                                                                    <div class="input-box checkbox-input">
                                                                                                        <span class="info-title"><?php echo (!empty($item_id['label'])) ? $item_id['label'] : ''; ?></span>
                                                                                                        <div class="check-box-info">
                                                                                                            <div class="checkbox-radio">
                                                                                                                <input type="checkbox" data-name="<?php echo $item['id'] ?>[index_id][<?php echo $item_id['id'] ?>]" />
                                                                                                                <div class="check-slider metaRadioCheck"></div>
                                                                                                            </div>
                                                                                                        </div>
                                                                                                    </div>
                                                                                                <?php break;
                                                                                                case 'textarea': ?>
                                                                                                    <div class="input-box textarea-input">
                                                                                                        <span class="info-title"><?php echo (!empty($item_id['label'])) ? $item_id['label'] : ''; ?></span>
                                                                                                        <textarea data-name="<?php echo $item['id'] ?>[index_id][<?php echo $item_id['id'] ?>]"></textarea>
                                                                                                    </div>
                                                                                                <?php break;
                                                                                                case 'colorpick': ?>
                                                                                                    <div class="input-box colorpicker-input">
                                                                                                        <span class="info-title"><?php echo (!empty($item_id['label'])) ? $item_id['label'] : ''; ?></span>
                                                                                                        <input type="text" data-name="<?php echo $item['id'] ?>[index_id][<?php echo $item_id['id'] ?>]" />
                                                                                                        <input type="color" />
                                                                                                    </div>
                                                                                                <?php break;
                                                                                                case 'select': ?>
                                                                                                    <div class="input-box select-input">
                                                                                                        <span class="info-title"><?php echo (!empty($item_id['label'])) ? $item_id['label'] : ''; ?></span>
                                                                                                        <select data-name="<?php echo $item['id'] ?>[index_id][<?php echo $item_id['id'] ?>]">
                                                                                                            <?php if (!empty($item_id['options'])) :
                                                                                                                foreach ($item_id['options'] as $key => $option) : ?>
                                                                                                                    <option value="<?php echo $key; ?>"><?php echo $option; ?></option>
                                                                                                            <?php endforeach;
                                                                                                            endif; ?>
                                                                                                        </select>
                                                                                                    </div>
                                                                                            <?php break;
                                                                                            endswitch; ?>
                                                                                    <?php endforeach;
                                                                                    endif; ?>
                                                                                </div>
                                                                            </div>
                                                                        </li>


                                                                        <?php if (!empty($option_value) && is_array($option_value)) :
                                                                            foreach ($option_value as $key => $option_item) : ?>

                                                                                <li class="list-item" data-index="<?php echo $key + 1; ?>">
                                                                                    <div class="head-item">
                                                                                        <span class="title"><?php echo (!empty($option_item['title'])) ? $option_item['title'] : 'مدیریت آیتم'; ?></span>
                                                                                        <div class="manage-btns">
                                                                                            <samp class="dashicons dashicons-trash delete"></samp>
                                                                                            <samp class="dashicons dashicons-edit edit"></samp>
                                                                                        </div>
                                                                                    </div>

                                                                                    <div class="inner-item">

                                                                                        <div class="main-content">
                                                                                            <?php if (!empty($item['list_items']) && is_array($item['list_items'])) :
                                                                                                foreach ($item['list_items'] as $item_id) :
                                                                                                    switch ($item_id['type']):
                                                                                                        case 'text': ?>
                                                                                                            <div class="input-box text-input">
                                                                                                                <span class="info-title"><?php echo (!empty($item_id['label'])) ? $item_id['label'] : ''; ?></span>
                                                                                                                <input type="text" class="<?php echo ($item_id['id'] == 'title') ? 'autoComplete' : ''; ?>" name="<?php echo $item['id'] ?>[<?php echo $key + 1; ?>][<?php echo $item_id['id'] ?>]" value="<?php echo (!empty($option_item[$item_id['id']])) ? $option_item[$item_id['id']] : ''; ?>" />
                                                                                                            </div>
                                                                                                        <?php break;
                                                                                                        case 'upload': ?>
                                                                                                            <div class="input-box upload-input">
                                                                                                                <span class="info-title"><?php echo (!empty($item_id['label'])) ? $item_id['label'] : ''; ?></span>
                                                                                                                <div class="upload-box-info">
                                                                                                                    <input type="text" name="<?php echo $item['id'] ?>[<?php echo $key + 1; ?>][<?php echo $item_id['id'] ?>]" value="<?php echo (!empty($option_item[$item_id['id']])) ? $option_item[$item_id['id']] : ''; ?>" />
                                                                                                                    <span class="modual-selectfile">انتخاب تصویر</span>
                                                                                                                    <span class="delete-file">حذف</span>
                                                                                                                </div>
                                                                                                            </div>
                                                                                                        <?php break;
                                                                                                        case 'checkbox': ?>
                                                                                                            <div class="input-box checkbox-input">
                                                                                                                <span class="info-title"><?php echo (!empty($item_id['label'])) ? $item_id['label'] : ''; ?></span>
                                                                                                                <div class="check-box-info">
                                                                                                                    <div class="checkbox-radio">
                                                                                                                        <input type="checkbox" name="<?php echo $item['id'] ?>[<?php echo $key + 1; ?>][<?php echo $item_id['id'] ?>]" <?php echo (!empty($option_item[$item_id['id']])) ? 'checked' : ''; ?> />
                                                                                                                        <div class="check-slider metaRadioCheck"></div>
                                                                                                                    </div>
                                                                                                                </div>
                                                                                                            </div>
                                                                                                        <?php break;
                                                                                                        case 'textarea': ?>
                                                                                                            <div class="input-box textarea-input">
                                                                                                                <span class="info-title"><?php echo (!empty($item_id['label'])) ? $item_id['label'] : ''; ?></span>
                                                                                                                <textarea name="<?php echo $item['id'] ?>[<?php echo $key + 1; ?>][<?php echo $item_id['id'] ?>]"><?php echo (!empty($option_item[$item_id['id']])) ? $option_item[$item_id['id']] : ''; ?></textarea>
                                                                                                            </div>
                                                                                                        <?php break;
                                                                                                        case 'colorpick': ?>
                                                                                                            <div class="input-box colorpicker-input">
                                                                                                                <span class="info-title"><?php echo (!empty($item_id['label'])) ? $item_id['label'] : ''; ?></span>
                                                                                                                <input type="text" name="<?php echo $item['id'] ?>[<?php echo $key + 1; ?>][<?php echo $item_id['id'] ?>]" value="<?php echo (!empty($option_item[$item_id['id']])) ? $option_item[$item_id['id']] : ''; ?>" />
                                                                                                                <input type="color" value="<?php echo (!empty($option_item[$item_id['id']])) ? $option_item[$item_id['id']] : ''; ?>">
                                                                                                            </div>
                                                                                                        <?php break;
                                                                                                        case 'select': ?>
                                                                                                            <div class="input-box select-input">
                                                                                                                <span class="info-title"><?php echo (!empty($item_id['label'])) ? $item_id['label'] : ''; ?></span>
                                                                                                                <select name="<?php echo $item['id'] ?>[<?php echo $key + 1; ?>][<?php echo $item_id['id'] ?>]">
                                                                                                                    <?php if (!empty($item_id['options'])) :
                                                                                                                        foreach ($item_id['options'] as $option_key => $option_name) : ?>
                                                                                                                            <option value="<?php echo $option_key; ?>" <?php echo (!empty($option_item[$item_id['id']]) && $option_item[$item_id['id']] == $option_key) ? 'selected' : ''; ?>><?php echo $option_name; ?></option>
                                                                                                                    <?php endforeach;
                                                                                                                    endif; ?>
                                                                                                                </select>
                                                                                                            </div>
                                                                                                    <?php break;
                                                                                                    endswitch; ?>
                                                                                            <?php endforeach;
                                                                                            endif; ?>
                                                                                        </div>
                                                                                    </div>
                                                                                </li>


                                                                        <?php endforeach;
                                                                        endif; ?>
                                                                    </ul>
                                                                </div>
                                                                <?php break; ?>
                                                <?php endswitch;
                                                    endforeach;
                                                endif; ?>

                                            </section>
                                <?php endif;
                                    endforeach;
                                endif; ?>
                            </div>
                            <div class="panel-form-btns">
                                <button class="remove-data" type="submit" name="remove-data" onclick="return confirm('از پاکسازی تنظیمات اطمینان دارید؟ قابل بازگشت نمیباشد');">پاکسازی تنظیمات</button>
                                <button class="save-data" type="submit" name="save-data">ذخیره تنظیمات</button>
                            </div>
                        </form>

                    </div>

                </section>
            </div>
<?php }
    }
    yascode_settings::get_instance();
endif;
