<?php
namespace MpSmi\Controller;

use Think\Controller;

header("Content-Type:text/html;CharSet=UTF-8");
class GalleryController extends BaseController
{
	/**
	 * 商品图库
	 */
	//图库列表
	public function galleryList()
	{    
        $store_id = session("store_id");//商家id
        if($_GET){
            $where['store_id'] = $store_id;
            $name = I("name");
            $map  .= " thumb_name LIKE '%".$name."%' AND";
            if($name){
                $where['thumb_name'] = array('like','%'.$name.'%');
                $this->assign("name",$name);
            }
        }
		$where['store_id'] = $store_id;
        $map .= ' store_id='.$store_id.' ';
        $Model = M("stores_goods");
        $count = $Model->field('id,goods_name,store_id,thumb,thumb_name,thumb_addtime,deleted,gid')
                      ->where($where)
                      ->group("thumb")
                      ->union('SELECT id,goods_name,store_id,thumb,thumb_name,thumb_addtime,deleted,gid FROM am_stores_goods_gallery where'.$map,true)
                      ->select();
        $count = count($count);
        $Page = new \Think\Page($count, 8);//分页显示记录数
        $show = $Page->show();// 分页显示输出   
        $list = $Model->field('id,goods_name,store_id,thumb,thumb_name,thumb_addtime,deleted,gid')
                      ->where($where)
                      ->group('thumb')
                      ->union('SELECT id,goods_name,store_id,thumb,thumb_name,thumb_addtime,deleted,gid FROM am_stores_goods_gallery where'.$map,true)
                      ->select();
        //获取图片属性
        foreach ($list as $key => &$value) {
        	$thumburl = "./Uploads/".$value['thumb'];
        	$thumbsize= getimagesize($thumburl);
        	$filesize = round(filesize($thumburl)/1024,2);
        	$value['thumbsize'] = "尺寸:".$thumbsize[0]."x".$thumbsize[1]."，大小:".$filesize."KB<br />上传时间:".date("Y-m-d H:i:s",$value['thumb_addtime'])."<br />".$value['thumb'];
        	
        	$thumbs = "";	
        	$thumbs = M("stores_goods")->where(array("store_id"=>session("store_id"),"thumb"=>$value['thumb']))->order("id desc")->select();
        	$count  = count($thumbs);
        	if($count > 1){
        		$thumbss .= $thumbs;
        		$value['goods_name'] = "";
        		foreach ($thumbs as $k => $v) {
        			$value['goods_name'] .= "<a href='".U('Shop/shop_add',array('id'=>$v['id']))."'>".$v['goods_name']."</a><br />";
        		}
        	}else{
                $value['goods_name'] = "";
                $value['goods_name'] .= "<a href='".U('Shop/shop_add',array('id'=>$thumbs[0]['id']))."'>".$thumbs[0]['goods_name']."</a><br />";
            }
        }
        //定义变量并显示模板
        $this->assign("categories",$categories);
        $this->assign('page', $show);// 赋值分页输出
        $this->assign("list", $list);
        $this->display();
	}
    //修改缩略图名称
    public function category_update()
    {
        if (IS_POST) {
            $p  = $_POST['p'];    
            $id = $_POST['id'];
            $gid = $_POST['sid'];
            $thumb_name = $_POST['name'];
            if(!$id && $id != 0){
                $this->error("id未获取到");die;
            }
            if($thumb_name){
                if($id == 0){
                    $data['thumb_name'] = $thumb_name;
                    $gallery = M("stores_goods_gallery")->where(array("gid"=>$gid))->save($data);
                }else{
                    $thumb = M("stores_goods")->getFieldById($id,'thumb');
                    $where['store_id'] = session("store_id");
                    $where['thumb']    = $thumb;
                    $Model = M("stores_goods");
                    $list  = $Model->where($where)->select() ;
                    foreach ($list as $key => $value) {
                        M("stores_goods")->where(array("id"=>$value['id']))->setField('thumb_name',$thumb_name);
                    }
                }                     
            }else{
                $this->error("名称不能为空");die;
            }  
            $this->redirect('galleryList',array('p'=>$p));die;
        }
    }
    //删除图库照片
    public function galleryDel()
    {   
        $p  = $_GET['p'];
        $gid = $_GET['gid'];
        if($gid){

            $result = M("stores_goods_gallery")->where(array("gid"=>$gid))->delete();
        }
        $this->redirect('galleryList',array('p'=>$p));die;
    }
}