<?php

namespace Modules\Mall\Http\Controllers\Api;

use App\Api\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Jason\Api\Api;
use Modules\Mall\Http\Resources\Api\Order\RefundCollection;
use Modules\Mall\Http\Resources\Api\Order\RefundLogResource;
use Modules\Mall\Http\Resources\Api\Order\RefundResource;
use Modules\Mall\Models\Refund;

class RefundController extends Controller
{

    /**
     * Notes: 走退货流程的订单
     * @Author: 玄尘
     * @Date  : 2021/5/18 9:02
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request): JsonResponse
    {
        $state = $request->state ?? 'refunding';//refunding refunded

        $list = Refund::byUser(Api::user())
                      ->when($state, function ($query) use ($state) {
                          switch ($state) {
                              case 'refunding':
                                  $query->whereIn('state', [
                                      Refund::REFUND_APPLY,
                                      Refund::REFUND_AGREE,
                                      Refund::REFUND_PROCESS,
                                      Refund::REFUND_DELIVER,
                                      Refund::REFUND_DELIVERED,
                                      Refund::REFUND_SIGNED,
                                  ]);
                                  break;
                              case 'refunded':
                                  $query->whereIn('state', [
                                      Refund::REFUND_REFUSE,
                                      Refund::REFUND_COMPLETED,
                                  ]);
                                  break;
                          }
                      })
                      ->paginate();

        return $this->success(new RefundCollection($list));
    }

    /**
     * Notes: 退单详情
     * @Author: 玄尘
     * @Date  : 2021/5/18 11:35
     * @param  \Modules\Mall\Models\Refund  $refund
     * @return mixed
     */
    public function show(Refund $refund)
    {
        if ($refund->user()->isNot(Api::user())) {
            return $this->failed('您没有权限查看', 404);
        }

        return $this->success(new RefundResource($refund));
    }

    /**
     * Notes: 退货
     * @Author: 玄尘
     * @Date  : 2021/5/17 15:02
     * @param  \Modules\Mall\Models\Refund  $refund
     * @param  \Illuminate\Http\Request     $request
     * @return mixed
     * @throws \Exception
     */
    public function deliver(Refund $refund, Request $request)
    {
        if ($refund->user()->isNot(Api::user())) {
            return $this->failed('您没有权限操作');
        }

        $validator = Validator::make($request->all(), [
            'company' => 'required',
            'number'  => 'required',
        ], [
            'company.required' => '缺少物流公司',
            'number.required'  => '缺少物流单号',
        ]);

        if ($validator->fails()) {
            return $this->failed($validator->errors()->first());
        }
        //        $express = Express::find($request->company);
        //        if (!$express) {
        //            return $this->failed('未找到物流公司');
        //        }

        $res = $refund->deliver($request->company, $request->number);
        if ($res === true) {
            return $this->success('退货成功，请等待退款');
        }

        return $this->failed('退货失败');
    }

    /**
     * Notes: 退单日志
     * @Author: 玄尘
     * @Date  : 2021/5/18 11:36
     * @param  \Modules\Mall\Models\Refund  $refund
     * @return \Illuminate\Http\JsonResponse
     */
    public function logs(Refund $refund): JsonResponse
    {
        $logs = $refund->logs()->latest()->get();

        return $this->success(RefundLogResource::collection($logs));
    }

}