<?php
/** 供货商营销
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author lss <lss@qq.com>
 */

namespace Shop\Model;
use Think\Model;
class ShopsModel extends Model{
	/**
	  *获取供货商分类、导航
	  *@param string $tablename 表名
	  *@param array $map 
	  *@return array
	  */
	public function cat_nav_list($tablename,$map){
		$list=M($tablename)->where($map)->field(true)->order('sort desc,id desc')->select();
		$str='';
	        	$Tree = new \Common\Vendor\Tree();
	        	$Tree->icon = array('&nbsp;&nbsp;&nbsp;   ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
	        	$Tree->nbsp = '&nbsp;&nbsp;&nbsp;';
	        	if($tablename=='Nav'){
	        		foreach ($list as &$values) {
			            $values['str_manage'] = '<a href="javascript:;" onclick="child_add('.$values['id'].')" class="operate">添加子菜单</a>&nbsp;&nbsp;<a class="operate" href="javascript:;" onclick="cat_nav_edit('.$values['id'].')">编辑</a>&nbsp;&nbsp;<a class="operate" href="javascript:;" onclick="$.showConfirm(\'' . L('SURE_DELETE') . '\',function(){window.location.href=\'' . U('Shops/remove', array('id' => $values['id'])) . '\'},\'hc\')">删除</a>';
			            $values['status'] = $values['status'] == 1 ? "正常" : "<span class='setRed'>禁用</span>";
			            $values['show_status'] = $values['show_status'] == 1 ? "显示" : "<span class='setRed'>隐藏</span>";
			            $values['new_open'] = $values['new_open'] == 1 ? "是" : "<span class='setRed'>否</span>";
			}
			$str = "<tr>
		        	<td>\$id</td>
		        	<td style='text-align:left;padding-left:10px;' title='\$name'>\$spacer\$name</td>
		        	<td>\$sort</td>
		        	<td>\$status</td>
		        	<td>\$show_status</td>
		        	<td>\$new_open</td>
		        	<td>\$str_manage</td>
		        	</tr>";
	        	}elseif($tablename=='ShopsCategory'){
	        		foreach ($list as &$values) {
	        			if($values['pid']!=0){
	        				$values['str_manage']= '<a onclick="child_add('.$values['id'].',1,1)" class="operate" style="color:#ccc;">' . L('ADD_CHILD_CAT') . '</a>&nbsp;&nbsp;<a onclick="cat_nav_edit('.$values['id'].',1)" class="operate">' . L('EDIT') . '</a>&nbsp;&nbsp;<a class="operate" href="javascript:;" onclick="$.showConfirm(\'' . L('SURE_DELETE') . '\',function(){window.location.href=\'' . geturl('Shops/remove', array('id' => $values['id'],'is_cat'=>1)) . '\'},\'hc\')">' . L('DELETE') . '</a>';	
	        			}else{
	        				$values['str_manage']= '<a onclick="child_add('.$values['id'].',1,0)" class="operate">' . L('ADD_CHILD_CAT') . '</a>&nbsp;&nbsp;<a onclick="cat_nav_edit('.$values['id'].',1)" class="operate">' . L('EDIT') . '</a>&nbsp;&nbsp;<a class="operate" href="javascript:;" onclick="$.showConfirm(\'' . L('SURE_DELETE') . '\',function(){window.location.href=\'' . geturl('Shops/remove', array('id' => $values['id'],'is_cat'=>1)) . '\'},\'hc\')">' . L('DELETE') . '</a>';
	        			}
		            	$values['status'] = '<td onclick="status(' . $values['id'] . ',' . "'status'" . ')" id="status_' . $values['id'] . '" style="text-align:center;"><img src="' . __ROOT__ . '/Application/Admin/View/' . get_default_theme() . '/common/images/status_' . $values['status'] . '.gif" /></td>';
	        		}
	        		$str = "<tr>
		        	<td>\$id</td>
		        	<td>\$spacer\$name</td>
		        	<td>\$sort</td>
		        	\$status
		        	<td>\$str_manage</td>
		        	</tr>";
	        	}
	        	$Tree->init($list);
	        	$data = $Tree->get_tree(0, $str);
        		return $data;
	}
	/**
	  *供货商导航分类的添加修改
	  */
	public function cat_nav_add($postdata){
		$shop_info=session('shop_info');
		if(empty($postdata['name'])){
			$return['status']=0;
			$return['msg']='请填写名称';
			return $return;
		}
		$data['name']=$postdata['name'];
	        	$data['pid']=$postdata['pid'];
	        	$data['status']=$postdata['status'];
	        	$data['sort']=$postdata['sort'];
	        	$where['id']=$postdata['id'];
		if($postdata['is_cat']==1){
			if($postdata['id']>0){
				$where['shop_id']=$shop_info['id'];
				$res=M('ShopsCategory')->where($where)->save($data);
			}else{
				$data['shop_id']=$shop_info['id'];
				$res=M('ShopsCategory')->add($data);
			}
			savecache('ShopsCategory');
		}else{
			$data['navtype']=$postdata['navtype'];
			$data['device']=$postdata['device'];
			$data['new_open']=$postdata['new_open'];
			if($postdata['id']>0){
				$where['shop_id']=$shop_info['id'];
				$res=M('Nav')->where($where)->save($data);
			}else{
				$data['shop_id']=$shop_info['id'];
				$res=M('Nav')->add($data);
			}
			savecache('Nav');
		}
		if($res){
			$return['status']=1;
			$return['msg']='操作成功';
		}else{
			$return['status']=0;
			$return['msg']='此次操作没有进行任何修改';
		}
		return $return;
	}
	/**
	  *供货商动态添加
	  */
	public function dynamic_add($postdata){
		$shop_info=session('shop_info');
		if(empty($postdata['title'])){
			$return['status']=0;
			$return['msg']='标题不能为空';
			return $return;
		}
		$data['title']=$postdata['title'];
		$data['content']=$postdata['content'];
		$data['status']=$postdata['status'];
		$data['sort']=$postdata['sort'];
		if($postdata['id']>0){
			$data['updatetime']=NOW_TIME;
			$where['id']=$postdata['id'];
			$where['shop_id']=$shop_info['id'];
			$res=M('ShopsArticle')->where($where)->save($data);
		}else{
			$data['addtime']=NOW_TIME;
			$data['shop_id']=$shop_info['id'];
			$res=M('ShopsArticle')->add($data);
		}
		if($res){
			$return['status']=1;
			$return['msg']='操作成功';
			$return['url']=$_POST['forward'] ? $_POST['forward'] : U('Shops/dynamic_list');
		}else{
			$return['status']=0;
			$return['msg']='操作失败';
		}
		return $return;
	}
	
}
?>