<?php

namespace App\Http\Controllers;

use App\TacExpressPayment;
use Illuminate\Http\Request;
use Carbon\Carbon;

use Illuminate\Support\Facades\Http;

class TacExpressPaymentController extends Controller
{
    public function generateTacExpressPayment(Request $request){

        $currentDateTime = Carbon::now();
        $expired_at = Carbon::now()->addMinutes(15);

        TacExpressPayment::on('mysql7')->create([
            'norujukan' => $request->norujukan,
            'tac_no' => $request->otp_token,
            'expired_at' => $expired_at
        ]);

        $mobileno = '6'.$request->mobileno;
        //$mobileno = '60139321786';
        $messages = 'AR-RAHN PKB ' . $request->cawangan . ' : TAC: ' . $request->otp_token . ' ' . $currentDateTime . '. AR-RAHN CARELINE CENTRE 1300-22-4343';

        $status = Http::get('http://gateway.onewaysms.com.my:10001/api.aspx',[
            'apiusername' => 'APIUK330HDAFO',
            'apipassword' => 'APIUK330HDAFOUK330',
            'senderid' => 'INFO',
            'mobileno' => $mobileno,
            'message'=> $messages,
            'languagetype'=> 1
        ]);

        return response($status);
    }

    public function getTacExpressPayment(Request $request){
        $currentDateTime = Carbon::now();
        $tac_data = TacExpressPayment::on('mysql7')->where([['norujukan',$request->norujukan],['expired_at','>',$currentDateTime]])->get();

        if(sizeof($tac_data) > 0){
            return response(1);
        }else{
            return response(0);
        }
    }

    public function verifyTacExpressPayment(Request $request){
        $currentDateTime = Carbon::now();
        $tac_data = TacExpressPayment::on('mysql7')->where([['norujukan',$request->norujukan],['tac_no',$request->tac_token],['expired_at','>',$currentDateTime]])->get();

        if(sizeof($tac_data) > 0){
            return response(1);
        }else{
            return response(0);
        }
    }
}
