<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Foundation\Auth\SendsPasswordResetEmails;
use Illuminate\Support\Facades\Password;
use Illuminate\Http\Request;

class ForgotPasswordController extends Controller {
	/*
	|--------------------------------------------------------------------------
	| Password Reset Controller
	|--------------------------------------------------------------------------
	|
	| This controller is responsible for handling password reset emails and
	| includes a trait which assists in sending these notifications from
	| your application to your users. Feel free to explore this trait.
	|
	*/

	use SendsPasswordResetEmails;

	/**
	 * Create a new controller instance.
	 *
	 * @return void
	 */
	public function __construct() {
		$this->middleware( 'guest' );
	}

	protected function sendResetLinkResponse( $response ) {
		return view( 'auth.passwords.forgot_mail_success' );
	}

	/**
	 * @api {post} /send-email-reset Gửi email reset mk
	 * @apiName send-email-reset
	 * @apiGroup Account
	 * @apiDescription Api Gửi email reset mk
	 *
	 * @apiParam {String} email
	 *
	 * @apiSuccess {Number} status 1 hoặc 0. 1 là thành công, 0 là không thành công.
	 * @apiSuccess {String} message  Tin nhắn hệ thống.
	 * @apiSuccess {String} data
	 */


	public function sendResetLinkEmailApi( Request $request ) {
		$email = $request->input( 'email' );
		//	$phone = $request->input('phone');

		if ( empty( $email ) ) {
			return response( [
				'status'  => 0,
				'message' => 'Không được để trống email'
			] );
		}

		$cnt = User::query()->where( 'email', $email )->count();

		if ( $cnt == 0 ) {
			return response( [
				'status'  => 0,
				'message' => 'Dữ liệu không hợp lệ'
			] );
		}

		$response = $this->broker()->sendResetLink(
			$request->only( 'email' )
		);

		$response == Password::RESET_LINK_SENT
			? $this->sendResetLinkResponse( $response )
			: $this->sendResetLinkFailedResponse( $request, $response );

		return response( [
			'status'  => 1,
			'message' => 'Thành công'
		] );
	}


}
