<?php

namespace Forum\Src;

use Dever;

class Posts
{
    private $list = '';
    public function __construct()
    {
        $this->config();
        $this->uid = Dever::input('uid');
        $this->cate = Dever::input('cate');
        $this->home = Dever::input('home');
        $this->id = Dever::input('id');
    }

    public function config()
    {
        if (empty($this->type)) {
            $this->type = Dever::load('forum/type-all');
        }

        return $this->type;
    }

    public function getList()
    {
        if (!$this->list) {
            $this->list = Dever::view("_model/3");
        }

        return $this->list;
    }

    public function top()
    {
        $data['display'] = 'display:none;';
        if ($this->cate > 0) {
            $data = Dever::load('forum/cate-one', $this->cate);
        } elseif ($this->uid > 0) {
            $data = Dever::load('passport/user-one', $this->uid);
        }

        if (isset($data['id'])) {
            $data['display'] = '';
            $data['total'] = '[' . Dever::total("current") . ']';
            $data['info'] = Dever::markdown($data['info']);

            $data['pic'] = isset($data['avatar']) ? $data['avatar'] : $data['pic'];
            $data['name'] = isset($data['username']) ? $data['username'] : $data['name'];
        } else {
            $data['pic'] = '';
            $data['name'] = '';
            $data['total'] = '[0]';
            $data['info'] = '';
            $data['display'] = 'display:none;';
        }

        return $data;
    }

    public function type($data)
    {
        $url = 'forum/list?';
        if ($this->cate > 0) {
            $url .= 'cate=' . $this->cate . '&';
        }

        if ($this->uid > 0) {
            $url .= 'uid=' . $this->uid . '&';
        }

        $url .= 'type=' . $data['id'];

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

        $data['class'] = '';
        if (Dever::input('type', 1) == $data['id']) {
            $data['class'] = 'selected';
        }

        return $data;
    }

    public function get($id = false)
    {
        $type = Dever::input('type', 1);
        $this->cate = Dever::input('cate', $this->cate);

        if (isset($this->type[$type])) {
            $param = array();

            $this->setParam($this->type[$type], $param, $id);

            if ($this->id > 0) {
                $param['option']['id'] = array('yes', '!=');
            }
            if ($this->cate > 0) {
                $param['option_cate_id'] = $this->cate;
            } elseif (!$this->uid) {
                $cate = Dever::load('forum/cate-all', array('option_state' => 1, 'option_type' => 1));
                $cate_id = implode(',', array_keys($cate));
                $param['option_cate_ids'] = $cate_id;
            }

            if ($this->uid > 0) {
                $param['option_uid'] = $this->uid;
                $param['option']['uid'] = 'yes';
            }

            $data = Dever::load('forum/posts-getAll', $param);
        } else {
            //Dever::alert(404);
            $data = array();
        }

        return $data;
    }

    private function setParam($type, &$param, $id)
    {
        if ($type['order']) {
            parse_str($type['order'], $param['order']);
        }
        if ($type['where']) {
            parse_str($type['where'], $param['option']);
            foreach ($param['option'] as $k => $v) {
                $param['option_' . $k] = $v;
            }
        }
        if ($id < 0 && $type['pagenum'] > 0) {
            $param['page'] = array($type['pagenum'], 'list');
        } else {
            $param['page'] = array();
            $param['limit'] = array($type['pagenum'], 0);
        }

        $param['col'] = '*';
    }

    public function _token()
    {
        $token['post'] = 1;
        $token = http_build_query(Dever::token($token));

        return $token;
    }

    public function _url()
    {
        return Dever::proxy('forum/posts.save', $this->_token());
    }

    public function save()
    {
        # 检测是否登录
        $user = Dever::load('passport/user.data');
        if (!$user) {
            Dever::alert('请先登录');
        }

        $param['option_cate_id'] = Dever::input('cate');
        $param['option_name'] = Dever::input('name');
        $param['option_content'] = Dever::input('content');
        $param['option_uid'] = $user['id'];

        if (!$param['option_cate_id']) {
            Dever::alert('请选择' . Dever::load('main/config-one#catename', 1) . '');
        }

        $cate = Dever::load('forum/cate-one', $param['option_cate_id']);

        if (!Dever::load('forum/cate.auth', $cate)) {
            Dever::alert('您没有权限在该' . Dever::load('main/config-one#catename', 1) . '下发布');
        }

        if ($cate['type'] != 1 && $cate['cate_id'] <= 0) {
            //Dever::alert('您所选择的'.Dever::load('main/config-one#catename', 1).'不能发布');
        }

        if (!$param['option_name']) {
            Dever::alert('请填写标题');
        }

        if (!$param['option_content']) {
            Dever::alert('请填写内容');
        }

        $info = Dever::load('forum/posts-one', $param);

        if ($info) {
            Dever::alert('你要发布的[<a href="' . Dever::url('forum/view?id=' . $info['id'], 'main') . '" target="_blank">' . $param['option_name'] . '</a>]已经存在，请不要重复发布');
        } else {
            $param['add_cate_id'] = $param['option_cate_id'];
            $param['add_cate_id_parent'] = $cate['cate_id'];
            $param['add_name'] = $param['option_name'];
            $param['add_content'] = $param['option_content'];
            $param['add_uid'] = $user['id'];
            $param['add_ip'] = Dever::ip();

            $id = Dever::load('forum/posts-insert', $param);

            $refer = Dever::input('refer');

            $refer = $refer ? $refer : Dever::url('forum/view?id=' . $id, 'main');

            Dever::out($refer);
        }
    }

    public function info($data = array())
    {
        if (!$data) {
            if ($this->id > 0) {
                $data = Dever::load('forum/posts-one', $this->id);

                Dever::setInput('cate', $data['cate_id']);
                Dever::setInput('post', $data['id']);

                $data['content'] = Dever::markdown($data['content']);
            }
        }

        if (!$data) {
            Dever::alert(404);
        }

        $data['user_name'] = Dever::load('passport/user-one#username', $data['uid']);
        $data['user_link'] = Dever::url('forum/list?uid=' . $data['uid'], 'main');
        $data['user_avatar'] = Dever::load('passport/user-one#avatar', $data['uid']);

        $data['cate_name'] = Dever::load('forum/cate-info#name', $data['cate_id']);
        $data['cate_link'] = Dever::url('forum/list?cate=' . $data['cate_id'], 'main');

        $comment = Dever::load('forum/cate-info#comment', $data['cate_id']);
        if ($comment == 2) {
            $data['comment_show'] = 'display:none;';
        } else {
            $data['comment_show'] = '';
        }

        $data['comment_link'] = Dever::url('forum/comment_list?post=' . $data['id'], 'main');

        $data['link'] = Dever::url('forum/view?id=' . $data['id'], 'main');

        if ($data['essence'] == 3) {
            $data['tui'] = 'display:inline;';
        } else {
            $data['tui'] = 'display:none;';
        }

        if ($data['essence'] == 2) {
            $data['essence'] = 'display:inline;';
        } else {
            $data['essence'] = 'display:none;';
        }

        if ($data['top'] == 2) {
            $data['top'] = 'display:inline;';
        } else {
            $data['top'] = 'display:none;';
        }

        $data['time'] = Dever::mdate($data['cdate'], 2);
        $data['cdate'] = date('Y-m-d H:i:s', $data['cdate']);

        $data['home'] = $this->home;

        return $data;
    }
}
