<?php
/**
 * @author xiaogg QQ:756663992
 * @copyright 2017
 */
namespace Admin\Controller;
class WxMemberCardController extends BaseController {
	public function index(){
        $this->data_arr();
	}
	//编辑会员券
	public function edit(){
		$id = I('get.id');
		$where['id'] = $id;
		$card = M('WxMemberCard')->where($where)->field(true)->find();
		$where_s['poi_id'] = array('in',$card['location_id_list']);
		$dian = M('Shops')->where($where_s)->field('poi_id,name')->select();
		$this->assign('dian',$dian);
		$this->shop();
		$this->assign('edit',$card);
		$this->display('update');
	}
	/* 得到节点树 */
	private function getcateTree($pid = '',$assign='',$brandid='') {
		$GoodsCategory = getcache('GoodsCategory');	
	   $Tree = new \Common\Vendor\Tree();
       $select_categorys='';
       $treemini=$Tree->treemini($GoodsCategory);
       $select_categorys.= $Tree->treeminioption($treemini,$pid);
	   if($assign)return $select_categorys;
       else $this->assign('select_categorys',$select_categorys);
	   //品牌
	   $brand=M('Brand')->where('status=1')->field('id,brand_name')->select();
	   foreach($brand as $k=>$v){
		   if($brandid == $v['id']){
			   $brand_op.="<option selected='selected' value='".$v['id']."'>".$v['brand_name']."</option>";
			}else{
				$brand_op.="<option value='".$v['id']."'>".$v['brand_name']."</option>";
			}
	 	}
		$this->assign('select_brand',$brand_op); 
	}
	//奖励配置
	public function _before_add(){
		$this->data_arr(1);
	}
	private function data_arr($type=0){
		// 积分		
		$config['WEB_INTEGRAL_RATIO'] = C('WEB_INTEGRAL_RATIO');
		$config['INTGRAL_PAY_RATIO'] = C('INTGRAL_PAY_RATIO');
		$config['LOGIN_GAIN_SELL_INTEGRAL'] = C('LOGIN_GAIN_SELL_INTEGRAL');
		$config['CARD_INTEGRAL'] = C('CARD_INTEGRAL');
		$config['RANK_PROMOTION_INTEGRAL'] = C('RANK_PROMOTION_INTEGRAL');
		$config['SALES_GAIN_SELL_INTEGRAL_RATE'] = C('SALES_GAIN_SELL_INTEGRAL_RATE');
		$config['GRADE_CHOICES'] = C('GRADE_CHOICES');
		$config['RECHARGE_SELL_INTEGRAL_RATE'] = C('RECHARGE_SELL_INTEGRAL_RATE');
		$this->assign($config);
		// 会员等级
		$map['status'] = 1;
		$member_rank = M('MemberRank')->where($map)->field('name,max_points,goods_price')->select();
		$this->assign('member_rank',$member_rank);
		// 会员等级特权
		$manypoints_rule = M('ManypointsRule')->where($map)->field(true)->select();
		$this->assign('manypoints_rule',$manypoints_rule);
		// 会员卡类型
		$res = M('Config')->field('value')->where("name='WX_CARD'")->find();
        $val = unserialize($res['value']);
        if($val['title']){
        	$title = $val['title'];
        	$logo_url = $val['logo_url'];
        }else{
        	$title = 'VIP会员卡';
        	$logo_url = '/images/lo.jpg';
        }
        $this->assign('title',$title);
        $this->assign('logo_url',$logo_url);
		if(empty($type))$this->display('edit');
	}
    //创建会员卡
	public function insert(){
		$postdata=I('post.');
		$postdata['open_card'] = 1;
		$Weixin = new \Common\Vendor\WeixinCard();
		// 添加会员卡
		$add_coupon = $Weixin->add_coupon($postdata);
		if($add_coupon['status'] == 1){
			$url = U('Admin/wx_member_card/add');
			$this->success('添加成功！',$url);
		}else{
			$url = U('Admin/wx_member_card/add');
            $msg=$add_coupon['msg']?$add_coupon['msg']:'添加失败！';
			$this->error($msg,$url);
		}
	}
	public function add_card(){
        $res = M('Config')->field('value')->where("name='WX_CARD'")->find();
        $val = unserialize($res['value']);
		$this->shop();
        if($val['card_id']){
        	if($val['location_id_list']){
	        	$where_s['poi_id'] = array('in',$val['location_id_list']);
	        	$shop = M('Shops')->where($where_s)->field('poi_id,name')->select();
	        	$this->assign('dian',$shop);
	        }
        	$this->assign('edit',$val);
			$this->display('update');
        }else{
        	$this->display();
        }
	}
	public function shop(){
		//$card_data = M('CardData')->cache(true)->field(true)->select();
		//$this->assign('card_data',$card_data);
		$where['status'] = 1;
		$where['poi_id'] = array('neq','0');
		$shopsData = M('Shops')->where($where)->field('poi_id,name')->select();
        $this->assign('shopsData', $shopsData);
		$this->getcateTree();
	}
	// 修改会员卡
	public function update(){
		$postdata = I('post.');
		$postdata['location_id_list'] = implode(',',$postdata['location_id_list']);
		$Weixin = new \Common\Vendor\WeixinCard();
		// 添加会员卡
		$add_coupon = $Weixin->card_update($postdata);
		if($add_coupon['status'] == 1){
			$url = U('Admin/wx_member_card/index');
			$this->success('编辑成功！',$url);
		}else{
			$url = U('Admin/wx_member_card/add_card');
			$this->error('编辑失败！',$url);
		}
	}
    
	public function detail(){
	   $res = M('Config')->field('value')->where("name='WX_CARD'")->find();
	   $val = unserialize($res['value']);
       
	   $Weixin = new \Common\Vendor\WeixinCard();
       $postdata['card_id']=$val['card_id'];
       $add_coupon = $Weixin->get_card_info($postdata);
       if($add_coupon['status'] == 1){
            $this->assign($add_coupon['data']['member_card']); 
			$this->display();
		}else{
			$url = U('Admin/wx_member_card/index');
			$this->error('调取会员卡信息失败',$url);
		}
	}
}
?>