<?php

/*
 * Copyright (c) 2018 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
 *
 * @author Fajar Dwi Rianto <fajar@earworks.co.id>
 * @since 1.0
 */
class Register extends Base_Controller {

    public $allowed_role = array(1, 2, 4);

    public function lists() {
        // check login
        if (!$this->is_login) {
            redirect(parent::BACKOFFICE_LOGIN);
        }
        $this->load->model(array('register_model', 'card_model'));
        $this->load->helper('form');
        $this->load->view('register/list', array(
            'data' => $this->register_model->get(array('orderby' => 'register.created ASC')),
            'card' => $this->card_model->dropdown()
        ));
    }

    public function index() {
        $this->load->template = 'main2';
        // check login access
        if (!$this->is_login) {
            redirect(parent::BACKOFFICE_LOGIN);
        }
        // load required model, library, or helper
        $this->load->model(array('register_model', 'card_model', 'company_model', 'visitor_model'));
        $this->load->helper(array('upload', 'form'));
        $this->load->library('form_validation');
        // set validation rules for register form
        $this->form_validation->set_rules('companyname', 'lang:company', 'trim|required');
        $this->form_validation->set_rules('dealername', 'lang:dealername', 'trim|required');
        $this->form_validation->set_rules('visitorname', 'lang:namevisitor', 'trim|required');
        $this->form_validation->set_rules('type', 'lang:typecompany', 'trim');

        if ($this->form_validation->run() === false) {
            // load register form and send required dropdown data
            $this->load->view('register/index', array(
                'card' => $this->card_model->dropdown(),
                // 'company' => $this->company_model->dropdown(),
                // 'visitor' => $this->visitor_model->dropdown()
            ));
        } else {
            // insert post data to table
            $apps = $this->register_model->insert(array(
                'cardid' => $this->input->post('cardid', true),
                'company' => $this->input->post('companyname', true),
                'dealer' => $this->input->post('dealername', true),
                'name' => $this->input->post('visitorname', true),
                'type' => 1
            ));
            if ($apps === true) {
                redirect('backoffice/register');
            } else {
                redirect(current_url());
            }
        }
    }

    public function media() {
        $this->load->template = 'main2';
        // CHECK AKSES LOGIN
        if (!$this->is_login) {
            redirect(parent::BACKOFFICE_LOGIN);
        }

        // LOAD MODEL, LIBRARY, HELPER
        $this->load->model(array('register_model', 'card_model', 'company_model', 'visitor_model'));
        $this->load->helper(array('upload', 'form'));
        $this->load->library('form_validation');

        // SET VALIDASI MODEL, LIBRARY, HELPER
        $this->form_validation->set_rules('companyname', 'lang:company', 'trim|required');
        $this->form_validation->set_rules('visitorname', 'lang:namevisitor', 'trim|required');
        $this->form_validation->set_rules('type', 'lang:typecompany', 'trim');

        if ($this->form_validation->run() === false) {
            $this->load->view('register/media', array(
                'card' => $this->card_model->dropdown(),
                'company' => $this->company_model->dropdown(),
                'visitor' => $this->visitor_model->dropdown()
            ));
        } else {
            $this->register_model->insert(array(
                'cardid' => $this->input->post('cardid', true),
                'company' => $this->input->post('companyname', true),
                'name' => $this->input->post('visitorname', true),
                'type' => 2
            ));
            redirect('backoffice/register/media');
        }
    }

    public function internal()
    {
        $this->load->template = 'main2';
        // LOGIN AKSES
        if (!$this->is_login) {
            redirect(parent::BACKOFFICE_LOGIN);
        }

        // LOAD MODEL, LIBRARY, OR HELPER
        $this->load->model(array('register_model', 'card_model', 'company_model', 'visitor_model'));
        $this->load->helper(array('upload', 'form'));
        $this->load->library('form_validation');
        // SET VALIDATION RULES FOR REGISTER FORM
        $this->form_validation->set_rules('companyname', 'lang:companyname', 'trim|required');
        $this->form_validation->set_rules('visitorname', 'lang:visitorname', 'trim|required');
        $this->form_validation->set_rules('type', 'lang:typecompany', 'trim');

        if ($this->form_validation->run() === false) {
            // LOAD REGSITER FORM AND SEND REQUIRED DROPDOWN DATA TO TABLE
            $this->load->view('register/internal', array(
                'card' => $this->card_model->dropdown(),
                'company' => $this->company_model->dropdown(),
                'visitor' => $this->visitor_model->dropdown()
            ));
        } 
        else 
        {
            // INSERT POST DATA TO TABLE
            $apps = $this->register_model->insert(array(
                'company' => $this->input->post('companyname', true),
                'name' => $this->input->post('visitorname', true),
                'type' => 3
            ));
            if ($apps === true) {
                redirect('backoffice/register/internal');
            } else {
                redirect(current_url());
            }
        }
    }

