
<?php
wp_register_style('select2.min.css', WOO2APP_CSS_URL . 'select2.min.css');
wp_enqueue_style('select2.min.css'.WOO2APP_FILES_VER);
global $wpdb;
$table_name = $wpdb->prefix . "woo2app_mainpage";
if($_POST){
    if (isset($_POST['woo2app-admin']) && wp_verify_nonce($_POST['woo2app-admin'], 'woo2app-admin') && isset($_POST['ins_list'])) {
        $title = ($_REQUEST["title"]);
        $value = $_POST['list_product'];
		$custom_product_list = get_option('mr2app_custom_product_list');
		if(!is_array($custom_product_list)){
			$custom_product_list = array();
		}
		$custom_product_list[] = array(
			'id' => uniqid(),
			'title' => $title,
			'list_product' => $value,
		);
		$r = update_option('mr2app_custom_product_list', $custom_product_list);
		if($r){
            echo "<p style='color:green'>" . _e('The list of favorite products has been saved. Sort the ordering section.', 'woo2app') . '</p>';
		}
	}
    if (isset($_POST['woo2app-admin']) && wp_verify_nonce($_POST['woo2app-admin'], 'woo2app-admin') && isset($_POST['del_q'])) {
        $id = ($_POST['id_q']);
		$custom_product_list = get_option("mr2app_custom_product_list");
		$f = "";
		$i = 0;
		foreach ($custom_product_list as $key) {
			if ($id == $key["id"]) {
				$f = $i;
			}
			$i++;
		}
		array_splice($custom_product_list, $f, 1);
		update_option("mr2app_custom_product_list",$custom_product_list);
		//exit();
		if($r){
            echo "<p style='color:green'>" . _e('Item was deleted.', 'woo2app') . '</p>';
		}
	}
}
$results = get_option('mr2app_custom_product_list');
?>
<div class="div_mainpage1" >
    <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('Title', 'woo2app'); ?> </label>
                </th>
                <td class="forminp">
                    <input type="text" name="title" id="title" class="regular-text"
                           placeholder="<?php _e('Title', 'woo2app'); ?>">
                </td>
            </tr>
            <tr valign="top"  >
                <th scope="row" class="titledesc">
                    <label> <?php _e('Product', 'woo2app'); ?> </label>
                </th>
                <td class="forminp">
                    <select name="list_product[]" data-id="product" multiple
                            data-placeholder="<?php _e('Please Select ...', 'woo2app'); ?>"
                            class="regular-text js-data-example-ajax">

                    </select>
                    <img class="display-none" height="25" width="25" id="img_load"
                         src="<?php echo WP_PLUGIN_URL . "/" . dirname(plugin_basename(__FILE__)) . '/../files/img/load.gif' ?>">
                </td>
            </tr>
            <tr>
                <th scope="row" class="titledesc">
                </th>
                <td class="forminp">
                    <input type="submit" name="ins_list" class="button button-primary"
                           value="<?php _e('Add', 'woo2app'); ?>" name="submit_banner">
                </td>
            </tr>
            </tbody>
        </table>
    </form>
    <hr>
    <h3 class="panel-title"><strong> <?php _e('Custom Product list', 'woo2app'); ?> </strong></h3>
    <table class="wp-list-table widefat  striped ">
        <thead>
        <tr>
            <th class="text-center"> <?php _e('Title', 'woo2app'); ?> </th>
            <th class="text-center"> <?php _e('Product', 'woo2app'); ?> </th>
            <th class="text-center"> -------- </th>
        </tr>
        </thead>
        <tbody>
		<?php
		$i = 0;
		if(is_array($results))
			foreach ($results as $key) {
				$i ++ ;
				?>
                <tr valign="top" >
                    <td class=""><?= $key['title']; ?></td>
                    <td class="">
						<?php
						$i=1;
						$option_value = $key['list_product'];
						foreach ($option_value as $key1) {
							echo $i++.' - '.get_the_title( $key1 ).' , ';
						}
						?>
                    </td>
                    <td class="vertical-align-middle;">
                        <form method="post" class="margin-right-5px">
                            <?php wp_nonce_field('woo2app-admin', 'woo2app-admin'); ?>
                            <input type="hidden" name="id_q" value="<?= $key['id'];?>">
                            <input type="submit"
                                   onclick="return confirm(<?php _e('Are you sure you want to delete the list?', 'woo2app'); ?>)"
                                   name="del_q" class="btn btn-danger btn-xs" value="<?php _e('Delete', 'woo2app'); ?>">
                        </form>
                    </td>
                </tr>
				<?php
			}
		?>
        </tbody>
    </table>
</div>
<?php
wp_enqueue_script( 'loadajax.js'.WOO2APP_FILES_VER , WOO2APP_JS_URL.'loadAjaxPosts.js', array('jquery'));
wp_enqueue_script('select2.min.js'.WOO2APP_FILES_VER, WOO2APP_JS_URL . 'select2.min.js');
?>