<?php

namespace Shop\Lib;

use Dever;

class Manage
{
    public function getOrderUrl($order_id, $order_num)
    {
        $url = Dever::load('manage/database')->url('list', $order_id, 'buy_order_goods&project=shop&order_id='.$order_id.'&page_type=1');

        $url = '<a href="'.$url.'">'.$order_num.'</a>';
        return $url;
    }
    public function showCash($cash, $p_cash = 0)
    {
        if (!$cash) {
            $cash = 0;
        }

        if (!$p_cash) {
            $p_cash = 0;
        }

        return $cash - $p_cash;
    }
    # 获取用户信息
    public function user($id)
    {
        $info = Dever::db('shop/sell_order')->one($id);

        if ($info['uid'] && $info['uid'] > 0) {
            $user = Dever::db('passport/user')->one($info['uid']);

            $result = $user['username'];
            if ($info['mobile']) {
                $result .= '('.$info['mobile'].')';
            }
        } else {
            $result = $info['mobile'];
        }
        
        return $result;
    }

    # 获取订单信息
    public function sell_order($id)
    {
        $table = array();
        $info = Dever::db('shop/sell_order')->one($id);

        $goods = Dever::db('shop/sell_order_goods')->select(array('order_id' => $info['id']));

        foreach ($goods as $k => $v) {
            $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v['goods_id'], $v['sku_id']);
            //print_r($goods_info);die;
            $table[$goods_info['name']] = $v['price'];
        }
        
        return Dever::table($table);
    }

    /**
     * 更新信息
     *
     * @return mixed
     */
    public function upInfo($id, $name, $data)
    {
        Dever::config('base')->hook = true;
        
    }

    /**
     * 更新信息
     *
     * @return mixed
     */
    public function printInsert($id, $name, $data)
    {
        Dever::config('base')->hook = true;
        $name = Dever::param('name', $data);
        $number = Dever::param('number', $data);
        $phonenum = Dever::param('phonenum', $data);
        $key = Dever::param('key', $data);
        if ($number && $key) {
            $data = array();
            $data['name'] = $name;
            $data['number'] = $number;
            $data['phonenum'] = $phonenum;
            $data['key'] = $key;
            $result = Dever::load('mshop/lib/feieyun')->add(array($data));
            if (isset($result['no'][0]) && $result['no'][0] && strstr($result['no'][0], '编号不合法')) {
                Dever::db('shop/print')->update(array('where_id' => $id, 'status' => 10));
            } else {
                Dever::db('shop/print')->update(array('where_id' => $id, 'status' => 1));
            }
        }
    }

    /**
     * 更新信息
     *
     * @return mixed
     */
    public function printUpdate($id, $name, $data)
    {
        Dever::config('base')->hook = true;
        $info = Dever::db('shop/print')->one($id);
        $name = Dever::param('name', $data);
        $phonenum = Dever::param('phonenum', $data);
        if ($info && $name) {
            if ($info['status'] == 10) {
                $this->printInsert($id, $name, $info);
            } else {
                Dever::load('mshop/lib/feieyun')->edit($info['number'], $name, $phonenum);
            }
        }
    }

    /**
     * 打印机状态
     *
     * @return mixed
     */
    public function printStatus($status, $sn)
    {
        return $status >= 10 ? '绑定失败' : Dever::load('mshop/lib/feieyun')->status($sn);
    }

    /**
     * 更新信息
     *
     * @return mixed
     */
    public function couponUpdate($id, $name, $data)
    {
        Dever::config('base')->hook = true;
        $update = array();
        $coupon = Dever::param('coupon', $data);
        if ($coupon) {
            $temp = is_string($coupon) ? explode(',', $coupon) : $coupon;
            $update['method'] = $temp[0];
            $update['coupon_id'] = $temp[1];
        }

        $shop_id = Dever::param('shop_id', $data);
        if ($shop_id) {
            $shop = Dever::db('shop/info')->find($shop_id);
            $update['city'] = $shop['city'];
        }

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

    /**
     * 更新信息
     *
     * @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];
            }
            if (isset($temp[3])) {
                $update['town'] = $temp[3];
            }
        }

        $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];

                $address = Dever::param('address', $data);
                $update['coord_address'] = Dever::load('shop/lib/info')->address($temp[1], $temp[2]);

                if (!$address && $update['coord_address']) {
                    $update['address'] = $update['coord_address'];
                }
            }
        }

        $act = Dever::param('act', $data);
        if ($act) {
            foreach ($act as $k => $v) {
                $w = array();
                $w['shop_id'] = $id;
                $w['act_id'] = $k;
                foreach ($v as $k1 => $v1) {
                    $w['shop_coupon_id'] = $v1;
                    $info = Dever::db('shop/coupon_act')->find($w);
                    if (!$info) {
                        Dever::db('shop/coupon_act')->insert($w);
                    }
                }
            }
        }

        //Dever::upLinkage($update, $id, $data, 'goods', 'shop/goods', 'shop_id', 'goods_id', 'category_id');
        //Dever::upLinkage($update, $id, $data, 'factory', 'shop/factory', 'shop_id', 'factory_id', 'city');
        //Dever::upLinkage($update, $id, $data, 'store', 'shop/store', 'shop_id', 'store_id', 'city');

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

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

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

    private function updateParam()
    {

    }

    /**
     * 更新商品信息
     *
     * @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('shop/goods')->one($id);
            $shop_id = $info['shop_id'];

            $shop = Dever::db('shop/info')->one($shop_id);

            if ($shop && $shop['goods']) {
                $goods = Dever::json_decode($shop['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'] = $shop_id;
                Dever::db('shop/info')->update($set);
            }
        }
    }

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

                if ($add_num < 0) {
                    # 记录出库日志
                } else {
                    # 记录入库日志
                }
            }
        }
    }

    # 获取供货商信息
    public function buyInfo($type = '', $type_id = '')
    {
        if ($type == 1) {
            $type_info = Dever::db('shop/info')->one($type_id);
            return $type_info['name'] . '('.$type_info['mobile'].')';
        } elseif ($type == 2) {
            $type_info = Dever::db('store/info')->one($type_id);
            return $type_info['name'] . '('.$type_info['mobile'].')';
        } elseif ($type == 3) {
            $type_info = Dever::db('factory/info')->one($type_id);
            return $type_info['name'] . '('.$type_info['mobile'].')';
        } else {
            return '无';
        }
    }

    /**
     * 更新信息
     *
     * @return mixed
     */
    public function buyOrderPs($id, $name, $data)
    {
        $order_id = Dever::param('order_id', $data);
        if ($order_id) {
            $update['where_id'] = $order_id;
            $update['set_status'] = 4;
            Dever::db('shop/buy_order')->update($update);
        }
    }

    # 获取门店
    public function search_api()
    {
        return Dever::search('shop/info');
    }
}