    public function ajax_getcompany() {
        $this->load->helper('api');
        if (!$this->input->is_ajax_request()) {
            exit('No access to this request');
        }
        if (!$this->is_login) {
            return output_error('Login required', 403);
        }
        $this->load->model('company_model');
        $apps = $this->company_model->dropdown($this->input->post(null, true));
        return output_blank($apps);
    }

    public function ajax_getdealer() {
        $this->load->helper('api');
        if (!$this->input->is_ajax_request()) {
            exit('No access to this request');
        }
        if (!$this->is_login) {
            return output_error('Login required', 403);
        }
        $apps = array('Alun', 'CKP', 'Eurotruck', 'HSB', 'KSM');
        return output_blank($apps);
    }
    

    public function ajax_getvisitor() {
        $this->load->helper('api');
        if (!$this->input->is_ajax_request()) {
            exit('No access to this request');
        }
        if (!$this->is_login) {
            return output_error('Login required', 403);
        }
        $this->load->model('visitor_model');
        $apps = $this->visitor_model->dropdown($this->input->post(null, true));
        return output_blank($apps);
    }

    public function ajax_getcompanydetail() {
        $this->load->helper('api');
        if (!$this->input->is_ajax_request()) {
            exit('No access to this request');
        }
        if (!$this->is_login) {
            return output_error('Login required', 403);
        }
        $this->load->library('form_validation');
        $this->form_validation->set_rules('name', 'lang:companyname', 'trim|required');
        if ($this->form_validation->run() === false) {
            // load register form and send required dropdown data
            return output_error(validation_errors(null, null), 405);
        } else {
            $this->load->model('company_model');
            $apps = $this->company_model->detail(array(
                'name' => $this->input->post('name', true)
            ));
            if (!$apps) {
                return output_error('not founded company');
            }
            if ($apps['type'] == 1) {
                $apps['companytype'] = 'Coorporate';
            } elseif ($apps['type'] == 2) {
                $apps['companytype'] = 'Media';
            }
            return output($apps);
        }
    }

    // no need edit and delete action
//    public function edit($id = null) {
//        if (!$this->is_login) {
//            redirect(parent::BACKOFFICE_LOGIN);
//        }
//
//        $this->load->helper('form');
//        $this->load->library('form_validation');
//
//        $data['registe_item'] = $this->register_model->detail($id);
//        $data['id'] = $id;
//        $data['card'] = $this->card_model->get();
//        $data['company'] = $this->company_model->get();
//        $data['visitor'] = $this->visitor_model->get();
//
//        if (!$data['register_item']) {
//            redirect('backoffice/register/create');
//        }
//
//        $this->form_validation->set_rules('cardid', 'lang:cardid', 'trim|required');
//        $this->form_validation->set_rules('company', 'lang:company', 'trim|required');
//        $this->form_validation->set_rules('name', 'lang:namevisitor', 'trim|required');
//        $this->form_validation->set_rules('type', 'lang:typecompany', 'trim|required');
//        if ($this->form_validation->run() == FALSE) {
//            $this->load->view('backoffice/register/create', $data);
//        } else {
//            $apps = $this->register_model->update(array(
//                'registerid' => $id,
//                'cardid' => $this->input->post('cardid', true),
//                'company' => $this->input->post('company', true),
//                'name' => $this->input->post('name', true),
//                'type' => $this->input->post('type', true),
//            ));
//            if ($apps === TRUE) {
//                redirect('backoffice/register/create/' . $data['registeritem']['cardid']);
//            } else {
//                redirect(current_url());
//            }
//        }
//    }
//
//    public function delete($id = null) {
//        if (!$this->is_login) {
//            redirect(parent::BACKOFFICE_LOGIN);
//        }
//
//        if (empty($id)) {
//            show_404();
//        }
//
//        $register_item = $this->register_model->delete($id);
//        redirect(base_url() . 'backoffice/register/create');
//    }
}
