<?php namespace Goods\Lib;
use Dever;
use Place;
use Place\Lib\Resource;
class Cart
{
    private $type = 3;
    # 购物车列表
    public function getList()
    {
        $where['uid'] = Place::$uid;
        $set = array();
        //$set['num'] = 100;
        $data = Dever::db('cart', 'goods')->select($where, $set);
        if ($data) {
            $resource = new Resource($this->type);
            foreach($data as &$v) {
                Dever::input('price_id', 'set', $v['sku_id']);
                $info = $resource->getInfo($v['info_id'], false);
                if (!$info) {
                    $v['del'] = 1;
                } else {
                    $v['del'] = 2;
                }
                $v['name'] = $info['name'];
                $v['pic'] = $info['pic'];
                $v['price'] = $info['price'];
                $v['cdate_str'] = date('Y-m-d H:i:s', $v['cdate']);
            }
        }
        return $data;
    }

    # 购物车增减
    public function up($info_id, $sku_id, $num)
    {
        $data['uid'] = Place::$uid;
        $data['info_id'] = $info_id;
        $data['sku_id'] = $sku_id;
        $info = Dever::db('cart', 'goods')->find($data);
        if ($info) {
            $total = $info['num'] + $num;
            if ($total <= 0) {
                Dever::db('cart', 'goods')->delete($info['id']);
            } else {
                $data['num'] = array('+', $num);
                Dever::db('cart', 'goods')->update($info['id'], $data);
            }
        } else {
            $data['num'] = $num;
            Dever::db('cart', 'goods')->insert($data);
        }
        return $this->getList();
    }

    # 购物车选中状态
    public function edit($info_id, $sku_id, $status)
    {
        $data['uid'] = Place::$uid;
        $data['info_id'] = $info_id;
        $data['sku_id'] = $sku_id;
        $info = Dever::db('cart', 'goods')->find($data);
        if ($info) {
            Dever::db('cart', 'goods')->update($info['id'], array('status' => $status));
        }
        return $this->getList();
    }

    # 删除商品
    public function delete($info_id, $sku_id)
    {
        $data['uid'] = Place::$uid;
        $data['info_id'] = $info_id;
        $data['sku_id'] = $sku_id;
        Dever::db('cart', 'goods')->delete($data);
        return $this->getList();
    }

    public function drop()
    {
        $data['uid'] = Place::$uid;
        Dever::db('cart', 'goods')->delete($data);
        return $this->getList();
    }

    public function confirm()
    {
        //$id = Dever::input('ids', 'is_string', '请选择商品');
        $where['uid'] = Place::$uid;
        $where['status'] = 1;
        $data = Dever::db('cart', 'goods')->select($where);
        if ($data) {
            $result = array();
            $result['value'] = 0;
            $result['original'] = 0;
            $result['text'] = '';
            $result['id'] = '';
            $result['name'] = '';
            $resource = new Resource($this->type);
            foreach($data as &$v) {
                $id = $v['id'];
                $num = $v['num'];
                $sku_id = $v['sku_id'];
                Dever::input('price_id', 'set', $sku_id);
                $info = $resource->getInfo($v['info_id'], false);
                if ($info) {
                    $v = $info;
                    $v['cart_id'] = $id;
                    $v['sku_id'] = $sku_id;
                    $v['num'] = $num;
                    $v['cash'] = $v['price']['value'];
                    $result['value'] += $v['cash'];
                    $result['original'] += $v['price']['original'];
                    $v['name'] = $v['name'];
                    if (isset($v['sku']['info']['name'])) {
                        $v['sku_name'] = $v['sku']['info']['name'];
                    }
                    if (isset($v['sku']['info']['pic']) && $v['sku']['info']['pic']) {
                        $v['pic'] = $v['sku']['info']['pic'];
                    } else {
                        $v['pic'] = $v['pic'][0];
                    }
                    if (!$result['id']) {
                        $result['id'] = $v['id'];
                    }
                    if (!$result['name']) {
                        $result['name'] = $v['name'];
                    }
                }
            }
            $result['original'] = Dever::number($result['original']);
            $result['value'] = Dever::number($result['value']);
            $result['text'] = Dever::load('price', 'place')->getText($result['value'], 3);
            $result['detail'] = $data;
            return $result;
        } else {
            Dever::error('请选择商品');
        }
    }
}