<?php
/** App会员控制器
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author ziyi <ziyi@206207038.com>
 */

namespace Pad\Controller;
use Common\Controller\HanchaoController;
class ShopsController extends BaseController {
    public function _initialize(){
        parent::_initialize ();
    }
    public function index(){
		$result = array();
		$shop_id = I('shop_id');
		$sid = I('sid');
		if(empty($shop_id) || empty($sid)){
			$result['status'] = 2;
			$result['msg']='门店ID不能为空';
			$this->ajaxReturn($result);
		}
		$result=D('App/Shops')->shops_info($shop_id,$sid);
		$this->ajaxReturn($result);
    }
	public function get_pid(){
		$result = array();
		$result['status'] = 1;
		$sid = I('sid');
		if(empty($sid)){
			$result['status'] = 2;
			$result['msg']='SID不能为空';
			$this->ajaxReturn($result);
		}
		$result=D('App/Shops')->pid_info($sid);
		$this->ajaxReturn($result);
	}
	//获取门店列表
	public function shops_list(){
		$result = $map = $data = array();
		$result['status'] = 1;
		$is_root = I('is_root');
		if($is_root != 1){
			$result['status'] = 3;
			$result['msg']='没有该权限';
			$this->ajaxReturn($result);
		}
		$result=D('App/Shops')->shops_lists();
		$this->ajaxReturn($result);
	}
	//添加或编辑门店
	public function save_shops(){
		$result=array();
		$postdata = I('post.');
		if(empty($postdata)){
			$result['status'] = 2;
			$result['msg']='参数不能为空';
			$this->ajaxReturn($result);
		}
		
		$postdata['pic_list']=serialize(json_decode(htmlspecialchars_decode($postdata['pic_list']),true));
		
		if(isset($postdata['shop_id']) && $postdata['shop_id'] > 0){
			$result=D('App/Shops')->edit_shops($postdata);
			$this->ajaxReturn($result);
		}
		
		if($postdata['is_root'] != 1){
			$result['status'] = 3;
			$result['msg']='没有该权限';
			$this->ajaxReturn($result);
		}
		if(isset($postdata['is_first']) && $postdata['is_first'] == 1){
			if($postdata['root_parent_id'] > 0){
				$ret = D('App/ShopLogin')->addparentid($postdata['mid']);
			}else{
				if(isset($postdata['invite_code'])){
					$ret = D('App/ShopLogin')->addparentid($postdata['mid'],1,$postdata['invite_code']);
				}else{
					$result['status'] = 3;
					$result['msg']='激活码必填';
					$this->ajaxReturn($result);
				}
			}
			if($ret['status'] != 1){
				$result['status'] = 3;
				$result['msg'] = $ret['info'];
				$this->ajaxReturn($result);
			}
		}
		$result=D('App/Shops')->add_shops($postdata);
		$result['shop_num'] = $shop_num+1;
		$result['shop_max_num'] = $shop_max_num;
		$this->ajaxReturn($result);
    }
    // 判断微信小程序开启权限
    public function check(){
    	$check = check_use_auth('WeChatMarket');
    	if($check['status'] == 1){
    		$return['status'] = 1;
    	}else{
    		$return['status'] = 3;
    		$return['msg'] = '您还没有开通微信营销,请到应用市场开通!';
    	}
    	$return['data'] = $check;
    	$this->ajaxReturn($return);
    }
	// 切换门店
	public function shops_change(){
		$return=array();
		$postdata = I('post.');
		if(empty($postdata)){
			$return['status'] = 3;
			$return['msg']='参数不能为空';
			$this->ajaxReturn($return);
		}
		$map=array();
		$map['shop_id'] = $postdata['shop_id'];
		$map['pid'] = 0;
		$map['status'] = 1;
		$map['is_delete'] = 0;
		$shop_info = M('ShopsAccount')->where($map)->field('id,password')->find();
		if($shop_info){
			$return['status'] = 1;
			$return['sid'] = $shop_info['id'];
			$return['token'] = $shop_info['password'];
		}else{
			$return['status'] = 3;
			$return['msg']='门店id错误';
		}
    	$this->ajaxReturn($return);
	}
	/**
    * 生成银联永久二维码
    */
    public function get_chinaums_img(){
		$return = array('status'=>3,'msg'=>'');
		$shop_id = I('shop_id');
		if(empty($shop_id)){
			$return['msg'] = '参数错误';
			$this->ajaxReturn($return);
		}
    	$is_check=I('is_check',0,'intval');
        $docheck=$is_check?0:1;
        $ret=deccacode($shop_id,$docheck);
		if(empty($ret['status'])){
            $return['msg'] = $ret['msg'];
        }else{
			$return = $ret;
		}
		$this->ajaxReturn($return);
    }
}
?>