<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models;
use Validator;
use Carbon\Carbon;
use App\ActivityLog;
use Illuminate\Support\Facades\Auth;
use App\Libs\Smsgateway;

class TopUpsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $topups = Models\OrganisationTopUp::with('orgInfo','userInfo')->latest()->paginate(10);
        $data = [
            'topups' => $topups,
             'page_title' => 'org_topup_list'
        ];
        return view('super_admin.topups',$data);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $organisations = Models\Organisation::get();
        $data = [
            'organisations' => $organisations,
            'page_title' => 'org_topups'
        ];
        return view('topups.create',$data);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'amount' => 'required',
            'payment' => 'required',
            'organisation' => 'required',
            'discount' => 'required|numeric'
        ]);

        // validate json request in laravel
        

        $organisation = Models\Organisation::findOrFail($request->organisation);
        $current_balance = $organisation->balance;

        $discount = 1 + ($request->discount/100);
        $new_amount = (float)$request->amount * $discount;

        $topup = new Models\OrganisationTopUp;
        $topup->amount = round($new_amount, 2);
        $topup->current_balance = $organisation->balance;
        $topup->org_id = $request->organisation;
        $topup->created_by = Auth::user()->id;
        $topup->user_id = Auth::user()->id;
        $topup->payment_method = $request->payment;
        $save = $topup->save();

        //Update Organisation Amt
        $retval = Models\Organisation::where('id', $request->organisation)->increment('balance', $new_amount);
        if ($save == true && $retval == true) {
            $desc = [
                'title'=>'Account Top Up',
                'data'=>'MWK'. $request->amount.' for '.$organisation->name.' payment by '. $request->payment,
                'type'=>'topup',
            ];
            $activity = new ActivityLog;
            $activity->user_id = Auth::user()->id;
            $activity->user_type = 'admin'; // Auth::user()->id;
            $activity->description = json_encode($desc);
            $activity->save();

            $smsgateway = new Smsgateway; 
            $organisation = Models\Organisation::findOrFail($request->organisation);

            $organisation_phone = $organisation->phone;
            $organisation_name = $organisation->name;
            $organisation_new_balance = $organisation->balance;
            $msg = "Hello $organisation_name, your account has been credited with $new_amount Kwacha and your new balance is : $organisation_new_balance";
            if (env('APP_ENV') != 'local') {
                $message_result = $smsgateway->sendSmsNew($organisation_phone, $msg);
            }
            \Log::info($msg);
            if($request->ajax()){{
                return response()->json([
                        'status' => 'success',
                        'code'  => 1,
                        'message' => 'Top Up Successful'
                    ]);
                }
            }
            else{
                return redirect()->to('/admin/topups')->with('admin-sucsess', 'Successfully Added New Top Up');
            }
        } 
        else {
            if($request->ajax()){{
                return response()->json([
                        'status' => 'fail`',
                        'code'  => 3,
                        'message' => 'Top Up Failed'
                    ]);
                }
            }
            else{
                return redirect()->back()->with('admin-error', 'Error');
            }
        }
    }
}