<?php
namespace app\api\controller;
use think\facade\Lang;
/**
 * ============================================================================
 * 
 * ============================================================================
 * 版权所有 2014-2028 浙江惠利玛产业互联网有限公司，并保留所有权利。
 * 网站地址: https://www.valimart.net/
 * ----------------------------------------------------------------------------
 * 
 * ============================================================================
 * 虚拟购买控制器
 */
class Membervrbuy extends MobileMember
{
    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        Lang::load(base_path() . 'home/lang/'.config('lang.default_lang').'/buy.lang.php');
    }



    /**
     * @api {POST} api/Membervrbuy/buy_step1 虚拟商品购买第一步，设置接收手机号
     * @apiVersion 1.0.0
     * @apiGroup Membervrbuy
     *
     * @apiHeader {String} X-DS-KEY 用户授权token
     *
     * @apiParam {Int} goods_id 商品ID
     * @apiParam {Int} quantity 数量
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     * @apiSuccess {Object} result.member_info  用户信息
     * @apiSuccess {Boolean} result.member_info.member_paypwd  是否已设置支付密码 true是false否
     * @apiSuccess {String} result.member_info.member_mobile  手机号
     * @apiSuccess {Float} result.member_info.available_predeposit  可用预存款余额
     * @apiSuccess {Float} result.member_info.available_rc_balance  可用充值卡余额
     */
    public function buy_step1()
    {
       if (!$this->member_info['is_buylimit']) {
            ds_json_encode(10001,lang('cart_buy_noallow'));
        }
        if(config('ds_config.member_auth') && $this->member_info['member_auth_state']!=3){
            ds_json_encode(10001,lang('cart_buy_noauth'));
        }
        $extra = array();
        //额外数据用来处理拼团等其他活动
        $pintuan_id = intval(input('param.pintuan_id'));
        if($pintuan_id>0){
            $extra['pintuan_id'] = $pintuan_id;#拼团ID
            #是否为开团订单
            $extra['pintuangroup_id'] = empty(input('param.pintuangroup_id')) ? 0:intval(input('param.pintuangroup_id'));
        }
        $logic_buyvirtual = model('buyvirtual','logic');
        $result = $logic_buyvirtual->getBuyStep2Data(input('post.goods_id'), input('post.quantity'), $this->member_info['member_id'],$extra);
        if (!$result['code']) {
            ds_json_encode(10001,$result['msg']);
        }
        else {
            $result = $result['data'];
            $member_info = array();
            $member_info['member_paypwd'] = $result['member_info']['member_paypwd'] ? true : false;
            $member_info['member_mobile'] = $result['member_info']['member_mobile'];
            $member_info['available_predeposit'] = $result['member_info']['available_predeposit'];
            $member_info['available_rc_balance'] = $result['member_info']['available_rc_balance'];
            unset($result['member_info']);
            $result['member_info'] = $member_info;
      
            
            
            if (isset($result['store_voucher_list']) && is_array($result['store_voucher_list']) && count($result['store_voucher_list']) > 0) {
                current($result['store_voucher_list']);
                $result['goods_info']['store_voucher_info'] = reset($result['store_voucher_list']);
                $result['goods_info']['store_voucher_info']['voucher_price'] = ds_price_format($result['goods_info']['store_voucher_info']['voucher_price']);
                $result['goods_info']['store_voucher_info']['voucher_enddate_text']=date('Y年m月d日',$result['goods_info']['store_voucher_info']['voucher_enddate']);
            }
            else {
                $result['goods_info']['store_voucher_info'] = array();
            }

            $result['goods_info']['store_voucher_list'] = isset($result['store_voucher_list'])?array_values($result['store_voucher_list']):array();
            
            ds_json_encode(10000, '', $result);
        }
    }


    /**
     * @api {POST} api/Membervrbuy/buy_step1 虚拟订单第二步，产生订单
     * @apiVersion 1.0.0
     * @apiGroup Membervrbuy
     *
     * @apiHeader {String} X-DS-KEY 用户授权token
     *
     * @apiParam {Int} goods_id 商品ID
     * @apiParam {Int} quantity 数量
     * @apiParam {String} buyer_phone 手机号
     * @apiParam {String} buyer_msg 买家留言
     * @apiParam {String} password 密码
     * @apiParam {String} rcb_pay 使用充值卡支付0是/1否
     * @apiParam {String} pd_pay 使用预存款支付0是/1否
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     * @apiSuccess {Int} result.order_id  订单ID
     * @apiSuccess {String} result.order_sn  订单编号
     */
    public function buy_step2()
    {
        $logic_buyvirtual = model('buyvirtual','logic');
        $input = array();
        $input['goods_id'] = input('post.goods_id');
        $input['quantity'] = input('post.quantity');
        $input['buyer_phone'] = input('post.buyer_phone');
        $input['buyer_msg'] = input('post.buyer_msg');
        //支付密码
        $input['password'] = !empty(input('post.password'))?input('post.password'):'';

        //是否使用充值卡支付0是/1否
        $input['rcb_pay'] = empty(input('post.rcb_pay'))?'0':'1';

        //是否使用预存款支付0是/1否
        $input['pd_pay'] = empty(input('post.pd_pay'))?'0':'1';

        $input['pintuan_id'] = input('post.pintuan_id');
        $input['pintuangroup_id'] = input('post.pintuangroup_id');
        
        $input['order_from'] = 2;
                
        $input['voucher'] = input('post.voucher');
        
        $result = $logic_buyvirtual->buyStep3($input, $this->member_info['member_id']);
        if (!$result['code']) {
            ds_json_encode(10001,$result['msg']);
        }
        else {
            ds_json_encode(10000, '', $result['data']);
        }
    }


    /**
     * 验证密码
     */
    public function check_password()
    {
        if (empty(input('post.password'))) {
            ds_json_encode(10001,lang('param_error'));
        }

        $member_model = model('member');

        $member_info = $member_model->getMemberInfoByID($this->member_info['member_id']);
        if ($member_info['member_paypwd'] == md5(input('post.password'))) {
            ds_json_encode(10000, '', 1);
        }
        else {
            ds_json_encode(10001,lang('password_mistake'));
        }
    }

    /**
     * 更换收货地址
     */
    public function change_address()
    {
        $logic_buy = model('buy','logic');
        $city_id=0;
        if (empty(input('post.city_id'))) {
            $city_id = input('post.area_id');
        }

        $data = $logic_buy->changeAddr(input('post.freight_hash'), $city_id, input('post.area_id'), $this->member_info['member_id']);
        if (!empty($data) && $data['state'] == 'success') {
            ds_json_encode(10000, '', $data);
        }
        else {
            ds_json_encode(10001,lang('param_error'));
        }
    }


    /**
     * @api {POST} api/Membervrbuy/pay 支付方式
     * @apiVersion 1.0.0
     * @apiGroup Membervrbuy
     *
     * @apiHeader {String} X-DS-KEY 用户授权token
     *
     * @apiParam {String} pay_sn 支付单号
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     * @apiSuccess {Float} result.pay_info 支付信息
     * @apiSuccess {Float} result.pay_info.member_available_pd  预存款余额
     * @apiSuccess {Float} result.pay_info.member_available_rcb  充值卡余额
     * @apiSuccess {Boolean} result.pay_info.member_paypwd  已设置支付密码 true是false否
     * @apiSuccess {Float} result.pay_info.pay_amount  支付金额
     * @apiSuccess {String} result.pay_info.pay_sn  支付单号
     * @apiSuccess {Float} result.pay_info.payed_amount  已支付金额
     * @apiSuccess {Object[]} result.pay_info.payment_list  支付方式列表
     * @apiSuccess {String} result.pay_info.payment_list.payment_code  支付方式代码
     * @apiSuccess {String} result.pay_info.payment_list.payment_name  支付方式名称
     * @apiSuccess {String} result.pay_info.payment_list.payment_platform  支付方式适用平台
     */
    public function pay()
    {
        $pay_sn = input('post.pay_sn');
        $condition = array();
        $condition[] = array('order_sn','=',$pay_sn);
        $order_info = model('vrorder')->getVrorderInfo($condition);
        
        $condition = array();
        $condition[] = array('payment_platform','=','h5');
        $payment_list = model('payment')->getPaymentOpenList($condition);

        $pay_info['pay_amount'] = $order_info['order_amount'];
        $pay_info['member_available_pd'] = $this->member_info['available_predeposit'];
        $pay_info['member_available_rcb'] = $this->member_info['available_rc_balance'];

        $pay_info['member_paypwd'] = true;
        if (empty($this->member_info['member_paypwd'])) {
            $pay_info['member_paypwd'] = false;
        }

        $pay_info['pay_sn'] = $order_info['order_sn'];
        $pay_info['payed_amount'] = $order_info['pd_amount']+$order_info['rcb_amount'];
        if ($pay_info['payed_amount'] > '0.00') {
            $pay_info['pay_amount'] = $pay_info['pay_amount'] - $pay_info['payed_amount'];
        }
        
        //如果线上线下支付金额都为0，转到支付成功页
        if (empty($pay_info['pay_amount'])) {
            ds_json_encode(12001,lang('pay_repeat'));
        }

        $pay_in["pay_info"] = $pay_info;
        $pay_in["pay_info"]["payment_list"] = $payment_list;
        ds_json_encode(10000, '', $pay_in);
    }

    /**
     * 支付密码确认
     */
    public function check_pd_pwd()
    {
        if ($this->member_info['member_paypwd'] != md5(input('post.password'))) {
            ds_json_encode(10001,lang('payment_password_error'));
        }
        else {
            ds_json_encode(10000, '', 'OK');
        }
    }
}