<?php
/**
 * File: nikisaz-create-notification.php
 * Description: صفحه ایجاد نوتیفیکیشن برای افزونه نیکی‌ساز
 */

// تنظیم زمان بر اساس تهران
date_default_timezone_set('Asia/Tehran');

// دریافت تنظیمات عمومی افزونه
$find = get_option("nikisaz_general_settings");

// اگر تابعی برای لیست پکیج‌ها دارید، آن را اینجا فراخوانی کنید.
// فرض بر این است که این تابع داده‌های لازم (API Key و آیکون) را برمی‌گرداند.
// اگر این تابع از لایسنس وابسته بود، اکنون باید منبع داده‌های خود را تغییر دهید.
$findPackage = nikisaz_get_packages_data(); 
?>

<div id="file_optimization" class="hdwebapp-Page">
    <div class="hdwebapp-sectionHeader">
        <h2 class="hdwebapp-title1"><?php _e( 'Create a Notification', 'nikisaz-webapp-generator' );?></h2>
    </div>
    
    <!-- اگر نیاز به نمایش پیام خطا هنگام نبودن تنظیمات دارید -->
    <div class="hdwebapp-fieldsContainer <?php if (!$find) echo 'disabled-div'; ?>">
        <form action="" method="post">
            <?php if (!$find) { ?>
                <fieldset class="hdwebapp-fieldsContainer-fieldset">
                    <div class="hdwebapp-warningContainer">
                        <?php echo _e('Please enter the general settings first.', 'nikisaz-webapp-generator'); ?>
                    </div>
                </fieldset>
            <?php } ?>
            
            <fieldset class="hdwebapp-fieldsContainer-fieldset">
                <div class="add-on-container ">
                    <div class="hdwebapp-field">
                        <div class="hdwebapp-flex">
                            <h4 class="hdwebapp-title3">
                                <?php _e("Push Notification", 'nikisaz-webapp-generator'); ?>
                            </h4>
                        </div>
                    </div>

                    <!-- انتخاب API Key -->
                    <div class="hdwebapp-field hdwebapp-field--checkbox hdwebapp-field--parent">
                        <div class="hdwebapp-multiple hdwebapp-multiple-default">
                            <label class="form-label" for="api_key">
                                <?php _e('Sender API', 'nikisaz-webapp-generator'); ?>:
                            </label>
                            <div class="hdwebapp-text">
                                <select name="api_key" id="api_key" class="col-12 form-select" dir="ltr"
                                        style="max-width: 50rem; !important;">
                                    <?php
                                    if (!empty($findPackage)) {
                                        foreach ($findPackage as $findPackageData) {
                                            // اطمینان از وجود داده‌ها قبل از خروجی
                                            $icon = isset($findPackageData->app_icon) ? esc_attr($findPackageData->app_icon) : '';
                                            $package = isset($findPackageData->app_package_name) ? esc_attr($findPackageData->app_package_name) : '';
                                            $apiKey = isset($findPackageData->najva_api_key) ? esc_attr($findPackageData->najva_api_key) : '';
                                            $webId = isset($findPackageData->najva_website_id) ? esc_attr($findPackageData->najva_website_id) : '';
                                            
                                            echo "<option data-icon='$icon' data-package='$package' value='$apiKey'>API : " . $apiKey . " / ID : " . $webId . "</option>";
                                        }
                                    } else {
                                        echo "<option value=''>" . __('No packages found', 'nikisaz-webapp-generator') . "</option>";
                                    }
                                    ?>
                                </select>
                                <span class="tooltip-span"
                                      data-tooltip="<?php _e('Select the API key for your app.', 'nikisaz-webapp-generator'); ?>"
                                      data-tooltip-location="left">
                                    <img src="<?php echo plugin_dir_url(dirname(__FILE__)) . '../styles/Information-icon.png'; ?>"
                                         width="15" height="15">
                                </span>
                            </div>
                        </div>
                    </div>

                    <!-- نوع نوتیفیکیشن -->
                    <div class="hdwebapp-field hdwebapp-field--checkbox hdwebapp-field--parent">
                        <div class="hdwebapp-multiple hdwebapp-multiple-default">
                            <label class="form-label" for="type">
                                <?php _e('Type of Notification', 'nikisaz-webapp-generator'); ?>:
                            </label>
                            <div class="hdwebapp-text">
                                <select name="type" id="type" class="col-12 form-select" dir="rtl"
                                        style="max-width: 50rem; !important;">
                                    <option value="app"><?php _e('Open App', 'nikisaz-webapp-generator'); ?></option>
                                    <option value="url"><?php _e('Open URL', 'nikisaz-webapp-generator'); ?></option>
                                    <option value="web"><?php _e('Send Webpage', 'nikisaz-webapp-generator'); ?></option>
                                    <option value="sms"><?php _e('Send SMS', 'nikisaz-webapp-generator'); ?></option>
                                    <option value="call"><?php _e('Open Dialer', 'nikisaz-webapp-generator'); ?></option>
                                </select>
                                <span class="tooltip-span"
                                      data-tooltip="<?php _e('Choose the action type for this notification.', 'nikisaz-webapp-generator'); ?>"
                                      data-tooltip-location="left">
                                    <img src="<?php echo plugin_dir_url(dirname(__FILE__)) . '../styles/Information-icon.png'; ?>"
                                         width="15" height="15">
                                </span>
                            </div>
                        </div>
                    </div>

                    <!-- مقدار نوع نوتیفیکیشن (پویا) -->
                    <div class="hdwebapp-field hdwebapp-field--checkbox hdwebapp-field--parent hidden" id="type_value_div">
                        <div class="hdwebapp-multiple hdwebapp-multiple-default">
                            <label class="form-label" for="type_value">
                                <?php _e('Type Value', 'nikisaz-webapp-generator'); ?>:
                            </label>
                            <div class="hdwebapp-text">
                                <input type="text" id="type_value" dir="ltr" class="col-12 form-input" name="type_value"
                                       autocomplete="off" value="">
                                <span class="input-group-addon hidden"><?php echo esc_url(get_site_url()); ?>/</span>
                            </div>
                        </div>
                    </div>

                    <!-- عنوان -->
                    <div class="hdwebapp-field hdwebapp-field--checkbox hdwebapp-field--parent">
                        <div class="hdwebapp-multiple hdwebapp-multiple-default">
                            <label class="form-label" for="title">
                                <?php _e('Title', 'nikisaz-webapp-generator'); ?>:
                            </label>
                            <div class="hdwebapp-text">
                                <input type="text" required dir="rtl" class="col-12" name="title" id="title"
                                       autocomplete="off" value="">
                                <span class="tooltip-span"
                                      data-tooltip="<?php _e('Title of the notification.', 'nikisaz-webapp-generator'); ?>"
                                      data-tooltip-location="left">
                                    <img src="<?php echo plugin_dir_url(dirname(__FILE__)) . '../styles/Information-icon.png'; ?>"
                                         width="15" height="15">
                                </span>
                            </div>
                        </div>
                    </div>

                    <!-- متن بدنه -->
                    <div class="hdwebapp-field hdwebapp-field--checkbox hdwebapp-field--parent">
                        <div class="hdwebapp-multiple hdwebapp-multiple-default">
                            <label class="form-label" for="body">
                                <?php _e('Body', 'nikisaz-webapp-generator'); ?>:
                            </label>
                            <div class="hdwebapp-text">
                                <textarea rows="3" type="text" required dir="rtl" class="col-12" name="body" id="body"
                                          autocomplete="off" value=""></textarea>
                                <span class="tooltip-span"
                                      data-tooltip="<?php _e('Message content.', 'nikisaz-webapp-generator'); ?>"
                                      data-tooltip-location="left">
                                    <img src="<?php echo plugin_dir_url(dirname(__FILE__)) . '../styles/Information-icon.png'; ?>"
                                         width="15" height="15">
                                </span>
                            </div>
                        </div>
                    </div>

                    <!-- تصویر کوچک -->
                    <div class="hdwebapp-field hdwebapp-field--checkbox hdwebapp-field--parent">
                        <div class="hdwebapp-multiple hdwebapp-multiple-default">
                            <label class="form-label" for="small_image">
                                <?php _e('Small Image', 'nikisaz-webapp-generator'); ?>:
                            </label>
                            <div class="hdwebapp-text">
                                <input type="text" dir="ltr" class="col-12 upload_image" name="small_image" id="small_image"
                                       autocomplete="off">
                                <button class="btn btn-primary input-group-btn upload_image_button1" type="button" id="small_image_btn"><?php _e('Upload', 'nikisaz-webapp-generator'); ?></button>
                                <span class="tooltip-span"
                                      data-tooltip="<?php _e('Image for the notification icon.', 'nikisaz-webapp-generator'); ?>"
                                      data-tooltip-location="left">
                                    <img src="<?php echo plugin_dir_url(dirname(__FILE__)) . '../styles/Information-icon.png'; ?>"
                                         width="15" height="15">
                                </span>
                            </div>
                        </div>
                    </div>

                    <!-- تصویر بزرگ -->
                    <div class="hdwebapp-field hdwebapp-field--checkbox hdwebapp-field--parent">
                        <div class="hdwebapp-multiple hdwebapp-multiple-default">
                            <label class="form-label" for="large_image">
                                <?php _e('Large Image', 'nikisaz-webapp-generator'); ?>:
                            </label>
                            <div class="hdwebapp-text">
                                <input type="text" dir="ltr" class="col-12 upload_image" name="large_image" id="large_image"
                                       autocomplete="off">
                                <button class="btn btn-primary input-group-btn upload_image_button2" id="large_image_btn" type="button"><?php _e('Upload', 'nikisaz-webapp-generator'); ?></button>
                                <span class="tooltip-span"
                                      data-tooltip="<?php _e('Banner image for the notification.', 'nikisaz-webapp-generator'); ?>"
                                      data-tooltip-location="left">
                                    <img src="<?php echo plugin_dir_url(dirname(__FILE__)) . '../styles/Information-icon.png'; ?>"
                                         width="15" height="15">
                                </span>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- پیش‌نمایش موبایل -->
                <div class="mobile-part">
                    <div class="android-clock"><?php echo date("H:i"); ?></div>
                    <div class="android-date"><?php echo date("l, F d"); ?></div>
                    <div class="android-contaier p-1">
                        <span class="android-tag pt-3 pl-3" dir="ltr">
                            <i class="fas fa-chevron-up"></i>
                            <img src="<?php echo plugins_url( '../../styles/icons/android.png', __FILE__ ) ?>" class="app_icon_preview" id="app_icon_preview"/>
                            <?php _e('Application Name', 'nikisaz-webapp-generator'); ?>
                            <i class="fab fa-chrome"></i>
                        </span>
                        <div class="android-body">
                            <img class="android-icon-default" style=""
                                 src="<?php echo plugins_url( '../../styles/camera.svg', __FILE__ ) ?>">
                            <div class="android-text">
                                <p class="android-title">
                                    <span id="title_preview"><?php _e('Title', 'nikisaz-webapp-generator'); ?></span>
                                </p>
                                <p class="android-text-body">
                                    <span id="body_preview"><?php _e('Body content...', 'nikisaz-webapp-generator'); ?></span>
                                </p>
                            </div>
                        </div>
                        <div class="android-image" id="android-image"></div>
                    </div>
                </div>
            </fieldset>

            <?php wp_nonce_field('nikisaz_create_notification'); ?>
            <?php if ($find && isset($findPackage) && count($findPackage) > 0) submit_button(__('Send Notification', 'nikisaz-webapp-generator'), 'primary', 'submit_app'); ?>
        </form>
    </div>
