<?php namespace Goods\Lib;
use Dever;
use Place;
class Info
{
    # 获取字段
    public function getCol()
    {
        return 'id,name,info,pic,spec_type,cdate';
    }

    # 获取基本信息
    public function getInfo($info)
    {
        # 获取价格
        $where = array('info_id' => $info['id']);
        $sku_id = Dever::input('sku_id');
        if ($sku_id) {
            $where['id'] = $sku_id;
        }
        $sku = Dever::load('sku', 'api')->getPrice($where, $info['spec_type'], 'goods');
        if (!$sku) {
            Dever::error('价格传入错误');
        }
        if (empty($sku['key'])) {
            Dever::error('价格传入错误');
        }
        $info['price'] = $sku['price'];
        $info['sku_name'] = $sku['name'];
        $info['sku_id'] = $sku['id'];
        $info['sku_key'] = $sku['key'];
        if (Dever::input('show_sku')) {
            $result['sku'] = Dever::load('sku', 'api')->getList(array('info_id' => $info['id']), $sku_id, 'goods');
        }
        $info['is_stock'] = 2;
        $stock = Dever::load('stock', 'goods')->yue($info['id'], $info['sku_id']);
        if ($stock !== false) {
            $info['is_stock'] = 1;
            $info['stock'] = $stock;
        }
        return $info;
    }

    public function submit($info, $type = 'price', $state = false)
    {
        $sku_id = Dever::input('sku_id');
        if ($state && !$sku_id) {
            Dever::error('下单失败，请传入价格');
        }
        $result = $info[$type];
        $result['id'] = $info['id'];
        $result['name'] = $info['name'];
        $result['is_stock'] = 2;
        if (isset($info['stock'])) {
            $result['is_stock'] = 1;
            $result['stock'] = $info['stock'];
        }
        if ($result['status'] == 3) {
            $result['text'] = '专享商品，升级会员可购买';
        } else {
            $result['status'] = 2;
            $result['sku'] = Dever::load('sku', 'api')->getList(array('info_id' => $info['id']), $sku_id, 'goods');
            if ($result['sku']['info']) {
                $detail = $result['sku']['info'];
                if (!$detail['pic']) {
                    $detail['pic'] = $info['pic'][0];
                }
                $detail['is_stock'] = 2;
                if (isset($info['stock'])) {
                    $detail['is_stock'] = 1;
                    $detail['stock'] = $info['stock'];
                }
                $detail['cash'] = $result['value'];
                $detail['sku_id'] = $detail['id'];
                $detail['sku_name'] = $detail['name'];
                $detail['id'] = $result['id'];
                $detail['name'] = $result['name'];
                $result['detail'][] = $detail;
            } elseif ($state) {
                Dever::error('下单失败，请传入价格');
            }
        }
        return $result;
    }
}