<?php
$is_solution_package = false;
$package = wp_get_theme(get_template())->get('Tags');
if( is_array($package) && (in_array('nb-advanced', $package) || in_array('nb-premium', $package) || in_array('nb-enterprise', $package))) {
    $is_solution_package = true;
}
    if(is_null($current)){
            if(isset($_GET['tab'])){
                $current = $_GET['tab'];
            }
            if(!isset($_GET['tab'])){
                $current = 'nb-invoice-import-allLayout';
            }
        }
        ?>
        <?php if(! $is_solution_package):?>
            <div class="container-fluid">
                <div class="row">
                    <div class="check_license_key">
                        <div id="license-error-settings_updated" style="display:none;" class="notice settings-error is-dismissible"> 
                            <p><strong id="license_text_value"><?php echo esc_html__("Add license successfully","netbase-smart-invoice");?></strong></p><button type="button" class="notice-dismiss"><span class="screen-reader-text"><?php echo esc_html__("Dismiss this notice.","netbase-smart-invoice");?></span></button>
                        </div>
                        <?php
                            if($this->_nd_invoice_license_key == ''){
                                ?>
                                <div class="form-inline">
                                    <div id="main-form-import" class="form-group mb-2">
                                        <h4><?php echo esc_html__("Lisence Key","netbase-smart-invoice");?></h4>
                                        <input type="text" name="license_key" class="form-control-plaintext" placeholder="license key" id="license_key" value="<?php if(isset($this->_nd_invoice_license_key)){ echo $this->_nd_invoice_license_key; }?>">
                                        <button onclick="NBInvoiceCheckLicense()" id="check_license" class="button button-primary" ><?php echo esc_html__("Add License","netbase-smart-invoice");?> </button>
                                    </div>
                                </div>
                                <?php
                            }
                            if($this->_nd_invoice_license_key != ''){
                                ?>
                                <div class="form-inline">
                                    <div id="main-form-import" class="form-group mb-2">
                                        <h4><?php echo esc_html__("Lisence Key","netbase-smart-invoice");?></h4>
                                        <input type="hidden" name="license_key" id="license_key" value="<?php if(isset($this->_nd_invoice_license_key)){ echo $this->_nd_invoice_license_key; }?>">
                                        <input disabled type="text" name="license_key" id="license_key" value="<?php if(isset($this->_nd_invoice_license_key)){ echo $this->_nd_invoice_license_key; }?>">
                                    </div>
                                </div>
                                <?php
                            }
                        ?>
                        <!-- disabled -->
                    </div>
                </div>
            </div>
        <?php endif;?>
            <?php
            $key_licence = $this->_nd_invoice_license_key;
                if($key_licence != "" || $is_solution_package){
                    ?>
                    <div class="container-fluid">
                        <div class="row">
                            <?php
                                $url = INVOICE_DATA_SERVER . '/wp-json/nbinvoice/templates';
                                if(!$is_solution_package) {
                                    $data = array('license_key' => $this->_nd_invoice_license_key);
                                }
                                else {
                                    $data = array('license_key' => 'sl_package');
                                }
                                
                                $options = array(
                                    'http' => array(
                                        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
                                        'method'  => 'POST',
                                        'content' => http_build_query($data)
                                    )
                                );
                                $context  = stream_context_create($options);
                                $result = file_get_contents($url, false, $context);
                                if ($result !== FALSE) {
                                    $templates = json_decode($result, true);
                                    if(count($templates) > 0){
                                        foreach ($templates as $template) {
                                            ?>
                                            <div class="col-lg-3 col-md-4 col-sm-6 main-invoice-template">
                                                <div class="invoice-template">
                                                    <div class="main-invoice-bg">
                                                        <button onclick="NBInvoiceCheckImport(this)" file-name="<?php echo $template["data-file"];?>" class="btn btn-success nb-invoice-but-import"><?php echo esc_html__("Get Import!","netbase-smart-invoice");?></button>
                                                    </div>
                                                    <div class="main-image">
                                                        <img src="<?php echo $template["image_thumb"];?>" alt="">
                                                    </div>
                                                </div>
                                            </div>
                                            <?php
                                        }
                                    } else {
                                        ?>
                                        <div class="col-md-12 text-center">
                                            <h2><?php echo esc_html__("Contact netbase team for support","netbase-smart-invoice"); ?></h2>
                                        </div>
                                        <?php
                                    }
                                }
                            ?>
                        </div>
                    </div>
                    <?php
                }
                else {
                    ?>
                    <div class="container-fluid">
                        <div class="row">
                            <?php
                                $url = INVOICE_DATA_SERVER . '/wp-json/nbinvoice/list_templates';
                                $data = array('license_key' => $this->_nd_invoice_license_key);
                                
                                $options = array(
                                    'http' => array(
                                        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
                                        'method'  => 'GET'
                                    )
                                );
                                $context  = stream_context_create($options);
                                $result = file_get_contents($url, false, $context);
                                if ($result !== FALSE) {
                                    $templates = json_decode($result, true);
                                    if(count($templates) > 0){
                                        foreach ($templates as $template) {
                                            ?>
                                            <div class="col-md-2 main-invoice-template">
                                                <div class="invoice-template">
                                                    <div class="main-invoice-bg">
                                                        <button type="button" onclick="NBInvoiceShowError()" class="btn btn-defautl nb-invoice-but-import"><?php echo esc_html__("Get Import!","netbase-smart-invoice");?></button>
                                                    </div>
                                                    <div class="main-image">
                                                        <img src="<?php echo $template["image_thumb"];?>" alt="">
                                                    </div>
                                                </div>
                                            </div>
                                            <?php
                                        }
                                    } else {
                                        ?>
                                        <div class="col-md-12 text-center">
                                            <h2><?php echo esc_html__("Contact netbase team for support","netbase-smart-invoice"); ?></h2>
                                        </div>
                                        <?php
                                    }
                                }
                            ?>
                        </div>
                    </div>
                    <?php
                }
            ?>
        </div>
