<?php namespace Vip\Src;

use Dever;

# 必须登录才能购买小刊
class Buy extends Base
{
	public function __construct()
    {
    	parent::__construct();

    	if (!$this->user) {
    		return $this->location('auth', array(), 'vip');
    	}
    }

    # 详细信息
    public function info()
    {
        $info = array();

        if ($this->user) {
            $info['mobile'] = $this->user['mobile'];
        } else {
            $info['mobile'] = '';
        }

        $vip = Dever::db('vip/config')->one(array('cate_id' => $this->cate));

        # 下载地址
        $info['download'] = $vip['download'];
        # 下载二维码
        $info['download_code'] = $vip['code'];
        # 名称
        $info['name'] = '点击下载“'.$vip['name'].'”APP查看电子刊';

        # 刷新当前页面
        $info['unpay'] = $this->url('pay', array(), 'vip');
        # 跳转至成功页面
        if ($this->source_type == 'android') {
            $info['ypay'] = 'javascript:JstyleApp.goBack();';
        } elseif ($this->source_type == 'ios') {
            $info['ypay'] = 'jstyle://goback';
        } else {
            $info['ypay'] = $this->url('success', array('buy' => -1, 'order_id' => -1), 'vip');
        }

        $info['buy_name'] = '';
        $info['buy_url'] = '';
        $info['buy_id'] = '';
        $info['order_id'] = '';
        $buy_id = Dever::input('buy_id');
        $order_id = Dever::input('order_id');
        if ($buy_id) {
            $buy = Dever::db('vip/time')->one($buy_id);
            if ($buy) {
                $info['buy_name'] = 'VIP会员' . $buy['name'] . ': <em>¥'.$buy['cash'].'</em>';
                $info['buy_url'] = $this->url('api.check', array('buy_id' => $buy_id, 'order_id' => $order_id), 'vip');
                $info['buy_id'] = $buy_id;
                $info['order_id'] = $order_id;
            }
        }
    
        # 是否显示弹窗
        $info['open'] = Dever::input('open', 1); 
        
        return $info;
    }

    # 购买页
    public function buy()
    {
        $data = Dever::db('vip/time')->state(array('cate_id' => $this->cate));

        if ($data) {
            $i = 0;
            foreach ($data as $k => $v) {

                $data[$k]['title'] = $data[$k]['name'];
                $data[$k]['price'] = '<i>￥</i><b>'.$v['cash'].'</b><em>￥' . $v['price'] . '</em>';

                if ($v['type'] == 2) {
                    $data[$k]['name'] .= '<i></i>';
                }
                if ($i == 0) {
                    $data[$k]['active'] = 'active';
                } else {
                    $data[$k]['active'] = '';
                }

                $data[$k]['cash_str'] = number_format($v['cash'], 2);
                
                $data[$k]['pay_url'] = $this->url('buy.pay', array('buy_id' => $v['id']), 'vip');

                $i++;
            }
        }
        return $data;
    }

    # 购买
    public function pay()
    {
        $uid = $this->user['id'];

        $buy_id = Dever::input('buy_id');
        $mobile = Dever::input('mobile');
        $type = Dever::input('type');

        if (!$buy_id) {
            Dever::alert('请传入正确的支付ID');
        }
        if (!$mobile) {
            Dever::alert('请传入正确的手机号');
        }

        $user = Dever::db('passport/user')->one(array('mobile' => $mobile));
        $buy = Dever::db('vip/time')->one($buy_id);

        if (!$buy || !$user) {
            Dever::alert('您输入的手机号还未注册');
        }

        if ($type == 'wechat') {
            $wechat = Dever::db('passport/wechat')->one(array('uid' => $uid, 'type' => 2));
        } elseif ($type != 'alipay') {
            Dever::alert('请传入正确的信息');
        }
        

        $order_id = $this->createOrder();
        $order_data['cate_id'] = $this->cate;
        $order_data['order_id'] = $order_id;
        $order_data['buy_id'] = $buy_id;
        $order_data['uid'] = $uid;
        $order_data['mobile'] = $mobile;
        $order_data['type'] = 1;
        $order_data['status'] = 1;

        $order_data['name'] = $buy['name'];
        $order_data['cash'] = $buy['cash'];

        # 仅供参考 以实际支付时间为准
        $order_data['start'] = time();
        $order_data['end'] = Dever::load('vip/lib/member')->getEnd($buy['time'], $buy['time_type'], $order_data['start']);

        $order_data['system'] = 2;
        if ($this->source) {
        	$order_data['source'] = $this->source;
        }

        if ($this->seller) {
            $order_data['seller_id'] = $this->seller;
        }

        $id = Dever::db('vip/order')->insert($order_data);

        if (!$id) {
            Dever::alert('支付失败');
        }

        //$param参数
        //$refer = $this->url('success', array('buy_id' => $buy_id, 'order_id' => $id), 'vip');
        $refer = $this->url('api.checkOrder', array('buy_id' => $buy_id, 'order_id' => $id), 'vip');
        $refer = str_replace('http:', 'https:', $refer);
        $param = array
        (
            'account_id' => 2,
            'project_id' => 2,
            'uid' => $uid,
            'username' => $user['username'],
            'name' => $order_data['name'],
            'cash' => $order_data['cash'],
            //'cash' => '0.01',
            'product_id' => $buy_id,
            'order_id' => $order_id,
            'refer' => urlencode($refer),
            'ip' => Dever::ip()
        );

        if (isset($wechat) && $wechat) {
            $param['account_id'] = 2;
            $param['openid'] = $wechat['openid'];
        } elseif ($type == 'alipay') {
            $param['account_id'] = 6;
        }

        if ($this->signature) {
            # 如果是app，则必须走h5
            $param['h5'] = 1;
        }

        if ($this->source_type) {
            //$param['refer'] = $this->source_type;
        }

        return Dever::load('pay/api.page', $param);
    }
}