<?php
/**
 * Created by mr2app.
 * User: Hani
 * Date: 4/20/2019
 * Time: 8:58 AM
 */

$page_login = get_option('woo2app_loginPage');
if(!is_array($page_login)){
	$page_login =  array(
		'background'       =>   '',
		'banner'           =>   '',
	);
}
?>
<form method="post" action="options.php">
	<?php
	settings_fields( 'woo2app_login-group' );
	do_settings_sections('woo2app_login-group' );
	?>
    <?php //wp_nonce_field('woo2app-admin', 'woo2app-admin'); ?>
    <div class="<?= get_locale() == 'fa_IR' ? 'float-right' : 'float-left' ?>">
        <h3 class="background-color-e5e5e5 padding-7px">
            <?php _e('Background', 'woo2app'); ?>
        </h3>
        <input type="hidden" id="background_val" value="<?= $page_login['background']?>" name="woo2app_loginPage[background]">
        <img id="background" class="images banner cursor-pointer width-282px height-500px"
             src="<?= ($page_login['background'] != "") ? $page_login['background'] : 'http://placehold.it/720x1280&text=Select' ?>">
        <div class="clear"></div>
        <h3 class="background-color-e5e5e5 padding-7px">
            <?php _e('B‌‌anner', 'woo2app'); ?>
        </h3>
        <input type="hidden" id="banner_val" value="<?= $page_login['banner']?>" name="woo2app_loginPage[banner]">
        <img id="banner" class="images banner cursor-pointer width-282px height-141px"
             src="<?= ($page_login['banner'] != "") ? $page_login['banner'] : 'http://placehold.it/450x225&text=Select' ?>">
        <div class="clear"></div>
        <div class="padding-left-10px">
		    <?php submit_button('Save changes'); ?>
        </div>
    </div>
    <div class="<?= get_locale() == 'fa_IR' ? 'float-left' : 'float-right' ?> padding-left-10px">
        <h3 class="background-color-e5e5e5 padding-7px">
            <?php _e('Sample login screen in app', 'woo2app'); ?>
        </h3>
        <img src="<?= WOO2APP_URL . "files/img/login-page.jpg" ?>"
             class="width-340px"/>
    </div>
</form>

<?php
wp_enqueue_media();
wp_enqueue_script( 'loadajax.js'.WOO2APP_FILES_VER , WOO2APP_JS_URL.'loadAjaxPosts.js', array('jquery'));
?>
<script>

    var custom_uploader_hami;
    jQuery('.images').click(function (e) {
        e.preventDefault();
        let id = jQuery(this).attr('id');
        custom_uploader_hami = wp.media.frames.custom_uploader_hami = wp.media({
            title: '<?php _e('Select image', 'woo2app'); ?>',
            library: {type: 'image'},
            button: {text: '<?php _e('Select', 'woo2app'); ?>'},
            multiple: false
        });
        custom_uploader_hami.on('select', function() {
            attachment = custom_uploader_hami.state().get('selection').first().toJSON();

            console.log(id);
            jQuery('#' + id + '_val').val(attachment.url);
            url_image = attachment.url;
            jQuery("#" + id).attr("src",url_image);
        });
        custom_uploader_hami.open();
    })

</script>