<?php

namespace Agent\Lib;

use Dever;

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

    # 获取名额和体验卡
    public function getData($id)
    {
        $order_id = Dever::input('order_id');
        $mid = Dever::input('mid');

        $col = 'role_id';
        $table = 'setting/role_goods';
        $value = $id;

        if ($mid) {
            $col = 'mid';
            $table = 'agent/member_goods';
            $value = $mid;
        } elseif ($order_id) {
            $check = Dever::db('agent/order_goods')->find(array('order_id' => $order_id));
            if ($check) {
                $col = 'order_id';
                $table = 'agent/order_goods';
                $value = $order_id;
            }
        }

        $data = array();

        /*
        $prize = Dever::db('setting/prize')->select();

        $data[0]['id'] = -1;
        $data[0]['name'] = '名额';
        $data[0]['price'] = 0;
        $data[0]['price_type'] = 2;
        $data[0]['select'] = 2;
        $data[0]['del'] = 1;
        $data[0]['total'] = 0;
        foreach ($prize as $k => $v) {
            $data[1]['children'][$k]['id'] = $v['id'] . '_3';
            $data[1]['children'][$k]['name'] = $v['name'];
            $data[1]['children'][$k]['price'] = $v['price'];
            $data[1]['children'][$k]['price_type'] = 2;
            $data[1]['children'][$k]['select'] = 2;
            $data[1]['children'][$k]['del'] = 1;
            $data[1]['children'][$k]['total'] = 0;
            $data[1]['children'][$k]['end'] = true;

            $info = Dever::db($table)->find(array('type' => 3, 'type_id' => $k, $col => $value));
            if ($info) {
                $data[0]['children'][$k-1]['select'] = 1;
                $data[0]['children'][$k-1]['total'] = $info['num'];
            }
        }

        $card = Dever::db('card/info')->select(array('status' => 1));
        if ($card) {
            $data[1]['id'] = -2;
            $data[1]['name'] = '厨人E卡';
            $data[1]['price'] = 0;
            $data[1]['price_type'] = 2;
            $data[1]['select'] = 2;
            $data[1]['del'] = 1;
            $data[1]['total'] = 0;
            foreach ($card as $k => $v) {
                $data[1]['children'][$k]['id'] = $v['id'] . '_2';
                $data[1]['children'][$k]['name'] = $v['name'];
                $data[1]['children'][$k]['price'] = $v['price'];
                $data[1]['children'][$k]['price_type'] = 2;
                $data[1]['children'][$k]['select'] = 2;
                $data[1]['children'][$k]['del'] = 1;
                $data[1]['children'][$k]['total'] = 0;
                $data[1]['children'][$k]['end'] = true;

                $info = Dever::db($table)->find(array('type' => 2, 'type_id' => $k, $col => $value));
                if ($info) {
                    $data[1]['children'][$k]['select'] = 1;
                    $data[1]['children'][$k]['total'] = $info['num'];
                }
            }
        }
        */

        $data = array_merge($data, Dever::load('goods/lib/set.getGoodsList', $value, $col, $table, 'type_id'));

        return Dever::outDiy($data);
    }

    # 获取商品列表
    public function goods_api()
    {
        $id = Dever::input('id', 1);
        return $this->getData($id);
    }

    # 设置商品
    public function home_api()
    {
        $data = array();
        $data['role_id'] = Dever::input('id', 1);
        $data['give'] = Dever::input('give', 1);
        $data['host'] = Dever::url('lib/set.home?id=' . $data['role_id'], 'agent');
        $data['url'] = Dever::url('lib/set.goods?id=' . $data['role_id'] . '&give=' . $data['give'], 'agent');
        $data['search'] = Dever::db('setting/role_goods')->config['set']['give'];
        $data['submit'] = Dever::url('lib/set.action_cmmmit?json=1', 'agent');

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

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

        $where['option_role_id'] = $role_id;
        $where['option_give'] = $give;
        $where['set_state'] = 2;
        Dever::db('setting/role_goods')->updates($where);

        foreach ($goods as $k => $v) {
            $type = 1;
            $type_id = $k;
            $sku_id = -1;
            if (strstr($k, '_')) {
                $temp = explode('_', $k);
                $type_id = $temp[0];
                $type = $temp[1];
            } elseif (strstr($k, '-')) {
                $temp = explode('-', $k);
                $type_id = $temp[0];
                $sku_id = $temp[1];
            }
            
            if ($v['total'] <= 0) {
            	continue;
            }
            $w = array();
            $w['type'] = $type;
            $w['type_id'] = $type_id;
            $w['role_id'] = $role_id;
            $w['sku_id'] = $sku_id;
            $w['give'] = $give;
            $info = Dever::db('setting/role_goods')->one($w);
            if ($v['del'] == 2) {
                if ($info) {
                    Dever::db('setting/role_goods')->update(array('where_id' => $info['id'], 'state' => 2));
                }
            } else {
            	$w['num'] = $v['total'];
                
                if (!$info) {
                    Dever::db('setting/role_goods')->insert($w);
                } else {
                    $w['where_id'] = $info['id'];
                    $w['state'] = 1;
                    Dever::db('setting/role_goods')->update($w);
                }
            }
        }

        return 'reload';
    }
}