<?php

use WHMCS\Session;
use Carbon\Carbon;
use WHMCS\Database\Capsule;

$hook = array(
    'hook' => 'AdminAreaClientSummaryActionLinks',
    'function' => 'suiten_AdminAreaClientSummaryActionLinks'
);

global $suiten_nomorwhatsapp;
global $suiten_Whatsapp_Message;
global $KoreksiNomor;
global $client_id, $client_info;

if (!function_exists('suiten_AdminAreaClientSummaryActionLinks')) {


	function suiten_AdminAreaClientSummaryActionLinks($ClientObject) {

		global $suiten_nomorwhatsapp;
		global $suiten_Whatsapp_Message;
		global $KoreksiNomor;
		global $client_id, $client_info;
		$global_module_name = 'SUITEN Whatsapp API';

        foreach (WHMCS\Database\Capsule::table('s10wa_config')->WHERE('suiten_Nama_Modul', $global_module_name)->get() as $modulwa_config) {
		    $gwhost = $modulwa_config->gwhost;
            $whatsapp_source = $modulwa_config->suiten_field_nomorwa;
            $whatsapp_metode_kirim = $modulwa_config->suiten_sending_method;
            $whatsapp_Signature_message = $modulwa_config->suiten_signature;
        }
        $client_id = $ClientObject['userid'];
        foreach (Capsule::table('tblclients')->where([['id','=',$client_id],])->get() as $ClientRecord) {
            $FirstName = $ClientRecord->firstname;
            $LastName = $ClientRecord->lastname;
            $email = $ClientRecord->email;
            $phonenumber = $ClientRecord->phonenumber;
            $alamat1 = $ClientRecord->address1;
            $alamat2 = $ClientRecord->address2;
            $kota = $ClientRecord->city;
            $provinsi = $ClientRecord->state;
            $kodepos = $ClientRecord->postcode;
            $negara = $ClientRecord->country;
        }
		$client_info = $FirstName . ' ' . $LastName . ' (' . $email . ')';
		
		$suiten_nomorwhatsapp = '';
        if($whatsapp_metode_kirim == 'Custom') {
			foreach (Capsule::table('tblcustomfieldsvalues')->where([['fieldid','=',$whatsapp_source],['relid','=',$client_id],])->get() as $datarecord) {
				$suiten_nomorwhatsapp = $datarecord->value;
			}
        } else {
            $suiten_nomorwhatsapp = $phonenumber;
        }
		$KoreksiNomor = suiten_koreksinomor($negara,$suiten_nomorwhatsapp);	
		// LogActivity("S10WA KoreksiNomor ".$negara. " - " . $suiten_nomorwhatsapp);
		$suiten_ContentAdminArray = [];

		if (strlen($KoreksiNomor)>6) {
			
			$flagWAnumber = '<script>$(document).ready(function(){$(\'td:contains("' . substr($KoreksiNomor,-7). '")\').append(\'<div class="btn btn-xs btn-danger">WA Unverified</div>\');});</script>';
			$suiten_ContentAdminArray[] = '<button class="btn btn-success btn-sm" data-toggle="modal" data-target="#SUITENWhatsapp" style="margin: 16px 0px; width: 96%; padding: 15px; font-size: 13px;"><i class="fab fa-whatsapp"></i> Send Message via WA</button>
				<a href="reports.php?report=client_statement&userid=744"><img src="images/icons/reports.png" border="0" align="absmiddle" /> Add to WA status</a>

			<div id="SUITENWhatsapp" class="modal fade" role="dialog">
				<div class="modal-dialog">
					<div class="modal-content">
						<div class="modal-header">
							<button type="button" class="close" data-dismiss="modal">&times;</button>
							<h4 class="modal-title">
								<i class="fab fa-whatsapp"></i> New Message via Whatsapp
							</h4>
						</div>

						<form action="addonmodules.php?module=suiten_whatsapp" method="POST">

							<input type="hidden" name="modaction" value="CustomMessage">
							<input type="hidden" name="s10wa_clientid" value="'.$client_id.'">
							<input type="hidden" name="s10wa_clientinfo" value="'.$client_info.'">
							<input type="hidden" name="s10wa_NomorWATujuan" value="'.$KoreksiNomor.'">

							<div class="modal-body">

								<div class="form-group btn btn-warning" style="cursor: inherit; width: 100%; padding: 15px; margin-bottom: 14px;">
									The message will be sent to: <strong>' . $KoreksiNomor . '</strong>
								</div>

								<div class="form-group">
									<label for="input">Message:</label>
									<textarea class="form-control" rows="5" placeholder="Write your text ..." name="s10wa_messageWhatsapp"></textarea>
								</div>
							</div>

							<div class="modal-footer">
								<button type="submit" class="btn btn-success">
									<i class="far fa-paper-plane"></i> Submit
								</button>
							</div>

						</form>

					</div>
				</div>' . $flagWAnumber . '</div>';
		} else {
			$flagWAnumber = '';
		}

		return $suiten_ContentAdminArray;


	}

}

return $hook;