<?php include "common-header.php"; ?>
<!-- Main content -->
<div class="main-content" id="panel">
    <!-- Topnav -->
    <!-- Button trigger modal -->
    <nav class="navbar navbar-top navbar-expand navbar-dark bg-primary border-bottom d-lg-none d-md-block">
        <div class="container-fluid">
            <div class="collapse navbar-collapse" id="navbarSupportedContent">
                <!-- Search form -->
                <!-- Navbar links -->
                <ul class="navbar-nav align-items-center  ml-auto ">
                    <li class="nav-item d-xl-none">
                        <!-- Sidenav toggler -->
                        <div class="pr-3 sidenav-toggler sidenav-toggler-dark" data-action="sidenav-pin"
                            data-target="#sidenav-main">
                            <div class="sidenav-toggler-inner">
                                <i class="sidenav-toggler-line"></i>
                                <i class="sidenav-toggler-line"></i>
                                <i class="sidenav-toggler-line"></i>
                            </div>
                        </div>
                    </li>
                </ul>
            </div>
        </div>
    </nav>
    <!-- Header -->
    <!-- Header -->
    <div class="header d-flex align-items-center"
        style="min-height: 100px;background-size: cover; background-position: center top;">
        <!-- Mask -->
        <span class="mask bg-gradient-default opacity-8"></span>
        <!-- Header container -->
        <div class="container-fluid align-items-center">
            <div class="row">
                <div class="col-lg-12 col-md-10">
                    <h1 class="display-2 text-white"><?php esc_html_e( 'Affiliate Management System - WooCommerce Amazon', 'ams-wc-amazon' ); ?></h1>
                </div>
            </div>
        </div>
    </div>
    <div class="inner_content_wrapper">
        <div class="container-fluid">
            <div class="setting_wrapper text-left">
                <ul class="nav nav-pills mb-3" id="pills-tab" role="tablist" style="display: none;">
                    <li class="nav-item" role="presentation">
                      <a  href="javascript:void(0)" class="nav-link active" id="pills-general-tab" data-bs-toggle="pill" data-bs-target="#pills-general" type="button" role="tab" aria-controls="pills-general" aria-selected="true">General</a>
                    </li>
                    <li class="nav-item" role="presentation">
                      <a   href="javascript:void(0)" class="nav-link" id="pills-az-settings-tab" data-bs-toggle="pill" data-bs-target="#pills-az-settings" type="button" role="tab" aria-controls="pills-az-settings" aria-selected="false">Amazon Settings</a>
                    </li>
                    <li class="nav-item" role="presentation">
                      <a  href="javascript:void(0)" class="nav-link" id="pills-az-products-tab" data-bs-toggle="pill" data-bs-target="#pills-az-products" type="button" role="tab" aria-controls="pills-az-products" aria-selected="true">Amazon Products Cron</a>
                    </li>
                  </ul>
                  <div class="tab-content" id="pills-tabContent">
                    <div  class="tab-pane fade <?= ( !isset($_GET['tab']) || $_GET['tab'] == "pills-general-tab")?'show active':''; ?> " id="pills-general" role="tabpanel" aria-labelledby="pills-general-tab">
                        <form method="post" action="<?php echo admin_url( 'admin-post.php' ); ?>" enctype="multipart/form-data">
                            <h3 class="mt-3"><?php esc_html_e( 'General', 'ams-wc-amazon'); ?></h3>
                            <p><?php esc_html_e( 'All General setting for Affiliate Management System - WooCommerce Amazon.', 'ams-wc-amazon'); ?></p>
                            <hr>
                            <div class="setting_inner">
                                <div class="row">
                                    <div class="col-lg-3">
                                        <h4><?php esc_html_e( 'Product Per Page', 'ams-wc-amazon' ); ?></h4>
                                    </div>
                                    <div class="col-lg-8">
                                        <input class="form-control w-auto" type="text" name="product_per_page"  value="<?php echo esc_attr( $this->get_option( 'ams_product_per_page' ) ); ?>" placeholder="10">
                                        <p class="mt-2"><?php esc_html_e( 'To display products per request to Amazon store by restful api.Example -5,6,8,10 etc and max value 10', 'ams-wc-amazon' ); ?></p>
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-lg-3">
                                        <h4><?php esc_html_e( 'Buy Now Label', 'ams-wc-amazon' ); ?></h4>
                                    </div>
                                    <div class="col-lg-8">
                                        <input type="text" class="form-control w-auto mb-3" name="buy_now_label" placeholder="Buy on Amazon" value="<?php echo esc_attr( $this->get_option( 'ams_buy_now_label' ) ); ?>">
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-lg-3">
                                        <h4><?php esc_html_e( 'Buy Now Button Action', 'ams-wc-amazon' ); ?></h4>
                                    </div>
                                    <div class="col-lg-8">
                                        <div class="radio d-block mr-3">
                                            <label><input type="radio"  name="buy_action_btn" class="ams-buy-action-btn" value="<?php echo esc_attr( 'redirect' ); ?>" <?php checked( $this->get_option( 'ams_buy_action_btn' ), 'redirect', true ); ?> /><?php esc_html_e( 'Direct Amazon Details Page (for Affiliate 24 hour cookie)', 'ams-wc-amazon' ); ?></label>
                                        </div>
                                        <div class="radio d-block">
                                            <label><input type="radio"  name="buy_action_btn" class="ams-buy-action-btn" value="<?php echo esc_attr( 'cart_page' ); ?>" <?php checked( $this->get_option( 'ams_buy_action_btn' ), 'cart_page', true ); ?> /><?php esc_html_e( 'Direct Amazon Cart Page (for Affiliate 90 day cookie)', 'ams-wc-amazon' ); ?></label>
                                        </div>
                                        <div class="radio d-block mr-3">
                                            <label><input type="radio"  name="buy_action_btn" class="ams-buy-action-btn" value="<?php echo esc_attr( 'multi_cart' ); ?>" <?php checked( $this->get_option( 'ams_buy_action_btn' ), 'multi_cart', true ); ?> /><?php esc_html_e( 'Product will be added to site cart whent click checkout it will be redirected to Amazon Cart Page (for Affiliate 90 day cookie)', 'ams-wc-amazon' ); ?></label>
                                        </div>
                                        <div class="radio d-block">
                                            <label><input type="radio"  name="buy_action_btn" class="ams-buy-action-btn" value="<?php echo esc_attr( 'dropship' ); ?>" <?php checked( $this->get_option( 'ams_buy_action_btn' ), 'dropship', true ); ?> /><?php esc_html_e( 'The product will be added to the site include the DropShip fee. Checkout will be done on your site', 'ams-wc-amazon' ); ?></label>
                                        </div>
                                    </div>
                                </div>
                                <div class="row dropship_percentage_profit <?php echo $this->get_option( 'ams_buy_action_btn' ) != 'dropship' ? esc_attr( 'd-none' ) : ''; ?>">
                                    <div class="col-lg-3">
                                        <h4><?php esc_html_e( 'Custom Tax (% Percentage) ', 'ams-wc-amazon' ); ?></h4>
                                    </div>
                                    <div class="col-lg-8">
                                        <input class="form-control w-auto" type="text" name="percentage_profit"  value="<?php echo esc_attr( get_option( 'ams_percentage_profit' ) ); ?>">
                                        <p class="mt-2"><?php esc_html_e( 'How many percentage will be added to the price as profit amount for single product', 'ams-wc-amazon' ); ?></p>
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-lg-3">
                                        <h4><?php esc_html_e( 'Enable No Follow to Link', 'ams-wc-amazon' ); ?></h4>
                                    </div>
                                    <div class="col-lg-8">
                                        <div class="radio d-block mr-3">
                                            <label><input type="radio"  name="enable_no_follow_link" value="<?php echo esc_attr( 'nofollow' ); ?>" <?php checked( $this->get_option( 'ams_enable_no_follow_link' ), 'nofollow', true ); ?> /><?php esc_html_e( 'Yes', 'ams-wc-amazon' ); ?></label>
                                        </div>
                                        <div class="radio d-block">
                                            <label><input type="radio"  name="enable_no_follow_link" value="<?php echo esc_attr( 'follow' ); ?>" <?php checked( $this->get_option( 'ams_enable_no_follow_link' ), 'follow', true ); ?> /><?php esc_html_e( 'No', 'ams-wc-amazon' ); ?></label>
                                        </div>
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-lg-3">
                                        <h4><?php esc_html_e( 'Product Import Default Category', 'ams-wc-amazon' ); ?></h4>
                                    </div>
                                    <div class="col-lg-8">
                                        <div id="container" class="dropDown_container mb-3">
                                            <select id="ams_default_category" name="ams_default_category" placeholder-text="<?= $this->get_option( 'ams_default_category' ) == "_auto_import_amazon" ? "Auto Import From Amazon" : $this->get_option( 'ams_default_category' ); ?>">
                                                <option value="_auto_import_amazon" class="select-dropdown__list-item" <?php selected( $this->get_option( 'ams_default_category' ), "_auto_import_amazon" ); ?>> Auto Import From Amazon</option>
                                                <?php
                                                foreach ( $this->get_wc_terms() as $value ) {
                                                    ?>
                                                    <option value="<?php echo esc_attr( $value['name'] ); ?>" class="select-dropdown__list-item" <?php selected( $this->get_option( 'ams_default_category' ), $value['name'] ); ?>><?php echo esc_html( $value['name'] ); ?></option>
                                                    <?php
                                                }
                                                ?>
                                            </select>
                                        </div>
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-lg-3">
                                        <h4><?php esc_html_e( 'Checkout Message', 'ams-wc-amazon' ); ?></h4>
                                    </div>
                                    <div class="col-lg-6">
                                        <textarea class="c_textarea mb-3" name="checkout_mass_redirected" cols="30" rows="10" style="height: 70px;"><?php echo get_option( 'ams_checkout_mass_redirected' ); ?></textarea>
                                    </div>
                                    <div class="col-lg-3">
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-lg-3">
                                        <h4><?php esc_html_e( 'Checkout redirect in', 'ams-wc-amazon' ); ?></h4>
                                    </div>
                                    <div class="col-lg-8">
                                        <input type="text" class="form-control w-auto" name="checkout_redirected_seconds"  value="<?php echo esc_attr( get_option( 'ams_checkout_redirected_seconds' ) ); ?>">
                                        <p class="mt-2"><?php esc_html_e( 'How many seconds to wait before redirect to Amazon!', 'ams-wc-amazon' ); ?></p>
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-lg-3">
                                        <h4><?php esc_html_e( 'Remote amazon images', 'ams-wc-amazon' ); ?></h4>
                                    </div>
                                    <div class="col-lg-8">
                                        <div id="container" class="dropDown_container mb-3">
                                            <select id="remote_amazon_images" name="remote_amazon_images" placeholder-text="<?= $this->get_option( 'ams_remote_amazon_images' ); ?>">
                                                <option value="<?php echo esc_attr( 'Yes' ); ?>" <?php selected( $this->get_option( 'ams_remote_amazon_images' ), 'Yes', true ); ?> class="select-dropdown__list-item"><?php esc_html_e( 'Yes', 'ams-wc-amazon' ); ?></option>
                                                <option value="<?php echo esc_attr( 'No' ); ?>" <?php selected( $this->get_option( 'ams_remote_amazon_images' ), 'No', true ); ?> class="select-dropdown__list-item"><?php esc_html_e( 'No', 'ams-wc-amazon' ); ?></option>
                                            </select>
                                        </div>
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-lg-3">
                                        <h4><?php esc_html_e( 'Select remote images sizes', 'ams-wc-amazon' ); ?></h4>
                                    </div>
                                    <div class="col-lg-8">
                                        <div id="container" class="dropDown_container mb-3">
                                            <?php
                                                switch ($this->get_option( 'ams_product_thumbnail_size' )) {
                                                    case 'hd':
                                                        $ams_product_thumbnail_size_placeholder = "HD (2048 X 2048)";
                                                        break;
                                                    case 'extra_large':
                                                        $ams_product_thumbnail_size_placeholder = "Extra Large (1024 X 1024)";
                                                        break;
                                                    case 'Large':
                                                        $ams_product_thumbnail_size_placeholder = "Large (500 X 500)";
                                                        break;
                                                    case 'Medium':
                                                        $ams_product_thumbnail_size_placeholder = "Medium (160 X 160)";
                                                        break;
                                                    case 'Small':
                                                        $ams_product_thumbnail_size_placeholder = "Small (75 X 75)";
                                                        break;
                                                }                                        
                                            ?>
                                            <select id="product_thumbnail_size" name="product_thumbnail_size" placeholder-text="<?= $ams_product_thumbnail_size_placeholder; ?>">
                                                <option value="<?php echo esc_attr( 'hd' ); ?>" <?php selected( $this->get_option( 'ams_product_thumbnail_size' ), 'hd', true ); ?> class="select-dropdown__list-item"><?php esc_html_e( 'HD (2048 X 2048)', 'ams-wc-amazon' ); ?></option>
                                                <option value="<?php echo esc_attr( 'extra_large' ); ?>" <?php selected( $this->get_option( 'ams_product_thumbnail_size' ), 'extra_large', true ); ?> class="select-dropdown__list-item"><?php esc_html_e( 'Extra Large (1024 X 1024)', 'ams-wc-amazon' ); ?></option>
                                                <option value="<?php echo esc_attr( 'Large' ); ?>" <?php selected( $this->get_option( 'ams_product_thumbnail_size' ), 'Large', true ); ?> class="select-dropdown__list-item"><?php esc_html_e( 'Large (500 X 500)', 'ams-wc-amazon' ); ?></option>
                                                <option value="<?php echo esc_attr( 'Medium' ); ?>" <?php selected( $this->get_option( 'ams_product_thumbnail_size' ), 'Medium', true ); ?> class="select-dropdown__list-item"><?php esc_html_e( 'Medium (160 X 160)', 'ams-wc-amazon' ); ?></option>
                                                <option value="<?php echo esc_attr( 'Small' ); ?>" <?php selected( $this->get_option( 'ams_product_thumbnail_size' ), 'Small', true ); ?> class="select-dropdown__list-item"><?php esc_html_e( 'Small (75 X 75)', 'ams-wc-amazon' ); ?></option>
                                            </select>
                                        </div>
                                    </div>
                                </div>
                                <div class="text-center">
                                    <input type="hidden" name="action" value="<?php echo esc_attr( 'ams-wc-general-setting' ); ?>">

                                    <input type="hidden" name="action_tab" value="pills-general-tab">
                                    
                                    <?php wp_nonce_field("general_setting_nonce")?>
                                    <button type="submit" name="general-setting-submit" id="general-setting-submit" value="Save Settings" class="btn btn-primary my-4"><?= esc_html__("Save Settings","ams-wc-amazon"); ?></button>
                                </div>
                            </div>
                        </form>
                    </div>
                    <div class="tab-pane fade <?= ( isset($_GET['tab']) && $_GET['tab'] == "pills-az-settings-tab")?'show active':''; ?> " id="pills-az-settings" role="tabpanel" aria-labelledby="pills-az-settings-tab">
                        <h3 class="mt-3"><?php esc_html_e( 'Credentials', 'ams-wc-amazon' ); ?></h3>
                        <hr>
                        <div class="setting_inner">
                            <form method="post" action="<?php echo admin_url( 'admin-post.php' ); ?>" enctype="multipart/form-data">
                                <div class="row">
                                    <div class="col-lg-3">
                                        <h4><?php esc_html_e( 'Access Key ID', 'ams-wc-amazon' ); ?></h4>
                                    </div>
                                    <div class="col-lg-8">
                                        <input type="text" id="access_key_id" name="access_key_id" value="<?php echo esc_attr( $this->get_option( 'ams_access_key_id' ) ); ?>" class="form-control w-auto mb-2" />
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-lg-3">
                                        <h4><?php esc_html_e( 'Secret Access Key','ams-wc-amazon' ); ?></h4>
                                    </div>
                                    <div class="col-lg-8">
                                        <input type="text" id="secret_access_key" name="secret_access_key" value="<?php echo esc_attr( $this->get_option( 'ams_secret_access_key' ) ); ?>"  class="form-control w-auto mb-3">
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-lg-3">
                                        <h4><?php esc_html_e( 'Amazon Affiliate Associate Tag', 'ams-wc-amazon' ); ?></h4>
                                    </div>
                                    <div class="col-lg-8">
                                        <input type="text" id="input-username" class="form-control w-auto mb-3" name="ams_associate_tag" placeholder="<?php echo esc_attr__( 'Associate ID', 'ams-wc-amazon' ); ?>" value="<?php echo esc_attr( $this->get_option( 'ams_associate_tag' ) ); ?>"/>
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-lg-3">
                                        <h4><?php esc_html_e( 'Select Country', 'ams-wc-amazon' ); ?></h4>
                                    </div>
                                    <div class="col-lg-8">
                                        <div id="container" class="dropDown_container mb-3">
                                            <?php
                                            $regions = ams_get_amazon_regions();
                                            $regions_placeholder = $this->get_option( 'ams_amazon_country' );
                                            foreach ($regions as $key=>$value) {
                                                if($regions_placeholder == $key) {
                                                    $regions_placeholder = $value;                             
                                                    break;
                                                }
                                            }
                                            ?>
                                        <select name="ams_amazon_country" id="amazon_country" placeholder-text="<?= $value['RegionName']; ?>">
                                            <?php
                                                foreach ( $regions as $key => $value ) {
                                                ?>
                                                <option class="select-dropdown__list-item" <?php selected( $this->get_option( 'ams_amazon_country' ), $key, true ); ?> value="<?php echo esc_attr( $key ); ?>"><?php echo esc_attr( $value['RegionName'] ); ?></option>
                                                <?php
                                                }
                                                ?>
                                            </select>
                                        </div>
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-lg-3">
                                        <h4><?php esc_html_e( 'WooCommerce Currency Setting', 'ams-wc-amazon' ); ?></h4>
                                    </div>
                                    <div class="col-lg-8">
                                        <div id="container" class="dropDown_container mb-3">
                                            <?php $store_currency = get_woocommerce_currency();
                                            $currency_code_options = get_woocommerce_currencies();
                                            $currency_placeholder = $store_currency;
                                            foreach ($currency_code_options as $code=>$value) {
                                                if($store_currency == $code) {
                                                    $currency_placeholder = $value;                             
                                                    break;
                                                }
                                            }
                                            ?>
                                        <select name="woocommerce_currency" id="woocommerce_currency" placeholder-text="<?= $currency_placeholder; ?>">
                                            <?php foreach ($currency_code_options as $code=>$value) { ?>
                                                <option value="<?php echo esc_attr( $code); ?>" <?php selected( $store_currency, $code ); ?>><?php echo esc_html( $value ); ?></option>
                                            <?php } ?>
                                        </select>
                                        </div>
                                    </div>
                                </div>
                                <div class="text-center">
                                    <input type="hidden" name="action" value="<?php echo esc_attr( 'ams_wc_amazon_general_setting' ); ?>">


                                    <input type="hidden" name="action_tab" value="pills-az-settings-tab">

                                    <?php wp_nonce_field( 'general_amazon_setting_nonce' ); ?>
                                    <button type="submit" name="general-setting-submit" id="general-setting-submit" value="<?= esc_html__( 'Save Settings', 'ams-wc-amazon' ); ?>" class="btn btn-primary my-4"><?= esc_html__( 'Save Settings', 'ams-wc-amazon' ); ?></button>
                                </div>
                            </form>
                            <h3 class="mt-3"><?php esc_html_e( 'Amazon api test', 'ams-wc-amazon' ); ?></h3>
                            <p><?php esc_html_e( 'Test your API settings to make sure everything is setup correctly. Save your settings before testing.', 'ams-wc-amazon' ); ?></p>
                            <hr>
                            <div class="text-left">
                                <button type="button" class="btn btn-primary ams-test-api-btn"><?php echo esc_html__( 'Test API Settings', 'ams-wc-amazon' ); ?></button>
                            </div>
                            <div class="ams-api-massage"></div>
                        </div>
                    </div>
                    <div class="tab-pane fade <?= ( isset($_GET['tab']) && $_GET['tab'] == "pills-az-products-tab")?'show active':''; ?> " id="pills-az-products" role="tabpanel" aria-labelledby="pills-az-products-tab">
                        <h3 class="mt-3"><?php esc_html_e( 'Amazon Cron', 'ams-wc-amazon'); ?></h3>
                        <p><?php esc_html_e( 'Amazon Cron setting for Affiliate Management System - WooCommerce Amazon.', 'ams-wc-amazon'); ?></p>
                        <hr>       
                        <div class="setting_inner">
                            <form method="post" action="<?php echo admin_url( 'admin-post.php' ); ?>" enctype="multipart/form-data">
                                <div class="row">
                                    <div class="col-lg-3">
                                        <h4><?php esc_html_e( 'Cron Command', 'ams-wc-amazon' ); ?></h4>
                                    </div>
                                    <div class="col-lg-8">
                                        <textarea class="c_textarea mb-3" name="checkout_mass_redirected" cols="30" rows="10" style="height: 70px;">wget -q -O - <?php echo get_bloginfo('wpurl')?>/wp-cron.php?doing_wp_cron >/dev/null 2>&1</textarea>
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-lg-3">
                                        <h4><?php esc_html_e( 'Price Cron', 'ams-wc-amazon' ); ?></h4>
                                    </div>
                                    <div class="col-lg-8">
                                        <label><input type="checkbox" class="ams-buy-action-btn" name="product_price_cron" value="1" <?php checked( $this->get_option( 'product_price_cron' ), '1', true ); ?> /></label>
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-lg-3">
                                        <h4><?php esc_html_e( 'SKU Cron', 'ams-wc-amazon' ); ?></h4>
                                    </div>
                                    <div class="col-lg-8">
                                        <label><input type="checkbox" class="ams-buy-action-btn" name="product_sku_cron" value="1" <?php checked( $this->get_option( 'product_sku_cron' ), '1', true ); ?> /></label>
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-lg-3">
                                        <h4><?php esc_html_e( 'Variants Cron', 'ams-wc-amazon' ); ?></h4>
                                    </div>
                                    <div class="col-lg-8">
                                        <label><input type="checkbox" class="ams-buy-action-btn" name="product_variants_cron" value="1" <?php checked( $this->get_option( 'product_variants_cron' ), '1', true ); ?> /></label>
                                    </div>
                                </div>
                                <!-- <div class="row">
                                    <div class="col-lg-3">
                                        <h4><?php esc_html_e( 'Tags Cron', 'ams-wc-amazon' ); ?></h4>
                                    </div>
                                    <div class="col-lg-8">
                                        <label><input type="checkbox" class="ams-buy-action-btn" name="product_tags_cron" value="1" <?php checked( $this->get_option( 'product_tags_cron' ), '1', true ); ?> /></label>
                                    </div>
                                </div> -->
                                <div class="row">
                                    <div class="col-lg-3">
                                        <h4><?php esc_html_e( 'Out Of Stock Cron', 'ams-wc-amazon' ); ?></h4>
                                    </div>
                                    <div class="col-lg-8">
                                        <label><input type="checkbox" class="ams-buy-action-btn" name="product_out_of_stock_cron" value="1" <?php checked( $this->get_option( 'product_out_of_stock_cron' ), '1', true ); ?> /></label>
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-lg-3">
                                        <h4><?php esc_html_e( 'Description Cron', 'ams-wc-amazon' ); ?></h4>
                                    </div>
                                    <div class="col-lg-8">
                                        <label><input type="checkbox" class="ams-buy-action-btn" name="product_description_cron" value="1" <?php checked( $this->get_option( 'product_description_cron' ), '1', true ); ?> /></label>
                                    </div>
                                </div>    
                                <div class="row">
                                    <div class="col-lg-3">
                                        <h4><?php esc_html_e( 'Update Image Cron', 'ams-wc-amazon' ); ?></h4>
                                    </div>
                                    <div class="col-lg-8">
                                        <label><input type="checkbox" class="ams-buy-action-btn" name="product_image_cron" value="1" <?php checked( $this->get_option( 'product_image_cron' ), '1', true ); ?> /></label>
                                    </div>
                                </div> 
                                <div class="row">
                                    <div class="col-lg-3">
                                        <h4><?php esc_html_e( 'Update Product Category', 'ams-wc-amazon' ); ?></h4>
                                    </div>
                                    <div class="col-lg-8">
                                        <label><input type="checkbox" class="ams-buy-action-btn" name="product_category_cron" value="1" <?php checked( $this->get_option( 'product_category_cron' ), '1', true ); ?> /></label>
                                    </div>
                                </div>                            
                                <div class="text-center">
                                    <input type="hidden" name="action" value="<?php echo esc_attr( 'ams_product_cron' ); ?>">


                                    <input type="hidden" name="action_tab" value="pills-az-products-tab">

                                    <?php wp_nonce_field("general_setting_nonce")?>
                                    <button type="submit" name="general-setting-submit" id="general-setting-submit" value="<?= esc_html__( 'Save Settings', 'ams-wc-amazon' ); ?>" class="btn btn-primary my-4"><?= esc_html__( 'Save Settings', 'ams-wc-amazon' ); ?></button>
                                </div>
                            </form>
                        </div>
                        <div>
                            <?php
                                global $wpdb;
                                @$product_tags_cron =  get_option('product_tags_cron');
                                @$product_variants_cron =  get_option('product_variants_cron');
                                @$product_sku_cron = get_option('product_sku_cron');
                                @$product_price_cron =  get_option('product_price_cron');
                                @$product_out_of_stock_cron = get_option('product_out_of_stock_cron');
                                @$product_description_cron = get_option('product_description_cron');
                                @$product_image_cron = get_option('product_image_cron');
                                @$product_category_cron = get_option('product_category_cron');
                                @$asins = ams_get_all_products_info();
                                @$id_asin = array_combine( $asins['id'], $asins['asin'] );
                                @$import_id = $asins['product_id'];
                                @$ids_bunch = array_chunk( $id_asin, 1, true);
                                $url = 'ams_product_availability';
                                $region=null;
                                if(isset($import_ids['method']) && $import_ids['method'] == 1){
                                    $url = 'ams_product_availability';
                                } else if(isset($import_ids['method']) && $import_ids['method'] == 2){
                                    $url = 'ams_product_availability';
                                }
                            ?>
                            <h3 class="mt-4">Cron Amazon Products</h3>
                            <p>Cron / Update Amazon products by method they are imported!</p>
                            <hr/>
                            <button id="prod_cron" class="btn btn-primary btn_cron mb-4" onclick="cron_run();" >Start Croning</button>
                            <div id="show_responce"></div>
                            <script type="text/javascript">
                                var cronIndex = 0;
                                var count = 0;
                                var idArr = [];
                                import_id = <?php echo json_encode($import_id)?>;
                                function cron_run(){
                                    cronIndex = 0;
                                    count = 0;
                                    idArr = <?php echo json_encode($ids_bunch)?>;
                                    jQuery('#prod_cron').html( '<span class="dashicons dashicons-update wca-spin" style="margin-top: 2px; margin-right: 5px;"></span> 0 out of <?= $asins['products_count']?> products updated.');
                                    jQuery('#prod_cron').attr('disabled', true);
                                    run_cron_single_bunch(idArr[cronIndex]);
                                    count_arry = Object.keys(idArr[cronIndex]).length;
                                    count += count_arry;
                                }
                                        // url:amsbackend.ams_product_availability,
                                function run_cron_single_bunch(id_array){
                                    pid = Object.keys(id_array)[0];
                                    url = 'ams_product_availability';
                                    region = asin = product_url ='';
                                    if(import_id[pid]){
                                        if(import_id[pid]['method'] == 1){
                                            url = 'ams_product_import';
                                            region = import_id[pid]['region'];
                                            asin = id_array[pid];
                                        } else {
                                            url = 'ams_product_availability';
                                            region = import_id[pid]['region'];
                                            asin = id_array[pid];
                                        }
                                        product_url = import_id[pid]['url'];
                                    }
                                    data = {data:id_array,action:url,region:region,is_cron:'1',asin:asin,prod_id:pid, product_url:product_url};
                                    jQuery.ajax({
                                        url:"<?php echo admin_url("admin-ajax.php") ?>",
                                        type:'POST',
                                        data:data,
                                        success: function(data){
                                            jQuery('#prod_cron').html( '<span class="dashicons dashicons-update wca-spin" style="margin-top: 2px; margin-right: 5px;"></span> '+count+' out of <?= $asins['products_count']?> products updated.');
                                            cronIndex++;
                                            if(idArr[cronIndex]){
                                                var count_arry = Object.keys(idArr[cronIndex]).length;
                                                count += count_arry;
                                                run_cron_single_bunch(idArr[cronIndex]);
                                            }else{
                                                jQuery('#prod_cron').html( 'Start Croning');
                                                jQuery('#prod_cron').removeAttr('disabled');
                                            }
                                            jQuery('#show_responce').append(data);
                                            console.log('done' + cronIndex);
                                        }
                                    });
                                }
                            </script>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
document.querySelectorAll('input[name="buy_action_btn"]').forEach(item => {
  item.addEventListener('change', event => {
    if(event.target.value == 'dropship'){
        document.querySelector('.dropship_percentage_profit').classList.remove("d-none");
    } else {
        document.querySelector('.dropship_percentage_profit').classList.add("d-none");
    }
  })
});


</script>