<?php
class ControllerModulePagseguroAbandoned extends Controller {
  public function index() {
    $language = $this->load->language('module/pagseguro_abandoned');
    $this->data = array_merge($this->data, $language);

    $this->document->setTitle($this->language->get('heading_title'));
    $this->document->addScript('admin/public/js/module/pagseguro_abandoned.js');

    $token = $this->session->data['token'];
    $this->data['token'] = $token;

    $this->data['breadcrumbs'] = [
      [
        'text' => $this->language->get('text_home'),
        'href' => $this->url->link('common/home', 'token=' . $token, 'SSL'),
      ],
      [
        'text' => $this->language->get('text_module'),
        'href' => $this->url->linkAnchor('extension/module', 'token=' . $token, 'tab-info=' . $this->request->get['type'], 'SSL'),
      ],
      [
        'text' => $this->language->get('heading_title'),
        'href' => $this->url->link('module/pagseguro_abandoned', 'token=' . $token . '&type=' . $this->request->get['type'], 'SSL'),
      ]
    ];

    $url_back = $this->url->linkAnchor(
      'extension/module',
      'token=' . $token,
      'tab-info=' . $this->request->get['type'],
      'SSL'
    );

    $url_action = $this->url->link(
      'module/pagseguro_abandoned/execute',
      'token=' . $token . '&type=' . $this->request->get['type'],
      'SSL'
    );

    $url_pagseguro_abandoned = $this->url->link('payment/pagseguro_abandoned','','SSL');

    $this->data['data'] = [
      'action' => str_replace('&amp;', '&', $url_action),
      'back' => str_replace('&amp;', '&', $url_back),
      'pagseguroAbandoned' => str_replace('/admin/', '/', $url_pagseguro_abandoned)
    ];

    $this->children = [
      'common/header',
      'common/footer',
    ];

    $this->response->setOutput($this->render('module/pagseguro_abandoned'));
  }

  public function execute() {
    $language = $this->load->language('module/pagseguro_abandoned');
    $this->data = array_merge($this->data, $language);

    $response = [];

    // if (!$this->user->hasPermission('modify', 'module/pagseguro')) {
    //   $response['warning'] = $this->language->get('error_permission');
    //   return $this->response->setOutputJson($response);
    // }

    if (!$this->validate()) {
      $response['success'] = $this->language->get('text_success');
    } else {
      $response['errors'] = $this->validate();
    }

    return $this->response->setOutputJson($response);
  }

  private function validate() {
    extract($this->request->post);

    return $this->validate->execute([
      'date_initial' => [
        'value' => $date_initial,
        'validates' => ['isEmpty'],
      ],
      'date_ended' => [
        'value' => $date_ended,
        'validates' => ['isEmpty'],
      ],
    ]);
  }
}
