<?php

wp_register_style( 'woo2app_style', WOO2APP_CSS_URL.'woo2app_style.css'  );
wp_enqueue_style( 'woo2app_style'.WOO2APP_FILES_VER );
if(get_locale() == 'fa_IR'){
	wp_register_style('myfont', WOO2APP_CSS_URL . 'font.css');
	wp_enqueue_style( 'myfont'.WOO2APP_FILES_VER);
}

$current_url = "//".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
function insert_woo2app_shop(){
    $name = ($_POST["name"]);
    $subdomain = ($_POST["subdomain"]);
    $pic = ($_POST["src_val"]);
	$uniqid = uniqid();

	$array = get_option("mr2app_shop");
	$array[] = array(
		"uniqid" => $uniqid ,
		"name" => $name ,
		"subdomain" => $subdomain ,
		"icon" => $pic ,
	);
	update_option("mr2app_shop",$array);
}
function update_woo2app_shop() {
	if ( isset( $_GET['field'] ) ) {
		if ( $_GET['field'] != "" ) {
            $name = ($_POST["name"]);
            $subdomain = ($_POST["subdomain"]);
            $pic = ($_POST["src_val"]);
			$key = $_GET["field"];
			$plans = get_option("mr2app_shop");
			$i = 0;
			foreach ($plans as $value) {
				if ($key == $value["uniqid"]) {
					$array[] = array(
						"uniqid" => $value["uniqid"] ,
						"name" => $name ,
						"subdomain" => $subdomain ,
						"icon" => $pic ,
					);
				}
				else{
					$array[] = array(
						"uniqid" => $value["uniqid"] ,
						"name" => $value["name"] ,
						"subdomain" => $value["subdomain"] ,
						"icon" => $value["icon"] ,
					);
				}
				$i++;
			}
			update_option("mr2app_shop",$array);
			?>
			<p>
                <?php _e('Edit done ...', 'woo2app'); ?>
				<br>
                <a href="admin.php?page=woo2app/woo2app_design/shop.php"> <?php _e('back', 'woo2app'); ?> </a>
			</p>
			<?php
			exit;
			//wp_redirect('admin.php?page=mr2app_sale_meta/sale_meta.php&tab=plans');
		}
	}
}

function delete_woo2app_shop(){
	if(isset($_GET['field'])){
		if($_GET['field'] != ""){
			$key = $_GET["field"];
			$plans = get_option("mr2app_shop");
			$f = "";
			$i = 0;
			foreach ($plans as $value) {
				if ($key == $value["uniqid"]) {
					$f = $i;
				}
				$i++;
			}
			array_splice($plans, $f, 1);
			update_option("mr2app_shop",$plans);
			?>
			<p>
                <?php _e('The store was deleted ...', 'woo2app'); ?>
				<br>
                <a href="admin.php?page=woo2app/woo2app_design/shop.php"> <?php _e('back ', 'woo2app'); ?></a>
			</p>
			<?php
			exit;
			//wp_redirect('admin.php?page=mr2app_sale_meta/sale_meta.php&tab=plans');
		}
	}
}
$for_edit = array();
$for_edit['uniqid'] = '';
$for_edit['name'] = '';
$for_edit['subdomain'] = '';
$for_edit['value'] = '';

