<?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');
        $prize_id = Dever::input('prize_id');
        $reward_give_id = Dever::input('reward_give_id');
        $total = Dever::input('total', 1);

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

        if ($mid) {
            $col = 'mid';
            $table = 'agent/member_goods';
            $value = $mid;
        } elseif ($prize_id) {
            $col = 'prize_id';
            $table = 'setting/prize_goods';
            $value = $prize_id;
        } elseif ($reward_give_id) {
            $col = 'reward_give_id';
            $table = 'setting/role_reward_goods';
            $value = $reward_give_id;
        } 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();

        if (!$prize_id) {
            $prize = Dever::db('setting/prize')->select();
            if ($prize) {
                $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[0]['children'][$k]['id'] = $v['id'] . '_3';
                    $data[0]['children'][$k]['name'] = $v['name'];
                    $data[0]['children'][$k]['price'] = $v['price'];
                    $data[0]['children'][$k]['price_type'] = 2;
                    $data[0]['children'][$k]['select'] = 2;
                    $data[0]['children'][$k]['del'] = 1;
                    $data[0]['children'][$k]['total'] = 0;
                    $data[0]['children'][$k]['end'] = true;

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

        $card = Dever::db('card/info')->getDataByType(array('type_id' => '1,4,6'));
        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'];
                if ($v['create_type'] == 3) {
                    if ($mid) {
                        $data[1]['children'][$k]['name'] .= '（自定义面值卡-已发卡无法更改价值）';
                    } else {
                        $data[1]['children'][$k]['name'] .= '（自定义面值卡-数量即百分比）';
                    }
                }
                $data[1]['children'][$k]['price'] = $v['value'];
                $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;

                if ($table == 'agent/member_goods') {
                    $info = Dever::db($table)->select(array('type' => 2, 'type_id' => $v['id'], $col => $value));
                    if ($info) {
                        $num = 0;
                        $data[1]['children'][$k]['price'] = 0;
                        $sku_id = array();
                        foreach ($info as $k1 => $v1) {
                            $num += ($v1['total_num']-$v1['sell_num']);
                            if ($v1['sku_id'] > 0) {
                                $code = Dever::db('card/code')->find($v1['sku_id']);
                                $data[1]['children'][$k]['price'] += $code['total_cash'];
                                $sku_id[] = $v1['sku_id'];
                            }
                        }
                        $data[1]['children'][$k]['id'] = $v['id'] . '-' . implode('i', $sku_id) . '_2';;
                        $data[1]['children'][$k]['select'] = 1;
                        $data[1]['children'][$k]['total'] = $num;
                    }
                } else {
                    $info = Dever::db($table)->find(array('type' => 2, 'type_id' => $v['id'], $col => $value));
                    if ($info) {
                        $data[1]['children'][$k]['select'] = 1;
                        $data[1]['children'][$k]['total'] = $info['num'] * $total;
                    }
                }
            }
        }

        $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['link'] = Dever::decode(Dever::input('refer'));
        $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_commit?json=1', 'agent');

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

    # 设置生产能力
    public function action_commit_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';
    }
}