<?php

namespace App\Http\Controllers\Pay;


use App\Cache\StatisticCache;
use App\Facade\Site;
use App\Libs\ApiResponse;
use App\Services\Pay\ProductService;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;

/**
 * 充值商品
 *
 */
class ProductController extends BaseController
{
    use ApiResponse;

    protected $productService;

    public function __construct(
        ProductService $productService
    )
    {
        $this->productService = $productService;
    }

    public function charge_list(Request $request)
    {
        $template_id = 1;

        // 充值类型站点
        $channelType  = Site::getChannelType();
        if ($channelType === 'RECHARGE') {
            $sendOrderId  = Site::getSendOrderId();
            $channelPayId = Site::getChannelPayId();

            // 获取派单信息
            $sendOrder = $this->productService->getSendOrderById($sendOrderId);
            $template_id = getProp($sendOrder, 'pay_id', $channelPayId);
        }

        \Log::info('chargeList:template_id:' . $template_id);

        // 获取模板充值列表
        $charge_list = ProductService::getChargeProduct($template_id);
        \Log::info('$charge_list:' . json_encode($charge_list));
        if (!$charge_list) {
            return response()->error('WAP_SYS_ERROR');
        }

        $data = [];
        foreach ($charge_list as $v) {
            $temp   = [
                'price'           => $v->price,
                'price_desc'      => $v->price_desc,
                'name_desc'       => $v->name_desc,
                'given'           => $v->given,
                'is_default'      => $v->is_default,
                'angle_sign_text' => $v->angle_sign_text,
                'type'            => $v->type,
                'product_id'      => $v->id,
            ];
            $data[] = $temp;
        }

        // 统计(uv|pv)
        StatisticCache::setPV('charge_list');
        $uid = Site::getUid();
        if ($uid) StatisticCache::setUV('charge_list', $uid);

        return response()->success($data);
    }

}