<script>
    function NBInvoiceCheckLicense(){
        jQuery("#license-error-settings_updated").css("display", "none");
        jQuery("#nb_loadding_license").css("display", "block");
        jQuery.post("<?php echo INVOICE_DATA_SERVER . '/wp-json/nbinvoice/checklicense';?>",
                {
                    license_key: jQuery("#license_key").val(),
                    url_site: "<?php echo home_url();?>"
                },
                function(data, status){
                    if (typeof data.item !== "undefined") { 
                        jQuery.post("?page=nb-smart-invoice-setting-pages",
                        {
                            license_key: jQuery("#license_key").val(),
                        },
                        function(data, status){
                            var $_textErr = "<?php echo esc_html__("Add license successfully","netbase-smart-invoice");?>";
                            jQuery("#license_text_value").text($_textErr);
                            jQuery("#license-error-settings_updated").removeClass("notice-error");
                            jQuery("#license-error-settings_updated").addClass("notice-success");
                            jQuery("#license-error-settings_updated").css("display", "block");
                            jQuery("#nb_loadding_license").css("display", "none");
                            var _dirUrl = window.location.href;
                            location.replace(_dirUrl);
                        });
                    } else {
                        var $_textErr = "<?php echo esc_html__("Additional license failed","netbase-smart-invoice");?>";
                        jQuery("#license_text_value").text($_textErr);
                        jQuery("#license-error-settings_updated").removeClass("notice-success");
                        jQuery("#license-error-settings_updated").addClass("notice-error");
                        jQuery("#license-error-settings_updated").css("display", "block");
                        jQuery("#nb_loadding_license").css("display", "none");
                    }
                }
            );
    }
    function NBInvoiceCheckImport(template){
        var partImport = jQuery(template).attr("file-name");
        jQuery.post("<?php echo INVOICE_DATA_SERVER . '/wp-json/nbinvoice/import';?>",
                {
                    <?php if(! $is_solution_package):?>
                    license_key: jQuery("#license_key").val(),
                    <?php else:?>
                    license_key: 'sl_package',
                    <?php endif;?>
                    file_name: partImport
                },
                function(data, status){
                    var _respon = JSON.parse(data);
                    console.log(_respon);
                    if (typeof data !== "undefined") {
                        jQuery.post("?page=nb-smart-invoice-setting-pages",
                        {
                            data_import : "true",
                            name_template: _respon.name_template,
                            template_landscape: _respon.template_landscape,
                            template_invoice_header: _respon.template_invoice_header,
                            template_invoice_content: _respon.template_invoice_content,
                            template_invoice_footer: _respon.template_invoice_footer,
                            template_custom_css: _respon.template_custom_css,
                            columns_number: _respon.columns_number,
                            item_title: _respon.item_title,
                            item_key_value: _respon.item_key_value,
                            item_title_color: _respon.item_title_color,
                            item_title_background: _respon.item_title_background,
                            public: _respon.public,
                            type_template: _respon.type_template,
                        },
                        function(data, status){
                            var $_textErr = "<?php echo esc_html__("Import Template successfully","netbase-smart-invoice");?>";
                            jQuery("#license_text_value").text($_textErr);
                            jQuery("#license-error-settings_updated").removeClass("notice-error");
                            jQuery("#license-error-settings_updated").addClass("notice-success");
                            jQuery("#license-error-settings_updated").css("display", "block");
                        });
                    }
                }
            );
    }
    function NBInvoiceShowError(){
        alert('You need to enter the license key');
    }
</script>