<?php
/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 2017/6/13
 * Time: 下午3:51
 */

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/third_author/signaturer.php');
require_once(BASE_ROOT_PATH . '/helper/predeposit_helper.php');
require_once(BASE_ROOT_PATH . '/helper/url_helper.php');

class mshopControl extends specialControl
{
    public function __construct() {
        parent::__construct();
    }

    public function indexOp()
    {
        $this->topup();

        $url = BASE_SITE_URL . "/mshop/";
        if(session_helper::need_wechat_author())
        {
            $author = new thrid_author\wxauthor();
            $url = $author->enter($url);
        }

        return self::outsuccess(['direct_uri' => $url],"redirect");
    }

    public function authorOp()
    {
        $url = $_GET['url'];

        $msg = "跳转的地址不能为空.";
        if(empty($url)) {
            return self::outerr(errcode::ErrParamter,$msg);
        }
        $url = urldecode($url);
        if(empty($url)) {
            return self::outerr(errcode::ErrParamter,$msg);
        }

        if(session_helper::need_wechat_author())
        {
            $author = new thrid_author\wxauthor();
            $url = $author->enter($url);
            return self::outsuccess(['direct_uri' => $url],"redirect");
        }
        else {
            return self::outsuccess(['direct_uri' => $url],"redirect");
        }
    }

    public function tabsOp()
    {
        return self::outsuccess(['tabs' => [['special_id' => 0,'name' => "首页"]]]);
    }

    public function goodsOp()
    {
        $this->topup();
        $goods_id = intval($_GET['goods_id']);

        if($goods_id <= 0) {
            $url = BASE_SITE_URL . "/mshop";
        }
        else {
            $url = BASE_SITE_URL . "/mshop/goods_detail?goods_id={$goods_id}";
        }

        $relay_id = session_helper::relay_id();
        fcgi_setcookie("relay_id","{$relay_id}",time() + 86400,'/',LOCAL_DOMAIN);


        if(session_helper::need_wechat_author())
        {
            $author = new thrid_author\wxauthor();
            $url = $author->enter($url);
            return self::outsuccess(['direct_uri' => $url],"redirect");
        }
        else {
            return self::outsuccess(['direct_uri' => $url],"redirect");
        }
    }

    public function specialOp()
    {
        $this->topup();
        $special_id = intval($_GET['special_id']);
        $title = $_GET['title'];

        if($special_id <= 0) {
            $url = BASE_SITE_URL . "/mshop";
        }
        else
        {
            if(empty($title)) {
                $title = $this->special_name($special_id);
                $title = util::base64url_encode($title);
            }

            $url = BASE_SITE_URL . "/mshop/special?special_id={$special_id}&is_special=true&title={$title}";
        }

        $relay_id = session_helper::relay_id();
        fcgi_setcookie("relay_id","{$relay_id}",time() + 86400,'/',LOCAL_DOMAIN);


        if(session_helper::need_wechat_author())
        {
            $author = new thrid_author\wxauthor();
            $url = $author->enter($url);
            return self::outsuccess(['direct_uri' => $url],"redirect");
        }
        else {
            return self::outsuccess(['direct_uri' => $url],"redirect");
        }
    }

    private function special_name($special_id)
    {
        $mod = Model('mb_special');
        $special = $mod->getMbSpecialList(['special_id' => $special_id]);
        if(empty($special)) return false;
        return $special[0]['special_desc'];
    }

    public function signurlOp()
    {
        $sign_url = urldecode($_GET['sign_url']);

        if(empty($sign_url)) {
            return self::outerr(errcode::ErrParamter);
        }
        Log::record("signurl={$sign_url}",Log::DEBUG);
        $result = thrid_author\signaturer::instance()->signurl($sign_url);
        if($result == false) {
            return self::outerr(errcode::ErrAuthor);
        } else {
            return self::outsuccess($result);
        }
    }
    public function testOp()
    {
        return self::outsuccess("",'signature');
    }

    public function shareOp()
    {
        $type  = $_GET['type'];
        $valid = intval($_GET['data']);

        if(empty($type) || $valid <= 0) {
            return self::outerr(errcode::ErrParamter);
        }

        $success = false;
        if($type == 'special')
        {
            $url = url_helper::mshop_special($valid);
            $title = $this->special_name($valid);
            if($title != false) {
                $success = true;
                $sub_title = "丽人丽妆 内买商城\n\n点击浏览";
                $img_url = RESOURCE_SITE_URL . "/mobile/defimg/panda.jpeg";
            }
        }
        elseif($type == 'goods')
        {
            $url = url_helper::mshop_goods($valid);
            $goods_info = $this->goods_share($valid);
            if ($goods_info != false)
            {
                $success = true;
                $name = $goods_info['goods_mobile_name'];
                if (session_helper::logined()) {
                    $nick_name = session_helper::nickname();
                    $title = "{$nick_name}向您推荐 {$name}";
                } else {
                    $title = "熊猫美妆向您推荐{$name}";;
                }
                $tip = $goods_info['goods_jingle'];
                if(empty($title)) {
                    $sub_title = "丽人丽妆 内买商城\n\n点击浏览";
                } else {
                    $sub_title = "{$tip}\n\n点击浏览";
                }

                $img_url = $this->img_url($goods_info['goods_image'],$goods_info['store_id']);
            }
        }

        if($success == false) {
            return self::outerr(errcode::ErrParamter);
        } else {
            return self::outsuccess(['url' => $url,'title' => $title,'sub_title' => $sub_title,'img_url' => $img_url]);
        }
    }

    private function goods_share($goods_id)
    {
        $mod_goods = Model('goods');
        $info = $mod_goods->getGoodsInfoByID($goods_id);
        if(empty($info)) {
            return false;
        } else {
            return $info;
        }
    }
    private function img_url($value,$store_id)
    {
        return cthumb($value, 240, $store_id);
    }

    private function topup()
    {
        if(session_helper::logined()) {
            $pred = new predeposit_helper($_SESSION['member_id']);
            $pred->topup_bonus($_SESSION['member_mobile']);
        }
    }
}