<?php

use Action\AbstractAction;
use Plugin\Helpers;
use LibModels\Web\Product\DetailData;

/**
 * 商品详情的控制器
 */
class DetailController extends AbstractAction
{

    /**
     * 商品详情
     *
     * @param int productId
     * @param int goodsId
     */
    public function indexAction()
    {
        $productId = $this->param('productId');
        if (!is_numeric($productId)) {
            $this->error();
        }
        $goodsId = $this->param('goodsId');
        if (!is_numeric($goodsId)) {
            $this->error();
        }
        $uid = $this->getUid();

        $vipLevel = 0;
        if (isset($this->_vip)) {
            $vipLevel = Helpers::getVipLevel($this->_vip);
        }

        $data = \Product\DetailModel::getBaseInfo($productId, $goodsId, $uid, $vipLevel);
        if (array() === $data) {
            $this->error();
        }
        $data['productDetailPage'] = true;
        
        if (isset($data['goodsName'])) {
            $this->setTitle($data['goodsName']);
        }

        // 渲染模板
        $this->_view->display('index', $data);
    }

    /**
     * 商品详情 (SKN)
     *
     * @param int productSkn
     */
    public function showAction()
    {
        $productSkn = $this->param('productSkn');
        if (!is_numeric($productSkn)) {
            $this->error();
        }
        $uid = $this->getUid();

        $vipLevel = 0;
        if (isset($this->_vip)) {
            $vipLevel = Helpers::getVipLevel($this->_vip);
        }

        $data = \Product\DetailModel::getBaseInfo(null, null, $uid, $vipLevel, $productSkn);
        if (array() === $data) {
            $this->error();
        }
        $data['goodsDetailPage'] = true;
        $data['pageFooter'] = true;

        if (isset($data['goodsName'])) {
            $this->setTitle($data['goodsName']);
        }
        $this->setNavHeader('商品详情');
        // 渲染模板
        $this->_view->display('index', $data);
    }

    /**
     * 尺码描述信息
     */
    public function introAction()
    {
        $productSkn = $this->param('productSkn');
        if (!is_numeric($productSkn)) {
            echo ' ';
        }
        $data = \Product\DetailModel::getSizeInfo($productSkn);
        if (array() === $data) {
            echo ' ';
            exit();
        }
        $this->_view->display('intro', $data);
    }

    /**
     * 购买评价列表
     *
     * @param int productId
     * @param int total
     */
    public function commentsAction()
    {
        $productId = $this->get('product_id', 0);
        $total = $this->get('total');
        if (!is_numeric($total)) {
            $total = 0;
        }
        if ($total) {
            $this->setNavHeader('购买评价(' . $total . ')');
        } else {
            $this->setNavHeader('购买评价');
        }
        $this->setTitle('购买评价');

        $data = array(
            'goodsCommentsPage' => true,
            'pageFooter' => true,
            'comments' => array(
                'list' => \Product\DetailModel::getComments($productId),
            ),
        );

        // 渲染模板
        $this->_view->display('comments', $data);
    }

    /**
     * 购买咨询列表
     *
     * @param int productId
     * @param int total
     */
    public function consultsAction()
    {
        $productId = $this->get('product_id', 0);
        $total = $this->get('total', 0);
        if (!is_numeric($total)) {
            $total = 0;
        }
        if ($total) {
            $this->setNavHeader('购买咨询(' . $total . ')');
        } else {
            $this->setNavHeader('购买咨询');
        }
        $this->setTitle('购买咨询');

        $uid = $this->getUid();
        $consults = \Product\DetailModel::getConsults($uid, $productId);
        $data = array(
            'goodsConsultsPage' => true,
            'pageFooter' => true,
            'consults' => array(
                'list' => $consults
            ),
            'faq' => \Product\DetailModel::getCommonConsults(),
            'showReadMore' => count($consults) > 2,
            'link' => Helpers::url('/product/detail/consultform', array('product_id' => $productId)),
        );

        // 渲染模板
        $this->_view->display('consults', $data);
    }

    /**
     * 我要咨询表单
     */
    public function consultformAction()
    {
        $uid = $this->getUid();
        if (!$uid) {
            $this->go(Helpers::url('/signin.html', array('refer' => $this->server('HTTP_REFERER', '/'))));
        }

        $this->setTitle('我要咨询');
        $this->setNavHeader('我要咨询');

        $productId = $this->get('product_id', 0);

        // 渲染模板
        $this->_view->display('consultform', array(
            'consultformPage' => true,
            'productId' => $productId,
            'formUrl' => Helpers::url('/product/detail/consultsubmit'),
        ));
    }

    /**
     * 添加咨询操作
     *
     * @param int product_id 商品ID
     * @param string content 咨询内容
     * @return json
     */
    public function consultsubmitAction()
    {
        $result = array('code' => 400, 'message' => '请输入咨询内容', 'data' => '');

        if ($this->isAjax()) {
            $uid = $this->getUid();
            $productId = $this->post('product_id', 0);
            $content = $this->post('content');
            $result = DetailData::addConsult($uid, $productId, $content);
        }

        $this->echoJson($result);
    }

    /**
     * 为你优选
     *
     * @param int productSkn 商品SKN号
     * @return json
     */
    public function preferenceAction()
    {
        $result = array();

        if ($this->isAjax()) {
            $productSkn = $this->get('productSkn');
            $brandId = $this->get('brandId');
            $channel = Helpers::getChannelByCookie();
            $result = \Product\DetailModel::getPreference($productSkn, $channel, $brandId);
        }

        if (empty($result)) {
            echo ' ';
        } else {
            $this->_view->display('preference', $result);
        }
    }

}