<?php

use Jenssegers\Mongodb\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;
use Illuminate\Support\Facades\Crypt;
use App\Models\Log\LogAcivityUserModel;
use App\Models\Perpus\SekolahModel;

function enc($data = null) {
    $encrypted = Crypt::encryptString($data);
    // $decrypted = Crypt::decryptString($encrypted);

    return ($encrypted);
}

function desc($data = null) {
    $decrypted = Crypt::decryptString($data);

    return ($decrypted);
}

function dmyToymd($date) {
    return Carbon::createFromFormat('d-m-Y', $date)->format('Y-m-d');
}

function ymdTodmy($date) {
    return Carbon::createFromFormat('Y-m-d', $date)->format('d-m-Y');
}

function createLogActivity($subject = null)
{
    $logactivity = new LogAcivityUserModel();
    return $logactivity->insertLogActivity($subject);
}

function cekKopSurat($idPSekolah = null) {
    $sekolah = SekolahModel::where('id', $idPSekolah)->first();
    return $sekolah && file_exists("upload/logo_sekolah/" . $sekolah->logo);
}

function kirimWhatsapp($phone, $message, $type = null) {

    $token = "9pKzgRHJ2RvLq69BJikqyARqcweBbFFE9coNgeqDExZs5umKWE";

    $curlUrl = 'https://app.ruangwa.id/api/send_message';
    if ($type == 'express') {
        $curlUrl = 'https://app.ruangwa.id/api/send_express';
    }

    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_URL => $curlUrl,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'POST',
        CURLOPT_POSTFIELDS => 'token='.$token.'&number='.$phone.'&message='.$message,
    ));

    $response = curl_exec($curl);
    curl_close($curl);
    $result = json_decode($response);
    return $result;
}