<?php

/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 16/8/24
 * Time: 下午10:49
 */

require_once (BASE_ROOT_PATH . "/helper/goods_helper.php");
require_once (BASE_ROOT_PATH . "/helper/activity_helper.php");
require_once (BASE_ROOT_PATH . '/helper/special_helper.php');
require_once (BASE_ROOT_PATH . '/helper/util_helper.php');
require_once (BASE_ROOT_PATH . '/helper/user_session/fcode.php');

class goods_commonControl extends mobileControl
{
    const pic_path = '/data/upload/shop/member';

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

    public function indexOp()
    {
        $common_id = intval($_GET['goods_commonid']);
        $goods_id  = intval($_GET['goods_id']);

        if($common_id <= 0) {
            $common_id = commonid_helper::instance()->common_id($goods_id);
        }

        if($goods_id > 0 && $common_id > 0)
        {
            if(!empty($_SESSION['member_id'])) {
                Model('goods_browse')->addViewedGoods($goods_id,$_SESSION['member_id'],$_SESSION['store_id']);
            }

            $helper = new goods_helper(false);
            $ret = $helper->get_spu($common_id,$goods_id,$err);
            if($ret == false) {
                return self::outerr($err['code'],$err['msg']);
            }
            else
            {
                $mod_eva = Model('evaluate_goods');
                $items = $mod_eva->getGoodsCommonEvaluateList($common_id,1,$this->page_size());

                if(empty($items)) {
                    $ret['comment'] = null;
                } else {
                    $comments = $this->format_comment($items);
                    $ret['comment'] = $comments[0];
                }
                return self::outsuccess($ret,"shop/goods_detail");
            }
        }
        else {
            return self::outerr(errcode::ErrParamter,"请输入正确的参数.");
        }
    }

    //商品评论
    private function format_image($member_id,$image)
    {
        if(empty($image)) return array();
        $images = explode(',',$image);
        if(empty($images)) {
            return array();
        }

        $result = [];
        foreach ($images as $img) {
            $path = BASE_SITE_URL . self::pic_path . "/{$member_id}/" . $img;
            $result[] = $path;
        }

        return $result;
    }

    private function format_comment($items)
    {
        $comments = [];
        foreach ($items as $item)
        {
            //$val = [];
            $val['geval_id'] = intval($item['geval_id']);
            $val['common_id'] = intval($item['geval_commonid']);
            $val['goods_id'] = intval($item['geval_goodsid']);
            $val['goods_name'] = $item['geval_goodsname'];
            $val['scores'] = intval($item['geval_scores']);
            $val['anonymous'] = empty($item['geval_isanonymous']) ? 0 : 1;
            $val['content'] = empty($item['geval_content']) ? "" : $item['geval_content'];
            $val['addtime'] = intval($item['geval_addtime']);
            $val['storeid'] = intval($item['geval_storeid']);
            $val['goods_image'] = util::goods_thumb_image($item['geval_goodsimage'],$val['storeid']);
            $val['storename'] = $item['geval_storename'];
            $member_id = intval($item['geval_frommemberid']);
            $val['explain'] = $item['geval_explain'];
            $val['images'] = $this->format_image($member_id,$item['geval_image']);

            $val['member_avatar'] = util::member_avatar($item['member_avatar']);
            $val['goods_spec'] = util::goods_spec($item['goods_spec']);
            $val['nickname'] = util::nickname($item['member_nickname'],$item['member_mobile'],$item['frommembername']);

            $comments[] = $val;
        }

        return $comments;
    }

    public function detailOp()
    {
        $commonid = 0;
        $goods_id = intval($_GET['goods_id']);
        if (empty($_GET['goods_id']) || $goods_id <= 0) {
            if (!empty($_GET['goods_commonid'])) {
                $commonid = intval($_GET['goods_commonid']);
            }
        } else {
            $info = Model('goods')->getGoodsInfoByID($goods_id, 'goods_commonid');
            $commonid = intval($info['goods_commonid']);
        }

        if ($commonid > 0) {
            $items = Model()->table('goods_common')->field('goods_body')->where(array('goods_commonid' => $commonid))->select();
            if (!empty($items) && count($items) > 0) {
                Tpl::output('goods_body', $items[0]['goods_body']);
            }
        }
        Tpl::showpage('goods_detail');
    }

    public function detail_ajaxOp()
    {
        $commonid = 0;
        $goods_id = intval($_GET['goods_id']);
        if (empty($_GET['goods_id']) || $goods_id <= 0) {
            if (!empty($_GET['goods_commonid'])) {
                $commonid = intval($_GET['goods_commonid']);
            }
        } else {
            $info = Model('goods')->getGoodsInfoByID($goods_id, 'goods_commonid');
            $commonid = intval($info['goods_commonid']);
        }

        if ($commonid > 0) {
            $items = Model()->table('goods_common')->field('goods_body')->where(array('goods_commonid' => $commonid))->select();
            if (!empty($items) && count($items) > 0) {
                return self::outsuccess(['body' => $items[0]['goods_body']]);
            }
        }

        return self::outsuccess(null);
    }

    public function commentsOp()
    {
        $common_id = intval($_GET['goods_commonid']);
        if($common_id <= 0) {
            return self::outerr(errcode::ErrParamter,"参数信息错误");
        }

        $mod_eva = Model('evaluate_goods');
        $items = $mod_eva->getGoodsCommonEvaluateList($common_id,0,$this->page_size());
        $page_count = $mod_eva->gettotalpage();
        $result = $this->format_comment($items);

        return self::outsuccess(array('comments' => $result,'mobile_page' =>mobile_page($page_count)));
    }
}

