{{ header }}{{ column_left }}
<div id="content">
  <div class="page-header">
    <div class="container-fluid">
      <div class="pull-right">
      &nbsp;  
      </div>
      <h1>{{ heading_title_seo }}</h1>
      <ul class="breadcrumb">
        {% for breadcrumb in breadcrumbs %}
        <li><a href="{{ breadcrumb.href }}">{{ breadcrumb.text }}</a></li>
        {% endfor %}
      </ul>
    </div>
  </div>
  <div class="container-fluid">{% if error_warning %}
    <div class="alert alert-danger alert-dismissible"><i class="fa fa-exclamation-circle"></i> {{ error_warning }}
      <button type="button" class="close" data-dismiss="alert">&times;</button>
    </div>
    {% endif %}
    {% if success %}
    <div class="alert alert-success alert-dismissible"><i class="fa fa-check-circle"></i> {{ success }}
      <button type="button" class="close" data-dismiss="alert">&times;</button>
    </div>
    {% endif %}
    
    <div class="row">
        <div id="filter-product" class="col-md-12 col-sm-12 hidden-sm hidden-xs">
        <div class="panel panel-default">
          <div class="panel-heading">
            <h3 class="panel-title"><i class="fa fa-filter"></i> {{ text_filter }}</h3>
          </div>
          <div class="panel-body">
              
            <div class="row">
                <div class="col-md-4">
                    <div class="form-group">
                      <label class="control-label" for="input-name">{{ entry_name }}</label>
                      <input type="text" name="filter_name" value="{{ filter_name }}" placeholder="{{ entry_name }}" id="input-name" class="form-control" />
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="form-group">
                      <label class="control-label" for="input-model">{{ entry_model }}</label>
                      <input type="text" name="filter_model" value="{{ filter_model }}" placeholder="{{ entry_model }}" id="input-model" class="form-control" />
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="form-group">
                      <label class="control-label" for="input-status">{{ entry_status }}</label>
                      <select name="filter_status" id="input-status" class="form-control">
                        <option value=""></option>
                          {% if filter_status == '1' %}
                        <option value="1" selected="selected">{{ text_enabled }}</option>
                          {% else %}
                        <option value="1">{{ text_enabled }}</option>
                          {% endif %}
                          {% if filter_status == '0' %}
                        <option value="0" selected="selected">{{ text_disabled }}</option>
                          {% else %}
                        <option value="0">{{ text_disabled }}</option>
                          {% endif %}
                      </select>
                    </div>
                </div>
            </div>
            <div class="form-group text-right">
              <button type="button" id="button-filter" class="btn btn-default"><i class="fa fa-filter"></i> {{ button_filter }}</button>
            </div>
            </div>
        </div>
      </div>
    </div>
    
    <div class="row">
      <div class="col-md-12 col-sm-12">
        <div class="panel panel-default">
          <div class="panel-heading">
            <h3 class="panel-title"><i class="fa fa-list"></i> {{ text_list_seo }}</h3>
          </div>
          <div class="panel-body">
            <form action="{{ delete }}" method="post" enctype="multipart/form-data" id="form-product">
              <div class="table-responsive">
                <table class="table table-bordered table-hover">
                  <thead>
                    <tr>
                      <td class="text-left">{% if sort == 'pd.name' %} <a href="{{ sort_name }}" class="{{ order|lower }}">{{ column_name }}</a> {% else %} <a href="{{ sort_name }}">{{ column_name }}</a> {% endif %}</td>
                      <td class="text-left">{{ column_description }}</td>
                      <td class="text-left">{{ column_focuskeyword }}</td>
                      <td class="text-left">{{ column_meta_title }}</td>
                      <td class="text-left">{{ column_meta_description }}</td>
                      <td class="text-left">{{ column_meta_keyword }}</td>
                      <td class="text-left">{{ column_seo_score }}</td>
                      <td class="text-left">{{ column_product_strength }}</td>
                      <td class="text-right">{{ column_action }}</td>
                    </tr>
                  </thead>
                  <tbody>
                  
                  {% if products %}
                  {% for product in products %}
                  <tr>
                     <td class="text-left">{{ product.name }}</td>
                     <td>
                          {% set full_desc = product.description %}
                          {% set short_desc = full_desc|split(' ')|slice(0, 30)|join(' ') %}
                          {% set description_len = product.description_length %}
                          <small style="color:{{ description_len < 150 ? 'red' : 'green' }};">
                            Length: {{ description_len }} chars (Recommended: 150+)
                          </small>
                          <br>
                          {% if product.focus_in_description %}
                            <small style="color:green;">Focus Keyword found in Description</small>
                          {% else %}
                            <small style="color:red;">Focus Keyword missing from Description</small>
                          {% endif %}
                        </td>
                    <td class="text-left">{{ product.focus_keyword }}</td>
                    <td>
                      {{ product.meta_title }}
                      <br>
                      {% set mt_len = product.meta_title|length %}
                      <small style="color:{{ mt_len < 50 or mt_len > 60 ? 'red' : 'green' }};">
                        Length: {{ mt_len }} chars (Recommended: 50–60)
                      </small>
                    </td>
                    <td>
                      {{ product.meta_description }}
                      <br>
                      {% set md_len = product.meta_description|length %}
                      <small style="color:{{ md_len < 120 or md_len > 160 ? 'red' : 'green' }};">
                        Length: {{ md_len }} chars (Recommended: 120–160)
                      </small>
                    </td>
                    <td>{{ product.meta_keyword }}</td>
                    
                    <td>
                      <button type="button" class="btn btn-warning btn-sm view-seo-issues"
                            data-toggle="modal"
                            data-target="#productSeoScorehModal"
                            data-issues='{{ product.product_seo_details|json_encode()|e('html_attr') }}'>
                            {{ product.product_seo_total_score }}/{{ product.product_seo_out_of }}
                        </button>
                    </td>
                    
                    <td>
                      <button type="button" class="btn btn-warning btn-sm view-product-strngth-issues"
                            data-toggle="modal"
                            data-target="#productStrengthModal"
                            data-issues='{{ product.product_strength_details|json_encode()|e('html_attr') }}'>
                            {{ product.product_strength_total_score }}/{{ product.product_strength_out_of }}
                        </button>
                      {% if product.product_strength_total_score >= 80 %}
                        <span style="color:green;">Good</span>
                      {% elseif product.product_strength_total_score >= 50 %}
                        <span style="color:orange;">Average</span>
                      {% else %}
                        <span style="color:red;">Poor</span>
                      {% endif %}
                    </td>
                    
                    <td class="text-right">
                        <div style="min-width: 120px;">
                          <div class="btn-group"> <a href="{{ constant('HTTP_CATALOG') }}?route=product/product&product_id={{ product.product_id }}" target="_blank" data-toggle="tooltip" title="{{ button_view }}" class="btn btn-primary"><i class="fa fa-eye"></i></a>
                            <button type="button" data-toggle="dropdown" class="btn btn-primary dropdown-toggle"><span class="caret"></span></button>
                            <ul class="dropdown-menu dropdown-menu-right">
                              <li><a href="{{ product.edit }}" target="_blank"><i class="fa fa-pencil"></i> {{ button_edit }}</a></li>
                              <li><a href="{{ product.google_search }}" target="_blank"><i class="fa fa-search"></i> Search Google</a></li>
                              <li><a href="{{ product.google_index_check }}" target="_blank"><i class="fa fa-globe"></i> Check Google Index</a></li>
                            </ul>
                          </div>
                        </div>
                    </td>
                    
                  </tr>
                  {% endfor %}
                  {% else %}
                  <tr>
                    <td class="text-center" colspan="8">{{ text_no_results }}</td>
                  </tr>
                  {% endif %}
                    </tbody>
                  
                </table>
              </div>
            </form>
            <div class="row">
              <div class="col-sm-6 text-left">{{ pagination }}</div>
              <div class="col-sm-6 text-right">{{ results }}</div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
  

