<?php

namespace App\Controllers\Mobile;

use App\Controllers\Mobile\BC;
use App\Models\Mobile\UserModel;
use CodeIgniter\API\ResponseTrait;

class Profile extends BC
{
    use ResponseTrait;
    public function index()
    {
        return view('Mobile/profile', ['ft' => 'profile']);
    }

    public function me()
    {
        if (!$this->request->isAJAX() && ENVIRONMENT != 'development') {
            exit('No direct script access allowed');
        }
        $data = user();
        return $this->respond([
            'nama' => $data->nama,
            'email' => $data->email,
            'photo' => $data->photo,
            'jurusan' => $data->nama_jurusan,
            'kelas' => $data->nama_kelas,
            'ruang' => $data->nama_ruang,
            'nomor_induk' => $data->nomor_induk,
            'phone' => $data->phone,
            'kode_kelas' => $data->kode_kelas,
            'kode_ruang' => $data->kode_ruang,
            'kode_jurusan' => $data->kode_jurusan,
            'csrf' =>  csrf_hash(),
        ]);
    }
    public function photo()
    {
        if (!$this->request->isAJAX() && ENVIRONMENT != 'development') {
            exit('No direct script access allowed');
        }
        $app = config('app');
        $allowed_mime = ['image/jpeg', 'image/png', 'image/gif', 'image/jpeg'];
        $file = $this->request->getFile('image');
        if (!$file->isValid()) return $this->fail(['message' => "File tidak valid!"]);
        if (!in_array($file->getClientMimeType(), $allowed_mime)) return $this->fail(['message' => "Format tidak valid!"]);;
        if ($file->hasMoved()) return $this->fail(['message' => "File tidak ditemukan!"]);
        $imgName = $file->getRandomName();
        $imgPath = "$app->imgPath/users";
        $db = db_connect();
        $db->transStart();
        $data['updated_at'] = time();
        $data['photo'] = $imgName;
        $db->table('m_siswa')->set($data)->where('id', user()->id)->update();
        resize_image($file->getTempName(), $file->getTempName(), ['width' => 100, 'height' => 100]);
        compress_image($file->getTempName(), FCPATH . "$imgPath/$imgName");
        rmbfe('php', FCPATH . $imgPath);
        $db->transComplete();
        if ($db->transStatus() === FALSE) return $this->fail(['message' => "Gagal menyimpan data"]);
        return $this->respond(['message' => "Berhasil menyimpan data", 'location' => base_url("$imgPath/$imgName")]);
    }
    public function save()
    {
        if (!$this->request->isAJAX() && ENVIRONMENT != 'development') {
            exit('No direct script access allowed');
        }
        $data['email'] = $this->request->getVar('email', FILTER_VALIDATE_EMAIL);
        $data['phone'] = $this->request->getVar('phone');
        if ($this->request->getVar('password')) {
            $data['password_hash'] = setMPassword($this->request->getVar('password'));
        }
        if ((new UserModel())->update(user()->id, $data)) return $this->respond(['message' => "Berhasil disimpan"]);
        return $this->fail(['message' => "Gagal menyimpan"]);
    }
}
