<?php

namespace Main\Src;

use Dever;
use Main\Lib\Core;

class Vip extends Core
{
    # 获取会员的信息接口
    public function config()
    {
        $this->data['vip_config'] = Dever::db('vip/config')->one();

        $this->data['vip_time'] = Dever::db('vip/time')->getAll();


        if ($this->data['uid'] > 0) {
            $user = Dever::load('passport/api')->info($this->data['uid']);
            $this->data['mobile'] = $user['mobile'];
        } else {
            $this->data['mobile'] = '';
        }

        return $this->data;
    }

    # 购买
    public function buy_action()
    {
        if (!$this->data['uid']) {
            Dever::alert('错误的用户信息');
        }
        $source_uid = $this->getSourceUid(false);
        $buy_id = Dever::input('buy_id');
        $mobile = Dever::input('mobile');
        $type = Dever::input('type');
        $cate = Dever::load('main/journal')->getCate();

        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' => $this->data['uid'], 'type' => 1, 'system_id' => $this->data['system']));
        } elseif ($type != 'alipay') {
            Dever::alert('请传入正确的信息');
        }

        $source_type = Dever::input('source_type');

        if ($source_type == 'ios') {
            $method = 'apple';
            $order_data['system'] = 3;
            $account_id = 3;
            # 使用苹果内购支付
            $receipt = Dever::input('receipt');
            if (!$receipt) {
                Dever::alert('苹果内购支付失败，没有receipt参数');
            }
        } elseif ($source_type == 'android') {
            $method = 'app';
            $order_data['system'] = 4;
            $account_id = 3;
        } else {
            $method = 'applet';
            $order_data['system'] = 1;
            $account_id = 1;
            # 根据cate_id走不同的支付id，暂时不需要
            if ($cate && $cate['id'] == 2) {
                $account_id = 4;
            } elseif ($cate && $cate['id'] == 3) {
                $account_id = 5;
            }
            //$account_id = $cate['pay_id'];
        }

        $order_id = $this->createOrder();

        $order_data['cate_id'] = $cate['id'];
        $order_data['order_id'] = $order_id;
        $order_data['buy_id'] = $buy_id;
        $order_data['uid'] = $this->data['uid'];
        $order_data['mobile'] = $mobile;

        $order_data['type'] = 1;
        $order_data['status'] = 1;
        $order_data['name'] = $buy['name'];
        $order_data['cash'] = $buy['cash'];

        $source = Dever::input('source');
        if ($source) {
            $order_data['source'] = $source;
        }
        $seller = Dever::input('seller');
        if ($seller) {
            $order_data['seller_id'] = $seller;
        }

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

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

        //$param参数
        $param = array
        (
            'account_id' => $account_id,
            'project_id' => 2,
            'uid' => $this->data['uid'],
            'username' => $user['username'],
            'name' => $order_data['name'],
            'cash' => $order_data['cash'],
            //'cash' => '0.01',
            'product_id' => $buy_id,
            'order_id' => $order_id,
        );

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

        if ($method == 'apple') {
            $param['other'] = $receipt;
        }

        $this->data['pay'] = Dever::load('pay/api.' . $method, $param);

        if ($order_data['system'] == 1) {
            if (isset($this->data['pay']['prepay_id'])) {
                Dever::load('act/lib/form')->submit($this->data['uid'], $this->data['pay']['prepay_id'], 2, $this->data['system']);
            } else {
                Dever::alert('支付失败');
            }
        }

        return $this->data;
    }

    private function createOrder($prefix = 'HY')
    {
        $order = Dever::order($prefix);

        //$order = Dever::rand(2, 2) . Dever::rand(6, 0);

        $info = Dever::db('vip/order')->one(array('order_id' => $order));

        if ($info) {
            return $this->createOrder($prefix);
        }

        return $order;
    }
}