<?php
namespace Smi\Controller;

use Think\Controller;

header("Content-Type:text/html;CharSet=UTF-8");
class ZixunController extends BaseController
{	
	protected $root;
	protected $business_id;

	public function __construct()
	{
		parent::__construct();

		$this->business_id = session('admin_id');
		$this->root = dirname(dirname(dirname(__DIR__)));
	}

	/**
	 * 资讯列表
	 */
	public function index($search = '', $hot = '')
	{	
		$business_id = session('admin_id');
			
		$where = 'admin_id = '.$business_id;

		if (!empty($search)) {
			$where .= ' AND title LIKE "%'.$search.'%"';
		}	

		if (!empty($hot)) {
			$where .= ' AND is_hot = '.$hot;
		}

		$count = M('zixun')->where($where)->count();
		
		$Page =  new \Think\Page($count, 20);
		$show = $Page->show();
		$limit = $Page->firstRow.','.$Page->listRows;

		$list = M('zixun')->where($where)->limit($limit)->order('sort ASC,create_time DESC')->select();

		if (!empty($list)) {
			foreach ($list as $k => &$v) {
				$v['img'] = __ROOT__.'/Uploads/'.$v['img'];
			}
		}

		$this->assign('page', $show);
		$this->assign('list', $list);
		$this->assign('search', $search);
		$this->display();
	}

	/**
	 * 添加资讯
	 */
	public function add()
	{	
		$business_id = session('admin_id');

		$data = I('post.');
		if (!empty($data)) {
			$add = array(
					'admin_id' => session('admin_id'),
					'title' => $data['title'],
					'content' => $data['content'],
					'sort' => $data['sort'],
					'is_hot' => $data['is_hot'],
					'status' => empty($data['status']) ? 1 : $data['status'],
					'create_time' => time()
				);

			if (empty($_FILES['img']['name'])) {
				$this->error('Please Upload Image');die;
			} else {
				$upload = ToolsController::upload('zixun/');
				if ($upload['status'] == -1) {
					$this->error('Image upload failed');die;
				} else {
					$add['img'] = $upload['file_path'];
				}
			}

			$result = M('zixun')->add($add);
			if ($result) {
				$this->success('Successfully Saved');die;
			} else {
				$this->error('Add failure');die;
			}
		}

		$this->assign('op', 'Add');
		$this->display();
	}

	/**
	 * 编辑资讯
	 */
	public function edit($id = '')
	{	
		$business_id = session('admin_id');

		$detail = M('zixun')->where(array('id' => $id))->find();
		$before_img = $detail['img'];

		$data = I('post.');
		if (!empty($data)) {
			
			$save = array(
					'title' => $data['title'],
					'content' => $data['content'],
					'sort' => $data['sort'],
					'is_hot' => $data['is_hot'],
					'status' => $data['status'],
					'update_time' => time(),
				);
			
			if (!empty($_FILES['img']['name'])) {
				$upload = ToolsController::upload('zixun/');
				if ($upload['status'] == -1) {
					$this->error('Thumbnail upload failed');die;
				} else {
					$save['img'] = $upload['file_path'];
				}
			}

			$result = M('zixun')->where(array('id' => $data['id']))->save($save);

			if ($result) {
				if (!empty($_FILES['img']['name'])) {
					unlink($this->root.'/Uploads/'.$before_img);
				}
				$this->success('Successfully Edited');die;
			} else {
				$this->error('Edit failed');die;
			}
		}

		$detail = M('zixun')->where(array('id' => $id))->find();
		$detail['img'] = __ROOT__.'/Uploads/'.$detail['img'];
		$this->assign('info', $detail);
		$this->assign('op', 'Edit');
		$this->display('add');
	}

	/**
	 * 删除资讯
	 */
	public function delete($id = '')
	{	
		$info = M('zixun')->where(array('id' => $id))->find();

		if ($info['admin_id'] != $this->business_id) {
			$this->error('Illegal operation');die;
		}

		$result = M('zixun')->where(array('id' => $id))->delete();

		if ($result) {
			unlink($this->root.'/Uploads/'.$info['img']);// 删除图片
			M('zixun_comment')->where(array('zixun_id' => $id))->delete();
			$this->success('Successfully Deleted');die;
		} else {
			$this->error('Delete failed');die;
		}
	}

	/**
	 * 资讯页面展示图片
	 */
	public function show()
	{	
		$data = I('post.');

		if (!empty($data)) {
			$result = false;
			if (empty($data['id'])) {// 保存
				if (empty($_FILES['img']['name'])) {
					$this->error('Please Upload Picture');die;
				}
				$add = array(
						'admin_id' => $this->business_id,
						'create_time' => time()
					);
				$upload = ToolsController::upload('zixun_show/');
				if ($upload['status'] == -1) {
					$this->error('Image upload failed');die;
				} else {
					$add['img'] = $upload['file_path'];
					$result = M('zixun_show')->add($add);

					if (!$result) {
						unlink($this->root.'/Uploads/'.$add['img']);
					}
				}
			} else { // 更新
				$info = M('zixun_show')->where(array('id' => $data['id']))->find();

				if (empty($_FILES['img']['name'])) {
					$this->error("No Modification Change");die;
				} else {
					$save = array(
							'update_time' => time(),
						);
					$upload = ToolsController::upload('zixun_show/');
					if ($upload['status'] == -1) {
						$this->error('Image upload failed');die;
					} else {
						$save['img'] = $upload['file_path'];
						$result = M('zixun_show')->where(array('id' => $data['id']))->save($save);
						
						if ($result) {
							unlink($this->root.'/Uploads/'.$info['img']);
						} else {
							unlink($this->root.'/Uploads/'.$save['img']);
						}
					}
				}
			}

			if ($result) {
				$this->success('Successfully Saved');die;
			} else {
				$this->error('The operation failure');die;
			}
		}

		$info = M('zixun_show')->where(array('admin_id' => $this->business_id))->find();

		$this->assign('info', $info);
		$this->display();
	}

	/**
	 * 资讯评论列表
	 */
	public function comment()
	{	
		$where = 'am_zixun.admin_id = '.$this->business_id.' AND am_users.admin_id = '.$this->business_id;

		$field = 'am_zixun_comment.id, am_zixun.title, am_zixun.img, am_zixun_comment.content, am_users.nickname, am_users.id user_id, am_zixun_comment.create_time';

		$count = M('zixun_comment')
				->join(array('INNER JOIN am_zixun ON am_zixun.id = am_zixun_comment.zixun_id', 'INNER JOIN am_users ON  am_users.openid = am_zixun_comment.openid'))
				->where($where)
				->field($field)
				->count();

		$Page = new \Think\Page($count, 15);
		$show = $Page->show();
		$limit = $Page->firstRow.', '.$Page->listRows;

		$list = M('zixun_comment')
				->join(array('LEFT JOIN am_zixun ON am_zixun.id = am_zixun_comment.zixun_id', 'INNER JOIN am_users ON am_users.openid = am_zixun_comment.openid'))
				->where($where)
				->field($field)
				->order('am_zixun_comment.create_time DESC')
				->limit($limit)
				->select();

		$this->assign('count', $count);
		$this->assign('page', $show);
		$this->assign('list', $list);
		$this->display('comment');
	}

	/**
	 * 删除评论
	 */
	public function delete_comment($id)
	{	
		$result = M('zixun_comment')->where(array('id' => $id))->delete();

		if ($result) {
			$this->success('Successfully Deleted');die;
		} else {
			$this->error('Delete failed');die;
		}
	}
}