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

namespace App\Controller;
use Think\Controller;
class MemberController extends BaseController {
    public function _initialize(){
        parent::_initialize ();
        // $this->checkuserauth();
    }
	/**
	 * 首页
	 */
	public function index(){
		$return=array('status'=>1,'msg'=>'');
        $uid=I('uid');
        $token=I('token');
        $map['id']=$uid;
        $info=M("Member")->where($map)->field('money,points,frozen_money')->find();
        $return['data'] = $info;
		$this->ajaxReturn($return);
	}
    public function member_list(){
        $getdata = I("get.");
        $keyword=I('keyword');
        $auth_code='member_list';
        $ret=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$getdata);
        if($ret['status']!=1) {$this->ajaxReturn($ret);}
        $current_id=I('current_id');
        $return = D('Member')->member_list($keyword,$current_id);
        $this->ajaxReturn($return);
    }
    public function member_infor(){
        $uid=I('uid');
		// user_ranks($uid);
		$type=I('type');
        $return = D('Member')->member_infor($uid,$type);
        $this->ajaxReturn($return);
    }
	public function checkmobile(){
        $return=array('status'=>3,'msg'=>'');
        $mobile = I('mobile');
        $id = $this->check_member_mobile($mobile);
        if($id>0){
            $return['msg'] = '该手机已经存在';
        }else{
            $return['status'] = 1;
        }
        $this->ajaxReturn($return);
    }
	function check_member_mobile($mobile,$type=1){
		$map = array();
		if($type == 1){
			$map['mobile'] = array('eq',$mobile);
			$id = M('Member')->where($map)->getField('id');
		}elseif($type == 2){
			$map['mobile'] = array('eq',$mobile);
			$id = M('ShopsAccount')->where($map)->getField('id');
		}
		return $id;
	}
	public function member_grade_add(){
		$return=array('status'=>3,'msg'=>'');
		$postdata = I('post.');
		if($postdata['is_root'] != 1){
			$return['msg']='您没有操作权限';
			$this->ajaxReturn($return);
		}
		unset($postdata['is_root'],$postdata['root_id'],$postdata['root_token']);
		$return = D('Member')->member_grade_add($postdata);
		$this->ajaxReturn($return);
	}
	public function member_add(){
		$return=array('status'=>3,'msg'=>'');
		$postdata = I('post.');
        if($postdata['id']){
            $auth_code='member_list_edit';
        }else{
            $auth_code='member_list_add';
        }

        $return=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$postdata);
        if($return['status']==1) {
            $return = D('Member')->member_add($postdata);
        }
		$this->ajaxReturn($return);
	}
	public function grade_list(){
		$return = D('Member')->grade_list();
        $this->ajaxReturn($return);
	}
	public function del_grade(){
		$return=array('status'=>3,'msg'=>'');
        $id=I('id');
		$is_root = I('is_root');
		if($is_root != 1){
			$return['msg']='您没有操作权限';
			$this->ajaxReturn($return);
		}
		$return = D('Member')->del_grade($id);
        $this->ajaxReturn($return);
	}
	public function del_many_points(){
		$return=array('status'=>3,'msg'=>'');
        $id=I('id');
		$is_root = I('is_root');
		if($is_root != 1){
			$return['msg']='您没有操作权限';
			$this->ajaxReturn($return);
		}
		$return = D('Member')->del_many_points($id);
        $this->ajaxReturn($return);
	}
	public function del_care(){
		$return=array('status'=>3,'msg'=>'');
        $id=I('id');
		$is_root = I('is_root');
		if($is_root != 1){
			$return['msg']='您没有操作权限';
			$this->ajaxReturn($return);
		}
		$return = D('Member')->del_care($id);
        $this->ajaxReturn($return);
	}
    public function get_memberinfo(){
        $id=I('id');
		$shop_id=I('shop_id');
        if($id){
            $auth_code=I("auth_code");
            $return=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$getdata);
            if($return['status']==1) {
                $return = D('Member')->get_memberinfo($id,$shop_id);
            }
        }else{
            $return = D('Member')->getdaogoulist($shop_id);
        }
        $this->ajaxReturn($return);
    }
	public function get_grade(){
        $return=array('status'=>3,'msg'=>'');
        $id=I('id');
		$is_root = I('is_root');
		if($is_root != 1){
			$return['msg']='您没有操作权限';
			$this->ajaxReturn($return);
		}
		$return = D('Member')->get_grade($id);
        $this->ajaxReturn($return);
	}
	public function get_gradeinfo(){
		$return=array('status'=>3,'msg'=>'');
        $id=I('id');
		$is_root = I('is_root');
		if($is_root != 1){
			$return['msg']='您没有操作权限';
			$this->ajaxReturn($return);
		}
		$return['status'] = 1;
		$return['data'] = array('0'=>array('id'=>1,'name'=>'等级1(最低)'),'1'=>array('id'=>2,'name'=>'等级2'),'2'=>array('id'=>3,'name'=>'等级3'),'3'=>array('id'=>4,'name'=>'等级4'),'4'=>array('id'=>5,'name'=>'等级5'),'5'=>array('id'=>6,'name'=>'等级6'),'6'=>array('id'=>7,'name'=>'等级7'),'7'=>array('id'=>8,'name'=>'等级8'),'8'=>array('id'=>9,'name'=>'等级9'),'9'=>array('id'=>10,'name'=>'等级10(最高)'));;
        $this->ajaxReturn($return);
	}
    public function integral_reward(){
        $return = D('Member')->integral_reward();
        $this->ajaxReturn($return);
    }
	public function many_points(){
		$return = D('Member')->many_points();
        $this->ajaxReturn($return);
	}
	public function get_mrlist(){
		$return=array('status'=>3,'msg'=>'');
		$is_root = I('is_root');
		if($is_root != 1){
			$return['msg']='您没有操作权限';
			$this->ajaxReturn($return);
		}
		$return = D('Member')->get_mrlist();
        $this->ajaxReturn($return);
	}
	public function many_points_add(){
		$return=array('status'=>3,'msg'=>'');
		$postdata = I('param.');
		$is_root = $postdata['is_root'];
		if($is_root != 1){
			$return['msg']='您没有操作权限';
			$this->ajaxReturn($return);
		}
		unset($postdata['is_root'],$postdata['root_id'],$postdata['root_token']);
		$return = D('Member')->many_points_add($postdata);
		$this->ajaxReturn($return);
	}
    public function save_reward(){
        $return=array('status'=>3,'msg'=>'');
        $list=I('list');
        $new_list = json_decode(htmlspecialchars_decode($list),true);
        $is_root = I('is_root');
        if($is_root != 1){
            $return['msg']='您没有操作权限';
            $this->ajaxReturn($return);
        }
        if($new_list){
            foreach ($new_list as $key => $value) {
                $new_lists[$key]['id'] = $value['id'];
                $new_lists[$key]['value'] = intval($value['value']);
            }
        }
        $return = D('Member')->save_reward($new_lists);
        $this->ajaxReturn($return);
    }
    public function member_discount(){
        $return=array('status'=>1,'msg'=>'');
        $uid=I('uid');
        $sid=I('sid');
        $token=I('token');
        if(empty($uid)){
            $return['status']=3;
            $return['msg']='请选择会员';
            $this->ajaxReturn($return);
        }
        $map=array();
        $map['id']=$uid;
        $map['status']=1;
        $map['is_tmp']=0;
        $count=M('Member')->where($map)->count();
        if($count){
            $member_info=M('Member')->where($map)->field('id,mobile,nickname,rank_id,usepoints,money,real_name,guide')->find();
            $map=array();
            $map['status']=1;
            $map['id']=$member_info['rank_id'];
            $member_discount=M('MemberRank')->where($map)->getField('goods_price');
            if(empty($member_discount)) $member_discount=0;
            //查询等级特权
            $map = array();
            $rank_limit = "'0,".$member_info['rank_id']."'";
            $map['rank_limit'] = array('IN',$rank_limit);
            $map['status'] = 1;
            $map['consumption_type']=2;
            $order='id desc';
            $info = M('ManypointsRule')->where($map)->order($order)->field(true)->find();
            if($info){
                $return['data']['huodong_min']=$info['money_limit'];
                $return['data']['huodong_num']=$info['give_num'];
            }
            $return['data']['guide']=$member_info['guide'];
            $return['data']['daogou_name']=$member_info['guide']>0?idtoname($member_info['guide'],'ShopsAccount','name','id',false):'';
            $return['data']['discount']=$member_discount;
            $return['data']['mobile']=$member_info['mobile'];
            $return['data']['money']=$member_info['money'];
//            $return['data']['username']=$member_info['real_name'];
            if($member_info['nickname']) {$return['data']['username'] = $member_info['nickname'];}
            elseif($member_info['username']) {$return['data']['username'] = $member_info['username'];}
            elseif($member_info['real_name']) {$return['data']['username'] = $member_info['real_name'];}
            else {$return['data']['username'] = $member_info['mobile'];}
            $return['data']['id']=$member_info['id'];
            $return['data']['usepoints']=$member_info['usepoints'];
            $return['data']['rank_id']=$member_info['rank_id'];
            $return['data']['pay_rate']=C('INTGRAL_PAY_RATIO');
            $return['data']['web_rate']=C('WEB_INTEGRAL_RATIO');
            
            
            
            $shop_id=I('shop_id');
            if($shop_id && ($return['data']['username'] || $return['data']['mobile']) ){
                $is_hide_phone=idtoname($shop_id,'Shops','is_hide_phone');
                if($is_hide_phone==1){
                    if($return['data']['username'] && strlen($return['data']['username'])==11)$return['data']['username']=str_cutmobile($return['data']['username']);
                    if($return['data']['mobile'])$return['data']['mobile']=str_cutmobile($return['data']['mobile']);
                }
            }
        }else{
            $return['status']=3;
            $return['msg']='请确认此会员是否存在';
        }
        //print_r($return);exit;
        $this->ajaxReturn($return);
    }
	public function get_many_point_info(){
        $return=array('status'=>3,'msg'=>'');
        $id=I('id');
		$is_root = I('is_root');
		if($is_root != 1){
			$return['msg']='您没有操作权限';
			$this->ajaxReturn($return);
		}
		$return = D('Member')->get_many_point_info($id);
        $this->ajaxReturn($return);
	}
    public function member_carelist(){
        // $return=array('status'=>3,'msg'=>'');
		$type = I('type');
        $return = D('Member')->member_carelist($type);
        $this->ajaxReturn($return);
    }
    public function get_careinfo(){
        $return = D('Member')->get_careinfo();
        $this->ajaxReturn($return);
    }
    public function add_membercare(){
        $return=array('status'=>3,'msg'=>'');
        $postdata=I('param.');
        $is_root = I('is_root');
        if($is_root != 1){
            $return['msg']='您没有操作权限';
            $this->ajaxReturn($return);
        }
        $return = D('Member')->add_membercare($postdata);
		
        $this->ajaxReturn($return);
    }
    public function check_care(){
        $return=array('status'=>3,'msg'=>'');
        $id = I('id');
        $is_root = I('is_root');
        if($is_root != 1){
            $return['msg']='您没有操作权限';
            $this->ajaxReturn($return);
        }
        $return = D('Member')->check_care($id);
        $this->ajaxReturn($return);
    }
	public function send_membercare(){
		$return=array('status'=>3,'msg'=>'');
		$is_root = I('is_root');
		if($is_root != 1){
            $return['msg']='您没有操作权限';
            $this->ajaxReturn($return);
        }
		$id = I('id');
		$page = I('page');
		$return = send_care($id,$page,10);
		$this->ajaxReturn($return);
	}
	public function change_status(){
		$return=array('status'=>3,'msg'=>'');
        $id = I('id');
		$status = I('status');
        $is_root = I('is_root');
        if($is_root != 1){
            $return['msg']='您没有操作权限';
            $this->ajaxReturn($return);
        }
		$ret = change_status('ManypointsRule',$id,$status);
		if($ret){
			$return['status'] = 1;
		}else{
			$return['msg'] = '操作失败';
		}
		$this->ajaxReturn($return);
	}
	public function get_member(){
		$return=array('status'=>3,'msg'=>'');
        $mobile = I('mobile');
		$return = D('Member')->get_member($mobile);
		$this->ajaxReturn($return);
	}
	public function get_memberpack(){
		$return=array('status'=>3,'msg'=>'');
        $postdata = I('param.');
		$return = D('Member')->get_memberpack($postdata);
		$this->ajaxReturn($return);
	}
	public function sub_recharge(){
		$return=array('status'=>3,'msg'=>'');
		$postdata = I('post.');
		$is_root = $postdata['is_root'];
        if($is_root != 1){
            $return['msg']='您没有操作权限';
            $this->ajaxReturn($return);
        }
		unset($postdata['is_root'],$postdata['root_id'],$postdata['root_token']);
		$return = D('Member')->sub_recharge($postdata);
		$this->ajaxReturn($return);
	}
	public function calculation(){
		$money = I('money');
		if(!$money)$money = 0.00;
		$uid = I('uid');
		$return = calculation($money,$uid);
		$this->ajaxReturn($return);
	}
	public function message_list(){
		$return=array('status'=>0,'msg'=>'');
        $paramdata = I('param.');$list['status']=1;
        $list=D('Member')->message_list($paramdata);
        if($list){$return['status']=1; $return['data'] = $list;}
        else{$return=array('status'=>2,'msg'=>'暂无数据');}
        $this->ajaxReturn($return);
	}
	public function save_config(){
		$return=array('status'=>3,'msg'=>'');
        $FIXED_SIGNATURE = I('FIXED_SIGNATURE');
        $is_root = I('is_root');
        if($is_root != 1){
            $return['msg']='您没有操作权限';
            $this->ajaxReturn($return);
        }
		$save['value'] = $FIXED_SIGNATURE;
		$map['name'] = 'FIXED_SIGNATURE';
		$ret = M('Config')->where($map)->save($save);
		if($ret){
			$return['status']=1;
		}
		$this->ajaxReturn($return);
	}
    public function daogou(){
        $return=array('status'=>3,'msg'=>'');
        $paramdata = I('post.');
        if(!$paramdata['shop_id']){
            $return['msg']='参数不对';
            $this->ajaxReturn($return);
        }
        $return = D('Member')->getdaogoulist($paramdata['shop_id']);
        $this->ajaxReturn($return);
    }
    public function get_memberattr(){
        $id=I('uid');
        $return  = D("Common/Member")->get_memberattrs($id);
        $this->ajaxReturn($return);
    }
    public function get_memberattr_new(){
        $id=I('uid');
        $user_info = D('Member')->get_memberinfo($id);
        $return = array("status"=>0,"msg"=>'');
        //基本属性
        $con_where = $con_data = $data = array();
        $con_where['name'] = 'MEMBERATTRIBUTE';
        $con_have = M("Config")->where($con_where)->field("id,name,value")->find();
        if($con_have || empty($con_have['value'])){
            $con_data = unserialize($con_have['value']);
            if($con_data['real_name']['is_shop'] || empty($con_have['value'])){
                $data[1] = $con_data['real_name'];
                $data[1]['name'] = "真实姓名";
                $data[1]['field'] = "real_name";
                $data[1]['type'] = "text";
            }
			if(empty($con_data['real_name']['is_shop']) && empty($con_data['real_name'])){
				$con_data['real_name']['is_shop'] = 0;
			}
            if($con_data['birthday']['is_shop'] || empty($con_have['value'])){
                $data[2] = $con_data['birthday'];
                $data[2]['name'] = "生日";
                $data[2]['field'] = "birthday";
                $data[2]['type'] = "date";
            }
			if(empty($con_data['birthday']['is_shop']) && empty($con_data['birthday'])){
				$con_data['birthday']['is_shop'] = 0;
			}
            if($con_data['sex']['is_shop'] || empty($con_have['value'])){
                $data[3] = $con_data['sex'];
                $data[3]['name'] = "性别";
                $data[3]['field'] = "sex";
                $data[3]['type'] = "radio";
            }
			if(empty($con_data['sex']['is_shop']) && empty($con_data['sex'])){
				$con_data['sex']['is_shop'] = 0;
			}
            if($con_data['idcard']['is_shop'] || empty($con_have['value'])){
                $data[4] = $con_data['idcard'];
                $data[4]['name'] = "身份证号";
                $data[4]['field'] = "idcard";
                $data[4]['type'] = "text";
            }
			if(empty($con_data['idcard']['is_shop']) && empty($con_data['idcard'])){
				$con_data['idcard']['is_shop'] = 0;
			}
            if($con_data['address']['is_shop'] || empty($con_have['value'])){
                $data[5] = $con_data['address'];
                $data[5]['name'] = "地区";
                $data[5]['field'] = "address";
                $data[5]['type'] = "radio";
            }
			if(empty($con_data['address']['is_shop']) && empty($con_data['address'])){
				$con_data['address']['is_shop'] = 0;
			}
            $con_data['IDcard']['is_shop'] = $con_data['idcard']['is_shop'];
            $return['jiben_data'] = $data;
            $return['con_data'] = $con_data;
        }


        //其他属性
        $where = $map = $data = array();
        $where['name'] = 'ATTRIBUTE';
        $have = M("Config")->where($where)->field("id,name,value")->find();
        $attr_list = json_decode($have['value'],true);
        $map['uid'] = $id;
        foreach ($attr_list as $key => $value) {
            if($value['is_shop']){
                $data[$key] = $value;
                $map['aid'] = $value['id'];
                $data[$key]['value'] = M("MemberAttribute")->where($map)->getField("content");
                if($value['type'] == 'date' && $data[$key]['value']){
                    $data[$key]['value'] = date("Y-m-d",$data[$key]['value']);
                }
                if(($value['type'] == 'radio') && $value['content']){
                    $data[$key]['data'] = explode(",",$value['content']);
                }
                if($value['type'] == 'checkbox' && $value['content']){  //多选
                    $con_data = array_filter(explode(",",$value['content']));   //多有的多选项
                    $vvv = array_filter(explode(",",trim($data[$key]['value'],',')));
                    foreach ($con_data as $cc => $vv) {
                        $data[$key]['data'][$cc]['name'] = $vv;
                        $ss = 0;
                        foreach ($vvv as $zz => $yy) {
                            if(in_array($yy,$con_data) && $yy == $vv){
                                $ss = 1;
                            }
                        }$data[$key]['data'][$cc]['is_have'] = $ss;
                    }
                    $data[$key]['value'] = trim($data[$key]['value'],',');
                }
            }
        }unset($key,$value);
        $label_list = $this->label_list($id);
        if($return['jiben_data']){
            $return['data'] = $data;
            // $return['info'] = $user_info['data'];
            $return['status'] = 1;
            $return['label_list'] = $label_list;
        }else{
            $return['status'] = 3;
            $return['msg'] = $user_info['msg'];
        }
        $this->ajaxReturn($return);
    }
    public function label_list($id){
        $map = array();
        $map['id'] = $id;
        $label_ids = M('Member')->where($map)->getField('label_ids');
        $label_ids_arr = array_filter(explode(',',$label_ids));
        $map = array();$map['pid'] = 40;
        $label_list = M('Type')->where($map)->field('id,name')->select();
        $html = '';
        $name_html = '';
        foreach ($label_list as $key => $value) {
            if(in_array($value['id'], $label_ids_arr)){
                $label_list[$key]['is_on'] = 1;
                $html .= ",".$value['id'];
                $name_html .= ",".$value['name'];
            }else{
                $label_list[$key]['is_on'] = 0;
            }
        }unset($key,$value);
        $return['info'] = $label_list;
        $return['html'] = $html;
        $return['name_html'] = ltrim($name_html,',');
        return $return;
    }
    public function get_memberattrinfo(){
        $postdata = I("param.");
        $return =  D("Common/Member")->memberattrinfo_list($postdata);
        $this->ajaxReturn($return);
    }
    public function add_member_attribute(){
        $postdata = I("param.");
        $return =  D("Common/Member")->addmemberattribute($postdata);
        $this->ajaxReturn($return);
    }
     public function save_labels(){
        $post = I('param.');
        $return =  D("Common/Member")->savelabels($post);
        $this->ajaxReturn($return);
    }
    public function add_label(){
        $post = I('param.');
        $auth_code='member_list_add_label';
        $return=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$post);
        if($return['status']==1) {$return =  D("Common/Member")->addlabel($post);}
        $this->ajaxReturn($return);
    }
	public function member_bind(){
		$return = array();
		$post = I('param.');
        $return =  D("Member")->member_bind($post);
        $this->ajaxReturn($return);
	}
	public function member_bind_add(){
		$return = array();
		$post = I('param.');
		$auth_code='main-face_recognition';
		$return=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$post);
		if($return['status']==1) {
			$return =  D("Member")->member_bind_add($post);
		}
        $this->ajaxReturn($return);
	}
}