<?php

namespace app\admin\controller\jdgw;

use app\common\controller\Backend;
use think\Db;

/**
 * 文章管理
 *
 * @icon fa fa-circle-o
 */
class Article extends Backend
{
    
    /**
     * Article模型对象
     * @var \app\admin\model\jdgw\Article
     */
    protected $model = null;
    protected $relationSearch = true;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\jdgw\Article;
        $this->view->assign("typedataList", $this->model->getTypedataList());
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->view->assign('nav', \app\admin\model\jdgw\Nav::getTree());
    }

    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->with('nav')
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    public function selectSearch()
    {
        $nav = \app\admin\model\jdgw\Nav::getTree();
        return json(["total" => count($nav), "rows" => $nav]);
    }

    /**
     * 普通收录
     */
    public function bdPush()
    {
        if (!\think\Hook::get('baidupush')) {
            $this->error(__('请在后台插件管理安装百度推送插件'));
        }

        $type = $this->request->post("type");
        $action = $this->request->post("action");
        $ids = $this->request->post("ids/a");
        $urls = [];
        foreach ($ids as $k=>$v){
            $urls[] = ($this->isHTTPS()? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . addon_url('jdgw/index/article', [':diyname'=> $v['diyname'], ':id'=>$v['id']], 'html');
        }

        $result = false;
        if ($action == 'urls') {
            $result = \addons\baidupush\library\Push::init(['type' => $type])->realtime($urls);
        } elseif ($action == 'del') {
            $result = \addons\baidupush\library\Push::init(['type' => $type])->delete($urls);
        }
        if ($result) {
            if ($action == 'urls') {
                $upData = [];
                foreach ($ids as $k=>$v){
                    $upData[] = ['id'=>$v['id'], 'bddata'=>1];
                }
                $this->model->saveAll($upData);
            } elseif ($action == 'del') {
                $upData = [];
                foreach ($ids as $k=>$v){
                    $upData[] = ['id'=>$v['id'], 'bddata'=>0];
                }
                $this->model->saveAll($upData);
            }
            $data = \addons\baidupush\library\Push::init()->getData();
            $this->success("推送成功", null, $data);
        } else {
            $this->error("推送失败：" . \addons\baidupush\library\Push::init()->getError());
        }
    }

    public function isHTTPS() {
        if ( !empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') {
            return true;
        } elseif ( isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https' ) {
            return true;
        } elseif ( !empty($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off') {
            return true;
        }
        return false;   
    }
}