class tpl_detail
{
    private $out_put;
    private $common_info;
    private $summary;

    public function __construct($out_put)
    {
        $this->out_put = $out_put;
        $this->common_info = $this->out_put['common_info'];
        $this->summary = $this->out_put['summary'];
    }

    public function ads_baner()
    {
        $images = $this->common_info['images'];

        if(!is_array($images)) return;

        foreach ($images as $val)
        {
            echo "<div class=\"swiper-slide\"><img src=\"{$val}\"></div>";
        }
    }
    public function search_show_id() {
        $show_goods = $this->common_info['show_goods'];
        return $show_goods;
    }
    public function show_summary($goods_id)
    {
        $summ = $this->summary;
        $arr['goods_id']=$goods_id;
        $goods_key = 0;
        foreach ($summ as $key => $su) {
            if (array_intersect_key($su, $arr)) {
                $goods_key = $key;
            }
        }
        $goods_info = $summ[$goods_key];

        echo "<div class=\"content-padded\">";
        echo  "<div class=\"products_name\" id=\"products_name\">{$goods_info['goods_mobile_name']}</div>
                <div class=\"action\">{$goods_info['goods_jingle']}</div>
                <div class=\"price_stock\">
                    <span class=\"this_price t\">¥{$goods_info['goods_price']}</span>
                    <span class=\"old_price o\">{$goods_info['goods_market_price']}</span>
                    <span class=\"stock\">库存{$goods_info['goods_storage']}件 / 已售{$goods_info['goods_salenum']}件</span>
                </div>
                <div class=\"item_size_box\">
                    <div class=\"item_title\">选择{$this->common_info['spec_name']}</div>
                    <div class=\"item-size\">";

                    if ($goods_info['goods_storage'] > 0)
                    {
                        echo "<a href=\"javascript:void(0)\" class=\"active\" data-goods-id=\"$goods_id\">{$goods_info['goods_spec']}</a>";
                    }
                    unset($summ[$goods_key]);
                    if(isset($summ) && is_array($summ))
                    {
                        foreach($summ as $su)
                        {
                            $goods_spec = $su['goods_spec'];
                            $goods_id = $su['goods_id'];

                            if ($su['goods_storage'] > 0) {
                                echo "<a href=\"javascript:void(0)\"  data-goods-id=\"$goods_id\">{$goods_spec}</a>";
                            } else {
                                echo "<a href=\"javascript:void(0)\" class=\"null\" data-goods-id=\"$goods_id\">{$goods_spec}</a>";
                            }
                        }
                    }

                        
        echo        "</div>
                    <span class=\"icon icon-right pull-right\"></span>
                </div>
            </div>";
    }

    public function show_pop($goods_id)
    {
        $summ = $this->summary;
        $arr['goods_id']=$goods_id;
        $goods_key = 0;
        foreach ($summ as $key => $su) {
            if (array_intersect_key($su, $arr)) {
                $goods_key = $key;
            }
        }
        $goods_info = $summ[$goods_key];
        echo " <ul>
                <li class=\"item-content products_content\">
                    <div class=\"item-inner row\">
                        <div class=\"col-33\"></div>
                        <div class=\"col-60\">
                            <div>
                                <span class=\"this_price t\">¥{$goods_info['goods_price']}</span>
                            </div>
                            <div>
                                <span class=\"stock\">库存{$goods_info['goods_storage']}件 / 已售{$goods_info['goods_salenum']}件</span>
                            </div>
                        </div>
                    </div>
                </li>
                <li class=\"item-content\">
                    <div class=\"item-title\">{$this->common_info['spec_name']}</div>
                </li>
                <li class=\"color_check_box specs\">
                    <div class=\"specs_box\">";

                if ($goods_info['goods_storage'] > 0)
                {
                    echo "<a href=\"javascript:void(0)\" class=\"active\" data-goods-id=\"{$goods_id}\">{$goods_info['goods_spec']}</a>";
                }
                unset($summ[$goods_key]);
                if(isset($summ) && is_array($summ))
                {
                    foreach($summ as $su) {
                        $goods_spec = $su['goods_spec'];
                        $goods_id = $su['goods_id'];
                        if ($su['goods_storage'] > 0) {
                            echo "<a href=\"javascript:void(0)\"  data-goods-id=\"$goods_id\">{$goods_spec}</a>";
                        } else {
                            echo "<a href=\"javascript:void(0)\" class=\"null\" data-goods-id=\"$goods_id\">{$goods_spec}</a>";
                        }
                    }
                }
        echo"            </div>
                </li>
                <li class=\"item-content number\">
                    <div class=\"item-inner\">
                        <div class=\"item-title\">数量</div>
                        <div class=\"item-after choose_box\"><span class=\"icon_number\" id=\"minus\">-</span><span
                                id=\"goods_storage\">1</span><span class=\"icon_number\" id=\"add\">+</span></div>
                    </div>
                </li>
            </ul>";
    }
    public function show_attrs()
    {
        $attrs = $this->common_info['attrs'];
        if(isset($attrs) && is_array($attrs))
        {
            foreach ($attrs as $val)
            {
                $name = $val['name'];
                $value = $val['value'];

                echo "<li class=\"item-content\">
                        <div class=\"item-inner\">
                            <div class=\"item-title\">{$name}</div>
                            <div class=\"item-after\">{$value}</div>
                        </div>
                    </li>";

            }
        }
    }
}