<?php
/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 2017/10/31
 * Time: 下午4:11
 */

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

require_once (BASE_ROOT_PATH . '/helper/schema_helper.php');


class webpushControl extends mobileHomeControl
{
    public function __construct()
    {
        parent::__construct();
    }

    public function fcodeOp()
    {
        $fc_id = intval($_GET['fc_id']);
        return self::outsuccess(['tpl' => new tpl_wpush_fcode($fc_id)],"webpush/fcode",'wap');
    }
    public function updateOp()
    {
        return self::outsuccess(['tpl' => new tpl_wpush_update()],"webpush/fcode",'wap');
    }

    public function bonusOp()
    {

    }
}

class tpl_wpush_fcode
{
    private $mFcode;
    public function __construct($fc_id)
    {
        $mod_fcode = Model('goods_fcode');
        $this->mFcode = $mod_fcode->getGoodsFCode(['fc_id' => $fc_id],true);
    }

    private function schema() {
        $common_id = $this->mFcode['goods_commonid'];
        $batch_code = $this->mFcode['batch_code'];
        $mobile = $this->mFcode['mobile'];

        $url = BASE_SITE_URL . "/mobile/index.php?act=fcode&op=index&common_id={$common_id}&batch_code={$batch_code}&mobile={$mobile}";
        $schema = schema_helper::openurl("购物首单礼",$url);
        return $schema;
    }
    private function summary()
    {
        $common_id = intval($this->mFcode['goods_commonid']);
        $helper = new goods_helper(false);
        $gids = commonid_helper::instance()->goods_ids($common_id);
        $ret = $helper->get_spu($common_id,$gids[0],$err);

        $summarys = $ret['summary'];
        foreach ($summarys as $summary)
        {
            if($common_id == $summary['goods_commonid'])
                return $summary;
        }
        return false;
    }

    public function show()
    {
        $schema = $this->schema();
        $summary = $this->summary();

        $price = intval($summary['goods_price'] * 100 + 0.5) / 100;
        $marketprice = intval($summary['goods_marketprice'] * 100 + 0.5) / 100;

        $str = "<div class=\"push_content\">
                    <a href=\"{$schema}\" class=\"schema_link\">
                        <p class=\"push_label\">恭喜您获得新客专享礼</p>
                        <h3 class=\"push_title\">{$summary['goods_mobile_name']}</h3>
                        <div class=\"goods_img\">
                            <img src=\"{$summary['goods_image_url']}\" class=\"img\">
                            <div class=\"label\">
                                <p class=\"sale_price\">{$price}元购</p>
                                <p class=\"original_price\"><span>专柜价{$marketprice}</span></p>
                            </div>
                        </div>
                        <div class=\"btn\"><span class=\"look_btn\">查看详情</span></div>
                    </a>
                </div>";
        return $str;
    }
}

class tpl_wpush_update
{
    public function __construct()
    {
    }
    public function show()
    {
        $head_img = RESOURCE_SITE_URL . "/mobile/push/update/head_img.png";
        echo   "<div class=\"push_content\">
                    <a href=\" \" class=\"schema_link\">
                    <div class=\"head_img\">
                        <img src=\"{$head_img}\" class=\"img\">
                    </div>
                    <div class=\"push_title t_center\">升级到新版本</div>";

        echo       "<div class=\"updata_focus\">";
        $tips = $this->tips();
        $i = 1;
        foreach ($tips as $tip) {
                    echo "<p>{$i}.{$tip}</p>";
            ++$i;
        }

        echo       "</div>
                    <div class=\"btn updata_btn\">
                        <span class=\"look_btn\">立即升级</span>
                    </div>
                    </a>
                </div>";
    }

    private function tips()
    {
        $tips = $GLOBALS['setting_config']['mobile_update_tips'];
        $artips = explode('#',$tips);
        return $artips;
    }
}