<?php

/*
 * Copyright (c) 2019 Earworks.
 * Codeigniter Project Template Modification
 * Please contact our email for more information :
 *  contact@earworks.co.id
 */
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Description of Card_model
 *
 * @author Andhy Taslin <andhy@earworks.co.id>
 * @since 1.0
 */
class Card_model extends CI_Model {

    /**
     * define selected table name
     */
    const TABLE = 'card';

    /**
     * list table 
     * @param type $input
     * @return type
     */
    public function get($input = array()) {
        $this->db->select('id, name, start, end');
        $this->db->from(self::TABLE);
        if (isset($input['name']) && !empty($input['name'])) {
            $this->db->where('name', $input['name']);
        }
        $query = $this->db->get();
        return $query->result_array();
    }

    /**
     * list dropdown card
     * @param type $input
     * @return type
     */
    public function dropdown($input = array()) {
        $this->db->select('id, name');
        $this->db->from(self::TABLE);
        $query = $this->db->get();
        $res = $query->result_array();
        return array_column($res, 'name', 'id');
    }

    /**
     * view 1 data
     * @param type $input
     * @return type
     */
    public function detail($input = array()) {
        $this->db->select('id, name, start, end, created, modified, createdby, modifiedby');
        $this->db->from(self::TABLE);
        if (isset($input['id']) && !empty($input['id'])) {
            $this->db->where('id', $input['id']);
        }
        $query = $this->db->get();
        return $query->row_array();
    }

    public function insert($input = array(), $return_id = false) {
        $user = $this->session->userdata(CI_NAME . 'log');
        $insert = array();
        if (isset($input['name']) && !empty($input['name'])) {
            $insert['name'] = $input['name'];
        }
        if (isset($input['start']) && !empty($input['start'])) {
            $insert['start'] = $input['start'];
        }
        if (isset($input['end']) && !empty($input['end'])) {
            $insert['end'] = $input['end'];
        }
        $insert['created'] = time();
        $insert['modified'] = time();
        $insert['createdby'] = $user['userid'];
        $insert['modifiedby'] = $user['userid'];
        $res = $this->db->insert(self::TABLE, $insert);
        return ($return_id === true) ? $this->db->insert_id() : $res;
    }

    public function update($input = array()) {
        $user = $this->session->userdata(CI_NAME . 'log');
        $update = array();
        if (isset($input['name']) && !empty($input['name'])) {
            $update['name'] = $update['name'];
        }
        if (isset($input['start']) && !empty($input['start'])) {
            $update['start'] = $update['start'];
        }
        if (isset($input['end']) && !empty($input['end'])) {
            $update['end'] = $update['end'];
        }
        if (!isset($input['id'])) {
            return false;
        }
        $update['modified'] = time();
        $update['modifiedby'] = $user['userid'];
        $this->db->set($update);
        $this->db->where('id', $input['id']);
        return $this->db->update(self::TABLE);
    }

    public function delete($input = array()) {
        if (!isset($input['id'])) {
            return false;
        }
        $this->db->where('id', $input['id']);
        return $this->db->delete(self::TABLE);
    }

}
