<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use app\admin\model\Menu;

class Index extends Base
{
    public function _initialize() {
        // 判断缓存中是否有静态化配置项
        if(!cache('static')){
            $static = DB::name('config')->where('varname','static')->value('value');
            cache('static',$static);
        }
        $this->assign('static',cache('static'));
    }

    public function index()
    {
        
      $newsres = Db::name('category')->field('a.id,a.catname,a.modelid,w.title,w.keywords,w.description,w.content,w.thumb')->alias('a')->join('article w','a.id = w.catid','RIGHT')->find($id);
       $this->assign('newsres',$newsres);
       
       $productres=db('product')->field('id,shoppic')->select();
      for($i=0;$i<count($productres);$i++){
              $imgdata=array();
              $imgdata=explode(',',$productres[$i]['shoppic']);
        $newid=$productres[$i]['id'];
              $listimgprodu[$newid]="<img src='/public/uploads/".$imgdata[0]."'  style='width:369px;height:270px' >";
        }
       $this->assign('listimgprodu',$listimgprodu);

       $caseres=db('case')->field('id,casepic')->select();
      for($i=0;$i<count($caseres);$i++){
              $imgdata=array();
              $imgdata=explode(',',$caseres[$i]['casepic']);
        $newid=$caseres[$i]['id'];
              $listimglist[$newid]="<img src='/public/uploads/".$imgdata[0]."'   >";
        }
       $this->assign('listimglist',$listimglist);
        return view('./application/index/view/default/index/index.html');
    }

    public function category(){
        $id = input('id'); //分类ID
        $this->assign('id',$id);
        $template = getModInfoById($id,'category_template');
        $catinfo = getCatInfoById($id);
        $catres=db('category');
        $cateid=$catres->where("id=".$id)->find();
        $cateres=$catres->field('id,catname')->where("modelid=1")->select();
        $artres=$catres->field('id,catname')->where("modelid=3")->select();
        $productres=$catres->field('id,catname')->where("modelid=2")->select();
        $this->assign('cateres',$cateres);
        $this->assign('artres',$artres);
        $this->assign('productres',$productres);
        $this->assign('cateid',$cateid);

       $caseres=db('case')->field('id,casepic')->select();
      for($i=0;$i<count($caseres);$i++){
              $imgdata=array();
              $imgdata=explode(',',$caseres[$i]['casepic']);
        $newid=$caseres[$i]['id'];
              $listimglist[$newid]="<img src='/public/uploads/".$imgdata[0]."' >";
        }
       $this->assign('listimglist',$listimglist);
       
       $productres=db('product')->field('id,shoppic')->select();
      for($i=0;$i<count($productres);$i++){
              $imgdata=array();
              $imgdata=explode(',',$productres[$i]['shoppic']);
        $newid=$productres[$i]['id'];
              $listimgprodu[$newid]="<img src='/public/uploads/".$imgdata[0]."'  style='width:369px;height:270px' >";
        }
       $this->assign('listimgprodu',$listimgprodu);
        return view('./application/index/view/default/index/'. $template);
    }

    

    public function info(){
        $id = input('id',0);//ID
        $catid = input('catid',0);//分类ID
        $modelInfo = getModInfoById($catid);
        $info = Db::name($modelInfo['tablename'])->where('id',$id)->find();
        $catinfo = getCatInfoById($catid);
        $p_catname = getCatInfoById($catinfo['parentid'],'catname');

        $this->assign('info',$info);
        $this->assign('catid',$catid);
        $this->assign('catinfo',$catinfo);
        $this->assign('catname',$catinfo['catname']);
        $this->assign('p_catname',$p_catname);
       $caseres=db('case')->field('id,casepic')->find($id);
       $imgdata=explode(',',$caseres['casepic']);
       $show=str_replace('\\','/',$imgdata);
          $this->assign('imgdata',$show); 
       $productres=db('product')->field('id,shoppic')->find($id);
       $imgdataproduct=explode(',',$productres['shoppic']);
       $show=str_replace('\\','/',$imgdataproduct);
          $this->assign('imgdataproduct',$show); 
       $productres=db('product')->field('id,shoppic')->select();
      for($i=0;$i<count($productres);$i++){
              $imgdata=array();
              $imgdata=explode(',',$productres[$i]['shoppic']);
        $newid=$productres[$i]['id'];
              $listimglist[$newid]="<img src='/public/uploads/".$imgdata[0]."'  style='width:369px;height:170px' >";
        }
       $this->assign('listimglist',$listimglist);

       //上一篇
       $arfront=db('article')->where("id>".$id)->order('id asc')->limit('1')->find();

       $this->assign('arfront',$arfront);
       //下一篇
       $arafter=db('article')->where("id<".$id)->order('id desc')->limit('1')->find();
       $this->assign('arafter',$arafter);



       //上一篇
       $front=db('product')->where("id>".$id)->order('id asc')->limit('1')->find();

       $this->assign('front',$front);
       //下一篇
       $after=db('product')->where("id<".$id)->order('id desc')->limit('1')->find();
       $this->assign('after',$after);

       $news=Db::table('zhandao_product')->where('id',$id)->setInc('views');

       $news=Db::table('zhandao_article')->where('id',$id)->setInc('views');
        return view('./application/index/view/default/index/' . $modelInfo['show_template']);
    }

    

}
