<?php
class ModelDesignBanner extends Model
{
	function RemoveSpecialChar($str) {
      $res = str_replace( array( '\'', '"',
      ',' , ';', '<', '>' ), ' ', $str);
      return $res;
    }

	public function addBanner($data)
	{
		$this->db->query("INSERT INTO " . DB_PREFIX . "banner SET `UUID` = '". $data['UUID'] ."' ,name = '" . $this->db->escape($data['name']) . "', status = '" . (int)$data['status'] . "'");

		$banner_id = $this->db->getLastId();

		if (isset($data['banner_image'])) {
			foreach ($data['banner_image'] as $banner_image) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "banner_image SET banner_id = '" . (int)$banner_id . "', link = '" .  $this->db->escape(htmlspecialchars_decode($banner_image['link']) ?? '') . "', image = '" .  $this->db->escape($banner_image['image']) . "'");

				$banner_image_id = $this->db->getLastId();

				foreach ($banner_image['banner_image_description'] as $language_id => $banner_image_description) {
					$this->db->query("INSERT INTO " . DB_PREFIX . "banner_image_description SET banner_image_id = '" . (int)$banner_image_id . "', language_id = '" . (int)$language_id . "', banner_id = '" . (int)$banner_id . "', title = '" .  $this->db->escape($banner_image_description['title']) . "'");
				}
			}
		}
		return $banner_id;
	}



	public function editBanner($banner_id, $data)
	{

	    if(strlen($banner_id) > 10){
	        $banner = $this->getBannerUUID($banner_id);
            $banner_id = $banner['banner_id'];
        }

		$this->db->query("UPDATE " . DB_PREFIX . "banner SET UUID = '". $data['UUID'] ."', name = '" . $this->db->escape($data['name']) . "', status = '" . (int)$data['status'] . "' WHERE banner_id = '" . (int)$banner_id . "'");

		$this->db->query("DELETE FROM " . DB_PREFIX . "banner_image WHERE banner_id = '" . (int)$banner_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "banner_image_description WHERE banner_id = '" . (int)$banner_id . "'");

		if (isset($data['banner_image']) AND $data['banner_image']) {
			foreach ($data['banner_image'] as $banner_image) {
			  if($banner_image['image'] === 'data/preview-product.png'){
          $banner_image['image'] = '';
          $banner_image['thumb'] = '';
        }
				$this->db->query("INSERT INTO " . DB_PREFIX . "banner_image SET banner_id = '" . (int)$banner_id . "', link = '" .  $this->db->escape(htmlspecialchars_decode($banner_image['link'])) . "', image = '" .  $this->db->escape($banner_image['image']) . "'");

				$banner_image_id = $this->db->getLastId();

				foreach ($banner_image['banner_image_description'] as $language_id => $banner_image_description) {
					$this->db->query("INSERT INTO " . DB_PREFIX . "banner_image_description SET banner_image_id = '" . (int)$banner_image_id . "', language_id = '" . (int)$language_id . "', banner_id = '" . (int)$banner_id . "', title = '" .  $this->db->escape($banner_image_description['title']) . "'");
				}
			}
		}
	}

	public function deleteBanner($banner_id)
	{
		$this->db->query("DELETE FROM " . DB_PREFIX . "banner WHERE banner_id = '" . (int)$banner_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "banner_image WHERE banner_id = '" . (int)$banner_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "banner_image_description WHERE banner_id = '" . (int)$banner_id . "'");
	}

	public function deleteBannerUUID($banner_id)
    {
        $delete = [];
        $UUID = $this->getBannerUUID($banner_id);

        if($UUID){
          $delete = $UUID;
        }else{
          $delete['banner_id'] = $banner_id;
        }

        if($banner_id){
            $this->db->query("DELETE FROM " . DB_PREFIX . "banner WHERE banner_id = '" . (int)$delete['banner_id'] . "'");
            $this->db->query("DELETE FROM " . DB_PREFIX . "banner_image WHERE banner_id = '" . (int)$delete['banner_id'] . "'");
            $this->db->query("DELETE FROM " . DB_PREFIX . "banner_image_description WHERE banner_id = '" . (int)$delete['banner_id'] . "'");
        }
    }

	public function getBanner($banner_id)
	{
		$query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "banner WHERE banner_id = '" . (int)$banner_id . "'");

		return $query->row;
	}

    public function getBannerUUID($banner_UUID)
    {
        $query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "banner WHERE `UUID` = '" . $banner_UUID . "'");

        return $query->row;
    }

	public function getBanners($data = array())
	{
		$InfoData = $this->RemoveSpecialChar($data);
		$sql = "SELECT * FROM " . DB_PREFIX . "banner";

		if (isset($InfoData['status'])) {
			$sql .= " WHERE status = '" . $InfoData['status'] . "' ";
		}

		$sort_data = array(
			'name',
			'status'
		);

		if (isset($data['sort']) && in_array($InfoData['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $InfoData['sort'];
		} else {
			$sql .= " ORDER BY name";
		}

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

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

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

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

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

		return $query->rows;
	}

	public function getBannerImages($banner_id)
	{
    $banner_image_data = [];
		$banner_image_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "banner_image WHERE banner_id = '" . (int)$banner_id . "'");

		foreach ($banner_image_query->rows as $banner_image) {
			$banner_image_description_data = array();

			$banner_image_description_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "banner_image_description WHERE banner_image_id = '" . (int)$banner_image['banner_image_id'] . "' AND banner_id = '" . (int)$banner_id . "'");

			foreach ($banner_image_description_query->rows as $banner_image_description) {
				$banner_image_description_data[$banner_image_description['language_id']] = array('title' => $banner_image_description['title']);
			}

			$banner_image_data[] = array(
				'banner_image_description' => $banner_image_description_data,
				'link'                     => $banner_image['link'],
				'image'                    => $banner_image['image']
			);

		}
    return $banner_image_data;
	}

	public function getTotalBanners()
	{
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "banner");

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


	public function getIdsBanners()
	{
		$query = $this->db->query("SELECT banner_id FROM " . DB_PREFIX . "banner");
		return array_map(function ($banner) {
			return $banner['banner_id'];
		}, $query->rows);
	}

    public function getUUIDsBanners()
    {
        $query = $this->db->query("SELECT banner_id, UUID FROM " . DB_PREFIX . "banner");
        return array_map(function ($banner) {
            return $banner['UUID'];
        }, $query->rows);
    }
}
