<?php
/**
 * @Statement: [ An Ordinary Person ]
 * @Author: 闻子 < QQ：270988107 >
 * @Copyright: 闻子 2016-2019 All rights reserved.
 *
 *    .--,       .--,
 *   ( (  \.---./  ) )
 *    '.__/o   o\__.'
 *       {=  ^  =}
 *        >  -  <
 *       /       \
 *      //       \\
 *     //|   .   |\\
 *     "'\       /'"_.-~^`'-.
 *        \  _  /--'         `
 *      ___)( )(___
 *     (((__) (__)))    高山仰止,景行行止.虽不能至,心向往之.
 *
 */

namespace app\index\controller;

use think\Db;

use app\common\model\Scode as ScodeModel;

use app\common\controller\Base as BaseController;

class Search extends BaseController
{
	/**
	 * 继承基类
	 * @Author: 闻子 < QQ：270988107 >
	 */
    use Basedata;

	/**
	 * 搜索页
	 * @Author: 闻子 < QQ：270988107 >
	 */
    public function index()
    {
    	$keyword = input('keyword');
    	if (!$keyword) {
    		return $this->error('请输入搜索关键词','/');
    	}
        $taginfo = Db::name('tag')->where('name',$keyword)->find();
        if ($taginfo) {
            $where = ['tagid' => $taginfo['id']];
        }else{
            if ($keyword) { 
                $where[] = ['title','like','%'.$keyword.'%'];
            }else{
                $where = [];
            }
        }
    	$scode_list = Db::name("scode")->where($where)->where('mid',input('id'))->order("id desc")->paginate(10);
    	$scode_count = Db::name("scode")->where($where)->where('mid',input('id'))->count();
    	$page = $scode_list->render();
	    if($keyword){
        	$list_to_array = $scode_list->toArray();
       		$scode_list = $list_to_array['data'];
	        foreach($scode_list as $key=>$item){
	            $scode_list[$key]['title'] = preg_replace("/($keyword)/i","<font color='red'><strong>{$keyword}</strong></font>",$item['title']);
	            $item['content'] = strip_tags($item['content']);
	            $scode_list[$key]['content'] = preg_replace("/($keyword)/i","<font color='red'><strong>{$keyword}</strong></font>",$item['content']);
	        }
	    }
	    return view('',compact('keyword','scode_list','scode_count','page'));
    }
}
