<?php
/**
 * 会员信息管理
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author lss <lss@qq.com>
 */
namespace Mobile\Controller;
use Think\Controller;
class UserInfoController extends BaseController
{
    public function _initialize()
    {
        parent::_initialize();
        if (!MID) {
            $this->redirect('Mobile/Login/index');
        }
        $map['id']=MID;
        $userinfo=M('Member')->where('id='.MID)->field(true)->find();
        $this->assign($userinfo);
        $constellationData = array('白羊座'=>'白羊座','金牛座'=>'金牛座','双子座'=>'双子座','巨蟹座'=>'巨蟹座','狮子座'=>'狮子座','处女座'=>'处女座','天枰座'=>'天枰座','射手座'=>'射手座','摩羯座'=>'摩羯座','水瓶座'=>'水瓶座','双鱼座'=>'双鱼座','天蝎座'=>'天蝎座');
		$this->assign('constellationData',$constellationData);
        $sexData = array('0'=>'保密','1'=>'男','2'=>'女');
		$this->assign('sexData',$sexData);
    }

    public function head_portrait()
    {
		if($this->m_info['is_tmp']==1&&!$this->m_info['mobile']){
			session('format_url',U('Mobile/UserInfo/head_portrait'));
			$this->redirect('Mobile/Login/perfect');
		}elseif($this->m_info['is_tmp']==1&&$this->m_info['mobile']){
			$this->error('您已经领取过会员卡了！');exit;
		}
		// $is_tmp = idtoname(MID,'Member','is_tmp','id',false);
		// if($is_tmp == 1){
			// $this->error('您还不是会员，请到会员中心领取会员卡完善信息！');
		// }
        $where = $map = $data = $con_where = $con_data = $c_data =  array();
        //基本属性
        $con_where['name'] = 'MEMBERATTRIBUTE';
        $con_have = M("Config")->where($con_where)->field("id,name,value")->find();
        if($con_have){
            $con_data = unserialize($con_have['value']);
            if(empty($con_data) || $con_data['real_name']['is_member']){
                $c_data[1] = $con_data['real_name'];
                $c_data[1]['name'] = "真实姓名";
                $c_data[1]['field'] = "real_name";
                $c_data[1]['type'] = "text";
            }
            if(empty($con_data) || $con_data['birthday']['is_member']){
                $c_data[2] = $con_data['birthday'];
                $c_data[2]['name'] = "生日";
                $c_data[2]['field'] = "birthday";
                $c_data[2]['type'] = "date";
            }
            if(empty($con_data) || $con_data['sex']['is_member']){
                $c_data[3] = $con_data['sex'];
                $c_data[3]['name'] = "性别";
                $c_data[3]['field'] = "sex";
                $c_data[3]['type'] = "radio";
            }
            if(empty($con_data) || $con_data['idcard']['is_member']){
                $c_data[4] = $con_data['idcard'];
                $c_data[4]['name'] = "身份证号";
                $c_data[4]['field'] = "idcard";
                $c_data[4]['type'] = "text";
            }
            if(empty($con_data) || $con_data['address']['is_member']){
                $c_data[5] = $con_data['address'];
                $c_data[5]['name'] = "地区";
                $c_data[5]['field'] = "address";
                $c_data[5]['type'] = "radio";
            }
            $this->assign("jiben_data",$c_data);
        }
        $where['name'] = 'ATTRIBUTE';
        $have = M("Config")->where($where)->field("id,name,value")->find();
        $attr_list = json_decode($have['value'],true);
        $map['uid'] = MID;
        foreach ($attr_list as $key => $value) {
            if($value['is_member']){
                $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'],',');
                }
                // if($value['type'] == 'checkbox' && $value['content']){
                //     $data[$key]['data'] = explode(",",$data[$key]['value']);
                // }
            }
        }unset($key,$value);
        $this->assign('attr_list', $data);
        $this->assign('tplseo_title', '修改资料' . '_' . C('WEB_SITE_TITLE'));
        $this->assign('page_title', '修改资料');
        $this->display();
    }

    /**
     *用户设置页面
     */
    public function member_account()
    {
        $this->assign('tplseo_title', '设置' . '_' . C('WEB_SITE_TITLE'));
        $this->assign('page_title', '设置');
        $this->display();
    }

    /**
     *修改登录密码页面
     */
    public function pass()
    {
		if($this->m_info['is_tmp']==1&&!$this->m_info['mobile']){
			$this->redirect('Mobile/Login/perfect');
		}elseif($this->m_info['is_tmp']==1&&$this->m_info['mobile']){
			$this->error('您已经领取过会员卡了！');exit;
		}
        $this->assign('tplseo_title', '登录密码' . '_' . C('WEB_SITE_TITLE'));
        $this->assign('page_title', '登录密码');
        $this->display();
    }
    /**
      *修改支付密码页面
      */
    public function security()
    {
		if($this->m_info['is_tmp']==1&&!$this->m_info['mobile']){
			session('format_url',U('Mobile/UserInfo/security'));
			$this->redirect('Mobile/Login/perfect');
		}elseif($this->m_info['is_tmp']==1&&$this->m_info['mobile']){
			$this->error('您已经领取过会员卡了！');exit;
		}
        $this->assign('tplseo_title', '支付密码' . '_' . C('WEB_SITE_TITLE'));
        $this->assign('page_title', '支付密码');
        $this->display();
    }
	/**
      *修改卡支付密码页面
      */
    public function card_security()
    {
		if($this->m_info['is_tmp']==1&&!$this->m_info['mobile']){
			$this->redirect('Mobile/Login/perfect');
		}elseif($this->m_info['is_tmp']==1&&$this->m_info['mobile']){
			$this->error('您已经领取过会员卡了！');exit;
		}
        $this->assign('tplseo_title', '卡支付密码' . '_' . C('WEB_SITE_TITLE'));
        $this->assign('page_title', '卡支付密码');
        $this->display();
    }

    public function feedback()
    {
        $goods_id = I('get.id');
        $order_id = I('get.order_id');
        $this->assign('goods_id', $goods_id);
        $this->assign('order_id', $order_id);
        $this->assign('tplseo_title', '用户反馈' . '_' . C('WEB_SITE_TITLE'));
        $this->assign('page_title', '用户反馈');
        $this->display();
    }

    /**
     *提交投诉、咨询
     */
    public function submit_service()
    {
        $postdata = I('post.');
        $data = D('Member')->submit_service($postdata);
        $this->ajaxReturn($data);
    }

    /**
     *修改会员信息，登录密码，支付密码
     */
    public function updateinfo()
    {
		if($this->m_info['is_tmp']==1&&!$this->m_info['mobile']){
			$this->redirect('Mobile/Login/perfect');
		}elseif($this->m_info['is_tmp']==1&&$this->m_info['mobile']){
			$this->error('您已经领取过会员卡了！');exit;
		}
        if (IS_POST) {
            $postdata = I('param.');
            $data = D('Common/Member')->user_infor($postdata);
			if($postdata['infos'] == 'paypwd'){
				$jump = session('jump_paypass');
				switch ($jump){
					case 1:
					  $data['url'] = U('Mobile/Takeout/order');
					break;  
					case 2:
					  $data['url'] = U('Mobile/Order/index');
					break;
					case 3:
					  $data['url'] = U('Mobile/Order/continue_payhtml');
					break;
                    case 4:
                        $data['url'] = U('Mobile/Index/coupon_pay');
                    break;
					default:
					  $data['url'] = U('Mobile/Member/index');
                    break;
				}
			}
            $this->ajaxReturn($data);
        }
    }

    public function logout()
    {
        $data = D('Common/Login')->logout();
        $this->ajaxReturn($data);
    }
    public function search_attr(){
        $postdata = I("param.");
        $return = array('status'=>0,'msg'=>'');
        if(empty($postdata['id'])){
            $return['msg'] = "参数错误";
            $this->ajaxReturn($return);
        }
        $member_mod = D('Member');
        $info = $member_mod->field(true)->find(MID);
        $where = $map = $info = array();
        $where['name'] = 'ATTRIBUTE';
        $have = M("Config")->where($where)->field("id,name,value")->find();
        $list = json_decode($have['value'],true);
        $map['uid'] = MID;
        $map['aid'] = $postdata['id'];
        $content = M("MemberAttribute")->where($map)->getField("content");
        $content = explode(",",$content);
        $text = explode(",",$list[$postdata['id']]['content']);
        if($text){
            foreach ($text as $key => $value) {
                if($value){
                    $info[$key]['name'] = $value;
                    $info[$key]['is_show'] = 0;
                }
            }unset($key,$value);
            foreach ($content as $key => $value) {
                if($value){
                    $k = array_search($value, $text);
                    if(in_array($value, $text)){
                        $info[$k]['is_show'] = 1;
                    }else{
                        $info[$k]['is_show'] = 0;
                    }
                }
            }unset($key,$value);
        }
        $return['status'] = 1;
        $return['data'] = $info;
        $return['count'] = count($info);
        $this->ajaxReturn($return);
    }
}