<?php

class ModelLocalisationOrderStatus extends Model
{
  public function addOrderStatus($data)
  {
    foreach ($data['order_status'] as $language_id => $value) {
      if (isset($order_status_id)) {
        $sql = "INSERT INTO " . DB_PREFIX . "order_status SET order_status_id = '" . (int)$order_status_id . "', language_id = '" . (int)$language_id . "', name = '" . $this->db->escape($value['name']) . "', order_type_status = '" . $this->db->escape($value['type']) . "', client_notify = '".$this->db->escape($value['client_notify'])."' ";
        $sql .= ",order_futuraserver_status = '" .  ((isset($value['order_futuraserver_status'])&&$value['order_futuraserver_status']!=null ) ? $this->db->escape($value['order_futuraserver_status']) : 'null')  . "', tracking = '".$this->db->escape($value['tracking'])."'" ;
        $this->db->query($sql);
      } else {
        $sql = "INSERT INTO " . DB_PREFIX . "order_status SET language_id = '" . (int)$language_id . "', name = '" . $this->db->escape($value['name']) . "', order_type_status = '" . $this->db->escape($value['type']) . "', client_notify = '".$this->db->escape($value['client_notify'])."' ";
        $sql .= ",order_futuraserver_status = '" .  ((isset($value['order_futuraserver_status'])&&$value['order_futuraserver_status']!=null ) ? $this->db->escape($value['order_futuraserver_status']) : 'null')  . "', tracking = '".$this->db->escape($value['tracking'])."'" ;
        $this->db->query($sql);

        $order_status_id = $this->db->getLastId();
      }
      /*if( utf8_strlen(trim($value['order_status_message'])) > 0 ){
        $dataMessage['status'][$order_status_id] = $value['order_status_message'];
        $this->addOrderStatusMessage($dataMessage);
      }*/
    }

    $this->cache->delete('order_status');
  }

  public function editOrderStatus($order_status_id, $data)
  {
    $this->db->query("DELETE FROM " . DB_PREFIX . "order_status WHERE order_status_id = '" . (int)$order_status_id . "'");
    foreach ($data['order_status'] as $language_id => $value) {
      $sql = "INSERT INTO " . DB_PREFIX . "order_status SET order_status_id = '" . (int)$order_status_id . "', language_id = '" . (int)$language_id . "', name = '" . $this->db->escape($value['name']) . "', order_type_status = '" . $this->db->escape($value['type']) . "', client_notify = '".$this->db->escape($value['client_notify'])."' ";
      $sql .= ",order_futuraserver_status = '" .  ((isset($value['order_futuraserver_status'])&&$value['order_futuraserver_status']!=null ) ? $this->db->escape($value['order_futuraserver_status']) : 'null')  . "', tracking='".$this->db->escape($value['tracking'])."'" ;
      $this->db->query($sql);

      if(isset($value['order_status_message'])&&(utf8_strlen(trim($value['order_status_message'])) > 0) ){
        $dataMessage['status'][$order_status_id] = $value['order_status_message'];
        $this->addOrderStatusMessage($dataMessage);
      }
    }

    $this->cache->delete('order_status');
  }

  public function deleteOrderStatus($order_status_id)
  {
    $this->db->query("DELETE FROM " . DB_PREFIX . "order_status WHERE order_status_id = '" . (int)$order_status_id . "'");
    $this->cache->delete('order_status');
  }

  public function getOrderStatus($order_status_id)
  {
    $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_status WHERE order_status_id = '" . (int)$order_status_id . "' AND language_id = '" . (int)$this->config->get('config_language_id') . "'");
    return $query->row;
  }

  public function getOrderStatuses($data = array())
  {
    if ($data) {
      $sql = "SELECT * FROM " . DB_PREFIX . "order_status WHERE language_id = '" . (int)$this->config->get('config_language_id') . "'";

      $sql .= " ORDER BY name";

      if (isset($data['order']) && ($data['order'] == 'DESC')) {
        $sql .= " DESC";
      } else {
        $sql .= " ASC";
      }

      if (isset($data['start']) || isset($data['limit'])) {
        if ($data['start'] < 0) {
          $data['start'] = 0;
        }

        if ($data['limit'] < 1) {
          $data['limit'] = 20;
        }

        $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
      }

      $query = $this->db->query($sql);

      return $query->rows;
    } else {
      $defaultLanguage = $this->config->get('config_language_id') ?? $this->config->get('select_language_default');

      $sql = "SELECT order_status_id, name FROM " . DB_PREFIX . "order_status WHERE language_id=" . $defaultLanguage;
      $query = $this->db->query($sql);
      $order_status_data = $query->rows;

      return $order_status_data;
    }
  }

  public function getOrderStatusDescriptions($order_status_id)
  {
    $this->load->type('tipofuturaserver');

    $order_status_data = array();
    $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_status WHERE order_status_id = '" . (int)$order_status_id . "'");

    foreach ($query->rows as $result) {
      $order_status_data[$result['language_id']] = array(
        'name' => $result['name'],
        'type' => $result['order_type_status'],
        'client_notify' => $result['client_notify'],
        'order_futuraserver_status' => $result['order_futuraserver_status'],
        'tracking' => $result['tracking']
      );

      $message = $this->getOrderStatusMessage($order_status_id);
      if (count($message)>0){
        $order_status_data[$result['language_id']]['order_status_message'] = $message[0]['message'];
      }

    }

    return $order_status_data;
  }

  public function getTotalOrderStatuses()
  {
    $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "order_status WHERE language_id = '" . (int)$this->config->get('config_language_id') . "'");

    return $query->row['total'];
  }

  // Mensagens de cada status
  public function addOrderStatusMessage($data = '')
  {
    if (isset($data['status']) && $data['status']) {
      foreach ($data['status'] as $order_status_id => $message) {
        $this->deleteOrderStatusMessage($order_status_id);
        $this->db->query("INSERT INTO " . DB_PREFIX . "order_status_message SET order_status_id = " . (int)$order_status_id . ", message = '" . $this->db->escape($message) . "' ");
      }
    }
  }

  public function deleteOrderStatusMessage($order_status_id)
  {
    if (isset($order_status_id) && $order_status_id) {
      $this->db->query("DELETE FROM " . DB_PREFIX . "order_status_message WHERE order_status_id = '" . (int)$order_status_id . "'");
    }
  }

  public function getOrderStatusMessage($order_status_id = '')
  {
    $order_status_data = array();
    if ($order_status_id) {
      $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_status_message WHERE order_status_id = " . (int)$order_status_id);
    } else {
      $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_status_message");
    }

    return $query->rows;
  }

  public function getOrderStatusTracking()
  {

    $query = $this->db->query("SELECT order_status_id FROM " . DB_PREFIX . "order_status WHERE tracking = ".Type_Status::Tp_Habilitado);

    return (count($query->rows) > 0) ? $query->rows : false;
  }

}