<?php
/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 2017/4/27
 * Time: 下午5:25
 */

namespace login;

use account_helper;
use session;
use session_helper;
use member_info;

require_once (BASE_ROOT_PATH . '/helper/model/member_info.php');

abstract class ILogin
{
    const mobile_login = 1;
    const wxopen_login = 2;
    const fields = '*';

//    const fields = 'member_id,member_mobile,member_wxunionid,member_wxopenid,member_name,member_truename,member_signname,' .
//    'member_nickname,member_avatar,member_sex,member_birthday,member_passwd,member_paypwd,member_email,member_mobile_bind,member_state';

    protected $mMemberId;
    protected $mMemberInfo;
    public function __construct() {
        $this->mMemberId = 0;
    }
    public function memberid() {
        return $this->mMemberId;
    }

    abstract public function ismember();
    abstract public function register($passwd,$user_info,$relay_id = 0);

    public function login()
    {
        if($this->mMemberId <= 0) return false;

        $mod_member = Model('member');
        $this->mMemberInfo = $mod_member->getMemberInfo(['member_id' => $this->mMemberId],ILogin::fields,true);
        if(empty($this->mMemberInfo)) return false;

        if(session_helper::logined()) {
            $mod_member->createSession($this->mMemberInfo);
            session::instance()->set_cookie($_SESSION['MPHPSESSID']);
        }
        else {
            $mod_member->createSession($this->mMemberInfo);
            Model('cart')->mergecart($this->mMemberInfo, $_SESSION['store_id']);
            Model('goods_browse')->mergebrowse($this->mMemberId, $_SESSION['store_id']);
            session::instance()->set_cookie($_SESSION['MPHPSESSID']);
        }
        account_helper::onLogin($this->mMemberId);

        return true;
    }
    public function bind($user_info,$fUseOpenid = true)
    {
        if($this->mMemberId <= 0) return false;
        $mod_member = Model('member');

        $update['member_nickname'] = $user_info['nickname'];
        $update['member_wxunionid'] = $user_info['unionid'];

        if($fUseOpenid) {
            $update['member_wxopenid'] = $user_info['openid'];
        }

        $update['member_avatar'] = $user_info['headimgurl'];
        $update['member_sex'] = intval($user_info['sex']);
        $ret = $mod_member->editMember(array('member_id' => $this->mMemberId), $update);
        return $ret;
    }

    public function unbind()
    {
        if($this->mMemberId <= 0) return false;
        $mod_member = Model('member');

        $tmp_info = $this->mMemberInfo;
        $tmp_info['member_nickname'] = '';
        $tmp_info['member_avatar'] = '';
        $tmp_info['member_sex'] = '';

        $member = new member_info($tmp_info);
        $update['member_nickname']   = $member->nickname();
        $update['member_wxunionid']  = '';
        $update['member_wxopenid']   = '';
        $update['member_avatar']     = $member->avatar();
        $update['member_sex']        = $member->sex();

        $this->mMemberInfo['member_nickname'] = $member->nickname();
        $this->mMemberInfo['member_wxunionid']  = '';
        $this->mMemberInfo['member_wxopenid']   = '';
        $this->mMemberInfo['member_avatar'] = $member->avatar();
        $this->mMemberInfo['member_sex'] = $member->sex();

        $ret = $mod_member->editMember(array('member_id' => $this->mMemberId), $update);
        return $ret;
    }
    public function unbind_mobile($mobile,$member_id)
    {
        if($this->mMemberId <= 0) return false;

        $mod_member = Model('member');
        $ret = $mod_member->delete(['member_id' => $member_id]);
        if($ret)
        {
            $ret = $mod_member->editMember(array('member_id' => $this->mMemberId), ['member_mobile' => $mobile]);
            if($ret && $mod_member->affected_rows > 0) {
                $this->login();
            }
        }

        return $ret;
    }
}