<?php
/**
 * cms首页
 *
 *
 *
 */

defined('InShopNC') or exit('Access Invalid!');

require_once(BASE_ROOT_PATH . '/helper/goods_helper.php');
require_once(BASE_ROOT_PATH . '/helper/special_helper.php');
require_once(BASE_ROOT_PATH . '/helper/index_tab.php');
require_once(BASE_ROOT_PATH . '/helper/util_helper.php');
require_once(BASE_ROOT_PATH . '/helper/third_author/wxauthor.php');
require_once(BASE_ROOT_PATH . '/mobile/control/special.php');
require_once(BASE_ROOT_PATH . '/helper/third_author/wxauthor.php');
require_once(BASE_ROOT_PATH . '/helper/session_helper.php');
require_once(BASE_ROOT_PATH . '/helper/url_helper.php');


class indexControl extends specialControl
{
    const HomeSpecialID = 0;

    public function __construct()
    {
        parent::__construct();
    }

    public function tabsOp()
    {
        $tabs = index_tab::instance()->tabs();
        return self::outsuccess(array('tabs' => $tabs));
    }

    public function splashOp()
    {
        $sig = $_GET['sig'];
        $ret = $this->pub_special($this->splash_id());
        if(empty($ret['special_list'])) {
            return self::outsuccess(null);
        }
        $block = $ret['special_list'][0];
        if(empty($block) || empty($block['items'])) {
            return self::outsuccess(null);
        }

        $image = $block['items'][0]['image'];
        if(empty($sig) || $sig != md5($image)) {
            return self::outsuccess(array('sig' => md5($image),'url' => $image));
        } else {
            return self::outsuccess(null);
        }
    }

    private function splash_id()
    {
        if(is_publish()) {
            return 144;
        } else {
            return 38;
        }
    }

    public function inoherbOp()
    {
        return self::outsuccess(['direct_uri' => 'http://p.lrlz.com/hfive/inoherb/index.html?2'],"redirect");
    }

    public function memberOp()
    {
        $idol = $_GET['member_id'];
        if(empty($idol)) {
            return self::outerr(errcode::ErrParamter);
        }

        if(session_helper::need_wechat_author()) {
            $author = new thrid_author\wxauthor();
            $url = BASE_SITE_URL . "/mobile/index.php?act=index&op=member&member_id={$idol}";
            $url = $author->enter($url);
            return self::outsuccess(['direct_uri' => $url],"redirect");
        }

        $idol = urldecode($idol);
        $idol = intval(util::decrypt_data($idol));

        if(session_helper::logined())
        {
            $myself = session_helper::memberid();
            $relateion = new \relation\mem_relation($myself);
            $isfriend =  $relateion->is_follower($idol);

            return self::outsuccess(array('info' => new member_info($idol),'is_friend' => $isfriend),"member/index",'wap');
        }
        else
        {
            $type_sn = account_helper::invite_bonus($idol);
            if($type_sn == false) {
                return self::outerr(errcode::ErrBonus,"生成邀请红包失败.");
            }
            else
            {
                $ref_url = url_helper::bonus_open_url($type_sn);
                return self::outsuccess(['direct_uri' => $ref_url],"redirect");
            }
        }
    }
}

class tpl_member
{
    private $mOutput;
    public function __construct($output)
    {
        $this->mOutput = $output;
    }

    public function output_group()
    {
        $minfo = $this->mOutput['info'];
        $amount = $minfo->available_bonus();
        $amount = intval($amount * 100 + 0.5) / 100;
        $discount = $minfo->discount_self();
        $discount = intval($discount * 100 + 0.5) / 100;

        $result = "<div class=\"cell\">
            <span class=\"cell_label\">红包余额</span><span class=\"cell_content\"><span class=\"letter_pro\">$amount</span>元</span>
        </div>
        <div class=\"cell\">
            <span class=\"cell_label\">累计节省</span><span class=\"cell_content\"><span class=\"letter_pro\">$discount</span>元</span>
        </div>";

        if(!session_helper::isapp()) {
            $from = '<div class="cell">
                <span class="cell_label">信息来源</span><span class="cell_content">来自扫一扫</span>
            </div>';
            $result .= $from;
        }
        return $result;
    }

    public function output_btn()
    {
        $is_friend = $this->mOutput['is_friend'];
        if(!session_helper::isapp())
        {

            $result = '<div class="submit">';
            if($is_friend) {
                $result .= '<p class="cell_label">你已经是他的粉丝了</p>';
            } else {
                $result .= '<button type="button" id="btn_add_friend" class="add_friend_btn">关注</button>';
            }
            $result .= '</div>';
        }
        else
        {
            $result =  '<div class="submit">
                            <button type="button" id="btn_thief_bonus" class="add_friend_btn">偷一点</button>
                        </div>
                        <div class="submit">
                            <button type="button" id="btn_send_bonus" class="send_bonus_btn">发红包</button>
                        </div>';
        }

        return $result;
    }
}
