<?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');
        $info['price'] = $sku['price'];
        $info['sku_name'] = $sku['name'];
        $info['sku_id'] = $sku['id'];
        if (Dever::input('show_sku')) {
            $result['sku'] = Dever::load('sku', 'api')->getList(array('info_id' => $info['id']), Dever::input('sku_id'), 'goods');
        }
        return $info;
    }

    public function submit($info, $type = 'price')
    {
        $sku_id = Dever::input('sku_id', 'is_numeric', '价格');
        $sku_num = Dever::input('sku_num', 'is_numeric', '数量', 1);
        $result = $info[$type];
        $result['id'] = $info['id'];
        $result['name'] = $info['name'];
        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['num'] = $sku_num;
                $detail['cash'] = $result['sku']['info']['value'];
                $detail['sku_id'] = $detail['id'];
                $detail['sku_name'] = $detail['name'];
                $detail['id'] = $result['id'];
                $detail['name'] = $result['name'];
                $result['detail'][] = $detail;
            } else {
                Dever::error('下单失败');
            }
        }
        return $result;
    }
}