<?php

namespace App\Models;

use CodeIgniter\Model;

class PengajuanModel extends Model
{
    protected $table = 'pengajuan';
    protected $primaryKey = 'id';
    protected $useAutoIncrement = true;
    protected $returnType = 'array';
    protected $allowedFields = [
        'user_id',
        'target_nip',
        'created_by',
        'periode',
        'tahun',
        'jenis_prosedur',
        'jenis_kp',
        'jenis_jabatan',
        'nomor_surat_pengantar',
        'status',
        'catatan_revisi',
        'sk_tte',
        'tempat_lahir',
        'tanggal_lahir',
        'pendidikan_lama',
        'tahun_lulus',
        'gelar_depan',
        'gelar_belakang',
        'unor',
        'unit_kerja_induk',
        'jabatan_lama',
        'jabatan_baru',
        'lokasi_kerja',
        'instansi_kerja',

        'gol_awal',
        'tmt_golongan_lama',
        'mkt_lama',
        'mkb_lama',
        'mkt_baru',
        'mkb_baru',
        'tmt_jabatan_lama',
        'gol_baru',
        'tmt_gol_baru',
        'atasan_nama',
        'atasan_nip',
        'atasan_jabatan',
        'atasan_gol',

        'angka_kredit_lama',
        'angka_kredit_baru',
        'keterangan_kp',
        'created_at',
        'updated_at'
    ];

    protected $useTimestamps = true;
    protected $createdField = 'created_at';
    protected $updatedField = 'updated_at';

    public function getWithPegawai()
    {
        return $this->select('pengajuan.*, 
                              target.nama as pegawai_name, 
                              target.nama as target_nama,
                              target.nip_baru as pegawai_nip,
                              target.nip_baru as target_nip,
                              pengaju.name as pengaju_nama')
            ->join('pegawai as target', 'target.nip_baru = pengajuan.target_nip', 'left')
            ->join('users as pengaju', 'pengaju.id = pengajuan.created_by', 'left')
            ->orderBy('pengajuan.created_at', 'DESC')
            ->findAll();
    }
}
