<?php

namespace App\Http\Controllers\Member;

use App\Models\Cart;
use App\Models\Address;
use Illuminate\Support\Facades\Auth;

class CartController extends MemberController
{
    use \App\Traits\ControllerTrait;

    /**
     * 前台购物车
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
     */
    public function index()
    {
        $seo = self::meta(trans("translate::shop")['shopping_cart']);

        return view('theme::member.cart', compact('seo'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy($id)
    {
        $result = Cart::memberId()->where('id', $id)->delete();

        return $result ? $this->success() : $this->error();
    }

    /**
     * 订单结算
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function shopping()
    {
        $seo     = self::meta(trans("translate::shop")['submit_orders']);
        $data    = Cart::with('product')->memberId()->where('status', 1)->latest()->get();
        $model   = Address::memberId();
        $address = $model->orderBy('status', 'DESC')->get();
        $addressId = $model->where('status', 1)->value('id');
        $orderId = substr(date('YmdHis').Auth::member()->get()->id.rand(1000, 9999), 0, 18);

        $totalPrice   = 0;
        foreach ($data as  $value) {
            $totalPrice += $value->subtotal;
        }

        return view('theme::member.shopping', compact('seo', 'data', 'totalPrice', 'address', 'addressId', 'orderId'));
    }
}
