<?php

namespace Main\Src;

use Dever;
use Main\Lib\Core;

//class Content extends Core
class Content
{
    public function test()
    {
        $test_url = 'http://sandboxapi.kdniao.com:8080/kdniaosandbox/gateway/exterfaceInvoke.json';
        $appkey = '32667432-5f6a-4b45-9a84-d8bad424d421';
        $param['RequestData'] = json_encode(array
        (
            'OrderCode' => '',
            'ShipperCode' => 'zhongtong',
            'LogisticCode' => '75323014559816',
        ));
        $param['EBusinessID'] = 'test1615206';
        $param['RequestType'] = '1002';
        $param['DataType'] = '2';
        $param['DataSign'] = urlencode(base64_encode(md5($param['RequestData'] . $appkey)));

        $data = Dever::curl($test_url, $param, 'post', true);
        print_r($data);die;
    }
    public function user()
    {
        $user = Dever::load('passport/user')->data();
        if ($user) {
            $url = Dever::url('passport/login.quit');
            $html = '<li>'.$user['email'].'</li><li><a href="'.$url.'">退出</a></li>';
        } else {
            $html = '<li><a href="javascript:;" onclick="login()">登录</a></li>
                    <li><a href="javascript:;" onclick="reg()">注册</a></li>';
        }

        $config = $this->config();
        $link = $config['email_link'];
        $html .= '<li><a href="'.$link.'" target="_blank">企业邮箱登录</a></li>';

        return $html;
    }

    # 基本配置
    public function config()
    {
        return Dever::db('main/config')->one();
    }

    # 单页信息
    public function page($key = false)
    {
        $key = Dever::input('key', $key);
        $data = Dever::db('content/page')->one(array('key' => $key));

        $data['link'] = Dever::url('page?key=' . $data['key'], 'main');
        return $data;
    }

    # pdf信息
    public function pdf()
    {
        $user = Dever::load('passport/user')->data();
        if (!$user) {
            Dever::alert('未登录');
        }
        $id = Dever::input('id');
        $data = Dever::db('content/magazine')->one($id);
        $data['view'] = 'var file = "'.$data['file'].'"';
        return $data;
    }

    # 文章信息
    public function view($id = false)
    {
        $data = array();
        $id = Dever::input('id', $id);
        if ($id > 0) {
            $data = Dever::db('content/article')->one($id);

            $cate = Dever::db('content/cate')->one($data['cate_id']);
            $data['cate_name'] = $cate['name'];
            $data['cate_link'] = Dever::url('list?cate=' . $data['cate_id'], 'main');

            $author = Dever::db('content/author')->one($data['author_id']);
            $data['author_name'] = '编辑：' . $author['name'];

            $data['date'] = date('Y-m-d', $data['cdate']);

            $data['share'] = 'var wbtitle = "'.$data['name'].'";var wbpic = "'.$data['pic_cover'].'";var wbappkey = "1867678939"';
        }
        
        return $data;
    }

    # 栏目信息
    public function cate($cate = false)
    {
        $data = array();
        $id = Dever::input('cate', $cate);
        if ($id > 0) {
            $data = Dever::db('content/cate')->one($id);
            $data['cate_link_1'] = Dever::url('list?cate=' . $data['id'], 'main');
            $data['cate_link_2'] = Dever::url('list_text?cate=' . $data['id'], 'main');
        }
        
        return $data;
    }

    # 菜单
    public function menu()
    {
        $config = $this->config();
        # 小程序去掉视频栏目
        $cate = array();
        //$cate['id_no'] = 2;
        $data = Dever::db('content/cate')->getAll($cate);

        if ($data) {
            foreach ($data as $k => $v) {
                $data[$k]['link'] = Dever::url('list?cate=' . $v['id'], 'main');
            }
        }

        $data[-2] = array
        (
            'id' => -2,
            'name' => '电子杂志',
            'link' => Dever::url('magazine?cate=-2', 'main'),
            'class' => '',
        );

        $data[-3] = array
        (
            'id' => -3,
            'name' => '报刊订阅',
            'link' => $config['buy_link'],
            'class' => '',
        );

        $index = array
        (
            'id' => -1,
            'name' => '首页',
            'link' => Dever::url('home', 'main'),
            'class' => '',
        );

        $cate = Dever::input('cate', -1);

        if ($cate == -1) {
            $index['class'] = 'active';
        } elseif ($cate == -2) {
            $data[-2]['class'] = 'active';
        } else {
            foreach ($data as $k => $v) {
                $data[$k]['class'] = '';
                if ($cate == $v['id']) {
                    $data[$k]['class'] = 'active';
                }
            }
        }
        array_unshift($data, $index);



        return $data;
    }

