<?php
namespace Api\Controller;

use Think\Controller;

header("Content-Type:text/html;CharSet=UTF-8");
class ZixunController extends BaseController
{
	/**
	 * 获取资讯首页展示图片
	 */
	public function index()
	{
		$img = M('zixun_show')->where(array('admin_id' => $this->data['business_id']))->getField('img');

		if (empty($img)) apiReturn(['status' => -1, 'msg' => '没有数据']);

		$img = $this->url.'/Uploads/'.$img;
		apiReturn(['status' => 1, 'msg' => '没有数据', 'data' => $img]);
	}

	/**
	 * 获取资讯列表
	 */
	public function get_list()
	{	
		if (empty($this->data['page'])) apiReturn(['status' => -1, 'msg' => 'page参数必须']);

		$list = M('zixun')->where(array('admin_id' => $this->data['business_id']))->field('id, title, img, browse_num, create_time')->page($this->data['page'])->order('sort ASC, create_time DESC')->select();

		if (empty($list)) apiReturn(['status' => -1, 'msg' => '没有数据']);

		foreach ($list as $k => &$vo) {
			$vo['create_time'] = date('Y-m-d H:i:s', $vo['create_time']);
			$vo['img'] = $this->url.'/Uploads/'.$vo['img'];
		}

		apiReturn(['status' => 1, 'msg' => '已获取数据', 'data' => $list]);
	}

	/**
	 * 资讯详情
	 */
	public function detail()
	{
		if (empty($this->data['id'])) apiReturn(['status' => -1, 'msg' => 'id不能为空']);
		
		$this->insert_browse();

		$detail = M('zixun')->where(array('id' => $this->data['id']))->field('id, title, content, browse_num, create_time')->find();

		if (empty($detail)) apiReturn(['status' => -1, 'msg' => '没有数据']);
		$detail['content'] = htmlspecialchars_decode($detail['content']);
		$detail['create_time'] = date('Y-m-d H:i:s', $detail['create_time']);
		apiReturn(['status' => 1, 'msg' => '已获取数据', 'data' => $detail]);
	}

	/**
	 * 资讯推荐
	 */
	public function hot_index()
	{
		$list = M('zixun')->where(array('admin_id' => $this->data['business_id'], 'is_hot' => 1))->field('id, title, img, browse_num, create_time')->page($this->data['page'])->order('sort ASC, create_time DESC')->limit(3)->select();

		if (empty($list)) apiReturn(['status' => -1, 'msg' => '没有数据']);

		foreach ($list as $k => &$vo) {
			$vo['create_time'] = date('Y-m-d H:i:s', $vo['create_time']);
			$vo['img'] = $this->url.'/Uploads/'.$vo['img'];
		}

		apiReturn(['status' => 1, 'msg' => '已获取数据', 'data' => $list]);
	}

	/**
	 * 资讯推荐
	 */
	public function hot_index_more()
	{
		if (empty($this->data['page'])) apiReturn(['status' => -1, 'msg' => 'page参数必须']);

		$list = M('zixun')->where(array('admin_id' => $this->data['business_id'], 'is_hot' => 1))->field('id, title, img, browse_num, create_time')->page($this->data['page'])->order('sort ASC, create_time DESC')->select();

		if (empty($list)) apiReturn(['status' => -1, 'msg' => '没有数据']);

		foreach ($list as $k => &$vo) {
			$vo['create_time'] = date('Y-m-d H:i:s', $vo['create_time']);
			$vo['img'] = $this->url.'/Uploads/'.$vo['img'];
		}

		apiReturn(['status' => 1, 'msg' => '已获取数据', 'data' => $list]);
	}

	/**
	 * 评论列表
	 */
	public function comment_list()
	{
		if (empty($this->data['page'])) apiReturn(['status' => -1, 'msg' => 'page参数必须']);

		// if (empty($this->data['openid'])) apiReturn(['status' => -1, 'msg' => 'openid参数必须']);

		if (empty($this->data['zixun_id'])) apiReturn(['status' => -1, 'msg' => 'zixun_id参数必须']);

		$list = M('zixun_comment')
				->join(array('INNER JOIN am_users ON am_zixun_comment.openid = am_users.openid'))
				->where(array('zixun_id' => $this->data['zixun_id'], 'am_users.admin_id' => $this->data['business_id']))
				->field('content, am_zixun_comment.create_time, nickname')
				->page($this->data['page'])
				->select();

		if (empty($list)) apiReturn(['status' => -1, 'msg' => '没有数据']);

		foreach ($list as $key => &$value) {
			$value['create_time'] = date('Y-m-d H:i:s', $value['create_time']);
		}

		apiReturn(['status' => 1, 'msg' => '已获取数据', 'data' => $list]);
	}

	/**
	 * 评论
	 */
	public function comment()
	{
		if (empty($this->data['openid'])) apiReturn(['status' => -1, 'msg' => 'openid参数必须']);

		if (empty($this->data['zixun_id'])) apiReturn(['status' => -1, 'msg' => 'zixunid参数必须']);

		if (empty($this->data['content'])) apiReturn(['status' => -1, 'msg' => 'content参数必须']);

		$result = M('zixun_comment')->add(
				array(
					'zixun_id' => $this->data['zixun_id'], 
					'openid' => $this->data['openid'], 
					'content' => $this->data['content'], 
					'create_time' => time()
				));

		if ($result) {
			apiReturn(['status' => 1, 'msg' => '评论成功']);
		} else {
			apiReturn(['status' => -1, 'msg' => '评论失败']);
		} 
	}

	/**
	 * 添加浏览量
	 */
	private function insert_browse()
	{	
		M('zixun')->where(array('id' => $this->data['id']))->setInc('browse_num');
	}
}