<?php

namespace Goods\Lib;

use Dever;

class Set
{
    # 获取商品及其属性列表
    public function getGoodsList($id, $type, $table, $col = 'goods_id')
    {
        $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;
                    }
                    $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') {
                        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 ($other) {
                            if (isset($other['min'])) {
                                $result[$i]['min'] = $other['min'];
                            }
                            if (isset($other['total_num'])) {
                                $result[$i]['total'] = $other['total_num'] - $other['sell_num'];
                            } else {
                                $result[$i]['total'] = $other['num'];
                            }
                            
                            $result[$i]['select'] = $other['state'];
                        } else {
                            $result[$i]['total'] = 0;
                        }
                    }
                    $result[$i]['children'] = array();
                    $num = count($sku);
                    if ($num == 1 && $sku[0]['key'] == -1) {
                        //$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;
                            }
                            $other_sku = Dever::db($table)->find($other_sku_where);

                            $children['select'] = 2;
                            if ($type == 'factory_id') {
                                $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'];
                                }
                                $children['total'] = 0;
                                if ($other_sku) {
                                    if (isset($other_sku['min'])) {
                                        $children['min'] = $other_sku['min'];
                                    }
                                    if (isset($other_sku['total_num'])) {
                                        $children['total'] = $other_sku['total_num'] - $other_sku['sell_num'];
                                    } else {
                                        $children['total'] = $other_sku['num'];
                                    }
                                    
                                    $children['select'] = $other_sku['state'];
                                }
                            }

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

        return $result;
    }
}