<?php

class UmkmModel extends CI_Model
{
    private $_table = "tb_umkm";

    public function save($id_pemilik)
    {
        $post = $this->input->post();
        $this->id_pemilik = $id_pemilik;
        $this->nama = $post["nama"];
        $this->omset_awal = $post["omset"];
        $this->aset_awal = $post["aset"];
        
        $this->karyawan_l = $post["karyawan_l"];
        $this->karyawan_p = $post["karyawan_p"];
        
        if($post["aset"]>500000000){
            $id_kategori = 3;
        } else{
            if($post["omset"]>2500000000){
                $id_kategori = 3;
            } elseif($post["omset"]>300000000){
                $id_kategori = 2;
            } else {
                $id_kategori = 1;
            }
        } 

        $this->id_kategori =  $id_kategori;
        $this->id_jenis =  $post["jenis"];

        if($post['kelurahan']!=='luar'){
            $kelurahan = $post['kelurahan'];
            $kecamatan = $this->KelurahanModel->getKecamatanByKelurahan($post['kelurahan']);
        } else {
            $kelurahan = 'Luar Kota Magelang';
            $kecamatan = 'Luar Kota Magelang';
        }
        $this->kelurahan = $kelurahan;
        $this->kecamatan = $kecamatan;

        $this->alamat = $post["alamat"];
        $this->telp = $post["telp"];
        $this->deskripsi = $post["deskripsi"];
        $this->foto_umkm = "umkm.png";
        return $this->db->insert($this->_table, $this);
    }

    public function setFirstImage($id_umkm,$foto_umkm)
    {
        $this->foto_umkm = $foto_umkm;
        return $this->db->update($this->_table, $this, array('id_umkm' => $id_umkm));
    }
    
    public function getUmkm(){
        return  $user = $this->db->get($this->_table)->result();
    }

    public function getUmkmsById($id_umkm){
        $this->db->join('tb_jenis','tb_jenis.id_jenis = tb_umkm.id_jenis');
        $this->db->join('tb_kategori','tb_kategori.id_kategori = tb_umkm.id_kategori');
        $this->db->where('id_umkm', $id_umkm);
        return  $umkm = $this->db->get($this->_table)->row();
    }

    public function getUmkmsByIdPemilik($id_pemilik){
        $this->db->join('tb_kategori','tb_kategori.id_kategori = tb_umkm.id_kategori');
        $this->db->join('tb_jenis','tb_jenis.id_jenis = tb_umkm.id_jenis');
        $this->db->where('id_pemilik', $id_pemilik);
        return  $umkm = $this->db->get($this->_table)->result();
    }
    
    public function getLastUmkmByIdPemilik($id_pemilik){
        $this->db->where('id_pemilik', $id_pemilik);
        $this->db->order_by('id_umkm', 'DESC');
        return  $umkm = $this->db->get($this->_table)->row();
    }


    public function getUmkmCountByUserId($id_pemilik){
        $this->db->where('id_pemilik', $id_pemilik);
        return  $user = count($this->db->get($this->_table)->result());
    }

    public function getUmkmCount(){
        return  $user = count($this->db->get($this->_table)->result());
    }

    public function update($id_umkm,$foto_umkm)
    {
        $post = $this->input->post();
        $this->nama = $post["nama"];
        $this->omset_awal = $post["omset"];
        $this->aset_awal = $post["aset"];
        $this->karyawan_l = $post["karyawan_l"];
        $this->karyawan_p = $post["karyawan_p"];
        
        if($post["aset"]>500000000){
            $id_kategori = 3;
        } else{
            if($post["omset"]>2500000000){
                $id_kategori = 3;
            } elseif($post["omset"]>300000000){
                $id_kategori = 2;
            } else {
                $id_kategori = 1;
            }
        } 
        
        $this->id_kategori =  $id_kategori;
        $this->id_jenis =  $post["jenis"];
        
        if($post['kelurahan']!=='luar'){
            $kelurahan = $post['kelurahan'];
            $kecamatan = $this->KelurahanModel->getKecamatanByKelurahan($post['kelurahan']);
        } else {
            $kelurahan = 'Luar Kota Magelang';
            $kecamatan = 'Luar Kota Magelang';
        }
        $this->kelurahan = $kelurahan;
        $this->kecamatan = $kecamatan;

        $this->alamat = $post["alamat"];
        $this->telp = $post["telp"];
        $this->deskripsi = $post["deskripsi"];
        $this->foto_umkm = $foto_umkm;
        return $this->db->update($this->_table, $this, array('id_umkm' => $id_umkm));
    }

    public function userUpdate($id_umkm,$foto_umkm)
    {
        $post = $this->input->post();
        $this->nama = $post["nama"];
        
       
        $this->id_jenis =  $post["jenis"];
       
        $this->telp = $post["telp"];
        $this->deskripsi = $post["deskripsi"];
        $this->foto_umkm = $foto_umkm;
        return $this->db->update($this->_table, $this, array('id_umkm' => $id_umkm));
    }

    public function delete($id_umkm)
    {
        return $this->db->delete($this->_table, array("id_umkm" => $id_umkm));
    }


}