<?php

// +-----------------------------------------------------------------------------------

// | Software: [ThinkPHP framework]

// | Version: V4.0

// | Site: http://www.czreg.com

// +-----------------------------------------------------------------------------------

// | Author: 飘叶 <cds425@163.com>

// | Copyright (c) 2012-2015, http://www.czreg.com All Rights Reserved.

// | time:2015.1.12

// | explain:产品类

// +-----------------------------------------------------------------------------------

namespace Home\Controller;

use Think\Controller;

class ProController extends PublicController {

    public function _initialize () {

        parent::_initialize();

        //+--------------------+

        //| 广告输出           |

        //+--------------------+

        $this->ad = M('Ad')->order('id desc')->where(array('title'=>3))->find();

        $this->mainstyle = 3;

    }



    public function index(){

        $type = I('get.type');

        //+--------------------+

        //| 最新产品输出	   |

        //+--------------------+

        $pro=M('Pro');

        $prefix=C('DB_PREFIX');



        $procls = M('Pro_cls')->order('id desc')->select();

        $this->procls = $procls;//产品分类



        $p=empty($_GET['p'])?'1':$_GET['p'];

        if(!empty($type)){
            $list = $pro->field('title,money,timeline,name,img,lable,type,'.$prefix.'pro.id,'.$prefix.'pro_cls.id as clsid')->join('__PRO_CLS__ ON __PRO_CLS__.id = __PRO__.type','LEFT')->where(array('type'=>$type))->order($prefix.'pro.id desc')->page($p.',6')->select();
            $this->list = $list;
            $count=$pro->field('id')->where(array('type'=>$type))->count();
            foreach ($procls as $v) {
                if($v['id']==$type){
                    $clsname = $v['name'];
                }
            }
            $this->clsname = $clsname;
        }elseif(I('get.keyword')!=''){
            //搜索
            $list = $pro->field('title,money,timeline,name,img,lable,type,'.$prefix.'pro.id,'.$prefix.'pro_cls.id as clsid')->join('__PRO_CLS__ ON __PRO_CLS__.id = __PRO__.type','LEFT')->where(array('title'=>array('like','%'.I('get.keyword').'%')))->order($prefix.'pro.id desc')->page($p.',6')->select();
                $this->list = $list;
                $count=$pro->field('id')->where(array('title'=>array('like','%'.I('get.keyword').'%')))->count();
                $this->clsname = '搜索结果';
        }else{
            $list = $pro->field('title,money,timeline,name,img,lable,type,'.$prefix.'pro.id,'.$prefix.'pro_cls.id as clsid')->join('__PRO_CLS__ ON __PRO_CLS__.id = __PRO__.type','LEFT')->order($prefix.'pro.id desc')->page($p.',6')->select();
            $this->list = $list;
        $count=$pro->field('id')->count();
        }
        $Page = new \Think\Page($count,C('PRO'));// 实例化分页类 传入总记录数和每页显示的记录数
        $Page->rollPage=5;//页码每页显示数量
        $show       = $Page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出

        $this->p=$p;

        $this->display();

    }



    //产品查看页

    public function show(){

        $id = I('get.id');

        $procls = M('Pro_cls')->order('id desc')->select();

        $this->procls = $procls;//产品分类

        

        $pro = M('pro')->where('id='.$id)->find();

        $cls = M('Pro_cls')->where('id='.$pro['type'])->find();

        $pro['clsname'] = $cls['name'];

        $this->pro = $pro;

        M('pro')->where('id='.$id)->setInc('readnum',1);

        //上一篇

        $front=M('pro')->field('title,id')->where("id<".$id)->order('id desc')->limit('1')->find();

        $f=!$front?'没有了':'<A href="'.U('show',array('id'=>$front['id'])).'">'.$front['title'].'</A>';

        $this->assign('front',$f);

        //下一篇

        $after=M('pro')->field('title,id')->where("id>".$id)->order('id desc')->limit('1')->find();

        $f=!$after?'没有了':'<A href="'.U('show',array('id'=>$after['id'])).'">'.$after['title'].'</A>';

        $this->assign('after',$f);

        $this->display();

    }



    //产品评论

    public function repro(){

        $data   = I('post.');

        if(check_user_login()===false){

            $this->error('请先登陆');

            exit;

        }

        $savere = M('repro')->add($data);

        if($savere !== false){

            $this->success('评论成功');

        }else{

            $this->error('评论失败');

        }



    }

}