<?php

/*
 * *************************************************
 * Copyright (C) 2020 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 DB;

/**
 * Description of Migrate
 *
 * @author andhy
 */
class Migrate extends CI_Controller {

    public function index() {
        $this->load->library('migration');

//        if ($this->migration->current() === FALSE) {
//            show_error($this->migration->error_string());
//        }
    }

    public function permission() {
        $this->load->model('permission_model');
        $permission_list = User_model::permission_list();
        $permission_saved = Permission_model::get_dbo()->get()->pluck('id')->toArray();

       $batch = [];
            foreach ($permission_list as $id => $data) {
                if (!in_array($id, $permission_saved)) {
                    $batch[] = [
                        'id' => $id,
                        'group' => $data['group'],
                        'name' => $data['name'],
                        'description' => $data['desc'],
                        'status' => 1,
                        'active' => 1,
                        'created_at' => date('Y-m-d H:i:s'),
                        'updated_at' => date('Y-m-d H:i:s'),
                    ];
                } else {
                    $permission = Permission_model::get_dbo()->where('id', $id)->first();
                    $permission->group = $data['group'];
                    $permission->name = $data['name'];
                    $permission->description = $data['desc'];
                    $permission->save();
                }
            }
            if ($batch) {
                Permission_model::insert($batch);
                foreach($batch as $res){
                    echo "Generate Permission: {$res['name']}<br />";
                }
            }
    }

    public function version($id = null) {
        $this->load->library('migration');
        $this->migration->version($id);
    }

    public function dummy($table_name = null) {
        $this->db->trans_begin();
        if ($table_name) {
            $table_name = '_' . $table_name;
            if (method_exists($this, $table_name)) {
                $this->$table_name();
            } else {
                echo 'Table not found';
            }
        } else {
            // define all table here for seeding
            $this->_UsrMaster();
            $this->_UsrAuthority();
            $this->_UsrControl();
            $this->_UsrListAuthority();
            $this->_UsrListControl();
            $this->_AppContent();
            $this->_UsrProfile();
        }
        if ($this->db->trans_status() === true) {
            $this->db->trans_commit();
        } else {
            $this->db->trans_rollback();
        }
    }

    private function _output_dummy($query, $table) {
        if (!$query) {
            echo "<pre>";
            print_r($this->db->error());
            echo "</pre>";
        } else {
            echo "<pre>";
            print_r('Dummy for ' . $table . ' complete.');
            echo "</pre>";
        }
    }

    // $2y$10$I3xMhrnqovcv6qVqRc12LO2Ie0lHiEroqfM2nAY7N7eAS2hXMMpZy
    //$2y$10$mbfBkMgakZfl5dCkqEldGup4OpLbQQJ5R9iBE9AUJvQx8H8GU494a
    private function _UsrMaster() {
        $this->db->empty_table('UsrMaster');
        $query = $this->db->query(<<<SQL
            INSERT INTO `UsrMaster` (`userId`, `userName`, `userPassword`, `userEmail`, `userLoginToken`, `userResetToken`, `userVerifyToken`, `userStatus`, `userState`, `createdAt`, `updatedAt`, `deletedAt`) VALUES
(1, 'admin@earworks.co.id', '', 'admin@earworks.co.id', '', '', '', 1, 1, '2020-08-21 16:03:34', '2020-12-11 02:48:23', NULL),
(2, 'adminuser@earworks.co.id', '', 'adminuser@earworks.co.id', '', '', '', 1, 1, '2020-12-10 14:47:39', '2020-12-11 02:48:13', NULL);
SQL
        );
        $this->_output_dummy($query, 'UsrMaster');
    }

}
