<?php
/*
 * @author: CloudZA(云之安) <2922619853@qq.com>
 * @hitokoto: 一场秋雨一场凉，秋心酌满泪为霜。
 * Copyright (c) 2022 by CloudZA, All Rights Reserved.
 */

$title = '支付结果';
require_once '../partials/head.php';
$epay          = new EpayCore([
    'apiurl' => __EPAY_URL__ ,
    'pid'    => __EPAY_APPID__ ,
    'key'    => __EPAY_KEY__ ,
]);
$verify_result = $epay->verifyReturn();
if ($verify_result) {// TODO:验证成功
    // TODO: 商户订单号
    $out_trade_no = $_GET[ 'out_trade_no' ];
    // TODO: 支付宝交易号
    $trade_no = $_GET[ 'trade_no' ];
    // TODO: 交易状态
    $trade_status = isset($_GET[ 'trade_status' ]);
    // TODO: 支付方式
    $type = $_GET[ 'type' ];
    //    if ($trade_status == 'TRADE_SUCCESS') {
    //        // TODO: 判断该笔订单是否在商户网站中已经做过处理
    //        // TODO: 如果没有做过处理，根据订单号（out_trade_no）在商户网站的订单系统中查到该笔订单的详细，并执行商户的业务程序
    //        // TODO: 如果有做过处理，不执行商户的业务程序
    //    }

    // 更新订单状态
    if ( !Db::table('app_order')
            ->where([
                'pay_order'  => $out_trade_no ,
                'pay_status' => 1
            ])
            ->find()) {
        Db::table('app_order')
          ->where(['pay_order' => $out_trade_no])
          ->update([
              'pay_orderno' => $trade_no ,
              'pay_status'  => 3
              // 处理中
          ]);
    }
    // 查询订单是否存在
    $query = Db::table('app_order')
               ->where([
                   'pay_order'   => $out_trade_no ,
                   'pay_orderno' => $trade_no
               ])
               ->find();
    if ( !$query) {
        $order_status = 0; // 不存在直接返回 0
    } else {
        // 获取订单内容的数据
        $query_data = json_decode($query[ 'pay_content' ] , true);
        // 判断订单状态
        switch ($query[ 'pay_status' ]) {
            case 3:
                // 1 | 开通会员 2 | 续费会员
                if ($query_data[ 'group_type' ] == 1) {
                    $action = openMember($query_data[ 'user_id' ] , NULL , $query_data[ 'plans_id' ]);
                    if ($action) {
                        $order_status = 1;
                    }
                } else if ($query_data[ 'group_type' ] == 2) {
                    $action = renewMemberWithChatLimit($query_data[ 'userid' ] , $query_data[ 'plans_period' ] , $query_data[ 'plans_chat_limit' ]);
                    if ($action) {
                        $order_status = 1;
                    }
                }
                if ($action) {
                    Db::table('app_order')
                      ->where(['pay_order' => $query[ 'pay_order' ]])
                      ->update(['pay_status' => 1]);
                }
                break;
            case 1:
                $order_status = 2;
                break;
            case 0:
                $order_status = 0;
                break;
        }

    }
} else {
    //验证失败
    $order_status = 0;
}

?>
    <div id="pjax-container">
        <div class="content">
            <div class="block block-rounded h-100 mb-2">
                <?php if ( $order_status === 1 || $order_status === 2 ): ?>
                <div class="rounded-top bg-image" style="background-image: url('../assets/media/image/photo@54.jpg');">
                    <div class="bg-primary-dark-op rounded-top">
                        <div class="content content-full text-center py-6">
                            <h1 class="h2 text-white mb-2">订单支付成功</h1>
                            <a class="btn btn-sm btn-alt-secondary mt-3" data-pjax="" href="index.php">
                                <i class="fa fa-fw fa-arrow-left text-primary"></i> 个人中心
                            </a>
                        </div>
                    </div>
                </div>
                <div class="block-content block-content-full">
                    <div class="block block-rounded block-bordered">
                        <div class="block-header border-bottom">
                            <h3 class="block-title">
                                <span>您的订单信息</span>
                            </h3>
                        </div>
                        <div class="block-content">
                            <ul class="nav nav-pills flex-column push">
                                <li class="nav-item mb-1">
                                    <div class="nav-link bg-gray-light d-flex justify-content-between align-items-center">
                                        <span class="text-muted fs-xs">商户订单号</span>
                                        <span class="badge rounded-pill bg-black-25 ms-1 fs-xs"><?php echo $query[ 'pay_order' ] ?></span>
                                    </div>
                                </li>
                                <li class="nav-item mb-1">
                                    <div class="nav-link bg-gray-light d-flex justify-content-between align-items-center">
                                        <span class="text-muted fs-xs">系统订单号</span>
                                        <span class="badge rounded-pill bg-black-25 ms-1 fs-xs"><?php echo $query[ 'pay_orderno' ] ?></span>
                                    </div>
                                </li>
                                <li class="nav-item mb-1">
                                    <div class="nav-link bg-gray-light d-flex justify-content-between align-items-center">
                                        <span class="text-muted fs-xs">支付方式</span>
                                        <span class="badge rounded-pill bg-black-25 ms-1 fs-xs"><?php echo displayPayType($type) ?></span>
                                    </div>
                                </li>
                                <li class="nav-item mb-1">
                                    <div class="nav-link bg-gray-light d-flex justify-content-between align-items-center">
                                        <span class="text-muted fs-xs">支付时间</span>
                                        <span class="badge rounded-pill bg-black-25 ms-1 fs-xs"><?php echo $query[ 'pay_time' ] ?></span>
                                    </div>
                                </li>
                                <li class="nav-item mb-1">
                                    <div class="nav-link bg-gray-light d-flex justify-content-between align-items-center">
                                        <span class="text-muted fs-xs">商品名称</span>
                                        <span class="badge rounded-pill bg-black-25 ms-1 fs-xs"><?php echo $query[ 'pay_name' ] ?></span>
                                    </div>
                                </li>
                                <li class="nav-item mb-1">
                                    <div class="nav-link bg-gray-light d-flex justify-content-between align-items-center">
                                        <span class="text-muted fs-xs">支付金额</span>
                                        <span class="badge rounded-pill bg-black-25 ms-1 fs-xs"><?php echo $query[ 'pay_money' ] ?> | RMB</span>
                                    </div>
                                </li>
                            </ul>
                            <?php else: ?>
                                <div class="block-content block-content-full">
                                    <div class="content content-full">
                                        <div class="py-4 text-center">
                                            <i class="si si-ghost text-primary display-3"></i>
                                            <h1 class="h2 fw-bold mt-4 mb-2 text-primary">Tips Info</h1>
                                            <h2 class="h5 fw-medium  mb-5 text-primary">未能找到您的订单信息</h2>
                                            <a class="btn btn-alt-primary fs-sm" data-pjax href="index.php"><i class="fa fa-arrow-right opacity-50 me-1"></i>返回</a>
                                        </div>
                                    </div>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
<? require_once '../partials/foot.php'; ?>