<?php

namespace Main\Src;

use Dever;
use Main\Lib\Core;
use Dever\Routing\Uri;

class Content
{
    # 基本配置
    public function config()
    {
        $data = Dever::db('main/config')->one();

        $data['contact'] = Dever::url('contact');

        return $data;
    }

    # 获取菜单
    public function menu()
    {
        $uri = Uri::$value;
        $type = Dever::input('type');
        $menu = array
        (
            'home' => array
            (
                'name' => '首&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;页',
                'active' => $uri == 'search' ? true : false,
            ),

            'news' => array
            (
                'name' => '资&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;讯',
                'active' => $type == 1 ? true : false,
                
            ),

            'xnr' => array
            (
                'name' => '虚&nbsp;拟&nbsp;人',
                'active' => false,
                'uri' => array('xnr-detail', 'zwz-detail'),
            ),

            'feature' => array
            (
                'name' => '专&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;题',
                'active' => $type == 2 ? true : false,
            ),

            'video' => array
            (
                'name' => '视&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;频',
                'active' => $type == 3 ? true : false,
            ),

            'activity' => array
            (
                'name' => '活&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;动',
                'active' => $type == 4 ? true : false,
            ),

            'contact' => array
            (
                'name' => '关于我们',
                'active' => false,
            ),
        );

        foreach ($menu as $k => $v) {
            $menu[$k]['link'] = Dever::url($k);

            if ($v['active'] || $uri == $k || (isset($v['uri']) && in_array($uri, $v['uri']))) {
                $menu[$k]['class'] = 'active';
            } else {
                $menu[$k]['class'] = '';
            }
        }

        return $menu;
    }

    # 首页广告
    public function ad()
    {
        $key = Dever::input('key', 'home_ad');
        $name = Dever::input('name', '首页广告');
        return Dever::load('push/lib/data')->get($key, $name, 3, '1,5');
    }
    
    # 焦点图
    public function focus()
    {
        $key = Dever::input('key', 'home_focus');
        $name = Dever::input('name', '首页焦点图');
        return Dever::load('push/lib/data')->get($key, $name, 5, '1,2,5');
    }

    # 获取热门推荐
    public function hot()
    {
        $type = Dever::input('type');
        if ($type == 1) {
            $key = 'news';
            $name = '资讯';
        } elseif ($type == 2) {
            $key = 'feature';
            $name = '专题';
        } elseif ($type == 3) {
            $key = 'video';
            $name = '视频';
        } elseif ($type == 4) {
            $key = 'act';
            $name = '活动';
        }
        $data = Dever::load('push/lib/data')->get($key . '_hot', $name . '详情页热门推荐', 4, '1,3,5', ($type + 2). ',10');
        if (!$data) {
            $data = Dever::load('content/lib/news')->getHome($type, 4);
        }

        return $data;
    }

    # 获取首页资讯
    public function home_news()
    {
        //$data = Dever::load('push/lib/data')->get('home_news', '首页资讯', 3, '1,2,3,4,5,6,7');
        $data = false;
        if (!$data) {
            $data = Dever::load('content/lib/news')->getHome(1, 3);
        }

        return $data;
    }

    # 获取首页专题
    public function home_feature()
    {
        //$data = Dever::load('push/lib/data')->get('home_feature', '首页专题', 5, '1,2,3,4,5,6,7');
        $data = false;
        if (!$data) {
            $data = Dever::load('content/lib/news')->getHome(2, 5);
        }

        return $data;
    }

    # 获取首页视频
    public function home_video_first()
    {
        //$data = Dever::load('push/lib/data')->get('home_video', '首页视频', 5, '1,2,3,4,5,6,7');
        $data = false;
        if (!$data) {
            $data = Dever::load('content/lib/news')->getHome(3, 5);
        }

        if ($data) {
            return $data[0];
        }

        return false;
    }

    public function home_video_other()
    {
        //$data = Dever::load('push/lib/data')->get('home_video', '首页视频', 5, '1,2,3,4,5,6,7');
        $data = false;
        if (!$data) {
            $data = Dever::load('content/lib/news')->getHome(3, 5);
        }

        if ($data) {
            unset($data[0]);
        }

        return $data;
    }

    # 获取首页活动
    public function home_act()
    {
        //$data = Dever::load('push/lib/data')->get('home_act', '首页活动', 4, '1,2,3,4,5,6,7');
        $data = false;
        if (!$data) {
            $data = Dever::load('content/lib/news')->getHome(4, 4);
        }

        return $data;
    }

    # 获取首页虚拟人
    public function home_xnr()
    {
        //$data = Dever::load('push/lib/data')->get('home_xnr', '首页虚拟人', 10, '1,2,3,4,5');
        $data = false;
        if (!$data) {
            $data = Dever::load('content/lib/xuniren')->getHome(10);
        }

        return $data;
    }

    # 获取资讯
    public function news()
    {
        $type = Dever::input('type', 1);
        $name = Dever::input('v');
        $data = Dever::load('content/lib/news')->getAll($type, $name);

        return $data;
    }

    # 获取资讯详情
    public function view()
    {
        $type = Dever::input('type', 1);
        $id = Dever::input('id');
        if (!$id) {
            Dever::alert('错误的数据id');
        }
        $data = Dever::load('content/lib/news')->getInfo($type, $id);

        return $data;
    }

    # 获取单页内容
    public function getPage()
    {
        $key = Dever::input('key', 1);
        $where['key'] = $key;
        $data =  Dever::db('main/page')->one($where);

        return $data;
    }

    # 获取单页内容
    public function getAbout()
    {
        Dever::setInput('key', 'about');
        return $this->getPage();
    }

    # 搜索页面
    public function search()
    {
        $url = Dever::url('search?v=');
        $html = 'location.href=\''.$url.'\'+$(\'#search\').val()';

        return $html;
    }

    # 手机搜索页面
    public function msearch()
    {
        $url = Dever::url('search?v=');
        $html = 'location.href=\''.$url.'\'+$(\'#msearch\').val()';

        return $html;
    }

    # 获取联系我们的需求分类
    public function contact_xuqiu()
    {
        return Dever::db('act/contact')->config['xuqiu'];
    }

    # 新增联系我们
    public function contact_add()
    {
        $username = Dever::input('username');
        $mobile = Dever::input('mobile');
        $email = Dever::input('email');
        $content = Dever::input('content');
        $xuqiu = Dever::input('xuqiu', 3);
        if (!$username) {
            Dever::alert('请填写姓名');
        }
        if (!$mobile) {
            Dever::alert('请填写联系电话');
        }
        if (!$email) {
            Dever::alert('请填写电子信箱');
        }
        Dever::load('act/lib/contact')->add($username, $mobile, $email, $content, $xuqiu);

        return 'ok';
    }

    # 新增订阅
    public function ding_add()
    {
        $username = Dever::input('username');
        $email = Dever::input('email');
        $qudao = Dever::input('qudao', 1);
        if (!$username) {
            Dever::alert('请填写用户名');
        }
        if (!$email) {
            Dever::alert('请填写电子信箱');
        }
        Dever::load('act/lib/ding')->add($username, $email, $qudao);

        return 'ok';
    }

    # 新增分享
    public function share_add()
    {
        $name = Dever::input('name');
        $link = Dever::input('link');
        $type = Dever::input('type', 1);
        if (!$name) {
            Dever::alert('请填写内容名称');
        }
        if (!$link) {
            Dever::alert('请填写链接');
        }
        Dever::load('act/lib/share')->add($name, $link, $type);

        return 'ok';
    }
}