<?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 Register_model
 *
 * @author Fajar Dwi Rianto <fajar@earworks.co.id>
 * @since 1.0
 */
class Register_model extends CI_Model {

    const TABLE = 'register';

    public function check($input = array())
    {
        $this->db->from(self::TABLE);
        if (isset($input['id']) && !empty($input['id'])) {
            $this->db->where('id', $input['id']);
        }
        if (isset($input['cardid']) && !empty($input['cardid'])) {
            $this->db->where('cardid', $input['cardid']);
        }
        if (isset($input['company']) && !empty($input['company'])) {
            $this->db->where('company', $input['company']);
        }
        if (isset($input['dealer']) && !empty($input['dealer'])) {
            $this->db->where('dealer', $input['dealer']);
        }
        if (isset($input['name']) && !empty($input['name'])) {
            $this->db->where('name', $input['name']);
        }
        return $this->db->count_all_results();
    }

    public function get($input = array()) {
        $select = array(
            'register.id', 'register.company', 'register.dealer', 'register.name', 'register.type',
            'register.testdrive', 'register.created', 'offroad.name AS `offroad`', 
            'onroad.name AS `onroad`', 'card.name AS cardname'
        );
        $this->db->select(implode(', ', $select));
        $this->db->from(self::TABLE);
        $this->db->join('vehicle `onroad`', 'register.testonroad = onroad.id','left');
        $this->db->join('vehicle `offroad`', 'register.testoffroad = offroad.id', 'left');
        $this->db->join('card', 'register.cardid = card.id', 'left');
        if (isset($input['name']) && !empty($input['name'])) {
            $this->db->where('register.name', $input['name']);
        }
        if (isset($input['cardid']) && !empty($input['cardid'])) {
            $this->db->where('register.cardid', $input['cardid']);
        }
        if (isset($input['orderby']) && !empty($input['orderby'])) {
            $this->db->order_by($input['orderby']);
        }
        $query = $this->db->get();
        return $query->result_array();
    }

    public function detail($input = array()) {
        $this->db->select('id, cardid, company, dealer, name, type, created, modified, createdby, modifiedby');
        $this->db->from(self::TABLE);
        if (isset($input['id']) && !empty($input['id'])) {
            $this->db->where('id', $input['id']);
        }
        if (isset($input['name']) && !empty($input['name'])) {
            $this->db->like('name', $input['name']);
        }
        if (isset($input['company']) && !empty($input['company'])) {
            $this->db->like('company', $input['company']);
        }
        if (isset($input['dealer']) && !empty($input['dealer'])) {
            $this->db->like('dealer', $input['dealer']);
        }
        $query = $this->db->get();
        return $query->row_array();
    }

    public function dropdown($input = array()) {
        $this->db->select('id, cardid, company, dealer, name, type, created');
        $this->db->from(self::TABLE);
        if (isset($input['name']) && !empty($input['name'])) {
            $this->db->where('name', $input['name']);
        }
        if (isset($input['cardid']) && !empty($input['cardid'])) {
            $this->db->where('cardid', $input['cardid']);
        }
        $query = $this->db->get();
        $res = $query->result_array();
        return array_column($res, 'name', 'id');
    }

    public function insert($input = array(), $return_id = false) {
        $user = $this->session->userdata(CI_NAME . 'log');
        $insert = array();
        if (isset($input['cardid']) && !empty($input['cardid'])) {
            $insert['cardid'] = $input['cardid'];
        }else{
            $insert['cardid'] = 1;
        }
        if (isset($input['company']) && !empty($input['company'])) {
            $insert['company'] = $input['company'];
        }
        if (isset($input['dealer']) && !empty($input['dealer'])) {
            $insert['dealer'] = $input['dealer'];
        }
        if (isset($input['name']) && !empty($input['name'])) {
            $insert['name'] = ucwords(strtolower($input['name']));
        }
        if (isset($input['type']) && !empty($input['type'])) {
            $insert['type'] = $input['type'];
        }
        $insert['created'] = time();
        $insert['modified'] = time();
        $insert['createdby'] = $user['userid'];
        $insert['modifiedby'] = $user['userid'];
        $check = $this->check(array(
            'cardid' => $input['cardid'],
            'company' => $input['company'],
            'dealer' => $input['dealer'],
            'name' => $input['name'],
        ));
        if ($check){
            return false;
        }
        $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['cardid']) && !empty($input['cardid'])) {
            $update['cardid'] = $input['cardid'];
        }
        if (isset($input['company']) && !empty($input['company'])) {
            $update['company'] = $input['company'];
        }
        if (isset($input['dealer']) && !empty($input['dealer'])) {
            $update['dealer'] = $input['dealer'];
        }
        if (isset($input['name']) && !empty($input['name'])) {
            $update['name'] = $input['name'];
        }
        if (isset($input['type']) && !empty($input['type'])) {
            $update['type'] = $input['type'];
        }
         if (isset($input['testdrive']) && !empty($input['testdrive'])) {
            $update['testdrive'] = $input['testdrive'];
        }
        $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('userid', $input['id']);
        return $this->db->delete(self::TABLE);
    }

}
