<?php

namespace Agent\Lib;

use Dever;

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

    # 获取商品列表
    public function goods_api()
    {
        $id = Dever::input('mid');
        return Dever::load('agent/lib/set')->getData($id);
    }

    # 设置商品
    public function home_api()
    {
        $data = array();
        $data['link'] = Dever::decode(Dever::input('refer'));
        $data['mid'] = Dever::input('mid');
        $data['search_api_mid'] = Dever::input('search_api_mid');
        $data['host'] = Dever::url('lib/member_set.home?mid=' . $data['mid'], 'agent');
        $data['url'] = Dever::url('lib/member_set.goods?mid=' . $data['mid'], 'agent');
        $data['submit'] = Dever::url('lib/member_set.action_commit?json=1', 'agent');

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

    # 设置
    public function action_commit_api()
    {
        $search_api_mid = Dever::input('search_api_mid');
        $goods = Dever::input('goods');
        if (!$goods) {
            Dever::alert('请传入商品');
        }
        $goods = Dever::json_decode($goods);
        $mid = Dever::input('mid');
        $desc = Dever::input('desc');

        $where['option_mid'] = $mid;
        $where['set_state'] = 2;
        Dever::db('agent/member_goods')->updates($where);

        foreach ($goods as $k => $v) {
            $type = 1;
            $type_id = $k;
            $sku_id = -1;
            if (strstr($k, '_')) {
                $temp = explode('_', $k);
                $k = $temp[0];
                $type_id = $temp[0];
                $type = $temp[1];
            }
            if (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['sku_id'] = $sku_id;
            $w['mid'] = $mid;
            $w['clear'] = true;

            if ($v['del'] == 2) {
                $info = Dever::db('agent/member_goods')->one($w);
                if ($info) {
                    Dever::db('agent/member_goods')->update(array('where_id' => $info['id'], 'state' => 2, 'total_num' => 0));

                    $name = $info['name'] ? $info['name'] : Dever::load("agent/lib/manage.getGoods#name", $info['type'], $info['type_id'], $info['sku_id']);
                    Dever::load('agent/lib/member_set')->log($w['mid'], '', $w['type'], $w['type_id'], $w['sku_id'], -1*$info['total_num'], $info['total_num'], 0, $name, $desc . '[删除]');
                }
            } else {
                $total = $v['total'];
                
                $cash = $price = 0;
                
                if ($type == 1) {
                    $goods_info = Dever::load('goods/lib/info')->getInfoBySku($w['type_id'], $w['sku_id']);
                    $price = $goods_info['price'];
                    $cash = $goods_info['price'] * $total;
                    if (isset($goods_info['sku'])) {
                        $sku = '-' . $goods_info['sku']['string'];
                    } else {
                        $sku = '';
                    }
                    $name = $goods_info['name'] . $sku;
                } elseif ($type == 2) {
                    # 体验卡
                    $card = Dever::db('card/info')->find($w['type_id']);
                    if (!$card) {
                        continue;
                    }
                    $name = $card['name'];
                    if ($sku_id == -1) {
                        if ($card['create_type'] == 2) {
                            $cash = $price = $card['value'] * $total;
                            $total = 1;
                            $w['sku_id'] = Dever::load('card/lib/code')->create($card, 2, $mid, -1, $cash);
                            if (!$w['sku_id']) {
                                continue;
                            }
                        } else {
                            if ($card['create_type'] == 3 && isset($v['price']) && $v['price'] > 0) {
                                $card['value'] = $v['price'];
                            }
                            $cash = $price = $card['value'];
                            for($i=0; $i<$total;$i++) {
                                $w['sku_id'] = Dever::load('card/lib/code')->create($card, 2, $mid, -1, $cash);
                                if ($w['sku_id']) {
                                    $w['total_num'] = 1;
                                    $w['cash'] = $cash;
                                    $w['price'] = $price;
                                    $w['name'] = $name;
                                    $state = Dever::db('agent/member_goods')->insert($w);
                                    if ($state) {
                                        Dever::load('agent/lib/member_set')->log($w['mid'], '', $w['type'], $w['type_id'], $w['sku_id'], 1, 0, 1, $name, $desc);
                                    }
                                }
                            }
                            continue;
                        }
                    } else {
                        $sku = explode('i', $sku_id);
                        if ($sku) {
                            foreach ($sku as $k1 => $v1) {
                                $w['sku_id'] = $v1;
                                $info = Dever::db('agent/member_goods')->one($w);
                                if ($info) {
                                    $cur = ($info['total_num']-$info['sell_num']);
                                    if ($cur <= 0) {
                                        unset($sku[$k1]);
                                    }
                                    $u = array();
                                    $u['where_id'] = $info['id'];
                                    $u['state'] = 1;
                                    $state = Dever::db('agent/member_goods')->update($u);
                                }
                            }
                        } 

                        $total = $v['total'] - count($sku);
                        
                        if ($total > 0) {
                            if ($card['create_type'] == 2) {
                                $cash = $price = $card['value'] * $total;
                                $total = 1;
                                $w['sku_id'] = Dever::load('card/lib/code')->create($card, 2, $mid, -1, $cash);
                                if (!$w['sku_id']) {
                                    continue;
                                }
                            } else {
                                if ($card['create_type'] == 3 && isset($v['price']) && $v['price'] > 0) {
                                    $card['value'] = $v['price'];
                                }
                                $cash = $price = $card['value'];
                                for($i=0; $i<$total;$i++) {
                                    $w['sku_id'] = Dever::load('card/lib/code')->create($card, 2, $mid, -1, $cash);
                                    if ($w['sku_id']) {
                                        $w['total_num'] = 1;
                                        $w['cash'] = $cash;
                                        $w['price'] = $price;
                                        $w['name'] = $name;
                                        $state = Dever::db('agent/member_goods')->insert($w);
                                        if ($state) {
                                            Dever::load('agent/lib/member_set')->log($w['mid'], '', $w['type'], $w['type_id'], $w['sku_id'], 1, 0, 1, $name, $desc);
                                        }
                                    }
                                }
                                continue;
                            }
                        } else {
                            continue;
                        }
                    }
                    
                } elseif ($type == 3) {
                    $prize = Dever::db('setting/prize')->find($w['type_id']);
                    $price = $prize['price'];
                    $cash = $prize['price'] * $total;
                    $name = $prize['name'];
                }
                $info = Dever::db('agent/member_goods')->one($w);
                $w['total_num'] = $total;
                $w['cash'] = $cash;
                $w['price'] = $price;
                $w['name'] = $name;
                if (!$info) {
                    $state = Dever::db('agent/member_goods')->insert($w);
                    if ($state) {
                    	Dever::load('agent/lib/member_set')->log($w['mid'], '', $w['type'], $w['type_id'], $w['sku_id'], $v['total'], 0, $w['total_num'], $name, $desc);
                    }
                } else {
                    $w['total_num'] += $info['sell_num'];
                    $w['where_id'] = $info['id'];
                    $w['state'] = 1;
                    $state = Dever::db('agent/member_goods')->update($w);
                    $num = $w['total_num']-$info['total_num'];
                    if ($state) {
                    	Dever::load('agent/lib/member_set')->log($w['mid'], '', $w['type'], $w['type_id'], $w['sku_id'], $num, $info['total_num'], $w['total_num'], $name, $desc);
                    }
                }
            }
        }

        $link = Dever::input('link');
        if ($link) {
            return $link;
        }
        
        if ($search_api_mid) {
            return Dever::url('project/database/list&table=member_goods&project=agent&search_option_state=1&search_api_mid='.$search_api_mid, 'manage');
        } else {
            return Dever::url('project/database/list&table=member_goods&project=agent&search_option_state=1&search_option_mid='.$mid, 'manage');
        }
        
    }

    # 记录日志
    public function log($mid, $parent_order_num, $type, $type_id, $sku_id, $num, $pnum, $fnum, $name = '', $desc = '')
    {
    	$data['mid'] = $mid;
    	$data['parent_order_num'] = $parent_order_num;
    	$data['type'] = $type;
    	$data['type_id'] = $type_id;
    	$data['sku_id'] = $sku_id;
    	$data['num'] = $num;
    	$data['pnum'] = $pnum;
    	$data['fnum'] = $fnum;
    	$data['desc'] = $desc;
        $data['name'] = $name;
    	$data['order_num'] = $this->getOrderId();
        $admin = Dever::load('manage/auth.info');
        if ($admin) {
            $data['admin'] = $admin['id'];
        }

    	$id = Dever::db('agent/member_goods_log')->insert($data);

    	return $id;
    }

    /**
     * 生成日志订单号
     *
     * @return mixed
     */
    public function getOrderId()
    {
        $where['order_num'] = Dever::order('QY');
        $state = Dever::db('agent/member_goods_log')->one($where);
        if (!$state) {
            return $where['order_num'];
        } else {
            return $this->getOrderId();
        }
    }
}