<?php

namespace Main\Src;

use Dever;
use Main\Lib\Core;

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

        if (!strstr($data['phone'], '电话')) {
            $data['phone'] = '电话：' . $data['phone'];
        }

        $sell_config = Dever::db('main/sell_config')->find();
        $manage_config = Dever::db('main/manage_config')->find();
        $factory_config = Dever::db('main/factory_config')->find();

        $data['sell_link'] = 'javascript:;';
        $data['sell_name'] = $sell_config['name'];
        $data['sell_applet'] = $sell_config['applet'];

        $data['manage_link'] = 'javascript:;';
        $data['manage_name'] = $manage_config['name'];
        $data['manage_applet'] = $manage_config['applet'];

        $data['factory_link'] = 'javascript:;';
        $data['factory_name'] = $factory_config['name'];

        $data['about_link'] = Dever::load('main/view')->getLink('about', '关于公司');
        $data['agreement_link'] = Dever::load('main/view')->getLink('agreement', '用户服务协议');
        $data['privacy_link'] = Dever::load('main/view')->getLink('privacy', '隐私政策');

        $data['news_link'] = Dever::url('news', 'main');
        $data['help_link'] = Dever::url('help', 'main');
        $data['home_link'] = Dever::url('home', 'main');
        $data['jd_link'] = Dever::url('jd', 'main');
        $data['contact_link'] = Dever::url('contact', 'main');

        $uri = Dever::uri();
        if (strstr($uri, 'home')) {
            $data['open_link'] = '#cities';
        } else {
            $data['open_link'] = Dever::url('home#cities', 'main');
        }

        $citys = Dever::split($data['citys']);
        $data['citys'] = array();
        if ($citys) {
            foreach ($citys as $k => $v) {
                $data['citys'][] = '<li><a href="javascript:;">'.$v.'</a></li>';
            }        
        }
        $data['citys'] = implode('', $data['citys']);

        $data['baidu'] = '<script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?4f5a8be51cf141219d4f6eb2c1efef73";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>
';

        return $data;
    }

    public function contact()
    {
        $data = Dever::db('main/config')->find();

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

        return $data['contact'];
    }

    # 获取帮助列表
    public function help()
    {
        $type = Dever::input('type', 5);
        if (!$type) {
            Dever::alert('您访问的页面有误');
        }
        $where['type'] = $type;
        $data = Dever::db('main/help')->select_page($where);

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

        return $data;
    }

    # 获取帮助详情
    public function help_view()
    {
        $id = Dever::input('id');
        $data = Dever::db('main/help')->find($id);

        return $data;
    }

    # 获取首页的企业动态
    public function getNews()
    {
        $data = Dever::db('main/news')->getList();

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

        return $data;
    }

    # 获取动态分类
    public function news_cate()
    {
        $cate = Dever::input('cate', 4);

        $data = Dever::db('main/cate')->select();

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

    # 获取动态列表
    public function news()
    {
        $cate = Dever::input('cate', 4);
        if (!$cate) {
            Dever::alert('您访问的页面有误');
        }
        $where['cate_id'] = $cate;
        $data = Dever::db('main/news')->getAll($where);

        if ($data) {
            foreach ($data as $k => $v) {
                $data[$k]['link'] = Dever::url('article?id=' . $v['id'], 'main');
                $data[$k]['cdate'] = date('Y-m-d H:i', $v['pdate']);
            }
        }
        return $data;
    }

    # 获取动态详情
    public function news_view()
    {
        $id = Dever::input('id');
        $data = Dever::db('main/news')->one($id);
        if ($data) {
            $data['cdate'] = '发布：' . date('Y-m-d H:i', $data['pdate']);
            $cate = Dever::db('main/cate')->one($data['cate_id']);
            $data['cate_name'] = '文章类型： ' . $cate['name'];
            $source = Dever::db('main/source')->one($data['source_id']);
            $data['source_name'] = '文章来源： ' . $source['name'];
        }

        return $data;
    }

    # 获取上一篇文章
    public function news_prev()
    {
        $where['id'] = Dever::input('id');
        $data = Dever::db('main/news')->one($where);
        $where['cate_id'] = $data['cate_id'];
        $data = Dever::db('main/news')->findPrev($where);
        if ($data) {
            $data['link'] = Dever::url('article?id=' . $data['id'], 'main');
            $data['state'] = 'display:block';
        } else {
            $data['state'] = 'display:none';
            $data['link'] = '';
            $data['name'] = '';
        }

        return $data;
    }

    # 获取上一篇文章
    public function news_next()
    {
        $where['id'] = Dever::input('id');
        $data = Dever::db('main/news')->one($where);
        $where['cate_id'] = $data['cate_id'];
        $data = Dever::db('main/news')->findNext($where);
        if ($data) {
            $data['link'] = Dever::url('article?id=' . $data['id'], 'main');
            $data['state'] = 'display:block';
        } else {
            $data['state'] = 'display:none';
            $data['link'] = '';
            $data['name'] = '';
        }

        return $data;
    }

    # 招聘信息
    public function zhaopin()
    {
        $data = Dever::db('main/zhaopin')->select_page();

        if ($data) {
            $type = Dever::db('main/zhaopin')->config['config_type'];
            foreach ($data as $k => $v) {
                $data[$k]['group'] = Dever::db('main/group')->find($v['group_id']);
                $data[$k]['group_name'] = $data[$k]['group']['name'];

                $data[$k]['type_name'] = $type[$v['type']];

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