<?php

namespace Main\Src;

use Dever;
use Main\Lib\Core;

class View extends Core
{
    private $addView = true;

    public function version()
    {
        $source_type = Dever::input('source_type');
        $version['source_type'] = $source_type;
        $this->data['version'] = Dever::db('main/version')->getOne($version);

        if ($this->data['version']) {
            if (isset($this->data['version']['link'])) {
                $this->data['version']['link'] = str_replace('http://', 'https://', $this->data['version']['link']);
            }

            if (isset($this->data['version']['file'])) {
                $this->data['version']['file'] = str_replace('http://', 'https://', $this->data['version']['file']);
            }
        }

        return $this->data;
    }
    # 获取评论列表
    public function comment()
    {
        $id = Dever::input('id');
        if (!$id) {
            Dever::alert('错误的ID');
        }
        $type = Dever::input('type');
        if (!$type) {
            Dever::alert('错误的类型');
        }

        $this->data['comment'] = Dever::load('act/lib/comment')->get($id, $type, $this->data['uid']);

        if ($type == 3) {
            $this->data['live_status'] = 2;
            # 获取直播的状态
            $live = Dever::db('video/live')->one($id);
            if ($live && $live['stream_id']) {
                $this->data['live_status'] = Dever::load('live/api')->status($live['stream_id']);
            }
        }

        return $this->data;
    }

    private function get($method, $name, $type = 1, $key = '')
    {
        $id = Dever::input('id');
        if (!$id) {
            Dever::alert('错误的'.$name.'ID');
        }
        $this->data['info'] = Dever::load($method)->get($id, $this->data['uid']);
        if (!$this->data['info']) {
            Dever::alert('错误的'.$name.'ID');
        }

        $this->data['info'] = $this->pic($this->data['info']);

        if ($this->data['info']['pdate'] > 0) {
            $this->data['info']['pdate_time'] = date('Y-m-d H:i:s', $this->data['info']['pdate']);
        }
        /*
        if (isset($this->data['info']['share_content'])) {
            $source = Dever::db('source/info')->one(1);
            $this->data['info']['share_link'] = $source['code'];
        }
        */

        if ($this->source_type == 'ioss') {
            $source = Dever::db('source/info')->one(1);
            $this->data['info']['share_link'] = $source['code'];
        } else {
            $link = Dever::url('view.get?type=' . $type . '&id=' . $id, 'h5');
            $this->data['info']['share_link'] = $link;
        }

        if ($this->data['info']['num_view'] > 100000) {
            //$this->data['info']['num_view'] = '10w+';
        }

        if (isset($this->data['info']['cate_id']) && $this->data['info']['cate_id']) {
            $this->data['info']['cate'] = Dever::db('content/cate')->one($this->data['info']['cate_id']);
        }

        if (isset($this->data['info']['author_id']) && $this->data['info']['author_id']) {
            $this->data['info']['author'] = Dever::db('content/author')->one($this->data['info']['author_id']);
        }

        # 获取评论列表
        $this->data['comment'] = Dever::load('act/lib/comment')->get($id, $type, $this->data['uid']);

        # 获取相关推荐
        $this->data['relation'] = Dever::load($method)->getRelation($this->data['info']);
        if ($this->data['relation']) {
            foreach ($this->data['relation'] as $k => $v) {
                $this->data['relation'][$k] = $this->getInfo($type, $v);
            }
        }

        # 用户行为数据
        $this->data['user_act'] = array();

        # 获取当前用户分享数
        if ($this->data['uid'] > 0) {
            $this->data['user_act']['share'] = Dever::load('act/lib/share')->getRefluxNum($this->data['uid'], $id, $type);

            # 当前用户是否点赞
            $this->data['user_act']['up'] = Dever::load('act/lib/like')->get($this->data['uid'], $id, $type);

            Dever::score($this->data['uid'], 'view_' . $key, '浏览' . $name);
            
        } else {
            $this->data['user_act']['share'] = 0;
            $this->data['user_act']['up'] = 2;
        }

        # 用户预约
        if ($type == 3) {
            if ($this->data['uid'] > 0) {
                $this->data['user_act']['note'] = Dever::load('act/lib/note')->get($this->data['uid'], $id, $type);
                
            } else {
                $this->data['user_act']['note'] = 2;
            }

            /*
            if (isset($this->data['info']['status']) && $this->data['info']['status'] == 2) {
                # 直播中
                Dever::load('act/lib/watch')->submit($this->data['uid'], $id, $type);
            }
            */
        }
        
        /*
        if ($this->addView) {
            # 浏览量+1
            Dever::load($method)->addView($id);
        }
        */

        # 传给下一步操作的参数
        $this->data['end']['type'] = $type;
        $this->data['end']['id'] = $id;
        $this->data['end']['uid'] = $this->data['uid'];
        $this->data['end']['method'] = $method;
        $this->data['end']['addview'] = $this->addView;
        $this->data['end']['status'] = isset($this->data['info']['status']) ? $this->data['info']['status'] : false;

        return $this->data;
    }

