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

class Update_model extends CI_Model
{
    // Menu Kelola Data
    // Bagian Marketing
    function update_marketing($inputan, $v_inputan)
    {
        $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' => $v_inputan['email'],
                'username' => $v_inputan['username'],
                'status_akun' => 'AKTIF',
                'nm_panggilan' => $inputan['nm_panggilan']
            );
            $this->db->where('id', $inputan['id']);
            $this->db->update('sistem_login', $data_login);
            $data_identitas = array(
                'nik' => $v_inputan['nik'],
                'nm_lengkap' => $inputan['nm_panggilan'],
                'jk' => $inputan['jk'],
                'tempat_lahir' => $inputan['tempat_lahir'],
                'tgl_lahir' => $inputan['tgl_lahir'],
                'alamat' => $inputan['alamat'],
                'no_hp' => $v_inputan['no_hp']
            );
            $this->db->where('login_id', $inputan['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();
            $data_login = array(
                'email' => $v_inputan['email'],
                'username' => $v_inputan['username'],
                'status_akun' => 'AKTIF',
                'nm_panggilan' => $inputan['nm_panggilan']
            );
            $this->db->where('id', $inputan['id']);
            $this->db->update('sistem_login', $data_login);
            $data_identitas = array(
                'nik' => $v_inputan['nik'],
                'nm_lengkap' => $inputan['nm_panggilan'],
                'jk' => $inputan['jk'],
                'tempat_lahir' => $inputan['tempat_lahir'],
                'tgl_lahir' => $inputan['tgl_lahir'],
                'alamat' => $inputan['alamat'],
                'no_hp' => $v_inputan['no_hp'],
                'foto_ktp' => $uploadData['file_name']
            );
            $this->db->where('login_id', $inputan['id']);
            $this->db->update('sistem_identitas', $data_identitas);
            $berkas_lama = './assets/ptgs_id/' . $uploadData['file_name'] . '';
            unlink($berkas_lama);
        endif;
    }
    // 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;
    }
    // Menambahkan foto jaminan 1
    function  tambahFotoJaminan1()
    {
        // Upload Gambar 1
        $bulan = date('m');
        $tahun = date('Y');
        $config['upload_path']   = './assets/foto_jaminan/' . $tahun . '/' . $bulan;
        $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('gambar_1')) :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Foto 1 Jaminan Tidak Berhasil di Upload!</div>');
            redirect('kegiatan/pengajuan_pinjaman');
        else :
            $_FILES['file']['name'] = $_FILES['gambar_1']['name'];
            $_FILES['file']['type'] = $_FILES['gambar_1']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['gambar_1']['tmp_name'];
            $_FILES['file']['size'] = $_FILES['gambar_1']['size'];
            $uploadGambar_1 = $this->upload->data();
            $gambar01 = array(
                'foto_1'       => $uploadGambar_1['file_name']
            );
            $this->db->where('kontrak_id', htmlspecialchars($this->input->post('kontrak_id')));
            $this->db->update('p_koperasi_agungan', $gambar01);
        endif;
    }
    // Menambahkan foto jaminan 2
    function  tambahFotoJaminan2()
    {
        // Upload Gambar 2
        $bulan = date('m');
        $tahun = date('Y');
        $config['upload_path']   = './assets/foto_jaminan/' . $tahun . '/' . $bulan;
        $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('gambar_2')) :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Foto 1 Jaminan Tidak Berhasil di Upload!</div>');
            redirect('kegiatan/pengajuan_pinjaman');
        else :
            $_FILES['file']['name'] = $_FILES['gambar_2']['name'];
            $_FILES['file']['type'] = $_FILES['gambar_2']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['gambar_2']['tmp_name'];
            $_FILES['file']['size'] = $_FILES['gambar_2']['size'];
            $uploadGambar_2 = $this->upload->data();
            $gambar02 = array(
                'foto_2'       => $uploadGambar_2['file_name']
            );
            $this->db->where('kontrak_id', htmlspecialchars($this->input->post('kontrak_id')));
            $this->db->update('p_koperasi_agungan', $gambar02);
        endif;
    }
    // Menambahkan foto jaminan 3
    function  tambahFotoJaminan3()
    {
        // Upload Gambar 3
        $bulan = date('m');
        $tahun = date('Y');
        $config['upload_path']   = './assets/foto_jaminan/' . $tahun . '/' . $bulan;
        $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('gambar_3')) :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Foto 1 Jaminan Tidak Berhasil di Upload!</div>');
            redirect('kegiatan/pengajuan_pinjaman');
        else :
            $_FILES['file']['name'] = $_FILES['gambar_3']['name'];
            $_FILES['file']['type'] = $_FILES['gambar_3']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['gambar_3']['tmp_name'];
            $_FILES['file']['size'] = $_FILES['gambar_3']['size'];
            $uploadGambar_3 = $this->upload->data();
            $gambar03 = array(
                'foto_3'       => $uploadGambar_3['file_name']
            );
            $this->db->where('kontrak_id', htmlspecialchars($this->input->post('kontrak_id')));
            $this->db->update('p_koperasi_agungan', $gambar03);
        endif;
    }
    // Menambahkan foto jaminan 4
    function  tambahFotoJaminan4()
    {
        // Upload Gambar 4
        $bulan = date('m');
        $tahun = date('Y');
        $config['upload_path']   = './assets/foto_jaminan/' . $tahun . '/' . $bulan;
        $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('gambar_4')) :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Foto 1 Jaminan Tidak Berhasil di Upload!</div>');
            redirect('kegiatan/pengajuan_pinjaman');
        else :
            $_FILES['file']['name'] = $_FILES['gambar_4']['name'];
            $_FILES['file']['type'] = $_FILES['gambar_4']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['gambar_4']['tmp_name'];
            $_FILES['file']['size'] = $_FILES['gambar_4']['size'];
            $uploadGambar_4 = $this->upload->data();
            $gambar04 = array(
                'foto_4'       => $uploadGambar_4['file_name']
            );
            $this->db->where('kontrak_id', htmlspecialchars($this->input->post('kontrak_id')));
            $this->db->update('p_koperasi_agungan', $gambar04);
        endif;
    }
    // Pencairan Dana Nasabah
    function pencairanDanaNasabah()
    {
        $bulan = date('m');
        $tahun = date('Y');
        $config['upload_path']   = './assets/doc_kontrak/' . $tahun . '/' . $bulan;
        $config['allowed_types'] = 'jpg|jpeg|png|pdf';
        $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('dokumen_kontrak')) :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Gagal Upload Bukti Pengakuan Pinjaman</div>');
        else :
            $_FILES['file']['name'] = $_FILES['dokumen_kontrak']['name'];
            $_FILES['file']['type'] = $_FILES['dokumen_kontrak']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['dokumen_kontrak']['tmp_name'];
            $_FILES['file']['size'] = $_FILES['dokumen_kontrak']['size'];
            $uploadData = $this->upload->data();
            $data_doc = array(
                'tgl_realisasi'     => date('Y-m-d'),
                'doc_kontrak'       => $uploadData['file_name']
            );
            $this->db->where('kontrak_id', htmlspecialchars($this->input->post('kontrak_id')));
            $this->db->update('p_koperasi_progres', $data_doc);
        endif;
    }
    function updateDataAnggota($data_inputan, $cek_alamat)
    {
        $link_foto_lama   = $data_inputan['foto_ktp_old'];
        $config['upload_path']   = './assets/foto_ktp';
        $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_anggota_koperasi = array(
                'nm_anggota'        => htmlspecialchars($this->input->post('nm_anggota')),
                'jk'                => htmlspecialchars($this->input->post('jk')),
                'no_hp'             => htmlspecialchars($this->input->post('no_hp')),
                'alamat_lengkap'    => htmlspecialchars($this->input->post('alamat_lengkap')),
                'id_alamat'         => $cek_alamat,
                'tempat_lahir'      => htmlspecialchars($this->input->post('tempat_lahir')),
                'tgl_lahir'         => htmlspecialchars($this->input->post('tgl_lahir')),
                'pekerjaan'         => htmlspecialchars($this->input->post('pekerjaan')),
                'suami_istri'       => htmlspecialchars($this->input->post('suami_istri'))
            );
            $this->db->where('id', $data_inputan['id_anggota']);
            $this->db->update('anggota', $data_anggota_koperasi);
        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_anggota_koperasi = array(
                'nm_anggota'        => htmlspecialchars($this->input->post('nm_anggota')),
                'jk'                => htmlspecialchars($this->input->post('jk')),
                'alamat_lengkap'    => htmlspecialchars($this->input->post('alamat_lengkap')),
                'no_hp'             => htmlspecialchars($this->input->post('no_hp')),
                'id_alamat'         => $cek_alamat,
                'tempat_lahir'      => htmlspecialchars($this->input->post('tempat_lahir')),
                'tgl_lahir'         => htmlspecialchars($this->input->post('tgl_lahir')),
                'pekerjaan'         => htmlspecialchars($this->input->post('pekerjaan')),
                'suami_istri'       => htmlspecialchars($this->input->post('suami_istri')),
                'foto_ktp'          => $uploadData['file_name']
            );
            $this->db->where('id', $data_inputan['id_anggota']);
            $this->db->update('anggota', $data_anggota_koperasi);
            $berkas_lama = './assets/foto_ktp/' . $link_foto_lama . '';
            unlink($berkas_lama);
        endif;
    }
    function updateDataAnggotaKK($data_inputan, $cek_alamat)
    {
        $link_foto_lama   = $data_inputan['foto_kk_old'];
        $config['upload_path']   = './assets/foto_ktp';
        $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')) :
            $data_anggota_koperasi = array(
                'nm_anggota'        => htmlspecialchars($this->input->post('nm_anggota')),
                'jk'                => htmlspecialchars($this->input->post('jk')),
                'alamat_lengkap'    => htmlspecialchars($this->input->post('alamat_lengkap')),
                'no_hp'             => htmlspecialchars($this->input->post('no_hp')),
                'id_alamat'         => $cek_alamat,
                'tempat_lahir'      => htmlspecialchars($this->input->post('tempat_lahir')),
                'tgl_lahir'         => htmlspecialchars($this->input->post('tgl_lahir')),
                'pekerjaan'         => htmlspecialchars($this->input->post('pekerjaan')),
                'suami_istri'       => htmlspecialchars($this->input->post('suami_istri'))
            );
            $this->db->where('id', $data_inputan['id_anggota']);
            $this->db->update('anggota', $data_anggota_koperasi);
        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();
            // Inputan
            $data_anggota_koperasi = array(
                'nm_anggota'        => htmlspecialchars($this->input->post('nm_anggota')),
                'jk'                => htmlspecialchars($this->input->post('jk')),
                'alamat_lengkap'    => htmlspecialchars($this->input->post('alamat_lengkap')),
                'no_hp'             => htmlspecialchars($this->input->post('no_hp')),
                'id_alamat'         => $cek_alamat,
                'tempat_lahir'      => htmlspecialchars($this->input->post('tempat_lahir')),
                'tgl_lahir'         => htmlspecialchars($this->input->post('tgl_lahir')),
                'pekerjaan'         => htmlspecialchars($this->input->post('pekerjaan')),
                'suami_istri'       => htmlspecialchars($this->input->post('suami_istri')),
                'foto_kk'           => $uploadDataKK['file_name']
            );
            $this->db->where('id', $data_inputan['id_anggota']);
            $this->db->update('anggota', $data_anggota_koperasi);
            $berkas_lama = './assets/foto_ktp/' . $link_foto_lama . '';
            unlink($berkas_lama);
        endif;
    }
    // Merubah data angsuran
    function merubahDataAngsuran($angsuran_id)
    {
        $data_angsuran = array(
            'tgl_bayar'       => date('Y-m-d'),
            'status_angsuran' => 'PROSES',
            'update_at'       => date('Y-m-d H:i:s')
        );
        $this->db->where('id', $angsuran_id);
        $this->db->update('p_koperasi_angsuran', $data_angsuran);
    }
    // Pinjaman Selesai
    function pinjamanSelesai($data_kontrak)
    {
        $kontrak_id = $data_kontrak['id'];
        $cek_macet  = $this->select_model->getDataAngsMacet($kontrak_id);
        $cek_lancar = $this->select_model->getDataAngsLancar($kontrak_id);
        if ($cek_macet['tagihan_macet'] > $cek_lancar['tagihan_lancar']) :
            $data_kontrak = array(
                'status_kontrak' => 'LUNAS_MACET'
            );
        else :
            $data_kontrak = array(
                'status_kontrak' => 'LUNAS_LANCAR'
            );
        endif;
        $this->db->where('id', $kontrak_id);
        $this->db->update('p_koperasi_kontrak', $data_kontrak);
    }
}
