<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Reward;
use App\Models\RewardPrice;
use Carbon\Carbon;


class RewardController extends Controller
{
    public $price = 0;

    public function __construct() {
        $rewardPrice = RewardPrice::find(1);
        $this->price = $rewardPrice->price;
    }

    public function getPrice(Request $request) {
        $rewardPrice = RewardPrice::find(1);
        return response()->json([
            "status" => 0,
            "data" => [
                "price" => $rewardPrice->price
            ]
        ]);
    }

    public function price(Request $request) {
        $price = $request->input("price");
        if($price) {
            $rewardPrice = RewardPrice::find(1);
            $rewardPrice->price = $price;
            $rewardPrice->save();
            $this->price = $price;

            return response()->json([
                "status" => 0,
                "message" => '修改成功'
            ]);
        } else {
            return Response()->json([
                "status" => -1,
                "message" => "信息不能为空!"
            ]);
        }
    }

    public function reward(Request $request) {

        $wechat_id = $request->header("openid");
        if($wechat_id) {
            $price = $this->price;
            $order_type = "reward";
            $reward = new Reward;
            $reward->status = 0;
            $reward->price = $price;
            $reward->wechat_id = $wechat_id;
            $reward->save();
            $order_id = $reward->id;

            $order = CommonController::getRecord($order_type, $reward->id);

            // 先判断微信交易是否失效，失效的话需要重新生成份新的系统内部订单，再重新统一下单
            if ($wechat_id && $order) {
                // 判断微信交易是否失效
                if(Carbon::now()->subHour()->gt($order->created_at)) {
                    $order_new = clone $order;
                    $order_new->id = null;
                    $order_new->exists = false;
                    $order_new->save();
                    $order->delete();
                    $order_id = $order_new->id;
                }


                $out_trade_no = $order_type . "-" . $order_id;
                $app = app('wechat.payment');

                $result = $app->order->unify([
                    'body' => '玥子轩家政',
                    'out_trade_no' => $out_trade_no,
                    'total_fee' => $order->price * 100,
                    'trade_type' => 'JSAPI',
                    'openid' => $wechat_id,
                    'notify_url' => 'https://wechat.sotype.com/api/payments/wechat-notify',
                    'time_expire' => Carbon::now()->addHour()->format('YmdHis')
                ]);

                if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
                    //第二次签名
                    //$result = $app->jssdk->appConfig($result['prepay_id']);
                    //$config = $app->configForAppPayment($result['prepay_id']);
                    $config = $app->jssdk->sdkConfig($result['prepay_id']);
                    return response()->json([
                        "status" => 0,
                        "code" => "success",
                        "msg" => $config,
                        "out_trade_no" => $out_trade_no
                    ]);
                } else {
                    return response()->json([
                        "status" => -1,
                        "message" => '微信支付签名失败:' . var_export($result, 1)
                    ]);
                }

            }
        }
    }
}
