<?php namespace Place_source\Lib;
use Dever;
use Place;
class Price
{
    public function get($info, $type, $button)
    {
        $num = Dever::input('num', 'is_numeric', '数量', 1);
        $result = array();
        $result['status'] = 1;
        $result['text'] = '';
        $result['original'] = $info['price'];
        $result['value'] = $info['price'];
        $result['button'] = $button;
        $result['num'] = $num;
        $result['list'] = array();
        $order = false;
        $order = Dever::db('source', 'place_order')->find(array('source_id' => $info['id'], 'uid' => Place::$uid, 'status' => 2));
        if ($order) {
            $result['value'] = $order['cash'];
        } else {

            $role = Dever::load('role', 'place_user')->getSourcePrice($info['price'], $info['id'], $type);
            
            $role = Dever::load('role', 'place_user')->getSourcePrice($info['price'], $info['id'], $type);
            if ($this->getAlone($role, $result)) {
                return $result;
            }
            if ($info['price'] > 0) {
                $this->getList($vip, $agent, $result);
                $info['price'] = $this->getDiscount($info['price'], $vip['list'], $agent['list']);
                if ($info['price'] > 0) {
                    if (isset($info['password'])) {
                        $result['password'] = 2;
                        if ($info['password']) {
                            $result['password'] = 1;
                        }
                    }
                    $result['status'] = 2;
                    $result['value'] = $info['price'];
                }
            }
        }
        $result['original'] = $result['original'] * $result['num'];
        $result['value'] = $result['value'] * $result['num'];
        $result['original_text'] = $this->getText($result['original'], $type);
        $result['text'] = $this->getText($result['value'], $type);
        return $result;
    }

    # 获取价格列表
    public function getList($vip, $agent, &$result)
    {
        if ($vip['list']) {
            $result['list'][] = array('name' => $vip['name'], 'list' => array_values($vip['list']));
        }
        if ($agent['list']) {
            $result['list'][] = array('name' => $agent['name'], 'list' => array_values($agent['list']));
        }
    }

    # 获取专享
    public function getAlone($vip, $agent, &$result)
    {
        $state = false;
        if ($vip['alone']) {
            if (isset($vip['alone'][Place::$user['vip_id']])) {
                $state = false;
            } else {
                $state = true;
                $result['list'][] = array('name' => $vip['name'], 'list' => array_values($vip['alone']));
            }
        }
        if ($agent['alone']) {
            if (isset($agent['alone'][Place::$user['agent_id']])) {
                $state = false;
            } else {
                $state = true;
                $result['list'][] = array('name' => $agent['name'], 'list' => array_values($agent['alone']));
            }
        }
        if ($state) {
            $result['status'] = 3;
            $result['text'] = '专享';
        }
        return $state;
    }

    # 获取折扣价
    public function getDiscount($price, $vip, $agent)
    {
        $result = array();
        if (Place::$uid) {
            # 获取会员价
            if ($vip && isset($vip[Place::$user['vip_id']])) {
                $price = $vip[Place::$user['vip_id']][1];
            }
            # 获取代理价
            if ($agent && isset($agent[Place::$user['agent_id']])) {
                $agent_price = $agent[Place::$user['agent_id']][1];
                if ($agent_price < $price) {
                    $price = $agent_price;
                }
            }
        }
        
        # 获取折扣+代金券的价格，暂时没有
        return $price;
    }

    public function getText($price, $type = 1)
    {
        if ($price > 0) {
            if ($type == 3) {
                $price = Place::$info['money_name'] . $price;
            } else {
                $price = $price . Place::$info['score_name'];
            }
        } else {
            $price = '免费';
        }
        return $price;
    }

    public function getValue($value, $price, $type)
    {
        if ($type == 3) {
            return $value ? number_format($price * ($value/100), 2) : 0.00;
        } elseif ($value || $value === '0') {
            return number_format($value, 2);
        }
        return $price;
    }
}