<?php

/*
 * *************************************************
 * Copyright (C) 2021 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');

use Illuminate\Database\Capsule\Manager as Capsule;

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

    public function index() {
        if (!$this->user) {
            redirect('login');
        }
        Ci::view(__METHOD__);
    }

    public function change_profile() {
        if (!$this->user) {
            redirect('login');
        }
        $this->load->helper('form');
        $user = User_model::get_dbo()->where('id', $this->user['user']['id'])->first(); // because stored as array in session

        $profile_model = $user->profile;
        // post data
        $data_user = [
            'username' => $user->username,
            'old_password' => $this->post('old_password'),
            'new_password' => $this->post('new_password'),
            'confirm_password' => $this->post('confirm_password')
        ];
        $data_profile = [
            'name' => $this->post('name', $profile_model->name),
            'email' => $this->post('email', $profile_model->email),
            'phone' => $this->post('phone', $profile_model->phone)
        ];
        $data = array_merge($data_user, $data_profile);
        // validate data
        $data_validate = [
            'name' => [
                'rules' => 'trim|required',
                'message' => ['required' => lang('%s_reqired')]
            ],
            'email' => [
                'rules' => 'trim|valid_email',
                'message' => ['valid_email' => lang('%s_not_valid_email')]
            ]
        ];
        if ($data['old_password']) {
            $data_validate = array_merge($data_validate, [
                'old_password' => [
                    'rules' => 'trim|required',
                    'message' => ['required' => lang('%s_reqired')]
                ],
                'new_password' => [
                    'rules' => 'trim|required',
                    'message' => ['required' => lang('%s_reqired')]
                ],
                'confirm_password' => [
                    'rules' => 'trim|required|matches[new_password]',
                    'message' => [
                        'required' => lang('%s_reqired'),
                        'matches' => lang('%s_not_match')
                    ]
                ]
            ]);
        }
        $validate = $this->validate_form($data_validate, false);
        // prosessing data
        if ($validate === true) {
            $this->load->helper('hash');
            Capsule::beginTransaction();
            // we can save profile information directly
            $profile_model->save_update($data_profile);
            // checking old password
            if ($data['old_password']) { // if old_password entered
                if (run_verify($data['old_password'], $user->password)) {
                    $user->save_update([
                        'password' => run_hash($data['new_password']),
                        'password_default' => ''
                    ]);
                    Capsule::commit();
                    $this->session->set_flashdata('changed_password', 1);
                    $this->session->set_flashdata('success', lang('update_profile_success'));
                    redirect($this->get_redir());
                } else {
                    $this->session->set_flashdata('danger', lang('old_password_not_match'));
                    Capsule::rollback();
                }
            } else {
                // assumtion is only change the profile information
                Capsule::commit();
                $this->session->set_flashdata('success', lang('update_profile_success'));
                redirect($this->get_redir());
            }
        } elseif ($validate[static::$field_status] === static::CODE_INVALID_PARAMETER) {
            $this->_show_alert($validate);
        }

        Ci::view(__METHOD__, [
            'data' => $data,
            'redir' => $this->get_redir(true), // true = encode url (for display)
            'model' => $user,
            'profile_model' => $profile_model,
        ]);
    }

}
