<?php
/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 2017/11/9
 * Time: 上午11:42
 */

require_once(BASE_ROOT_PATH . '/helper/algorithm.php');
require_once(BASE_ROOT_PATH . '/helper/login_helper.php');
require_once(BASE_ROOT_PATH . '/helper/session_helper.php');
require_once(BASE_ROOT_PATH . '/helper/sms_helper.php');
require_once(BASE_ROOT_PATH . '/helper/url_helper.php');
require_once(BASE_ROOT_PATH . '/helper/model_helper.php');
require_once(BASE_ROOT_PATH . '/helper/invite_helper.php');
require_once(BASE_ROOT_PATH . '/helper/third_author/wxauthor.php');
require_once(BASE_ROOT_PATH . '/helper/user_session/storage.php');

class invite_friendControl extends mobileControl
{
    public function __construct()
    {
        parent::__construct();
    }

    public function indexOp()
    {
        $user = intval($_GET['curuser']);
        if(session_helper::need_wechat_author())
        {
            $author = new thrid_author\wxauthor();
            $url = author_url::inviter_url($user);
            $url = $author->enter($url);
            return self::outsuccess(['direct_uri' => $url],"redirect");
        }

        if($user <= 0)
        {
            if(session_helper::logined()) {
                $user = session_helper::memberid();
            }
        }

        if(session_helper::logined()) {
            $type_sn = "";
        } else {
            $type_sn = account_helper::invite_bonus($user);
        }

        if($user > 0) {
            return self::outsuccess(['tpl' => new tpl_invite_friend($user,$type_sn)],"invite_friend/invite",'wap');
        }
        else {
            return self::outerr(errcode::ErrParamter);
        }
    }

    public function call_userOp()
    {
        if(!session_helper::logined()) {
            return self::outerr(errcode::ErrUnLogin,"召唤操作有可能会骚扰用户，只有在登录时才可以操作");
        }
        $user = intval($_GET['user']);
        if($user > 0)
        {
            $mod_member = Model('member');
            $minfo = $mod_member->getMemberInfo(['inviter_id' => session_helper::memberid(),'member_id' => $user]);
            if(!empty($minfo)) {
                $caller = new friend_caller();
                $caller->call($user);
                push_helper::call_friends(session_helper::memberid(),$user);
                return self::outsuccess(null);
            }

        }
        return self::outerr(errcode::ErrParamter);
    }
}

class friend_caller extends user_session\storage
{
    public function __construct()
    {
        parent::__construct();
    }
    public function limit_type()
    {
        return user_session\storage::DAILY_SUPPORT;;
    }
    public function storage_tag()
    {
        return 'friend_caller';
    }
    public function called($user) {
        return $this->base_supported($user);
    }
    public function call($user) {
        parent::base_support($user);
    }
}

class tpl_invite_friend
{
    private $mUser;
    private $mRewardedInvites;
    private $mRewardingInvites;

    private $mUserInfo;
    private $mShareInfo;
    private $mTypesn;
    private $mMineInfo;
    private $mRank;
    private $mMine;
    private $mCaller;

    public function __construct($user,$type_sn)
    {
        $this->mUser = $user;
        $this->mRewardedInvites = [];
        $this->mRewardingInvites = [];

        $this->init($user);
        $this->mShareInfo = invite_helper::share_info(session_helper::share_id());
        $this->mTypesn = $type_sn;
        $this->mMine = (session_helper::memberid() == $user);
        $this->mCaller = new friend_caller();
    }

    private function init($user)
    {
        if($user <= 0) return false;
        $mod_member = Model('member');
        $items = $mod_member->getMemberList(['member_id|inviter_id' => ['_multi' => true,$user,$user]],'*', 0, 'reward_amount desc');

        $cur_user = session_helper::memberid();

        $index = 0;
        foreach ($items as $item)
        {
            $user_info = new member_info($item);
            $uid = $user_info->member_id();
            if($uid == $user) {
                $this->mUserInfo = $user_info;
            }
            elseif ($uid == $cur_user) {
                $this->mMineInfo = $user_info;
                $this->mRank = $index + 1;
            }
            else
            {
                if($user_info->rewared_inviter()) {
                    $this->mRewardedInvites[] = $user_info;
                } else {
                    $this->mRewardingInvites[] = $user_info;
                }
            }
            $index++;
        }
    }

    public function show()
    {
        $this->show_relay();
        if(!empty($this->mTypesn)) {
            $this->show_bonus();
        }
        $rewared = $this->show_rewared();
        $rewarding = $this->show_rewarding();
        if($rewared == false && $rewarding == false) {
            $this->show_noinvitee();
        }
        $this->show_prompt();
    }
    private function show_noinvitee()
    {
        echo "<div class=\"bonus_list\">";
        echo "<span class=\"invite_null\">TA尚未邀请好友</span>";
        echo "</div>";
    }

    private function show_relay()
    {
        $minfo = $this->mUserInfo;
        $inviter_id = $minfo->inviter_id();
        $str = "<div class=\"user_msg\">
                    <div class=\"user\">
                        <div class=\"user_img\"><img src=\"{$minfo->avatar()}\"></div>
                        <div class=\"user_name\">{$minfo->nickname()}</div>";
        echo $str;

        if($inviter_id > 0) {
            $url = BASE_SITE_URL . "/mobile/index.php?act=invite_friend&op=index&client_type=wap&curuser={$inviter_id}";
            $inviter = new member_info($inviter_id);
            $str = "<div class=\"invite_user\">
                        <a href=\"{$url}\">
                            <span class=\"label\">邀请者：</span><img src=\"{$inviter->avatar()}\" class=\"invite_img\">
                        </a>
                    </div>";
            echo $str;
        }

        $str =     "</div>
                    <div class=\"get_bonus\">
                        <div class=\"total\"><span class=\"bag_icon\"></span><span class=\"price\">{$minfo->reward_amount()}</span>元</div>
                        <div class=\"bonus_label\">累计获得100%红包</div>
                    </div>
                </div>";
        echo $str;
    }

