<?php

namespace Goods\Lib;

use Dever;

class Set
{
    # 获取商品及其属性列表
    public function getGoodsList($id, $type, $table, $col = 'goods_id', $sku_state = true)
    {
        $total = Dever::input('total', 1);
        $where['status'] = 1;

        if ($type != 'factory_id') {
            //$where['price_type'] = '1,2,3';
        }
        $data = Dever::db('goods/info')->getData($where);

        $result = array();
        if ($data) {
            $i = 0;
            foreach ($data as $k => $v) {
                
                $sku = Dever::db('goods/info_sku')->select(array('info_id' => $v['id']));

                if ($sku) {
                    $other_where = array($type => $id, $col => $v['id'], 'sku_id' => -1);
                    if ($col == 'type_id') {
                        $other_where['type'] = 1;
                    }
                    $give = Dever::input('give');
                    if ($give) {
                        $other_where['give'] = $give;
                    } elseif ($table == 'setting/role_goods') {
                        $where_type = Dever::input('type', 1);
                        if ($where_type) {
                            $other_where['give'] = $where_type;
                        }
                    }
                    if ($table == 'agent/order_goods') {
                        $where_type = Dever::input('type', 1);
                        if ($where_type) {
                            $other_where['type'] = $where_type;
                        }
                    }
                    
                    $other = Dever::db($table)->find($other_where);
                    $result[$i]['id'] = $v['id'];
                    $result[$i]['name'] = $v['name'];
                    $result[$i]['price'] = $v['price'];
                    $result[$i]['price_type'] = $v['price_type'];
                    $result[$i]['select'] = 2;
                    $result[$i]['del'] = 1;
                    if ($type == 'factory_id') {
                        $result[$i]['total'] = 0;
                        if ($other) {
                            $result[$i]['p_price'] = $other['p_price'];
                            $result[$i]['select'] = $other['state'];
                        } else {
                            $result[$i]['p_price'] = $v['price'];
                        }
                    } else {
                        if ($type == 'store_id') {
                            $result[$i]['price'] = $v['f_price'];
                        }
                        if ($type == 'store_id' || $type == 'shop_id') {
                            $result[$i]['price_template'] = Dever::db('price/goods')->select(array('goods_id' => $v['id']));
                            $result[$i]['price_template_id'] = (isset($other['price_id']) && $other['price_id']) ? $other['price_id'] : 0;
                        }
                        if ($other) {
                            if (isset($other['min'])) {
                                $result[$i]['min'] = $other['min'];
                            }
                            if (isset($other['total_num'])) {
                                /*
                                if ($col == 'type_id') {
                                    $result[$i]['total'] = $other['total_num'];
                                } else {
                                    $result[$i]['total'] = $other['total_num'] - $other['sell_num'];
                                }
                                */
                                if (isset($other['sell_num'])) {
                                    $result[$i]['total'] = $other['total_num'] - $other['sell_num'];
                                } else {
                                    $result[$i]['total'] = $other['total_num'];
                                }
                                
                            } else {
                                $result[$i]['total'] = $other['num'];
                            }
                            
                            $result[$i]['select'] = $other['state'];
                        } else {
                            $result[$i]['total'] = 0;
                        }
                    }

                    $result[$i]['total'] = $result[$i]['total'] * $total;
                    $result[$i]['children'] = array();
                    $num = count($sku);
                    if (($num == 1 && $sku[0]['key'] == -1) || !$sku_state) {
                        //$result[$i]['id'] .=  '-' . $sku[0]['id'];
                        $result[$i]['end'] = true;
                    } else {
                        foreach ($sku as $k1 => $v1) {
                            $v1['sku_name'] = '';
                            if ($v1['attr']) {

                                $v1['attr'] = Dever::json_decode($v1['attr']);
                                if ($v1['attr']) {
                                    $v1['attr'] = Dever::load('attr/api')->getInfoByJson($v1['attr']);
                                    $v1['sku_name'] = $v1['attr']['string'];
                                }
                            }

                            $name = $v['name'];
                            if ($v1['sku_name']) {
                                $name .= '-' . $v1['sku_name'];
                            }

                            $children = array
                            (
                                'id' => $v['id'] . '-' . $v1['id'],
                                'name' => $name,
                                'price' => $v1['price'] ? $v1['price'] : 0,
                                'price_type' => $v['price_type'],
                                'del' => 1,
                                'end' => true,
                            );
                            $other_sku_where = array($type => $id, $col => $v['id'], 'sku_id' => $v1['id']);
                            if ($col == 'type_id') {
                                $other_sku_where['type'] = 1;
                            }
                            $give = Dever::input('give');
                            if ($give) {
                                $other_sku_where['give'] = $give;
                            } elseif ($table == 'setting/role_goods') {
                                $where_type = Dever::input('type', 1);
                                if ($where_type) {
                                    $other_sku_where['give'] = $where_type;
                                }
                            }
                            if ($table == 'agent/order_goods') {
                                $where_type = Dever::input('type', 1);
                                if ($where_type) {
                                    $other_sku_where['type'] = $where_type;
                                }
                            }

                            $other_sku = Dever::db($table)->find($other_sku_where);

                            $children['select'] = 2;
                            if ($type == 'factory_id') {
                                $children['total'] = 0;
                                $children['p_price'] = $v1['price'];
                                if ($other_sku) {
                                    $children['p_price'] = $other_sku['p_price'];
                                    $children['select'] = $other_sku['state'];
                                }
                            } else {
                                if ($type == 'store_id') {
                                    $children['price'] = $v1['f_price'];
                                }
                                if ($type == 'store_id' || $type == 'shop_id') {
                                    $children['price_template'] = $result[$i]['price_template'];
                                    $children['price_template_sku'] = Dever::db('price/goods_sku')->getDataByPrice(array('goods_id' => $v['id'], 'sku_id' => $v1['id']));
                                    $children['price_template_id'] = (isset($other_sku['price_id']) && $other_sku['price_id']) ? $other_sku['price_id'] : 0;
                                }
                                $children['total'] = 0;
                                if ($other_sku) {
                                    if (isset($other_sku['min'])) {
                                        $children['min'] = $other_sku['min'];
                                    }
                                    if (isset($other_sku['total_num'])) {
                                        /*
                                        if ($col == 'type_id') {
                                            $children['total'] = $other_sku['total_num'];
                                        } else {
                                            $children['total'] = $other_sku['total_num'] - $other_sku['sell_num'];
                                        }
                                        */
                                        if (isset($other_sku['sell_num'])) {
                                            $children['total'] = $other_sku['total_num'] - $other_sku['sell_num'];
                                        } else {
                                            $children['total'] = $other_sku['total_num'];
                                        }
                                        
                                        
                                    } else {
                                        $children['total'] = $other_sku['num'];
                                    }
                                    
                                    $children['select'] = $other_sku['state'];
                                }
                            }

                            $children['total'] = $children['total']*$total;
                            $result[$i]['children'][] = $children;
                        }
                    }
                    $i++;
                }
            }
        }

        return $result;
    }
}