<?php

namespace Store\Lib;

use Dever;

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

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

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

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

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

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

        $where['option_store_id'] = $store_id;
        $where['set_state'] = 2;
        Dever::db('store/goods')->updates($where);

        Dever::db('store/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['store_id'] = $store_id;
                $w['sku_id'] = $sku_id;
                $sku_info = Dever::db('store/goods_sku')->one($w);

                if ($sku_info) {
                    Dever::db('store/goods_sku')->update(array('where_id' => $sku_info['id'], 'state' => 2));
                }
                unset($w['sku_id']);
                $t = $w;
                $t['state'] = 1;
                $total = Dever::db('store/goods_sku')->total($t);
                if ($total <= 0) {
                    $info = Dever::db('store/goods')->one($w);
                    if ($info) {
                        Dever::db('store/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['store_id'] = $store_id;
                $info = Dever::db('store/goods')->one($w);

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

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

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

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