<?php

/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 16/8/25
 * Time: 下午4:18
 */


require_once(BASE_ROOT_PATH . '/helper/brand_helper.php');
require_once (BASE_ROOT_PATH . '/helper/user_session/fcode.php');


abstract class common_base
{
    protected $commonid;
    protected $common_info;
    protected $store_id;

    public function __construct($param)
    {
        if (is_array($param)) {
            $this->common_info = $param;
            $this->commonid = intval($this->common_info['goods_commonid']);
            $this->store_id = intval($this->common_info['store_id']);
        }
    }

    public abstract function format();
}

class common_sumary extends common_base
{
    public function __construct($param)
    {
        parent::__construct($param);
    }

    public function format()
    {
        $ret['goods_commonid'] = intval($this->common_info['goods_commonid']);
        $ret['goods_commend']  = intval($this->common_info['goods_commend']);
        $ret['comments']       = intval($this->common_info['comments']);

        $brand_id = intval($this->common_info['brand_id']);
        $ret['brand_id'] = $brand_id;
        $ret['brand_name'] = brand_helper::instance()->name($brand_id);

        $ret['type_id'] = intval($this->common_info['type_id']);
        $ret['gc_id'] = intval($this->common_info['gc_id_3']);
        $ret['goods_selltime'] = intval($this->common_info['goods_selltime']);
        $ret['goods_freight'] = doubleval($this->common_info['goods_freight']);

        $is_fcode = intval($this->common_info['is_fcode']) == 1 ? true : false;
        $ret['is_fcode'] = $is_fcode;

        if($is_fcode) {
            $fcode = new user_session\fcode();
            $ret['has_fcode'] = $fcode->usable_num($this->commonid,$lock_num) == false ? false : true;
        } else {
            $ret['has_fcode'] = false;
        }

        if($ret['has_fcode']) {
            $ret['fcode_desc'] = "你有F码，可以购买";
        }
        else
        {
            if(isset($lock_num) && $lock_num > 0) {
                $ret['fcode_desc'] = "F码处于锁定状态，激活后可以购买";
            } else {
                $ret['fcode_desc'] = "你没有该商品的F码";
            }
        }

        return $ret;
    }
}

class common_spec extends common_base
{
    private $have_spec;
    private $spec_name;
    private $spec_list;
    private $attr_list;

    public function __construct($param,$goods_list)
    {
        parent::__construct($param);

        $spec_name = unserialize($this->common_info['spec_name']);
        if(empty($spec_name)) {
            $this->have_spec = false;
            $this->spec_name = '';
        }
        else
        {
            $this->have_spec = true;
            foreach ($spec_name as $spid => $spname) {
                $this->spec_name = $spname;
                break;
            }
        }

        $spec_value = unserialize($this->common_info['spec_value']);
        if(!empty($spec_value))
        {
            foreach ($spec_value as $sp_id => $sp_val)
            {
                foreach ($sp_val as $spv_id => $spv_name) {
                    $this->spec_list[$spv_id]['spv_name'] = $spv_name;
                }
            }
        }
        else
        {
            $this->spec_list = array();
        }

        foreach ($goods_list as $value)
        {
            $goods_id = intval($value['goods_id']);
            $spec = unserialize($value['goods_spec']);
            if(!empty($spec))
            {
                foreach ($spec as $spv_id => $spv_name) {
                    $this->spec_list[$spv_id]['goods_id'] = $goods_id;
                }
            }
        }

        $attrs = unserialize($this->common_info['goods_attr']);
        $this->attr_list = [];
        if(!empty($attrs))
        {
            foreach ($attrs as $key => $val)
            {
                foreach ($val as $attr_key => $attr_val)
                {
                    if($attr_key === 'name') {
                        $name = $attr_val;
                    } else {
                        $value = $attr_val;
                    }
                }
                $this->attr_list[] = ["name" => $name,"value" => $value];
            }
        }
    }

    public function format()
    {
        $ret['have_spec'] = $this->have_spec;
        $ret['spec_name'] = $this->spec_name;
        $ret['skus'] = array();
        foreach ($this->spec_list as $key => $value) {
            array_push($ret['skus'],$value);
        }
        $ret['attrs'] = $this->attr_list;

        return $ret;
    }
}

class goods_common extends common_base
{
    private $goods_list;
    private $goods_ids;
    private $show_gid;

    public function __construct($common_info,$goods_list,$show_gid)
    {
        parent::__construct($common_info);
        $this->goods_list = $goods_list;
        $this->goods_ids = $this->goods_ids();
        $show_gid = intval($show_gid);
        if(in_array($show_gid,$this->goods_ids)) {
            $this->show_gid = $show_gid;
        } else {
            $this->show_gid = $this->goods_ids[0];
        }
    }

    public function goods_ids()
    {
        $ids = array();
        foreach ($this->goods_list as $value) {
            $gid = intval($value['goods_id']);
            array_push($ids,$gid);
        }
        return $ids;
    }
    static private function img_url($value,$store_id)
    {
        return cthumb($value, 1280, $store_id);
    }

    private function goods_images()
    {
        $mod = Model('goods');
        $items = $mod->getGoodsImageListEx(array('goods_commonid' => $this->commonid));

        $images = array();
        foreach ($items as $val) {
            $img = $val['goods_image'];
            $images[] = self::img_url($img,$this->store_id);
        }
        return $images;
    }

    public function format()
    {
        $summary = new common_sumary($this->common_info);
        $base_info = $summary->format();

        $goods_spec = new common_spec($this->common_info,$this->goods_list);
        $spec = $goods_spec->format();

        $ret = array_merge($base_info,$spec);
        $ret['images'] = $this->goods_images();
        $ret['show_goods'] = $this->show_gid;

        return $ret;
    }
}