<?php

namespace Store\Lib;

use Dever;

class Manage
{
    /**
     * 更新信息
     *
     * @return mixed
     */
    public function infoUpdate($id, $name, $data)
    {
        $update = array();
        $area = Dever::param('area', $data);
        if ($area) {
            $temp = is_string($area) ? explode(',', $area) : $area;
            $update['province'] = $temp[0];
            $update['city'] = $temp[1];
            if (isset($temp[2])) {
                $update['county'] = $temp[2];
            }
            
        }

        $map = Dever::param('map', $data);
        if ($map) {
            $temp = is_string($map) ? explode(',', $map) : $map;
            if (isset($temp[1])) {
                $update['lng'] = $temp[1];
                $update['lat'] = $temp[2];
            }
            
        }

        $goods = Dever::param('goods', $data);
        if ($goods) {
            $goods = Dever::json_decode($goods);

            if ($goods) {
                foreach ($goods as $k1 => $v1) {
                    if ($v1['value']) {
                        foreach ($v1['value'] as $k => $v) {
                            if ($v['state'] == 1) {
                                $w = array();
                                $w['goods_id'] = $v['id'];
                                $w['store_id'] = $id;
                                $info = Dever::db('store/goods')->one($w);

                                $w['category_id'] = $v1['id'];
                                if (!$info) {
                                    Dever::db('store/goods')->insert($w);
                                } else {
                                    $w['where_id'] = $info['id'];
                                    $w['state'] = 1;
                                    Dever::db('store/goods')->update($w);
                                }
                            } else {
                                unset($goods[$k1]['value'][$k]);
                            }
                        }
                    }
                }

                $update['goods'] = Dever::json_encode($goods);
            }
        }

        if (isset($update) && $update) {
            $update['where_id'] = $id;
            Dever::db('store/info')->update($update);
        }

        $truename = Dever::param('truename', $data);
        $mobile = Dever::param('mobile', $data);
        if ($mobile) {
            $member = Dever::db('store/member')->one(array('store_id' => $id, 'mobile' => $mobile));

            $update = array();
            $update['store_id'] = $id;
            $update['name'] = $truename;
            $update['mobile'] = $mobile;
            if ($member) {
                $update['where_id'] = $member['id'];
                Dever::db('store/member')->update($update);
            } else {
                Dever::db('store/member')->insert($update);
            }
        }
    }

    /**
     * 更新商品信息
     *
     * @return mixed
     */
    public function goodsUpdate($id, $name, $data)
    {
        $update = array();
        $state = Dever::param('state', $data);
        if ($state) {
            Dever::config('base')->after = true;
            $info = Dever::db('store/goods')->one($id);
            $store_id = $info['store_id'];

            $store = Dever::db('store/info')->one($store_id);

            if ($store && $store['goods']) {
                $goods = Dever::json_decode($store['goods']);

                if (isset($goods['level_' . $info['category_id']]['value']['id_' . $info['goods_id']])) {
                    if ($state == 2) {
                        unset($goods['level_' . $info['category_id']]['value']['id_' . $info['goods_id']]);
                    }

                    if (count($goods['level_' . $info['category_id']]['value']) <= 0) {
                        unset($goods['level_' . $info['category_id']]);
                    }
                } elseif ($state == 1) {
                    $goods_info = Dever::db('goods/info')->one($info['goods_id']);
                    $cate = Dever::load('category/api')->string($info['category_id']);
                    $goods['level_' . $info['category_id']]['id'] = $info['category_id'];
                    $goods['level_' . $info['category_id']]['name'] = $cate;
                    $goods['level_' . $info['category_id']]['value']['id_' . $info['goods_id']] = array
                    (
                        'id' => $info['goods_id'],
                        'name' => $goods_info['name'],
                        'state' => 1,
                    );
                }
                
                $set['goods'] = Dever::json_encode($goods);
                $set['where_id'] = $store_id;
                Dever::db('store/info')->update($set);
            }
        }
    }

    /**
     * 更新库存信息
     *
     * @return mixed
     */
    public function skuUpdate($id, $name, $data)
    {
        $update = array();
        $add_num = Dever::param('add_num', $data);
        if ($add_num) {
            $goods_sku = Dever::db('store/goods_sku')->one($id);
            $state = Dever::db('store/goods_sku')->updateTotal(array('where_id' => $id, 'total_num' => $add_num));
            if ($state) {
                $state = Dever::db('store/goods_sku')->update(array('where_id' => $id, 'add_num' => 0, 'operdate' => time()));
                $goods = Dever::db('store/goods')->one(array('goods_id' => $goods_sku['goods_id'], 'store_id' => $goods_sku['store_id']));
                if ($goods) {
                    Dever::db('store/goods')->update(array('where_id' => $goods['id'], 'total_num' => $goods_sku['total_num'], 'add_num' => 0));
                }

                $log['sid'] = $id;
                $log['store_id'] = $goods_sku['store_id'];
                $log['goods_id'] = $goods_sku['goods_id'];
                $log['sku_id'] = $goods_sku['sku_id'];
                $log['sell_num'] = $goods_sku['sell_num'];
                $log['total_num'] = $goods_sku['total_num'];
                $log['num'] = $add_num;
                $log['after_total_num'] = $goods_sku['total_num'] + $add_num;
                $log['cur_num'] = $log['total_num'] - $goods_sku['sell_num'];
                $log['after_cur_num'] = $log['after_total_num'] - $goods_sku['sell_num'];
                if ($log['cur_num'] < 0) {
                    $log['cur_num'] = 0;
                }
                if ($log['after_cur_num'] < 0) {
                    $log['after_cur_num'] = 0;
                }
                
                $log['online_num'] = Dever::load("shop/lib/manage.getGoodsTotal", 2, $goods_sku['store_id'], $goods_sku['goods_id'], $goods_sku['sku_id']);

                Dever::db('store/goods_log')->insert($log);
            }
        }
    }

    # 根据分类获取商品
    public function getInfo_api()
    {
        $where['city'] = Dever::input('city');

        $data = Dever::db('store/info')->select($where);

        return $data;
    }

    public function search_api()
    {
        return Dever::search('store/info');
    }

    # 获取仓库的商品列表
    public function goods_api()
    {
        $source_id = Dever::input('source_id', 1);
        return Dever::outDiy(Dever::load('store/lib/goods')->getSkuList($source_id));
    }

    # 获取收银台
    public function cashier_api()
    {
        $data = array();
        $data['source_id'] = Dever::input('source_id', 1);
        $data['host'] = Dever::url('lib/manage.cashier', 'store');
        $data['url'] = Dever::url('lib/manage.goods?source_id=' . $data['source_id'], 'store');
        $data['search'] = Dever::url('lib/manage.search?yes='.$data['source_id'].'&json=1', 'store');
        $data['search_store'] = Dever::url('lib/manage.search?json=1', 'store');
        $data['submit'] = Dever::url('lib/order.action_commit?json=1', 'store');

        
        return Dever::render('cashier', $data);
    }
}