</div>

<?php
// تابع کمکی برای فرمت نام پکیج (در صورت نیاز)
function nikisaz_package_name($number) {
    if (preg_match("/^\d+$/", $number)) {
        return "hd" . $number;
    } else {
        return $number;
    }
}

// بارگذاری مدیا اپلودر وردپرس
wp_enqueue_media();
?>

<script>
jQuery(document).ready(function($) {
    // تغییر نوع نوتیفیکیشن
    $('#type').on("change", function (e) {
        $("#type_value_div").find(".input-group-addon").addClass("hidden");
        if ($(this).val() === "app") {
            $("#type_value_div").addClass("hidden");
        } else {
            $("#type_value_div").removeClass("hidden");
            if ($(this).val() === "url"){
                $("#type_value").attr("placeholder", "");
                $("#type_value_div").find(".input-group-addon").removeClass("hidden");
            }
            if ($(this).val() === "web")
                $("#type_value").attr("placeholder", "https://");
            if ($(this).val() === "sms")
                $("#type_value").attr("placeholder", "+989121234567");
            if ($(this).val() === "call")
                $("#type_value").attr("placeholder", "+989121234567");
        }
    });

    // آپدیت آیکون پیش‌نمایش هنگام تغییر API
    mobile_preview($('#api_key'));
    $('#api_key').on("change", function (e) {
        mobile_preview($(this));
    });

    function mobile_preview(e) {
        var src = $(e).find(":selected").data("icon");
        if(src) {
            $("#app_icon_preview").attr("src", src);
        }
    }

    // پیش‌نمایش عنوان
    $("#title").on("keyup", function () {
        $("#title_preview").text($(this).val());
        $("#title").next().removeClass("hidden");
        $("#title").prop('maxlength', '35');
        if($(this).val()==""){
            $("#title").next().addClass("hidden");
            $("#title_preview").text("<?php _e('Title', 'nikisaz-webapp-generator'); ?>");
        }
    });

    // پیش‌نمایش بدنه
    $("#body").on("keyup", function () {
        $("#body_preview").text($(this).val());
        $("#body").next().removeClass("hidden");
        $("#body").prop('maxlength', '150');
        if($(this).val()==""){
            $("#body").next().addClass("hidden");
            $("#body_preview").text("<?php _e('Body content...', 'nikisaz-webapp-generator'); ?>");
        }
    });

    // آپلود تصویر کوچک
    $('#small_image_btn').on('click', function (e) {
        e.preventDefault();
        var textName = $(this).prev();
        var custom_uploader;

        if (custom_uploader) {
            custom_uploader.open();
            return;
        }
        
        custom_uploader = wp.media.frames.file_frame = wp.media({
            title: 'انتخاب تصویر',
            button: {
                text: 'انتخاب تصویر'
            },
            multiple: false
        });

        custom_uploader.on('select', function () {
            var attachment = custom_uploader.state().get('selection').first().toJSON();
            textName.val(attachment.url);
            $(".android-icon-default").attr("src", attachment.url);
        });
        custom_uploader.open();
    });

    $('#small_image').on('change', function (e) {
        if($(this).val()=="")
            $(".android-icon-default").attr("src","<?php echo plugins_url( '../styles/camera.svg', __FILE__ ) ?>");
        else
            $(".android-icon-default").attr("src",$(this).val());
    });

    // آپلود تصویر بزرگ
    $('#large_image_btn').on('click', function (e) {
        e.preventDefault();
        var textName = $(this).prev();
        var custom_uploader;

        if (custom_uploader) {
            custom_uploader.open();
            return;
        }

        custom_uploader = wp.media.frames.file_frame = wp.media({
            title: 'انتخاب تصویر',
            button: {
                text: 'انتخاب تصویر'
            },
            multiple: false
        });

        custom_uploader.on('select', function () {
            var attachment = custom_uploader.state().get('selection').first().toJSON();
            textName.val(attachment.url);
            $(".android-image").css("background-image", "url("+attachment.url+")");
            $(".android-image").css("height", "175px");
        });
        custom_uploader.open();
    });

    $('#large_image').on('change', function (e) {
        if($(this).val()==""){
            $(".android-image").css("background-image", "url()");
            $(".android-image").css("height", "0");
        } else {
            $(".android-image").css("background-image", "url("+$(this).val()+")");
            $(".android-image").css("height", "175px");
        }
    });
});
</script>