<?php

namespace Shop\Lib;

use Dever;

# 批量设置商品
class Set
{
    # 导出订单
    public function __construct()
    {
        Dever::load('manage/auth.init');
    }

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

    # 获取商品列表
    public function goods_api()
    {
        $shop_id = Dever::input('shop_id', 1);
        return Dever::outDiy(Dever::load('goods/lib/set')->getGoodsList($shop_id, 'shop_id', 'shop/goods_sku'));
    }

    # 设置生产能力
    public function home_api()
    {
        $data = array();
        $data['shop_id'] = Dever::input('id', 1);
        $data['host'] = Dever::url('lib/set.home', 'shop');
        $data['url'] = Dever::url('lib/set.goods?shop_id=' . $data['shop_id'], 'shop');
        $data['search'] = Dever::url('lib/set.search?yes='.$data['shop_id'].'&json=1', 'shop');
        $data['submit'] = Dever::url('lib/set.action_commit?json=1', 'shop');

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

    # 设置生产能力
    public function action_commit_api()
    {
        $goods = Dever::input('goods');
        if (!$goods) {
            Dever::alert('请传入商品');
        }
        $goods = Dever::json_decode($goods);
        $shop_id = Dever::input('shop_id');

        $shop = Dever::db('shop/info')->one($shop_id);
        $where['option_shop_id'] = $shop_id;
        $where['set_state'] = 2;
        Dever::db('shop/goods')->updates($where);

        Dever::db('shop/goods_sku')->updates($where);

        foreach ($goods as $k => $v) {
            $temp = explode('-', $k);
            $goods_id = $temp[0];
            if (isset($temp[1])) {
                $sku_id = $temp[1];
            } else {
                $sku_id = -1;
            }
            if ($v['num'] < 0) {
            	continue;
            }
            if ($v['del'] == 2) {
                $w = array();
                $w['goods_id'] = $goods_id;
                $w['shop_id'] = $shop_id;
                $w['sku_id'] = $sku_id;
                $sku_info = Dever::db('shop/goods_sku')->one($w);

                if ($sku_info) {
                    Dever::db('shop/goods_sku')->update(array('where_id' => $sku_info['id'], 'state' => 2));
                }
                unset($w['sku_id']);
                $t = $w;
                $t['state'] = 1;
                $total = Dever::db('shop/goods_sku')->total($t);
                if ($total <= 0) {
                    $info = Dever::db('shop/goods')->one($w);
                    if ($info) {
                        Dever::db('shop/goods')->update(array('where_id' => $info['id'], 'state' => 2));
                    }
                }
            } else {
                $goods_info = Dever::db('goods/info')->one($goods_id);
                $w = array();
                $w['goods_id'] = $goods_id;
                $w['shop_id'] = $shop_id;
                $info = Dever::db('shop/goods')->one($w);

                $w['status'] = $goods_info['status'];
                $w['total_num'] = $v['num'];
                $w['min'] = $v['min'];
                if (!$info) {
                    Dever::db('shop/goods')->insert($w);
                } else {
                    $w['total_num'] += $info['total_num'];
                    $w['where_id'] = $info['id'];
                    $w['state'] = 1;
                    Dever::db('shop/goods')->update($w);
                }

                $w = array();
                $w['goods_id'] = $goods_id;
                $w['shop_id'] = $shop_id;
                $w['sku_id'] = $sku_id;
                $info = Dever::db('shop/goods_sku')->one($w);

                $w['status'] = $goods_info['status'];
                $w['total_num'] = $v['num'];
                $w['min'] = $v['min'];
                $w['area'] = $shop['area'];
                $w['price_id'] = $v['price_template_id'];
                if (!$info) {
                    Dever::db('shop/goods_sku')->insert($w);
                } else {
                    $w['total_num'] += $info['total_num'];
                    $w['where_id'] = $info['id'];
                    $w['state'] = 1;
                    Dever::db('shop/goods_sku')->update($w);
                }
            }
        }

        return Dever::url('project/database/list&project=shop&&table=info&menu=shop&search_option_state=1', 'manage');
    }

    # 获取工厂列表
    public function getFactory_api()
    {
        $shop_id = Dever::input('shop_id', 1);
        $shop_factory = Dever::db('shop/factory')->getAll(array('shop_id' => $shop_id));
        $factory = Dever::db('factory/info')->select(array('status' => 1));

        $result = array();
        foreach ($factory as $k => $v) {
            $key = $v['area'];
            $area = Dever::load('area/api')->string($key, '', true, '');
            if (!isset($result[$key])) {
                $result[$key] = array();
                $result[$key]['id'] = $v['id'];
                $result[$key]['name'] = $area;
                $result[$key]['select'] = 2;
                $result[$key]['end'] = false;
                $result[$key]['children'] = array();
            }

            $data = array();
            $data['id'] = $v['id'];
            $data['name'] = $v['name'];
            $data['area'] = $area;
            $data['select'] = 2;
            $data['end'] = true;
            if (isset($shop_factory[$v['id']]) && $shop_factory[$v['id']]) {
                $data['select'] = 1;
            }
            $result[$key]['children'][] = $data;
        }
        $result = array_values($result);
        return Dever::outDiy($result);
    }

    # 分配工厂
    public function factory_api()
    {
        $data = array();
        $data['shop_id'] = Dever::input('id', 1);
        $data['host'] = Dever::url('lib/set.factory', 'shop');
        $data['url'] = Dever::url('lib/set.getFactory?shop_id=' . $data['shop_id'], 'shop');
        $data['search'] = Dever::url('lib/set.search?yes='.$data['shop_id'].'&json=1', 'shop');
        $data['submit'] = Dever::url('lib/set.source_commit?json=1&type=factory', 'shop');

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

    # 获取仓库列表
    public function getStore_api()
    {
        $shop_id = Dever::input('shop_id', 1);
        $shop_store = Dever::db('shop/store')->getAll(array('shop_id' => $shop_id));
        $store = Dever::db('store/info')->select(array('status' => 1));

        $result = array();
        foreach ($store as $k => $v) {
            $key = $v['area'];
            $area = Dever::load('area/api')->string($key, '', true, '');
            if (!isset($result[$key])) {
                $result[$key] = array();
                $result[$key]['id'] = $v['id'];
                $result[$key]['name'] = $area;
                $result[$key]['select'] = 2;
                $result[$key]['end'] = false;
                $result[$key]['children'] = array();
            }

            $data = array();
            $data['id'] = $v['id'];
            $data['name'] = $v['name'];
            $data['area'] = $area;
            $data['select'] = 2;
            $data['end'] = true;
            if (isset($shop_store[$v['id']]) && $shop_store[$v['id']]) {
                $data['select'] = 1;
            }
            $result[$key]['children'][] = $data;
        }
        $result = array_values($result);
        return Dever::outDiy($result);
    }

    # 分配仓库
    public function store_api()
    {
        $data = array();
        $data['shop_id'] = Dever::input('id', 1);
        $data['host'] = Dever::url('lib/set.store', 'shop');
        $data['url'] = Dever::url('lib/set.getStore?shop_id=' . $data['shop_id'], 'shop');
        $data['search'] = Dever::url('lib/set.search?yes='.$data['shop_id'].'&json=1', 'shop');
        $data['submit'] = Dever::url('lib/set.source_commit?json=1&type=store', 'shop');

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

    public function source_commit_api()
    {
        $value = Dever::input('value');
        if (!$value) {
            Dever::alert('未选择');
        }
        $value = Dever::json_decode($value);
        $shop_id = Dever::input('shop_id');
        $type = Dever::input('type');

        $col = $type . '_id';
        $table = 'shop/' . $type;
        $where['option_shop_id'] = $shop_id;
        $where['set_state'] = 2;
        Dever::db($table)->updates($where);

        foreach ($value as $k => $v) {
            $w = array();
            $w[$col] = $v['id'];
            $w['shop_id'] = $shop_id;
            $info = Dever::db($table)->one($w);

            if (!$info) {
                Dever::db($table)->insert($w);
            } else {
                $w['where_id'] = $info['id'];
                $w['state'] = 1;
                Dever::db($table)->update($w);
            }
        }

        return Dever::url('project/database/list&project=shop&&table=info&menu=shop&search_option_state=1', 'manage');
    }
}