    # 获取图文详情
    public function article()
    {
    	return $this->get('content/lib/article', '图文', 1, 'article');
    }

    # 获取视频详情
    public function vod()
    {
        return $this->get('video/lib/vod', '视频', 2, 'vod');
    }

    # 获取直播详情
    public function live()
    {
    	return $this->get('video/lib/live', '直播', 3, 'live');
    }

    # 获取专题详情
    public function feature()
    {
        return $this->get('feature/lib/info', '专题', 6, 'feature');
    }

    # 获取单页详情
    public function getPage()
    {
        $this->data['info'] = $this->page();

        return $this->data;
    }

    # 获取单页详情
    public function page()
    {
        $key = Dever::input('key');
        if (!$key) {
            Dever::alert('您访问的页面有误');
        }

        $info = Dever::db('content/page')->one(array('key' => $key . '_' . $this->data['system']));
        if (!$info) {
            $info = Dever::db('content/page')->one(array('key' => $key));
        }

        return $info;
    }

    # 根据类型，获取详情的html页面
    public function getHtml()
    {
        $this->addView = false;
        $type = Dever::input('type');
        $id = Dever::input('id');
        $signature = Dever::input('signature');
        $data = array();
        switch($type) {
            case 1:
                $data = $this->article();
                break;
            case 2:
                $data = $this->vod();
                break;
            case 3:
                $data = $this->live();
                break;
            case 4:
                # 这个有点特殊
                $info = Dever::db('journal/content')->one($id);
                if (!$info) {
                    Dever::alert('错误的数据');
                }
                $data = Dever::load('main/journal')->readContent($info);
                break;
            case 6:
                $data = $this->feature();
                break;
        }

        if ($data) {
            if (isset($data['info']['content_array'])) {
                $data['content'] = $data['info']['content_array'];
            } elseif (isset($data['info']['content'])) {
                $data['content'][] = array
                (
                    'type' => 1,
                    'content' => $data['info']['content']
                );
            }

            if ($type == 6) {
                $data['css'] = 'feature';
            } else {
                $data['css'] = 'index';
            }
            
            return Dever::render('content', $data);
        } else {
            Dever::alert('错误的数据');
        }
    }

    # 获取会员的信息接口
    public function vip()
    {
        $this->data['vip_config'] = Dever::db('vip/config')->one(array('cate_id' => $this->data['system']));

        $this->data['vip_time'] = Dever::db('vip/time')->getAll(array('cate_id' => $this->data['system']));


        if ($this->data['uid'] > 0) {
            $user = Dever::load('passport/api')->info($this->data['uid']);
            $this->data['mobile'] = $user['mobile'];
        } else {
            $this->data['mobile'] = '';
        }

        return $this->data;
    }
}