<?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 Vehicle_model
 *
 * @author Andhy Taslin <andhy@earworks.co.id>
 * @since 1.0
 */
class Vehicle_model extends CI_Model {

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

    /**
     * list table 
     * @param type $input
     * @return type
     */
    public function get($input = array()) {
        $this->db->select('id, name, testtype');
        $this->db->from(self::TABLE);
        if (isset($input['testtype']) && !empty($input['testtype'])) {
            $this->db->where('testtype', $input['testtype']);
        }
        if (isset($input['status']) && !empty($input['status'])) {
            $this->db->where('status', $input['status']);
        }
        if (isset($input['orderby']) && !empty($input['orderby'])) {
            $this->db->order_by($input['orderby']);
        }
        $query = $this->db->get();
        return $query->result_array();
    }
     public function getall($input = array()) {
         $sub = 'SELECT MAX(`offroad`.`ordernumber`) FROM `offroad` WHERE `offroad`.`vehichleid` = vehicle.id';
        $this->db->select('vehicle.id, vehicle.name, vehicle.testtype, vehicle.status, ('.$sub.') as maxorder');
        $this->db->from(self::TABLE);
        if (isset($input['testtype']) && !empty($input['testtype'])) {
            $this->db->where('vehicle.testtype', $input['testtype']);
        }
        if (isset($input['status']) && !empty($input['status'])) {
            $this->db->where('vehicle.status', $input['status']);
        }
        if (isset($input['orderby']) && !empty($input['orderby'])) {
            $this->db->order_by($input['orderby']);
        }
        $this->db->group_by('vehicle.id');
        $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);
        if (isset($input['testtype']) && !empty($input['testtype'])) {
            $this->db->where('testtype', $input['testtype']);
        }
        if (isset($input['status']) && !empty($input['status'])) {
            $this->db->where('status', $input['status']);
        }
        $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, testtype, 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 update($input = array(), $where = array()) {
        $user = $this->session->userdata(CI_NAME . 'log');
        $update = array();
        if (isset($input['name']) && !empty($input['name'])) {
            $update['name'] = $input['name'];
        }
        if (isset($input['testtype']) && !empty($input['testtype'])) {
            $update['testtype'] = $input['testtype'];
        }
        if (isset($input['status']) && !empty($input['status'])) {
            $update['status'] = $input['status'];
        }
        if (!isset($where['id'])) {
            return false;
        }
        $insert['modified'] = time();
        $insert['modifiedby'] = $user['userid'];
        $this->db->set($update);
        $this->db->where('id', $where['id']);
        return $this->db->update(self::TABLE);
    }

}
