<?php namespace Place_order\Lib\Source;
use Dever;
use Place;
class Cart
{
    # 购物车列表
    public function getList()
    {
        $where['uid'] = Place::$uid;
        $set = array();
        //$set['num'] = 100;
        $data = Dever::db('source_cart', 'place_order')->select($where, $set);
        if ($data) {
            foreach($data as &$v) {
                Dever::input('num', 'set', $v['num']);
                Dever::input('sku_id', 'set', $v['sku_id']);
                $info = Dever::load('info', 'place_source')->getInfo($v['source_id']);
                if (!$info) {
                    $v['del'] = 1;
                } else {
                    $v['del'] = 2;
                    unset($info['price']['list']);
                }
                $v['name'] = $info['name'];
                $v['sku_name'] = $info['sku_name'];
                $v['pic'] = $info['pic'][0] ?? '';
                $v['price'] = $info['price'];
                $v['cdate_str'] = date('Y-m-d H:i:s', $v['cdate']);
                $v['is_stock'] = 2;
                if (isset($info['stock'])) {
                    $v['is_stock'] = 1;
                    $v['stock'] = $info['stock'];
                }
            }
        }
        return $data;
    }

    # 购物车增减
    public function up($source_id, $sku_id, $num)
    {
        $data['uid'] = Place::$uid;
        $data['source_id'] = $source_id;
        $data['sku_id'] = $sku_id;

        $info = Dever::db('info', 'place_source')->find($source_id);
        if (!$info) {
            Dever::error('商品不存在');
        }

        $stock = Dever::load('stock', 'place_source')->yue($data['source_id'], $data['sku_id']);
        if ($stock !== false && $num > $stock) {
            Dever::error('库存不足');
        }
        $info = Dever::db('source_cart', 'place_order')->find($data);
        if ($info) {
            if ($num <= 0) {
                Dever::db('source_cart', 'place_order')->delete($info['id']);
            } else {
                $data['num'] = $num;
                Dever::db('source_cart', 'place_order')->update($info['id'], $data);
            }
        } else {
            $data['num'] = $num;
            $data['status'] = 1;
            Dever::db('source_cart', 'place_order')->insert($data);
        }
        return $this->getList();
    }

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

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

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

    public function confirm()
    {
        //$id = Dever::input('ids', 'is_string', '请选择商品');
        $where['uid'] = Place::$uid;
        $where['status'] = 1;
        $data = Dever::db('source_cart', 'place_order')->select($where);
        if ($data) {
            $result = array();
            $result['value'] = 0;
            $result['original'] = 0;
            $result['text'] = '';
            $result['id'] = '';
            $result['name'] = '';
            $result['num'] = 0;
            foreach($data as &$v) {
                $id = $v['id'];
                $num = $v['num'];
                $sku_id = $v['sku_id'];
                Dever::input('num', 'set', $num);
                Dever::input('sku_id', 'set', $sku_id);
                $info = Dever::load('info', 'place_source')->getInfo($v['source_id']);
                if ($info) {
                    $v = $info;
                    $v['cart_id'] = $id;
                    $v['sku_id'] = $sku_id;
                    $v['num'] = $num;
                    $v['cash'] = $v['price']['value'];
                    if (isset($info['stock'])) {
                        if (!isset($result['stock'])) {
                            $result['stock'] = 0;
                        }
                        $result['stock'] += $info['stock'];
                    }
                    $result['num'] += $v['price']['num'];
                    $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['original_text'] = Dever::load('price', 'place')->getText($result['value'], 3);
            $result['text'] = Dever::load('price', 'place')->getText($result['value'], 3);
            $result['detail'] = $data;
            return $result;
        } else {
            Dever::error('请选择商品');
        }
    }
}