<?php
namespace app\wap\controller;
use think\Controller;
use think\Db;
use app\xmziqiangzheadmin\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()
    {
        

       
        $category = Db::name('category')->where('modelid',1)->order('listorder')->select();
        foreach ($category as $key => $value) {
            $category[$key]['article'] = Db::name('article')->where('catid',$value['id'])->field('id,catid,title,description,thumb,inputtime')->order('listorder desc')->limit(6)->select();
        }
       $this->assign('category',$category);
       
       $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='height:160px' >";
        }
       $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]."' width='100%' >";
        }
       $this->assign('listimglist',$listimglist);
        return view('./application/wap/view/default/index/index.html');
    }

    public function category(){
        $id = input('id'); //分类ID
        $this->assign('id',$id);
        $template = getModInfoById($id,'category_template');
        $pahotoimg=db('advertisement')->field('id,advename,advepic,advedesc,advewz,sort,adveurl')->order('sort desc')->select();
       $this->assign('pahotoimg', $pahotoimg);
       $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/wap/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);
       $articleres=db('article')->field('id,tags')->find($id);
       $imgdataart=explode(',',$articleres['tags']);
       $this->assign('imgdataart',$imgdataart); 
       $news=Db::table('zhandao_article')->where('id',$id)->setInc('views');
        return view('./application/wap/view/default/index/' . $modelInfo['show_template']);
    }

}