    # 焦点图
    public function focus()
    {
        $cate = Dever::input('cate', -1);
        $key = 'focus_' . $cate;
        $name = '焦点图';
        if ($cate > 0) {
            $cate = Dever::db('content/cate')->one($cate);
            if ($cate) {
                $name = $cate['name'] . $name;
            }
        } else {
            $name = '首页' . $name;
        }
        $data = Dever::load('push/lib/data')->get($key, $name, 6);
        return $data;
    }

    # 获取推荐列表
    public function getPush($data)
    {
        return Dever::load('push/lib/data')->get($data['key'], $data['name'], $data['num']);
    }

    # 获取广告位数据
    public function getAd($name)
    {
        $info = Dever::load('push/lib/data')->getOne('ad', '广告位', 20, $name);
        if ($info && $info['pic']) {
            return '<a href="'.$info['link'].'" target="_blank"><img src="'.$info['pic'].'" alt="'.$info['name'].'"></a>';
        } else {
            return '';
        }
    }

    # 获取首页文章列表
    public function getList($data)
    {
        $where['cate_id'] = $data['cate_id'];
        $where['limit'] = $data['num'];
        $data = Dever::db('content/article')->getList($where);
        if ($data) {
            foreach ($data as $k => $v) {
                if (isset($v['pic_index']) && $v['pic_index']) {
                    $data[$k]['pic'] = $v['pic_index'];
                } else {
                    $data[$k]['pic'] = $v['pic_cover'];
                }
                
                $data[$k]['link'] = $this->link($v);
            }
        }

        return $data;
    }

    # 获取文章列表
    public function getAll()
    {
        $where['cate_id'] = Dever::input('cate');
        $data = Dever::db('content/article')->getAll($where);
        if ($data) {
            foreach ($data as $k => $v) {
                $data[$k]['pic'] = $v['pic_cover'];
                $data[$k]['link'] = $this->link($v);
            }
        }

        return $data;
    }

    # 获取杂志列表
    public function getMagazine()
    {
        $where['cate_id'] = Dever::input('cate_id', 1);
        $data = Dever::db('content/magazine')->getAll($where);
        $user = Dever::load('passport/user')->data();
        if ($data) {
            foreach ($data as $k => $v) {
                $data[$k]['pic'] = $v['pic_cover'];
                if ($user) {
                    $data[$k]['link'] = Dever::url('pdf?id=' . $v['id']);
                } else {
                    $data[$k]['link'] = 'javascript:login()';
                }
            }
        }

        return $data;
    }

    # 获取杂志分类列表
    public function getMagazineCate()
    {
        $data = Dever::db('content/magazine_cate')->getAll();
        if ($data) {
            foreach ($data as $k => $v) {
                $data[$k]['pic'] = $v['pic_cover'];
                $data[$k]['link'] = Dever::url('magazine_list?cate=-2&cate_id=' . $v['id']);
            }
        }

        return $data;
    }

    # 获取文章列表
    public function getRelation()
    {
        $id = Dever::input('id');
        $info = Dever::db('content/article')->one($id);
        $where['noid'] = $id;
        $where['cate_id'] = $info['cate_id'];
        $data = Dever::db('content/article')->getRelation($where);
        if ($data) {
            foreach ($data as $k => $v) {
                $data[$k]['pic'] = $v['pic_cover'];
                $data[$k]['link'] = $this->link($v);
            }
        }

        return $data;
    }

    public function link($data)
    {
        if ($data['template'] == 2) {
            $link = Dever::url('video?cate='.$data['cate_id'].'&id=' . $data['id'], 'main');
        } else {
            $link = Dever::url('view?cate='.$data['cate_id'].'&id=' . $data['id'], 'main');
        }
        return $link;
    }
}