<?php

namespace Forum\Src;

use Dever;
use Dever\Template\View;

class Model
{
	public function __construct()
	{
		$this->page = Dever::input('model_page');
		$this->position = Dever::input('model_position');
		$this->cate = Dever::input('cate');
		$this->post = Dever::input('id');

		Dever::$global['base']['_data'] = array();
	}
	
	public function top()
	{
		$this->position = 3;
		
		return $this->get();
	}
	
	public function right()
	{
		$this->position = 1;
		
		return $this->get();
	}
	
	public function bottom()
	{
		$this->position = 2;
		
		return $this->get();
	}

	public function getOne($data)
	{
		$html = '';
		if ($data['page'] != 1 && $data['cate_id'] > 0 && $data['cate_id'] != $this->cate) {
			return $html;
		}
		
		if ($data['type'] == 1 && $this->cate > 0 && !Dever::load('forum/cate.auth', $this->cate)) {
			return $html;
		}

		if ($data['title'] == 1) {
			$data['style'] = '';
			if ($data['align'] == 2) {
				$data['style'] = 'text-align:center';
			} elseif ($data['align'] == 3) {
				$data['style'] = 'text-align:right';
			}
		} 

		if ($data['type'] == 2 || $data['type'] == 3) {
			//$this->posts();
		}

		Dever::$global['base']['_data'][$data['id']] = $data;

		Dever::setInput('model', $data['id']);

		if($data['type'] >= 31)
		{
			$data['type'] = 31;
		}
		$html .= Dever::view('_model/' . $data['type']);

		return $html;
	}

	public function get($state = false)
	{
		$param['option_page'] = $this->page;
		
		if ($state == false && $this->cate > 0) {
			$param['option_cate_id'] = $this->cate;
			
			$state = Dever::load('forum/model-one', $param);
			
			if (!$state) {
				return $this->get(true);
			}
		}
		if ($this->position > 0) {
			$param['option_position'] = $this->position;
		}

		$data = Dever::load('forum/model-state', $param);

		return $data;
	}

	public function data($id)
	{
		if (is_array($id)) {
			return array();
		}
		return isset(Dever::$global['base']['_data'][$id]) ? Dever::$global['base']['_data'][$id] : array();
	}

	public function info($id = false)
	{
		$data = $this->data($id);

		$result = array();
		$result['title'] = isset($data['name']) ? $data['name'] : "";
		$result['style'] = isset($data['style']) ? $data['style'] : "display:none;";
		$result['head'] = isset($data['title']) && $data['title'] == 1 ? '' :"display:none;";

		$result['more'] = '';
		if (isset($data['rule']) && $data['rule']) {
			$cate = $this->cate > 0 ? $this->cate : $data['cate_id'];
			if ($cate > 0) {
				$link = Dever::url('forum/list?cate=' . $cate . '&type=' . $data['rule'], 'main');
			} else {
				$link = Dever::url('forum/list?type=' . $data['rule'], 'main');
			}
		
			$result['more'] = '<a href="'.$link.'">查看更多'.$data['name'].'...</a>';
		}

		if (isset($data['type'])) {
			if ($data['type'] == 31) {
				$result['class'] = 'dever-panel-body';
				$result['info'] = Dever::markdown($data['info']);
			} elseif ($data['type'] == 32) {
				$result['class'] = 'dever-panel-body';
				$info = explode('||', $data['info']);

				$index = array_rand($info);

				$result['info'] = Dever::markdown($info[$index]);
			} elseif ($data['type'] == 33) {
				$result['class'] = 'alert alert-warning';
				$result['info'] = Dever::markdown($data['info']);
			}
		}
		

		return $result;
	}

	public function total()
	{
		$result = array();
		$result['member'] = Dever::load('passport/user-total');
		$result['news'] = Dever::load('forum/posts-total');

		return $result;
	}

	# 链接
	public function link()
	{
		$link = array();
		if ($this->cate > 0) {
			$link['post'] = 'forum/post?cate=' . $this->cate;
		} else {
			$link['post'] = 'forum/post';
		}

		$link['post'] = Dever::url($link['post'], 'main');

		return $link;
	}
	
	public function posts($param)
	{
		if (isset($param['model'])) {
			$id = $param['model'];
		} elseif($param) {
			$id = $param;
		}

		$data = $this->data($id);

		if (isset($data['rule']) && $data['rule'] > 0) {
			Dever::setInput('type', $data['rule']);
		}
		if (isset($data['cate_id']) && $data['cate_id'] > 0) {
			Dever::setInput('cate', $data['cate_id']);
		} elseif (!Dever::input('cate')) {
			Dever::setInput('cate', -1);
		}

		if (empty($data['id'])) {
			$data['id'] = -1;
		}

		$data = Dever::load('forum/posts.get', $data['id']);

		return $data;
	}
}