<?php

namespace App\Http\Controllers\Member;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class AjaxController extends MemberController
{
    /**
     * @param  \Illuminate\Http\Request  $request
     * @param string $model
     * @return action 值返回回调函数结果
     */
    public function index(Request $request, $model)
    {
        $model = config('system.models')[$model];

        return call_user_func_array([$this, $request->input('action')], [$model,  $request->all()]);
    }

    public function lists(Request $request, $model)
    {
        $query = config('system.models')[$model]::with(['product', 'member'])->memberId();
        $data['carts'] = $query->latest()->get()->toArray();
        $data['ids']   = $query->where('status', 1)->lists('id')->toArray();

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

    //加入购物车
    private function addCart($model, $data)
    {
        $product = config('system.models')['product']::with('description')->find($data['id']);

        if ($cart = $model::memberId()->where('product_id', $data['id'])->first()) {
            $cart->update([
                'num' => $cart->num + $data['num'],
                'subtotal' => $cart->subtotal + $data['num'] * $product->price,
            ]);
        } else {
            $model::create([
                'product_id' => $data['id'],
                'title'      => $product->description->title,
                'img'        => $data['img'] ?: $product->imgs[0],
                'price'      => $product->price,
                'num'        => $data['num'],
                'subtotal'   => $data['num'] * $product->price,
                'member_id'  => Auth::member()->get()->id
            ]);
        }

        return $this->success();
    }

    // 验证库存是否足够
    private  function shopNum($model, $data)
    {
        $num = $model::where('id', $data['id'])->value('stock');

        return $this->success(['num_status' => $num >= $data['num'] ? 2 : 1]);
    }

    private function update($model, $data)
    {
        if (isset($data['all_status'])) {
            $model::memberId()->update(['status' => $data['all_status']]);
        } else {
            $num = config('system.models')['product']::where('id', $data['product_id'])->value('stock');
            if ($num < $data['num']) return $this->success(['num_status' => 1]);
            $model::memberId()->find($data['id'])->update($data);
        }

        return $this->success(['num_status' => 2]);
    }

    //修改默认收货地址
    private function status($model, $data)
    {
        $result = $model::memberId()->where('id', $data['id'])->update(['status' => 1]);

        if ($result) {
            $model::memberId()->where('id', '!=', $data['id'])->update(['status' => 0]);
            return $this->success();
        }

        return $this->error();
    }

    private function delete($model, $data)
    {
        if (isset($data['orderId'])) {
            $orderLists = config('system.models')['order_list']::where('order_id', $data['orderId'])->get();
            foreach ($orderLists as $orderList) {
                config('system.models')['product']::where('id', $orderList->product_id)->increment('stock', $orderList->num);
                $orderList->delete();
            }
        }

        $result = $model::memberId()->whereIn('id', array_str($data['id']))->delete();

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