<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Blog extends MY_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */

	public $blogs;
	public $tags;

	public function __construct()
	{
		parent::__construct();
		$this->blogs = [
			[
				'title' => '聖誕新春大激賞<br />$398包日本岩盤浴連皇牌經絡按摩／面部護理',
				'date' => '2020-12-04',
				'image' => 'blog-image-01.png',
				'tags' => [
					7,
				]
			],
			[
				'title' => '缺水恩物　補濕神器',
				'date' => '2020-12-04',
				'image' => 'blog-image-02.png',
				'tags' => [
					2, 4,
				]
			],
			[
				'title' => '皇版Derma Veil童顏再生針守住妳逆齡的秘密',
				'date' => '2020-12-04',
				'image' => 'blog-image-03.png',
				'tags' => [
					6,
				]
			],
			[
				'title' => '展開逆齢之旅　皇牌PDO蛋白埋線買一送一優惠',
				'date' => '2020-12-04',
				'image' => 'blog-image-04.png',
				'tags' => [
					10,
				]
			],
			[
				'title' => '想學湯怡一樣擁有年輕緊緻肌！',
				'date' => '2020-12-04',
				'image' => 'blog-image-05.png',
				'tags' => [
					3,
				]
			],
			[
				'title' => 'Belkyra永久溶脂　攞脫頑固鬼祟多餘脂肪',
				'date' => '2020-12-04',
				'image' => 'blog-image-06.png',
				'tags' => [
					8,
				]
			],
			[
				'title' => '808冰點激光全身脫毛嫩膚療程優惠',
				'date' => '2020-12-04',
				'image' => 'blog-image-07.png',
				'tags' => [
					9, 10,
				]
			],
			[
				'title' => '皇版Derma Veil童顏再生針守住妳逆齡的秘密',
				'date' => '2020-12-04',
				'image' => 'blog-image-08.png',
				'tags' => [
					3,
				]
			],
			[
				'title' => '徹底解決脂肪雙下巴　嶄新引入Belkyra永久溶脂',
				'date' => '2020-12-04',
				'image' => 'blog-image-09.png',
				'tags' => [
					8, 6,
				]
			],
		];
		$this->tags = [
			[
				'word' => '全部',
				'color' => '#618b68',
			],
			[
				'word' => '#Keystoyoung',
				'color' => '#5a94a3',
			],
			[
				'word' => '#美容影片',
				'color' => '#4de9f9',
			],
			[
				'word' => '#最新優惠',
				'color' => '#c248c5',
			],
			[
				'word' => '#療程體驗',
				'color' => '#6b2e3f',
			],
			[
				'word' => '#尊貴系列',
				'color' => '#d4aa06',
			],
			[
				'word' => '#美容護理',
				'color' => '#c11853',
			],
			[
				'word' => '#養生美肌',
				'color' => '#c684ae',
			],
			[
				'word' => '#纖體瘦身',
				'color' => '#d150a3',
			],
			[
				'word' => '#美容儀器',
				'color' => '#32ca3f',
			],
			[
				'word' => '#醫學美容',
				'color' => '#c3b34e',
			],
		];
	}

	public function index()
	{
		$this->load->model('at-admin/Hashtag_model', 'hashtag');
		$this->load->model('at-admin/Blog_model', 'blog');
		$tags = $this->hashtag->get_hot_hashtag(20);
		$blogs = $this->blog->get();
//		$tags_per_row = 6;
//		$num_rows = floor(count($tags) / $tags_per_row);
//		if (count($tags) % $tags_per_row !== 0) {
//			$num_rows++;
//		}
		$this->load->view('index', array_merge($this->viewParams, [
			'page' => 'blog',
			'tags' => $tags,
//			'tags_per_row' => $tags_per_row,
//			'num_rows' => $num_rows,
			'blogs' => $blogs,
			'hashtagColors' => $this->hashtagColors,
			'css' => [
			],
			'js' => [],
		]));
	}

	public function details()
	{
		if (! $this->uri->segment(3)) {
			echo "<h3>Error</h3>";
			exit;
		}
		$this->load->model('at-admin/Blog_model', 'blog');
		$blog = $this->blog->get_by_title(urldecode($this->uri->segment(3)));
		if (is_null($blog)) {
			echo "<h3>Error</h3>";
			exit;
		}
		$this->load->model('at-admin/Hashtag_model', 'hashtag');
		$this->blog->viewed($blog['id']);
		$this->load->view('index', array_merge($this->viewParams, [
			'page' => 'blog_view',
			'blog' => $blog,
			'hashtagColors' => $this->hashtagColors,
//			'tags' => $this->tags,
			'hot_tags' => $this->hashtag->get_hot_hashtag(),
			'hot_blogs' => $this->blog->get_hot_blogs(),
			'css' => [
			],
			'js' => [],
		]));
	}
}
