<?php

/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 16/6/29
 * Time: 下午11:01
 */

require_once (BASE_ROOT_PATH . '/helper/util_helper.php');
require_once (BASE_ROOT_PATH . '/helper/url_helper.php');

class member_info
{
    private $member_id;
    private $member_info;

    public function __construct($member_id)
    {
        if(is_array($member_id)) {
            $this->member_info = $member_id;
            $this->member_id = intval($this->member_info['member_id']);
        }
        else {
            $this->member_id = intval($member_id);
            $mod_member = Model('member');
            $this->member_info = $mod_member->getMemberInfoByID($this->member_id);
            if(empty($this->member_info)) {
                throw new Exception("该用户不存在~", errcode::ErrMemberNotExist);
            }
        }
    }

    public function member_id()
    {
        return $this->member_id;
    }
    public function enmember_id()
    {
        $mid = urlencode(util::encrypt_data($this->member_id));
        return $mid;
    }

    public function nickname()
    {
        if(isset($this->member_info['member_nickname']) && !empty($this->member_info['member_nickname'])) {
            return $this->member_info['member_nickname'];
        }

        if(isset($this->member_info['member_truename']) && !empty($this->member_info['member_truename'])) {
            return $this->member_info['member_truename'];
        }

        if(isset($this->member_info['member_mobile']) && !empty($this->member_info['member_mobile']))
        {
            $mobile = $this->member_info['member_mobile'];
            if(strlen($mobile) == 11) {
                return substr_replace($mobile, '****', 3, 4);
            }
        }

        return '';
    }
    public function mobile() {
        return $this->member_info['member_mobile'];
    }
    public function truename() {
        return $this->member_info['member_truename'];
    }
    public function sex() {
        $sex = intval($this->member_info['member_sex']);
        return $sex;
    }

    public function is_man() {
        return (intval($this->member_info['member_sex']) == 1);
    }
    public function avatar()
    {
        if(is_null($this->member_info['member_avatar']) || empty($this->member_info['member_avatar']))
        {
            if($this->is_man()) {
                $img = 'male.png';
            } else {
                $img = 'female.png';
            }
            return RESOURCE_SITE_URL . '/mobile/defimg/' . $img;
        }
        else
        {
            $path = $this->member_info['member_avatar'];
            if(strncasecmp($path,"http://",strlen("http://")) == 0) {
                $url = $path;
            } else {
                $url = UPLOAD_SITE_URL . "/shop/avatar{$path}";
            }

            return $url;
        }
    }
    public function available_bonus()
    {
        if(is_null($this->member_info['available_bonus']) || empty($this->member_info['available_bonus'])) {
            return 0.00;
        } else {
            return doubleval($this->member_info['available_bonus']);
        }
    }
    public function discount_self()
    {
        if(is_null($this->member_info['discount_self_amount']) || empty($this->member_info['discount_self_amount'])) {
            return 0.00;
        } else {
            return doubleval($this->member_info['discount_self_amount']);
        }
    }
    public function invitees() {
        return intval($this->member_info['invitees']);
    }
    public function reward_amount() {
        return $this->member_info['reward_amount'];
    }
    public function filter()
    {
        $info['member_id'] = $this->member_id();
        $info['is_man'] = $this->is_man();
        $info['nickname'] = $this->nickname();
        $info['avatar'] = $this->avatar();
        $info['available_bonus'] = $this->available_bonus();
        $info['discount_self']   = $this->discount_self();
        $info['member_index'] = url_helper::member_index($info['member_id']);
        $info['enmember_id'] = $this->enmember_id();

        return $info;
    }

    public function reg_time() {
        return intval($this->member_info['member_time']);
    }
    public function order_num() {
        return intval($this->member_info['order_num']);
    }
    public function first_present() {
        return intval($this->member_info['first_present']);
    }

    public function client_type()
    {
        $client_type = intval($this->member_info['client_type']);
        return $client_type;
    }

    public function inviter_id() {
        return intval($this->member_info['inviter_id']);
    }

    public function can_sms() {
        $logtm = intval($this->member_info['member_login_time']);
        return ($logtm > (time() - 180 * 86400));
    }

    public function wxauthored() {
        $unionid = $this->member_info['member_wxunionid'];
        return (!empty($unionid));
    }
    public function rewared_inviter() {
        return intval($this->member_info['rewared_inviter']) == 1;
    }
    public function is_app() {
        return intval($this->member_info['client_type']) > 0;
    }
}