<?php

namespace App\Http\Controllers\App;

use App\Http\Controllers\Controller;
use App\Models\User;
use App\Models\Version;
use App\Services\ShopService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class ShopController extends Controller
{
    /**
     * 获取商品列表
     * @param Request $request
     * @param ShopService $shopService
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\JsonResponse
     */
    public function getGoodsList(Request $request, ShopService $shopService)
    {
        try {
            $keyword = $request->get('keyword');
            $page = $request->get('page', 1);
            $params = [
                'page' => $page,
                'keyword' => $keyword
            ];
            $res = $shopService->request('get', 'shops', $params);
            if (isset($res['code']) && $res['code'] == 1) {
                return $this->failure($res['message']);
            }
            $data = $res['data'] ?? [];
            //增加特定逻辑处理特定业务场景
            $version = $request->get('version');
            $version_status = Version::where('version', $version)->value('status');
            if ($version_status == Version::STATUS_CHECK) {
                foreach ($data['data'] as $index => $item) {
                    if ($item['id'] == 8192) {
                        unset($data['data'][$index]);
                    }
                }
                $data['data'] = array_values($data['data']);
            }
            return $this->success('ok', $data);
        } catch (\Exception $e) {
            Log::error('getGoodsList:' . $e->getMessage());
            return $this->failure('请求失败');
        }

    }

    /**
     * 获取商品详情
     * @param Request $request
     * @param ShopService $shopService
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\JsonResponse
     */
    public function getGoodsDetail(Request $request, ShopService $shopService)
    {
        try {
            $user = auth()->user();
//            $open_id = $user->wechat->openid;
            $goods_id = $request->get('id');
            if (empty($goods_id)) {
                return $this->failure('请选择商品');
            }
            $params = [
//                'openid' => $open_id
                'accid' => User::SAAS_PREFIX.$user->id
            ];
            $res = $shopService->request('get', "shops/$goods_id", $params);
            if (isset($res['code']) && $res['code'] == 1) {
                return $this->failure($res['message']);
            }
            $data = $res['data'] ?? [];
            return $this->success('ok', $data);
        } catch (\Exception $e) {
            Log::error('getGoodsDetail:' . $e->getMessage());
            return $this->failure('请求失败');
        }
    }

    /**
     * 获取订单列表
     * @param Request $request
     * @param ShopService $shopService
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\JsonResponse
     */
    public function getShopOrderList(Request $request, ShopService $shopService)
    {
        try {
            $user = auth()->user();
//            $open_id = $user->wechat->openid;
            $page = $request->get('page', 1);
//            if (empty($open_id)) {
//                return $this->failure('获取用户openid失败');
//            }
            $status = $request->get('status');
            $params = [
                'page' => $page,
//                'openid' => $open_id,
                'pay_status' => $status,
                'accid' => User::SAAS_PREFIX.$user->id
            ];
            $res = $shopService->request('get', 'shops/orders', $params);
            if (isset($res['code']) && $res['code'] == 1) {
                return $this->failure($res['message']);
            }
            $data = $res['data'] ?? [];
            return $this->success('ok', $data);
        } catch (\Exception $e) {
            Log::error('getMyOrderList:' . $e->getMessage());
            return $this->failure('请求失败');
        }
    }

    /**
     * 获取订单详情
     * @param Request $request
     * @param ShopService $shopService
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\JsonResponse
     */
    public function getOrderDetail(Request $request, ShopService $shopService)
    {
        try {
            $user = auth()->user();
//            $open_id = $user->wechat->openid;
//            if (empty($open_id)) {
//                return $this->failure('获取用户openid失败');
//            }
            $order_id = $request->get('id');
            if (empty($order_id)) {
                return $this->failure('请选择订单');
            }
            $params = [
//                'openid' => $open_id
                'accid' => User::SAAS_PREFIX.$user->id
            ];
            $res = $shopService->request('get', "shops/orders/$order_id", $params);
            if (isset($res['code']) && $res['code'] == 1) {
                return $this->failure($res['message']);
            }
            $data = $res['data'] ?? [];
            return $this->success('ok', $data);
        } catch (\Exception $e) {
            Log::error('getOrderDetail:' . $e->getMessage());
            return $this->failure('请求失败');
        }
    }

    /**
     * 购买商品
     * @param Request $request
     * @param ShopService $shopService
     * @return \Illuminate\Http\JsonResponse|void
     */
    public function payOrder(Request $request,ShopService $shopService)
    {
        try {
            $from_user_id = $request->input('from_user_id');
            Log::info("商城下单 分享人id $from_user_id");
            $id = $request->input('id');
            if (empty($id)) {
                return $this->failure('请选择商品');
            }
            $sku_id = $request->input('sku_id');
//            if (empty($sku_id)) {
//                return $this->failure('请选择规格');
//            }
            $name = $request->input('name');
            if (empty($name)) {
                return $this->failure('请填写收货人');
            }
            $mobile = $request->input('mobile');
            if (empty($mobile)) {
                return $this->failure('请填写电话');
            }
            $num = $request->input('num');
            if (empty($num)) {
                return $this->failure('请选择数量');
            }
            $address = $request->input('address');
            if (empty($address)) {
                return $this->failure('请选择地址');
            }

            $user = auth()->user();
            $open_id = $user->wechat->openid;
            if (empty($open_id)) {
                return $this->failure('获取用户openid失败');
            }
            $app_id = config('wechat.mini_program.app_id');

            $params = [
                'accid' => User::SAAS_PREFIX . $user->id,
                'sku_id' => $sku_id,
                'openid' => $open_id,
                'app_id' => $app_id,
                'name' => $name,
                'mobile' => $mobile,
                'num' => $num,
                'address' => $address,
                'nickname' => $user->name ?? '',
                'pic' => $user->avatar ?? '',
                'from_accid' => $from_user_id ? User::SAAS_PREFIX . $from_user_id : null,
            ];
            $res = $shopService->request('post',"shops/$id/orders",$params);
            if (isset($res['code']) && $res['code'] == 1) {
                return $this->failure($res['message']);
            }
            $data = $res['data'] ?? [];
            return $this->success('ok', $data);
        } catch (\Exception $e) {
            Log::error('payOrder:' . $e->getMessage());
            return $this->failure('支付失败');
        }
    }

    /**
     * 订单回调
     * @param Request $request
     * @param ShopService $shopService
     * @return \Illuminate\Http\JsonResponse|void
     */
    public function orderCallback(Request $request,ShopService $shopService)
    {
        try {
            $trade_no = $request->get('trade_no');
            if (empty($trade_no)) {
                return $this->failure('订单号不为空');
            }
            $res = $shopService->request('post',"order/$trade_no/callback");
            if (isset($res['code']) && $res['code'] == 1) {
                return $this->failure($res['message']);
            }
            $data = $res['data'] ?? [];
            return $this->success('ok', $data);
        } catch (\Exception $e) {
            Log::error('orderCallback:' . $e->getMessage());
            return $this->failure('支付失败');
        }
    }

    /**
     * 签收订单
     * @param Request $request
     * @param ShopService $shopService
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\JsonResponse
     */
    public function takeGoods(Request $request,ShopService $shopService)
    {
        try {
            $order_id = $request->input('id');
            if (empty($order_id)) {
                return $this->failure('订单id不为空');
            }
            $user = auth()->user();
//            $open_id = $user->wechat->openid;

            $params = [
//                'openid' => $open_id
                'accid' => User::SAAS_PREFIX.$user->id
            ];
            $res = $shopService->request('post',"sign/orders/$order_id",$params);
            if (isset($res['code']) && $res['code'] == 1) {
                return $this->failure($res['message']);
            }
            $data = $res['data'] ?? [];
            return $this->success('ok', $data);
        } catch (\Exception $e) {
            Log::error('takeGoods:' . $e->getMessage());
            return $this->failure('签收失败');
        }
    }
}