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

class Post extends Mocuz_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->model('sharecircle/post_model', 'post');
    }

    //帖子管理
    public function index()
    {
        $data = array();
        $where = array();
        $page = !isset($_POST['page']) ? 1 : $_POST['page'];
        $pagesize = !isset($_POST['pagesize']) ? 10 : $_POST['pagesize'];

        $limit = ($page - 1) * $pagesize;
        $fields = '*';
        if(!empty($_POST)){
            if($_POST['status']!=""&&$_POST['status']!="-1"){
                $where[] = "`isdelete` = ".$_POST['status']; 
            }
            if($_POST['title']!=""){
                $where[] = "position('".$_POST['title']."' in `content`)";
            }

            if($_POST['uname']!=""){
                $where[] = "position('".$_POST['uname']."' in `username`)";
            }
        }
        $pagecount = ceil($this->post->get_count($where) / $pagesize);

        $data['post_list'] = $this->post->get_post_list($where,$fields, $pagesize, $limit); //获取帖子数据
        $data['pagecount']=$pagecount;
        if(!empty($_POST)){
            echo json_encode($data);
            exit;
        }

        $this->load->view('common/innerheader');
        $this->load->view('sharecircle/post_list',$data);
    }

    //修改帖子浏览量
    public function changeReadCount(){
        $num = (int)$_POST['num'];
        $id = $_POST['id'];
        if($num<0 || !$id){
            echo json_encode(array('error'=>'-1','errormessage'=>'no parameter'));
            exit;
        }
        $where = array('id='.$id);
        $data = array('readcount'=>$num);
        $result = $this->post->update($data,$where);
        echo json_encode(array('error'=>'0','errormessage'=>$result));
    }

    //禁用 or 启用帖子
    
    public function changePostState(){
        $state = (int)$_POST['state'];
        $id = $_POST['id'];
        if($state<0 || !$id){
            echo json_encode(array('error'=>'-1','errormessage'=>'no parameter'));
            exit;
        }
        $where = array('id in('.$id.')');
        $data = array('isdelete'=>(int)$state);
        $result = $this->post->update($data,$where);
        echo json_encode(array('error'=>'0','errormessage'=>$result));
    }
}