<?php
wp_register_style('select2.min.css', WOO2APP_CSS_URL . 'select2.min.css');
wp_enqueue_style('select2.min.css'.WOO2APP_FILES_VER);
$current_url = admin_url().'admin.php?page=woo2app/woo2app_design/woo2app_show_design.php&tab=chideman&sub_page=banner';

if (isset($_POST['woo2app-admin']) && wp_verify_nonce($_POST['woo2app-admin'], 'woo2app-admin')) {
    if($_REQUEST["select_action_banner"] && $_REQUEST["src_val"]){
        $title = $_REQUEST["title"];
        $type = ($_REQUEST["select_action_banner"]);
        $showtype = 1;
        $pic = esc_url_raw($_REQUEST["src_val"]);
        $value = "";
        if($type == 1) {
            $value = ($_REQUEST["product"]);
        }
        elseif($type == 2){
            $value = ($_REQUEST["cat"]);
            if(isset($_POST['sub_category']) && $_POST['sub_category'] == 'on'){
                $type = 7;
                $x = get_term_by('slug', $value, 'product_cat');
                if($x){
                    $value = $x->term_id;
                }
            }
        }
        elseif ($type == 3) {
            $value = ($_REQUEST["link"]);
        }
        elseif ($type == 4) {
            $value = ($_REQUEST["post"]);
        }
        elseif ($type == 5) {
            $value = ($_REQUEST["post_cat"]);
        }
        elseif ($type == 8) {
            $value = ($_REQUEST["tags"]);
        }
        elseif ($type == 9) {
            $value = explode(',', ($_REQUEST["CustomList"]));
        }
        $arr['banner1']             = array();
        $arr['banner1'][0]['title'] = ($_REQUEST["title"]);
        $arr['banner1'][0]['type']  = (isset($_REQUEST["sub_category"]) && $_REQUEST["sub_category"] == 'on') ? '7' : $type;
        $arr['banner1'][0]['value'] = $value;
        if(isset($_REQUEST["sub_category"]) && $_REQUEST["sub_category"] == 'on'){
            $x = get_term_by('slug', $value, 'product_cat');
            if($x){
                $arr['banner1'][0]['value'] = $x->term_id;
            }
        }
        $arr['banner1'][0]['pic']   = $_REQUEST["src_val"];
        $arr['height'] = ($_REQUEST["height"]);
        $value = json_encode( $arr );
        global $wpdb;
        $table_name = $wpdb->prefix . "woo2app_mainpage";
        $res = $wpdb->get_results("select MAX(mp_order) AS max_order from $table_name");
        if(!is_null($res[0]->max_order)){
            foreach ($res as $key) {
                $max = $key->max_order + 1;
            }
        }else{
            $max = 1;
        }
        $r = $wpdb->query( $wpdb->prepare("INSERT INTO $table_name 
		( mp_title , mp_type , mp_value , mp_showtype , mp_pic , mp_order ,mp_sort) 
		VALUES ( %s, %d, %s, %d, %s, %d, %s )", $title,$type,$value,$showtype,$pic,$max, 100) );
        if($r){
            echo "<p style='color:green'>" . _e('The banner has been saved. Sort the order.', 'woo2app') . '</p>';
        }
    }
    else{
        echo "<p style='color:red'>" . _e('Unfortunately, we encountered a problem.', 'woo2app') . '</p>';
    }
}
$args = array(
    'hide_empty' => 0 ,
    'taxonomy' => 'product_cat'
);
$cats = get_categories($args);
$args = array(
    'hide_empty' => 0 ,
    'taxonomy' => 'category'
);
$post_cats = get_categories($args);
$args = array(
    'hide_empty' => 0 ,
    'taxonomy' => 'product_tag'
);
$product_tags = get_categories($args);
?>
<form action="" method="post">
    <?php wp_nonce_field('woo2app-admin', 'woo2app-admin'); ?>
    <table class="form-table float-left">
        <tbody>
        <tr valign="top" class="">
            <th scope="row" class="titledesc">
                <label> <?php _e('Height', 'woo2app'); ?> </label>
            </th>
            <td class="forminp">
                <input type="text" class="regular-text" name="height" id="height" value=""
                       placeholder="<?php _e('Height(%)', 'woo2app'); ?>">
                <p>
			        <?php _e('Image Height (Percentage of display)', 'woo2app'); ?>
                </p>
            </td>
        </tr>

        <tr valign="top" class="">
            <th scope="row" class="titledesc">
                <label> <?php _e('Title', 'woo2app'); ?> </label>
            </th>
            <td class="forminp">
                <input type="text" class="regular-text" name="title" id="title" value=""
                       placeholder="<?php _e('Title', 'woo2app'); ?>">
            </td>
        </tr>
        <tr valign="top" class="">
            <th scope="row" class="titledesc">
                <label> <?php _e('Action', 'woo2app'); ?>  </label>
            </th>
            <td class="forminp">
                <select class="regular-text" id="select_action_banner" name="select_action_banner" required="required">
                    <option value=""><?php _e('Please Select ...', 'woo2app'); ?></option>
                    <option value="1"><?php _e('Product', 'woo2app'); ?></option>
                    <option value="2"> <?php _e('Product Category ', 'woo2app'); ?></option>
                    <option value="8"><?php _e('Product Tag', 'woo2app'); ?> </option>
                    <option value="3"><?php _e('Link', 'woo2app'); ?></option>
                    <option value="4"><?php _e('Post', 'woo2app'); ?></option>
                    <option value="5">  <?php _e('Post Categories', 'woo2app'); ?></option>
                    <option value="9"> <?php _e('Custom List', 'woo2app'); ?></option>
                    <option value="6"> <?php _e('No Action', 'woo2app'); ?> </option>
                </select>
            </td>
        </tr>
        <tr valign="top" class="el_type display-none" id="el_products">
            <th scope="row" class="titledesc">
                <label> <?php _e('Product ', 'woo2app'); ?></label>
            </th>
            <td class="forminp">
                <select name="product" data-id="product" class="js-data-example-ajax regular-text">

                </select>
            </td>
        </tr>
        <tr valign="top" class="el_type display-none" id="el_CustomList">
            <th scope="row" class="titledesc">
                <label> <?php _e('Custom list', 'woo2app'); ?> </label>
            </th>
            <td class="forminp">
                <select name="CustomList" id="value_custom_list" class="regular-text"
                        data-live-search="true" title="<?php _e('Select ...', 'woo2app'); ?>">
		            <?php
		            $results =  get_option('mr2app_custom_product_list');
		            foreach ($results as $key) {
			            ?>
                        <option value='<?= implode(',',$key['list_product'])?>'> <?= $key['title'];?></option>
			            <?php
		            }
		            ?>
                </select>
                <div class="clear"></div>
                <p>
                    <a href="<?= $current_url . '&sub_page=product_list'?>" class="thickbox">
                        <?php _e('Create Custom Lists', 'woo2app'); ?>
                    </a>
                </p>
            </td>
        </tr>
        <tr valign="top" class="el_type display-none" id="el_cats">
            <th scope="row" class="titledesc">
                <label><?php _e('Product Category', 'woo2app'); ?></label>
            </th>
            <td class="forminp">
                <select class="regular-text" name="cat" id="cat">
                    <?php
                    foreach ($cats as $cat) {
                        ?>
                        <option value="<?= $cat->slug; ?>"><?= $cat->name; ?></option>
                        <?php
                    }
                    ?>
                </select>

                <input type="checkbox" name="sub_category"/>
                <small><?php _e('Open sub categories of this category in the app', 'woo2app'); ?></small>
            </td>
        </tr>
        <tr valign="top" class="el_type display-none" id="el_tags">
            <th scope="row" class="titledesc">
                <label> <?php _e('Tag', 'woo2app'); ?> </label>
            </th>
            <td class="forminp">
                <select name="tags" data-id="tags" class="js-data-example-ajax regular-text">

                </select>
            </td>
        </tr>
        <tr valign="top" class="el_type display-none" id="el_link">
            <th scope="row" class="titledesc">
                <label> <?php _e('Link', 'woo2app'); ?> </label>
            </th>
            <td class="forminp">
                <input type="text" class="regular-text" name="link" id="link" value=""
                       placeholder="<?php _e('Link (example : https://my-site.com)', 'woo2app'); ?>">
            </td>
        </tr>
        <tr valign="top" class="el_type display-none" id="el_post">
            <th scope="row" class="titledesc">
                <label> <?php _e('Post', 'woo2app'); ?></label>
            </th>
            <td class="forminp">
                <select name="post" id="value_post_notif" class="regular-text js-data-example-ajax"></select>
            </td>
        </tr>
        <tr valign="top" class="el_type display-none" id="el_post_cats">
            <th scope="row" class="titledesc">
                <label> <?php _e('Post category', 'woo2app'); ?> </label>
            </th>
            <td class="forminp">
                <select class="regular-text" name="post_cat" id="post_cat">
                    <?php
                    foreach ($post_cats as $cat) {
                        ?>
                        <option class="text-right" value="<?= $cat->term_id; ?>"><?= $cat->name; ?></option>
                        <?php
                    }
                    ?>
                </select>
            </td>
        </tr>
        <tr>
            <th scope="row" class="titledesc">
                <label dir="rtl"><?php _e('Select Image', 'woo2app'); ?></label>
                <p>
                    <hr>
                    <?php _e('Best scale', 'woo2app'); ?> : 3x1
                    <hr>
                    <?php _e('Recommended Resolution', 'woo2app'); ?> : 1500x500
                    <hr>
                    <?php _e('Max Size', 'woo2app'); ?> : 150KB
                    <hr>
                    <?php _e('Format', 'woo2app'); ?> : PNG - JPG - JPEG - GIF
                </p>
            </th>
            <td class="forminp">
                <input type="hidden" id="txt_url_item_hami" name="src_val">
                <img id="div_image_item_hami" class="banner cursor-pointer width-450px height-150px"
                     src="<?= WOO2APP_URL . "files/img/750x250&text=3x1.png" ?>">
            </td>
        </tr>
        <tr >
            <th scope="row" class="titledesc">
            </th>
            <td class="forminp">
                <input type="submit" class="button button-primary" value="<?php _e('+ Add to page', 'woo2app'); ?>"
                       name="submit_banner">
            </td>
        </tbody>
    </table>
</form>
<?php
wp_enqueue_media();
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');

?>
<script>

    jQuery('#select_action_banner').on('change', function() {
        var type_val = (jQuery('#select_action_banner').val());
        //alert(type_val)
        jQuery('.el_type').css('display','none');
        if(type_val == 1){
            jQuery('#el_products').css('display','table-row');
        }
        else if(type_val == 2){
            jQuery('#el_cats').css('display','table-row');
        }
        else if(type_val == 3){
            jQuery('#el_link').css('display','table-row');
        }
        else if(type_val == 4){
            jQuery('#el_post').css('display','table-row');
        }
        else if(type_val == 5){
            jQuery('#el_post_cats').css('display','table-row');
        }
        else if(type_val == 8){
            jQuery('#el_tags').css('display','table-row');
        }
        else if(type_val == 9){
            jQuery('#el_CustomList').css('display','table-row');
        }
    });
    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();
    })
    jQuery('#search_product_banner1').keyup(function(){
        // alert(11)
        var  v = jQuery(this).val();
        if(v.length >= 3){
            jQuery("#img_load").attr("style",'display:block');
            jQuery.ajax({
                url: ajaxurl,
                data: {
                    action: "load_all_product",
                    search_product : v
                },
                success:function(data) {
                    jQuery("#img_load").attr("style",'display:none');
                    jQuery("#value_post_items_menu1").html(data);
                }
            });
        }
    });
</script>