<?php
// +------------------------------------------------+
// |http://www.cjango.com                           |
// +------------------------------------------------+
// | 修复BUG不是一朝一夕的事情，等我喝醉了再说吧！  |
// +------------------------------------------------+
// | Author: 小陈叔叔 <Jason.Chen>                  |
// +------------------------------------------------+
namespace App\Merchant\Controllers\Setting;

use App\Merchant\Controllers\Controller;
use Auth;
use Illuminate\Http\Request;
use Validator;

class IndexController extends Controller
{

    /**
     * 修改密码
     * @param  Request $request 数据集
     * @return [type]           [description]
     */
    public function password(Request $request)
    {
        $user = Auth::guard('merchant')->user();

        if ($request->isMethod('post')) {

            $validator = Validator::make($request->all(), [
                'password'              => 'required|confirmed',
                'password_confirmation' => 'required',
            ], [
                'password.required'              => '新密码必须填写',
                'password_confirmation.required' => '确认密码必须填写',
                'password.confirmed'             => '两次密码不一致',
            ]);

            if ($validator->fails()) {
                return $this->error($validator->errors()->first());
            }

            $user->password = $request->password;
            $res            = $user->save();
            if ($res === true) {
                return $this->success('修改成功');
            } else {
                return $this->error('修改失败');
            }
        } else {
            return view('Merchant::setting.password');
        }
    }

}
