<?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){
		$shops_info=session('shops_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']=$shops_info['id'];
				$res=M('ShopsCategory')->where($where)->save($data);
			}else{
				$data['shop_id']=$shops_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']=$shops_info['id'];
				$res=M('Nav')->where($where)->save($data);
			}else{
				$data['shop_id']=$shops_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){
		$shops_info=session('shops_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']=$shops_info['id'];
			$res=M('ShopsArticle')->where($where)->save($data);
		}else{
			$data['addtime']=NOW_TIME;
			$data['shop_id']=$shops_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;
	}
	// $is_type 是门店类型,$type 是ShopsOption的选择项对应的id
	public function option($id,$type=1){
		/*if($id){
			$cachename='shops_option';
			$cachedata = S($cachename);
			if(empty($cachedata)){
				$cache = M('ShopsOption')->field(true)->select();
				foreach ($cache as $key => $value) {
					$cachedata[$value['id']] = $value;
				}
				S($cachename,$cachedata,86400);
			}
			return $type==1?$cachedata[$id]['z_shops']:$cachedata[$id]['j_shops'];	
		}*/
	}
	
	/**
	*	商家中心大权限 (注意：不验证后三个)
	*@param int $shop_id 没有返回权限字段数组  有则验证权限 
	*@param  str $model 要验证的字段  有则返回 bool (不验证后三个) 没有返回array
	*/
	public function shop_option($shop_id='',$model=''){
		 $menu=array(
            array('model'=>'shop_controller','name'=>'商家管理中心'),
            array('model'=>'money','name'=>'资金管理'),
            array('model'=>'account','name'=>'员工管理'),
            array('model'=>'goods','name'=>'商品管理'),
            array('model'=>'custom_goods_price','name'=>'商品价格'),// 门店自定义销售价格
            array('model'=>'guide','name'=>'导购设置'),
            array('model'=>'ship','name'=>'配送设置'),
            array('model'=>'promot','name'=>'营销设置'),
            array('model'=>'is_share_user','name'=>'会员信息共享'), //专用字段存储
            array('model'=>'is_hide_phone','name'=>'会员隐私保护'), //专用字段存储
            array('model'=>'fans','name'=>'粉丝查看范围','val'=>array('0'=>'全部粉丝','1'=>'门店粉丝')),
            array('model'=>'is_share_money','name'=>'会员资金共享','val'=>array('0'=>'不共享','1'=>'共享','2'=>'付费共享')), //专用字段存储
           );
		if(!$shop_id){return $menu;};
		unset($menu['is_share_user']);unset($menu['is_hide_phone']);unset($menu['is_share_money']);
		$shop_rule=idtoname($shop_id,'Shops','shop_center_rule');
		$result_arr='';//商店权限数组
		if(!$shop_rule){
			// 没有的就有全部权限
			foreach ($menu as $k => $val) {
				$result_arr[$val['model']]=1;				
			}
		}else{
			$result_arr=json_decode($shop_rule,true);
		}
		// menu 数组中没有的则默认为true
		return $model?(isset($result_arr[$model])?$result_arr[$model]:true):$result_arr;

	}

	// 账号权限
	public function shop_account_option($auth_code){
		$postdata=[];
		$postdata['auth_code']=$auth_code;
        $postdata['auth_appkey']='HCauth'.$auth_code;
		$shops_info=session('shops_info');
        $postdata['sid']=$shops_info['id'];
        $ret=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$postdata);
        return $ret;

	}


	
}
?>