<?php

namespace App\Http\Controllers\Channel;

use App\Http\Controllers\Controller;
use App\Services\Channel\ChannelUserService;
use Illuminate\Http\Request;
use App\Consts\ErrorConst;
use App\Libs\Utils;
use App\Facade\Site;
use App\Libs\ApiResponse;
use App\Exceptions\ApiException;

class ChannelUserController extends Controller
{
    use ApiResponse;

    private $channelUserService;

    public function __construct(ChannelUserService $channelUserService)
    {
        $this->channelUserService = $channelUserService;
    }

    /**
     * 登录
     *
     * @param Request $request
     * @return mixed
     * @throws ApiException
     */
    public function login(Request $request)
    {
        $all      = $request->all();
        $account  = trim(getProp($all, 'account'));
        $password = trim(getProp($all, 'password'));
        if (strlen($account) < 1 || strlen($password) < 1) {
            Utils::throwError(ErrorConst::PARAM_ERROR_CODE);
        }

        // 登录
        $user = $this->channelUserService->login($account, $password);
        return $this->success($user);
    }

    /**
     * 退出登录
     *
     * @return mixed
     */
    public function logout()
    {
        // 当前登录用户
        $uid = Site::getUid();

        // 退出
        // $result = $this->channelUserService->logout($uid);
        $result = true;
        return $this->success(compact('result'));
    }

    /**
     * 修改密码
     *
     * @param Request $request
     * @return mixed
     * @throws ApiException
     */
    public function resetPassword(Request $request)
    {
        // 当前登录用户
        $uid         = Site::getUid();
        $newPassword = trim($request->get('new_password', ''));
        if (strlen($newPassword) < 1) {
            Utils::throwError(ErrorConst::PARAM_ERROR_CODE);
        }

        // 退出
        $result = $this->channelUserService->resetPassword($uid, $newPassword);
        return $this->success(compact('result'));
    }
}