<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Home extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->model(array('Home_model'));
        $this->load->database();
        $this->load->library(array('form_validation', 'upload'));
        $this->load->helper(array('url', 'language'));
    }

    public function index()
    {
        $data = array(
            'button' => 'PROSES',
            'action' => site_url('home/wellcome'),
        );
        $this->load->view('home/index', $data);
    }

    public function wellcome()
    {
        $id = $this->input->post('id', TRUE);
        $cek = $this->Home_model->get_by_id($id);
        $tanggal = date('Y-m-d');
        if ($cek) {
            $data = array(
                'button' => 'PROSES',
                'action' => site_url('home/store'),
                'id_card' => set_value('id_card', $id),
                'tanggal' => set_value('tanggal', $tanggal),
                'nik' => set_value('nik', $cek->nik),
                'nama' => set_value('nama', $cek->nama_pegawai),
                'tempat_lahir' => set_value('tempat_lahir', $cek->tempat_lahir),
                'tanggal_lahir' => set_value('tanggal_lahir', $cek->tanggal_lahir),
                'jenis_kelamin' => set_value('jenis_kelamin', $cek->jenis_kelamin),
                'alamat' => set_value('alamat', $cek->alamat),
                'telepon' => set_value('telepon'),
                'dari' => set_value('dari'),
                'tujuan' => set_value('tujuan'),
                'sub_tujuan' => set_value('sub_tujuan'),
                'keperluan' => set_value('keperluan'),
                'foto' => set_value('foto'),
            );
        } else {
            $data = array(
                'button' => 'PROSES',
                'action' => site_url('home/store'),
                'id_card' => set_value('id_card', $id),
                'tanggal' => set_value('tanggal', $tanggal),
                'nik' => set_value('nik'),
                'nama' => set_value('nama'),
                'tempat_lahir' => set_value('tempat_lahir'),
                'tanggal_lahir' => set_value('tanggal_lahir'),
                'jenis_kelamin' => set_value('jenis_kelamin'),
                'alamat' => set_value('alamat'),
                'telepon' => set_value('telepon'),
                'dari' => set_value('dari'),
                'tujuan' => set_value('tujuan'),
                'sub_tujuan' => set_value('sub_tujuan'),
                'keperluan' => set_value('keperluan'),
                'foto' => set_value('foto'),
            );
        }
        $this->load->view('home/wellcome', $data);
    }

    public function store()
    {
        $this->_rules();

        if ($this->form_validation->run() == FALSE) {
            $this->wellcome();
        } else {
            $nama_file = 'kunjungan-' . date("YmdHis");

            $config['upload_path'] = './public/data/upload/';
            $config['allowed_types'] = 'pdf|doc|docx|xls|xlsx|cdr|psd|png|bmp|jpg|jpeg';
            $config['file_name'] = $nama_file;

            $this->upload->initialize($config);
            if (!empty($_FILES['foto']['name'])) {
                if ($this->upload->do_upload('foto')) {
                    $file_upld = $this->upload->data();
                    $data = array(
                        'id_card' => $this->input->post('id_card', TRUE),
                        'tanggal' => $this->input->post('tanggal', TRUE),
                        'nik' => $this->input->post('nik', TRUE),
                        'nama' => $this->input->post('nama', TRUE),
                        'tempat_lahir' => $this->input->post('tempat_lahir', TRUE),
                        'tanggal_lahir' => $this->input->post('tanggal_lahir', TRUE),
                        'jenis_kelamin' => $this->input->post('jenis_kelamin', TRUE),
                        'alamat' => $this->input->post('alamat', TRUE),
                        'telepon' => $this->input->post('telepon', TRUE),
                        'dari' => $this->input->post('dari', TRUE),
                        'tujuan' => $this->input->post('tujuan', TRUE),
                        'sub_tujuan' => $this->input->post('sub_tujuan', TRUE),
                        'keperluan' => $this->input->post('keperluan', TRUE),
                        'foto' => $file_upld['file_name'],
                    );
                    $this->Home_model->insert($data);
                    $this->session->set_flashdata('message', '1111');
                    redirect(site_url('home'));
                } else {
                    $this->session->set_flashdata('message', '7373');
                    redirect(site_url('home'));
                }
            } else {
                $data = array(
                    'id_card' => $this->input->post('id_card', TRUE),
                    'tanggal' => $this->input->post('tanggal', TRUE),
                    'nik' => $this->input->post('nik', TRUE),
                    'nama' => $this->input->post('nama', TRUE),
                    'tempat_lahir' => $this->input->post('tempat_lahir', TRUE),
                    'tanggal_lahir' => $this->input->post('tanggal_lahir', TRUE),
                    'jenis_kelamin' => $this->input->post('jenis_kelamin', TRUE),
                    'alamat' => $this->input->post('alamat', TRUE),
                    'telepon' => $this->input->post('telepon', TRUE),
                    'dari' => $this->input->post('dari', TRUE),
                    'tujuan' => $this->input->post('tujuan', TRUE),
                    'sub_tujuan' => $this->input->post('sub_tujuan', TRUE),
                    'keperluan' => $this->input->post('keperluan', TRUE),
                );
                $this->Home_model->insert($data);
                $this->session->set_flashdata('message', '1111');
                redirect(site_url('home'));
            }
        }
    }

    public function _rules()
    {
        $this->form_validation->set_rules('id_card', 'id card', 'trim|required');
        $this->form_validation->set_rules('tanggal', 'tanggal', 'trim|required');
        $this->form_validation->set_rules('nik', 'nik', 'trim|required');
        $this->form_validation->set_rules('nama', 'nama', 'trim|required');
        $this->form_validation->set_rules('tempat_lahir', 'tempat lahir', 'trim|required');
        $this->form_validation->set_rules('tanggal_lahir', 'tanggal lahir', 'trim|required');
        $this->form_validation->set_rules('jenis_kelamin', 'jenis kelamin', 'trim|required');
        $this->form_validation->set_rules('alamat', 'alamat', 'trim|required');
        $this->form_validation->set_rules('telepon', 'telepon', 'trim|required');
        $this->form_validation->set_rules('dari', 'dari', 'trim|required');
        $this->form_validation->set_rules('tujuan', 'tujuan', 'trim|required');
        $this->form_validation->set_rules('sub_tujuan', 'sub tujuan', 'trim');
        $this->form_validation->set_rules('keperluan', 'keperluan', 'trim|required');
        $this->form_validation->set_rules('foto', 'foto', 'trim|');

        $this->form_validation->set_rules('id_kunjungan', 'id_kunjungan', 'trim');
        $this->form_validation->set_error_delimiters('<span class="text-danger">', '</span>');
    }

    public function get_sub_tujuan($id)
    {
        $data = $this->Home_model->get_sub_tujuan($id);
        echo json_encode($data);
    }
}
