<?php defined('BASEPATH') or exit('No direct script access allowed');

class Update_model extends CI_Model
{
    function updateDataKaryawanLama($data_inputan, $alamat_id)
    {
        $link_foto_lama   = $data_inputan['foto_ktp_old'];
        $config['upload_path']   = './assets/ptgs_id';
        $config['allowed_types'] = 'jpg|jpeg|png';
        $config['encrypt_name']  = true;
        $config['overwrite']     = true;
        $config['quality']       = '50%';
        $config['max_size']      = 10024; // 10MB
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload('foto_ktp')) :
            $data_login = array(
                'email'             => $data_inputan['email_baru'],
                'no_hp'             => $data_inputan['no_hp_baru'],
                'nm_panggilan'      => $data_inputan['nm_panggilan']
            );
            $this->db->where('id', $data_inputan['login_id']);
            $this->db->update('sistem_login', $data_login);
            $data_identitas = array(
                'nik'             => $data_inputan['nik'],
                'jk'              => $data_inputan['jk'],
                'tempat_lahir'    => $data_inputan['tempat_lahir'],
                'tgl_lahir'       => $data_inputan['tgl_lahir'],
                'alamat_id'       => $alamat_id,
                'tgl_gabung'      => $data_inputan['tgl_gabung']
            );
            $this->db->where('login_id', $data_inputan['login_id']);
            $this->db->update('sistem_identitas', $data_identitas);
        else :
            $_FILES['file']['name'] = $_FILES['foto_ktp']['name'];
            $_FILES['file']['type'] = $_FILES['foto_ktp']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['foto_ktp']['tmp_name'];
            $_FILES['file']['size'] = $_FILES['foto_ktp']['size'];
            $uploadData = $this->upload->data();
            // Inputan
            $data_login = array(
                'email'             => $data_inputan['email_baru'],
                'no_hp'             => $data_inputan['no_hp_baru'],
                'nm_panggilan'      => $data_inputan['nm_panggilan']
            );
            $this->db->where('id', $data_inputan['login_id']);
            $this->db->update('sistem_login', $data_login);
            $data_identitas = array(
                'nik'             => $data_inputan['nik'],
                'jk'              => $data_inputan['jk'],
                'tempat_lahir'    => $data_inputan['tempat_lahir'],
                'tgl_lahir'       => $data_inputan['tgl_lahir'],
                'alamat_id'       => $alamat_id,
                'foto_ktp'        => $uploadData['file_name'],
                'tgl_gabung'      => $data_inputan['tgl_gabung']
            );
            $this->db->where('login_id', $data_inputan['login_id']);
            $this->db->update('sistem_identitas', $data_identitas);
            $berkas_lama = './assets/ptgs_id/' . $link_foto_lama . '';
            unlink($berkas_lama);
        endif;
    }
    function merubahDataAngsuran($angsuran_id, $status_angsuran)
    {
        $data_angsuran = array(
            'tgl_bayar'       => date('Y-m-d'),
            'status_angsuran' => $status_angsuran,
            'update_at'       => date('Y-m-d H:i:s')
        );
        $this->db->where('id', $angsuran_id);
        $this->db->update('p_koperasi_angsuran', $data_angsuran);
    }
    function vnota_arisan($kd_invoice)
    {
        $data_update     = array(
            'status_nota'       => 'TERIMA',
            'kasir'             => $this->session->userdata('isloggedin')
        );
        $this->db->where('id', $kd_invoice);
        $this->db->update('nota', $data_update);
    }
    function vnota_arisanTunggakan($kd_invoice)
    {
        $data_update     = array(
            'status_nota'       => 'TERIMA_TUNGGAKAN',
            'kasir'             => $this->session->userdata('isloggedin')
        );
        $this->db->where('id', $kd_invoice);
        $this->db->update('nota', $data_update);
    }
    // Pencairan Dana Nasabah
    function pencairanDanaNasabah()
    {
        $data_doc = array(
            'tgl_realisasi'     => date('Y-m-d')
        );
        $this->db->where('kontrak_id', htmlspecialchars($this->input->post('kontrak_id')));
        $this->db->update('p_koperasi_progres', $data_doc);
    }
    // Pinjaman Selesai
    function pinjamanSelesai($data_kontrak, $status_kontrak)
    {
        $kontrak_id = $data_kontrak['id'];
        $data_kontrak = array(
            'status_kontrak' => $status_kontrak
        );
        $this->db->where('id', $kontrak_id);
        $this->db->update('p_koperasi_kontrak', $data_kontrak);
    }
    // Menambahkan Foto KK dan mengubah data anggota
    function menambahFotoKKAnggota($anggota_id)
    {
        // Upload Gambar Dulu
        // Upload KTP
        $config['upload_path']   = './assets/foto_kk';
        $config['allowed_types'] = 'jpg|jpeg|png';
        $config['encrypt_name']  = true;
        $config['overwrite']     = true;
        $config['quality']       = '50%';
        $config['max_size']      = 10024; // 10MB
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload('foto_kk')) :
            $_FILES['file']['name'] = $_FILES['foto_kk']['name'];
            $_FILES['file']['type'] = $_FILES['foto_kk']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['foto_kk']['tmp_name'];
            $_FILES['file']['size'] = $_FILES['foto_kk']['size'];
            $uploadDataKK = $this->upload->data();
            $data_anggota = array(
                'foto_kk'       => 'Default.png'
            );
            $this->db->where('id', $anggota_id);
            $this->db->update('anggota', $data_anggota);
        else :
            $_FILES['file']['name'] = $_FILES['foto_kk']['name'];
            $_FILES['file']['type'] = $_FILES['foto_kk']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['foto_kk']['tmp_name'];
            $_FILES['file']['size'] = $_FILES['foto_kk']['size'];
            $uploadDataKK = $this->upload->data();
            $data_anggota = array(
                'foto_kk'       => $uploadDataKK['file_name']
            );
            $this->db->where('id', $anggota_id);
            $this->db->update('anggota', $data_anggota);
        endif;
    }
}