<div class="modal fade" id="productSeoScorehModal" tabindex="-1" role="dialog" aria-labelledby="productSeoScorehModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="productSeoScorehModalLabel">SEO Score</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <table class="table table-bordered table-striped">
          <thead>
            <tr>
              <th>#</th>
              <th>Check</th>
              <th>Score</th>
              <th>Out Of</th>
              <th>Issue</th>
            </tr>
          </thead>
          <tbody id="seo-details-body">
            <!-- JS will populate this -->
          </tbody>
        </table>
      </div>
    </div>
  </div>
</div>


  <div class="modal fade" id="productStrengthModal" tabindex="-1" role="dialog" aria-labelledby="productStrengthModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="productStrengthModalLabel">Product Strength</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <table class="table table-bordered table-striped">
          <thead>
            <tr>
              <th>#</th>
              <th>Check</th>
              <th>Score</th>
              <th>Out Of</th>
              <th>Issue</th>
            </tr>
          </thead>
          <tbody id="product-strength-body">
            <!-- JS will populate this -->
          </tbody>
        </table>
      </div>
    </div>
  </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', function () {
  const buttons = document.querySelectorAll('.view-product-strngth-issues');

  buttons.forEach(button => {
    button.addEventListener('click', function () {
      const data = this.getAttribute('data-issues');
      let details = [];

      try {
        details = JSON.parse(data);
      } catch (e) {
        alert('Failed to parse SEO details.');
        return;
      }

      const tbody = document.getElementById('product-strength-body');
      tbody.innerHTML = '';

      if (details.length === 0) {
        tbody.innerHTML = '<tr><td colspan="5">No details available.</td></tr>';
        return;
      }

      details.forEach((item, index) => {
        const tr = document.createElement('tr');
        tr.innerHTML = `
          <td>${index + 1}</td>
          <td>${item.label}</td>
          <td>${item.earned}</td>
          <td>${item.max}</td>
          <td>${item.note ?? '-'}</td>
        `;
        tbody.appendChild(tr);
      });
    });
  });
});
</script>

