<?php

namespace App\Http\Controllers\Agencies;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use App\Http\Controllers\Controller;
use App\User;
use Auth;
use DB;
use App\FundsRegisterModel;
use Illuminate\Support\Facades\Crypt;
class FundsRegisterController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $user_id = Auth::user()->id;
        $data = FundsRegisterModel::where('user_id', $user_id)
        ->get();

        return view('agencies.funds.show', compact('data'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create($Digit = 9)
    {
        $user = Auth::user();
        $account_name = $user->user_firstname. ' ' .$user->user_lastname;
        $max = FundsRegisterModel::max('id') + 1;
        $increas = $max+0;
        (string)$increas;
        while(strlen($increas) < $Digit){
            $increas = "0".$increas;
        }
        $increas = chunk_split($increas, 3, ' ');
        return view('agencies.funds.create',compact('increas','account_name' ));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request, FundsRegisterModel $user)
    {
        
        $rules = [
                'account_name'  => 'required|unique:nso007_funds,account_name',
                'account_number'  => 'required|unique:nso007_funds,account_number',
                'username'   => 'required',
                'password'    => 'required|same:confirm_password',
                'user_id' =>  'required|unique:nso007_funds,user_id',
        ];
        $customMessages = [

            'user_id.unique' => 'you has already funds account!',
            'account_number.unique' => 'you has already funds account!',
            'account_name.unique' => 'Account name unavialable !',
            'user_id.unique' => 'you has already funds account!',
            'account_number.unique' => 'Account Number unavialable !'
        ];

        $this->validate($request, $rules, $customMessages);


        $funds = new FundsRegisterModel;
        $funds->user_id = $request->user_id;
        $funds->account_name = $request->account_name;
        $funds->account_number = $request->account_number;
        $funds->username = $request->username;
        $funds->password =$request->password;
        $funds->request_date = date('Y-m-d H:i:s A');
        $funds->save();
        date_default_timezone_set('Asia/Bangkok');
        $data = array(
            'user_code' => Auth::user()->user_code,
            'user_name' => Auth::user()->user_firstname.' '.Auth::user()->user_lastname,
            'email' => Auth::user()->email,
            'account_number' => $request->account_number,
            'account_name' => $request->account_name,
            'created_at' => date('Y-m-d H:i:s A')
        );
        Mail::send('agencies.funds-transaction.mail-register-fund', compact('request', 'user', 'data'), 
        function($message) use ($request, $user, $data){
            $message->to('nso.webteam.info@gmail.com');
            $message->subject('Request Deposit');
        });

        
        \LogActivity::addToLog('Funds Register successfully !');
        return redirect()->route('dashboard-agencies.index')
        ->with('success','Funds Register successfully !');
    }

    public function change_password(Request $request)
    {
        $rules = [
            'new_password'    => 'required|same:confirm_password',
        ];
        $customMessages = [
            'password.required' => 'you has already funds account!'
        ];
        $this->validate($request, $rules, $customMessages);

        $user_id = Auth::user()->id; 
        $funds = FundsRegisterModel::where('user_id',$user_id)->get(); 
        $funds_update = FundsRegisterModel::find($funds[0]->id);

        if($request->password == $funds_update->password){
            if($request->new_password == $request->confirm_password){
                $funds_update->password = $request->new_password;
                $funds_update->update();
                \LogActivity::addToLog('Password updated successfully !');
                return back()->with('success','Password updated successfully');
            }else{
                \LogActivity::addToLog('Your confirm password is valid !');
                return back()->with('danger','Your confirm password is valid !');
            }
        }else{
            \LogActivity::addToLog('Your old password is valid !');
            return back()->with('danger','Your old password is valid !');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */

    public function show($id)
    {
        return view('agencies.funds.show');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
