<?php

namespace App\Controllers\Panel\Whatsapp;

use App\Controllers\Panel\BC;
use CodeIgniter\API\ResponseTrait;

class Setting extends BC
{
    use ResponseTrait;
    public function __construct()
    {
        parent::__construct();
    }
    public function index()
    {
        $data['page_title'] = "Whatsapp - Pengaturan";
        return view('Panel/Whatsapp/setting', $data);
    }

    public function data()
    {
        if (!$this->request->isAJAX() && ENVIRONMENT != 'development') {
            exit('No direct script access allowed');
        }
        $data = json_decode(options('whatsapp'));
        $data->webhook = base_url('api/whatsapp-hook?key=' . hash('sha256', options('sck')));
        return $this->respond($data);
    }
    public function save()
    {
        if (!$this->request->isAJAX() && ENVIRONMENT != 'development') {
            exit('No direct script access allowed');
        }
        $data = (array) $this->request->getVar();
        $setting['message_siswa_presensi_masuk'] = $data['message_siswa_presensi_masuk'];
        $setting['message_siswa_presensi_keluar'] = $data['message_siswa_presensi_keluar'];
        $setting['siswa_presensi_masuk'] = $data['siswa_presensi_masuk'];
        $setting['siswa_presensi_keluar'] = $data['siswa_presensi_keluar'];
        $setting['randomize'] = $data['randomize'];
        if (!save_options(json_encode($setting), 'whatsapp')) return $this->fail("Gagal menyimpan pengaturan");
        return $this->respond(['message' => "Berhasil menyimpan pengaturan"]);
    }
}