if(isset($_GET["action"])){
	if($_GET['action'] == 'delete'){
		delete_woo2app_shop();
	}
	elseif($_GET['action'] == 'edit'){
		if (
			isset($_POST["name"])
			&& $_POST["name"] != ""
		)
		{
			update_woo2app_shop();
		}
		else{
			if(isset($_GET['field'])){
				$plans = get_option("mr2app_shop");
				$i = 0;
				$key = $_GET['field'];
				foreach ($plans as $value) {
					if ($key == $value["uniqid"]) {
						$for_edit = array(
							"uniqid" => $value["uniqid"] ,
							"name" => $value["name"] ,
							"subdomain" => $value["subdomain"] ,
							"value" => $value["icon"] ,
						);
					}
				}
			}
		}
	}
}
else{
	if (
		isset($_POST["name"])
		&& $_POST["name"] != ""
	)
	{
		insert_woo2app_shop();
	}
}
$plans = get_option("mr2app_shop");
if(!is_array($plans)) $plans = array();
?>
<div class="wrap">
    <h1>
        <?php _e('Multi-store', 'woo2app'); ?>
    </h1>

	<form action="" method="post">
        <?php wp_nonce_field('woo2app-admin', 'woo2app-admin'); ?>
        <table class="form-table">
			<tbody>
			<tr valign="top" class="">
				<th scope="row" class="titledesc">
                    <label> <?php _e('Name', 'woo2app'); ?> </label>
				</th>
				<td class="forminp">
                    <input type="text" class="regular-text" value="<?= $for_edit['name'] ?>" name="name"
                           placeholder=" <?php _e('Name', 'woo2app'); ?>">
				</td>
			</tr>
			<tr>
			<th scope="row" class="titledesc">
                <label>  <?php _e('Domain', 'woo2app'); ?>  </label>
			</th>
			<td class="forminp">
                <input type="text" class="regular-text" value="<?= $for_edit['subdomain'] ?>" name="subdomain"
                       placeholder="<?php _e('Domain OR SubDomain', 'woo2app'); ?> ">
			</td>
			</tr>

			<tr  valign="top"  class="el_type"  id="image"  >
				<th scope="row" class="titledesc">
                    <label dir="rtl">  <?php _e('Image', 'woo2app'); ?>  </label>
				</th>
				<td class="forminp">
					<input type="hidden" value="<?= $for_edit['value']?>" id="txt_url_item_hami" name="src_val">
                    <img id="div_image_item_hami" class="banner cursor-pointer height-128px"
                         src="<?= ($for_edit['value'] != '') ? $for_edit['value'] : 'http://placehold.it/512x512' ?>">
				</td>
			</tr>

			<tr >
				<th scope="row" class="titledesc"></th>
				<td class="forminp">
                    <input type="submit" value="<?php _e('Save', 'woo2app'); ?>" name="submit_banner">
				</td>
			</tr>
			</tbody>
		</table>
	</form>
	<hr>
	<table class="wp-list-table widefat striped">
		<tbody>
		<tr valign="top" class="">
			<th scope="row" class="">
				<label > #  </label>
			</th>
			<th scope="row" class="">
                <label>  <?php _e('Image', 'woo2app'); ?>  </label>
			</th>
			<th scope="row" class="">
                <label> <?php _e('Name', 'woo2app'); ?>  </label>
			</th>
			<th scope="row" class="">
                <label> <?php _e('Domain / SubDomain', 'woo2app'); ?> </label>
			</th>
			<th scope="row" class="">
                <label>  <?php _e('Edit / Delete', 'woo2app'); ?>  </label>
			</th>
		</tr>
		<?php
		$i = 0;
		foreach ($plans as $key){
			$i++;
			?>
			<tr valign="top" class="">
				<th scope="row" class="">
					<label > <?= $i; ?>  </label>
				</th>
				<th scope="row" class="">
                    <img class="width-92px" src="<?= $key['icon'] ?>">
				</th>
				<th scope="row" class="">
					<label > <?= $key['name'];?>   </label>
				</th>
				<th scope="row" class="">
					<label > <?= $key['subdomain'];?>   </label>
				</th>
				<th scope="row" class="">
                    <a href="<?= $current_url . '&action=edit&field=' . $key['uniqid'] ?>"> <?php _e('Edit', 'woo2app'); ?></a>
					|
                    <a href="<?= $current_url . '&action=delete&field=' . $key['uniqid'] ?>"
                       class="red">  <?php _e('Delete', 'woo2app'); ?> </a>
				</th>
			</tr>
			<?php
		}
		?>
		</tbody>
	</table>
</div>



<?php
wp_enqueue_media();
?>
<script>

    var custom_uploader_hami;
    jQuery('#div_image_item_hami').click(function (e) {
        e.preventDefault();
        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();
            jQuery('#txt_url_item_hami').val(attachment.url);
            url_image = attachment.url;
            jQuery('#div_image_item_hami').attr("src",url_image);
        });
        custom_uploader_hami.open();
    })
</script>
