<?php

namespace Factory\Lib;

use Dever;

class Manage
{
    # 导出订单
    public function out_api()
    {
        print_r(Dever::input());die;
    }

    /**
     * 更新信息
     *
     * @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['factory_id'] = $id;
                                $info = Dever::db('factory/goods')->one($w);

                                $w['category_id'] = $v1['id'];
                                if (!$info) {
                                    Dever::db('factory/goods')->insert($w);
                                } else {
                                    $w['where_id'] = $info['id'];
                                    $w['state'] = 1;
                                    Dever::db('factory/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('factory/info')->update($update);
        }

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

            $update = array();
            $update['factory_id'] = $id;
            $update['name'] = $truename;
            $update['mobile'] = $mobile;
            if ($member) {
                $update['where_id'] = $member['id'];
                Dever::db('factory/member')->update($update);
            } else {
                Dever::db('factory/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('factory/goods')->one($id);
            $factory_id = $info['factory_id'];

            $factory = Dever::db('factory/info')->one($factory_id);

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

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

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

        return $data;
    }

    # 获取工厂
    public function search_api()
    {
        return Dever::search('factory/info');
    }

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

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

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