<script>
document.addEventListener('DOMContentLoaded', function () {
  const buttons = document.querySelectorAll('.view-seo-issues');

  buttons.forEach(button => {
    button.addEventListener('click', function () {
      const data = this.getAttribute('data-issues');
      let details = [];

      try {
        details = JSON.parse(data);
      } catch (e) {
        alert('Failed to parse SEO details.');
        return;
      }

      const tbody = document.getElementById('seo-details-body');
      tbody.innerHTML = '';

      if (details.length === 0) {
        tbody.innerHTML = '<tr><td colspan="5">No details available.</td></tr>';
        return;
      }

      details.forEach((item, index) => {
        const tr = document.createElement('tr');
        tr.innerHTML = `
          <td>${index + 1}</td>
          <td>${item.title}</td>
          <td>${item.score}</td>
          <td>${item.out_of}</td>
          <td>${item.issue ?? '-'}</td>
        `;
        tbody.appendChild(tr);
      });
    });
  });
});
</script>
  
  <script type="text/javascript"><!--
$('#button-filter').on('click', function() {
	var url = '';

	var filter_name = $('input[name=\'filter_name\']').val();

	if (filter_name) {
		url += '&filter_name=' + encodeURIComponent(filter_name);
	}

	var filter_model = $('input[name=\'filter_model\']').val();

	if (filter_model) {
		url += '&filter_model=' + encodeURIComponent(filter_model);
	}

	var filter_price = $('input[name=\'filter_price\']').val();

	if (filter_price) {
		url += '&filter_price=' + encodeURIComponent(filter_price);
	}

	var filter_quantity = $('input[name=\'filter_quantity\']').val();

	if (filter_quantity) {
		url += '&filter_quantity=' + encodeURIComponent(filter_quantity);
	}

	var filter_status = $('select[name=\'filter_status\']').val();

	if (filter_status !== '') {
		url += '&filter_status=' + encodeURIComponent(filter_status);
	}

	location = 'index.php?route=catalog/product/seo&user_token={{ user_token }}' + url;
});
//--></script> 
  <script type="text/javascript"><!--
// IE and Edge fix!
$('button[form=\'form-product\']').on('click', function(e) {
	$('#form-product').attr('action', $(this).attr('formaction'));
});
  
$('input[name=\'filter_name\']').autocomplete({
	'source': function(request, response) {
		$.ajax({
			url: 'index.php?route=catalog/product/autocomplete&user_token={{ user_token }}&filter_name=' +  encodeURIComponent(request),
			dataType: 'json',
			success: function(json) {
				response($.map(json, function(item) {
					return {
						label: item['name'],
						value: item['product_id']
					}
				}));
			}
		});
	},
	'select': function(item) {
		$('input[name=\'filter_name\']').val(item['label']);
	}
});

$('input[name=\'filter_model\']').autocomplete({
	'source': function(request, response) {
		$.ajax({
			url: 'index.php?route=catalog/product/autocomplete&user_token={{ user_token }}&filter_model=' +  encodeURIComponent(request),
			dataType: 'json',
			success: function(json) {
				response($.map(json, function(item) {
					return {
						label: item['model'],
						value: item['product_id']
					}
				}));
			}
		});
	},
	'select': function(item) {
		$('input[name=\'filter_model\']').val(item['label']);
	}
});
//--></script>
</div>
{{ footer }} 