<?php

namespace Modules\Mall\Http\Controllers\Api;

use App\Api\Controllers\Controller;
use Exception;
use Illuminate\Http\Request;
use Jason\Api\Api;
use Modules\Mall\Http\Requests\Address\AddressRequest;
use Modules\Mall\Http\Resources\Api\Address\AddressResource;
use Modules\Mall\Models\Address;
use Modules\Mall\Models\Region;

class AddressController extends Controller
{

    /**
     * Notes: 地址列表
     *
     * @Author: <C.Jason>
     * @Date  : 2020/11/5 5:09 下午
     * @return mixed
     */
    public function index()
    {
        $addresses = Address::byUser(Api::user())->orderBy('is_default', 'desc')->get();

        return $this->success(AddressResource::collection($addresses));
    }

    /**
     * Notes: 创建地址，展示地区联动数据
     *
     * @Author: <C.Jason>
     * @Date  : 2020/11/5 5:10 下午
     */
    public function create(Request $request)
    {
        $parentId = $request->parent_id ?? 1;

        $list = Region::where('parent_id', $parentId)->get();

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

    /**
     * Notes: 保存地址
     *
     * @Author: <C.Jason>
     * @Date  : 2020/11/5 5:13 下午
     * @param  \Modules\Mall\Http\Requests\Address\AddressRequest  $request
     * @return mixed
     */
    public function store(AddressRequest $request)
    {
        $result = Address::create([
            'user_id'     => Api::userId(),
            'name'        => $request->name,
            'mobile'      => $request->mobile,
            'province_id' => $request->province_id,
            'city_id'     => $request->city_id,
            'district_id' => $request->district_id,
            'address'     => $request->address,
            'is_default'  => $request->is_default,
        ]);
        if ($result) {
            return $this->success('操作成功');
        } else {
            return $this->failed('失败');
        }
    }

    /**
     * Notes: 地址详情，如果不是自己的地址，不显示
     *
     * @Author: <C.Jason>
     * @Date  : 2020/11/5 5:31 下午
     * @param  Address  $address
     * @return mixed
     */
    public function show(Address $address)
    {
        if ($address->user()->isNot(Api::user())) {
            return $this->failed('', 404);
        }

        return $this->success(new AddressResource($address));
    }

    /**
     * Notes: 设置默认地址
     *
     * @Author: <C.Jason>
     * @Date  : 2020/11/5 5:34 下午
     * @param  Address  $address
     * @return mixed
     */
    public function setDefault(Address $address)
    {
        if ($address->user()->isNot(Api::user())) {
            return $this->failed('', 404);
        }
        try {
            $address->setDefault();

            return $this->success('操作成功');
        } catch (Exception $exception) {
            return $this->failed('失败');
        }
    }

    /**
     * Notes: 更新地址
     *
     * @Author: <C.Jason>
     * @Date  : 2020/11/5 5:40 下午
     */
    public function update(AddressRequest $request, Address $address)
    {
        if ($address->user()->isNot(Api::user())) {
            return $this->failed('', 404);
        }

        $address->update([
            'name'        => $request->name,
            'mobile'      => $request->mobile,
            'province_id' => $request->province_id,
            'city_id'     => $request->city_id,
            'district_id' => $request->district_id,
            'address'     => $request->address,
            'is_default'  => $request->is_default,
        ]);

        return $this->success('操作成功');
    }

    /**
     * Notes: 删除地址
     *
     * @Author: <C.Jason>
     * @Date  : 2020/11/5 5:52 下午
     * @param  Address  $address
     * @return mixed
     */
    public function destroy(Address $address)
    {
        if ($address->user()->isNot(Api::user())) {
            return $this->failed('', 404);
        }

        try {
            $address->delete();

            return $this->success('操作成功');
        } catch (Exception$exception) {
            return $this->failed('失败');
        }
    }

}