    private function show_bonus()
    {
        echo "<div class=\"open_bonus_link\">
                    <div class=\"bonus\">
                        <div class=\"bonus_top\"></div>
                        <div class=\"bonus_center\"></div>
                        <div class=\"bonus_bg\">
                            <span id=\"coin\" class=\"open_bonus_btn gold_coin\"></span>
                        </div>
                    </div>
              </div>";
    }

    private function show_rewared()
    {
        if(empty($this->mMineInfo) && empty($this->mRewardedInvites)) {
            return false;
        }

        if(!empty($this->mMineInfo) && $this->mMineInfo->rewared_inviter()) {
            $count = count($this->mRewardedInvites) + 1;
        } else {
            $count = count($this->mRewardedInvites);
        }

        echo "<div class=\"bonus_list\">";
        echo "<div class=\"prompt_label\">
                    <span class=\"title\">已带来收益的好友<span class=\"invite_num\">({$count})</span></span>
              </div>";

        if(!empty($this->mMineInfo) && $this->mMineInfo->rewared_inviter()) {
            $this->show_invitee($this->mMineInfo);
        }
        foreach ($this->mRewardedInvites as $minfo) {
            $this->show_invitee($minfo);
        }
        echo "</div>";

        return true;
    }

    private function show_rewarding()
    {
        if(empty($this->mMineInfo) && empty($this->mRewardingInvites)) {
            return false;
        }

        if(!empty($this->mMineInfo) && $this->mMineInfo->rewared_inviter() == false) {
            $count = count($this->mRewardingInvites) + 1;
        } else {
            $count = count($this->mRewardingInvites);
        }

        echo "<div class=\"bonus_list\">";
        echo "<div class=\"prompt_label\">
                    <span class=\"title\">尚未带来收益的好友<span class=\"invite_num\">({$count})</span></span>
              </div>";

        if(!empty($this->mMineInfo) && $this->mMineInfo->rewared_inviter() == false) {
            $this->show_invitee($this->mMineInfo);
        }
        foreach ($this->mRewardingInvites as $minfo) {
            $this->show_invitee($minfo);
        }
        echo "</div>";

        return true;
    }

    private function show_prompt()
    {
        $str = "<div class=\"prompt\">
                    <div class=\"prompt_label\">
                        <span class=\"title\">提示</span>
                    </div>
                    <div class=\"prompt_list\">
                        <p>1.由于近期发现恶意刷红包用户，为保障体验，在邀请好友之后，需要好友微信授权、注册、下载APP登录后才发邀请奖励。</p>
                        <p>2.为了您的长期收益，我们建议您邀请真实的朋友。</p>
                        <p>3.您可以点击\"召唤TA\",邀请好友回来。(同一用户每天只能召唤一次)</p>
                        <p>4.熊猫有权解散违规邀请的用户，收回已发红包并关闭相关订单。(具体细节查看规则)</p>
                    </div>
                </div>
                <div class=\"briefing_address\"></div>";
        echo $str;
    }

    private function show_invitee($minfo)
    {
        if(empty($minfo)) return;
        $user = $minfo->member_id();
        $called = $this->mCaller->called($user);

        $url = BASE_SITE_URL . "/mobile/index.php?act=invite_friend&op=index&client_type=wap&curuser={$user}";
        $str = "<div class=\"item\">
                        <a href=\"{$url}\">
                            <div class=\"user_img\">
                            <img src=\"{$minfo->avatar()}\">
                            </div>
                            <div class=\"user_name text-overflow\">
                              <p class=\"text-overflow\"> {$minfo->nickname()}</p>
                              <p class=\"invite_msg\">邀请{$minfo->invitees()}人,收益<span class=\"price\">{$minfo->reward_amount()}</span>元</p>
                            </div>";
        if($this->mMine == false)
        {
            $str        .= "<div class=\"look_btn\">
                               <span class=\"arrow\"></span>
                            </div>";
        }
        $str         .= "</a>";

        if($this->mMine)
        {
            if($called) {
                $str .=     "<span class=\"call_btn called\" data-user=\"{$user}\">召唤TA</span>";
            } else {
                $str .=     "<span class=\"call_btn\" data-user=\"{$user}\">召唤TA</span>";
            }
        }
        $str .= "</div>";
        echo $str;
    }

    public function show_open()
    {
        if(!session_helper::isapp())
        {
            echo "<div class=\"fixed_bottom\">
                        <span class=\"logo\"></span>
                        <div class=\"label\">
                            <p>熊猫美妆</p>
                            <p>下载APP即可得更多奖励</p>
                        </div>
                        <a href=\"#\"  class=\"download_app_btn\" id=\"download_app\">打开APP</a>
                  </div>";
        }
    }

    public function bonus_url() {
        $url = BASE_SITE_URL . "/mobile/index.php?act=bonusex&op=grab&client_type=wap&type_sn={$this->mTypesn}";
        return $url;
    }
    public function share_url()
    {
        return url_helper::inviter_url();
    }
    public function share_title()
    {
        return $this->mShareInfo['title'];
    }
    public function share_image()
    {
        return $this->mShareInfo['img'];;
    }
    public function share_subtitle()
    {
        return $this->mShareInfo['sub_title'];;
    }
}