<?php

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

class Bkoadmin extends Admin_Controller {

    public function session() {
        $data = array();
        //   $session_table="";
        $mysql = "SELECT l.added,l.loc,u.firstname,u.lastname,u.email,l.action FROM bko_users_log l LEFT JOIN bko_users u ON u.id=l.user_id ORDER by l.added DESC LIMIT 20";
        $q = $this->db->query($mysql);
        $data['recent_logs'] = $q->result();


        $tbq['count_query'] = "SELECT * FROM backoffice_session";
        $tbq['query'] = "SELECT * FROM backoffice_session";
        $out = $this->returnAdminTable($tbq, '/bkoadmin/users');
        //   print_r( $out );
        $data['session_table'] = $out['output_table'];

        $this->renderAdminPage('view_bko_session', $data);
    }

   public function service(){
         $data = array();
        $mysql = "SELECT * FROM dryclean_service ORDER BY id ASC";
        

        $tbq['count_query'] = $mysql;
        $tbq['query'] = $mysql;
        $out = $this->returnAdminTable($tbq, '/bkoadmin/users');
        //   print_r( $out );
        $data['dryclean_table'] = $out['output_table'];

        $this->renderAdminPage('bkoadmin/view_bko_dryclean', $data);
        
    }
    public function users() {
        $data = array();
        $mysql = "SELECT l.added,l.loc,u.firstname,u.lastname,u.email,l.action FROM bko_users_log l LEFT JOIN bko_users u ON u.id=l.user_id ORDER by l.added DESC LIMIT 15";
        $q = $this->db->query($mysql);
        $data['recent_logs'] = $q->result();

        $tbq['count_query'] = "SELECT id, pid, firstname,lastname ,email,username,plevel,added::date,last_login from bko_users";
        $tbq['query'] = "SELECT id, pid, firstname,lastname ,email,username,plevel,added::date,last_login,'<button class=\"btn-primary\">Edit</button>' AS action FROM bko_users";
        $out = $this->returnAdminTable($tbq, '/bkoadmin/users');
        //   print_r( $out );
        $data['output_table'] = $out['output_table'];
        $data['links'] = $out['links'];


        $this->renderAdminPage('bkoadmin/view_bko_users', $data);
    }

    public function adduser() {


        $data = array();

        $data = array();
        $mysql = "SELECT l.added,l.loc,u.firstname,u.lastname,u.email,l.action FROM bko_users_log l LEFT JOIN bko_users u ON u.id=l.user_id ORDER by l.added DESC LIMIT 15";
        $q = $this->db->query($mysql);
        $data['recent_logs'] = $q->result();


        $data['firstname'] = $data['lastname'] = $data['email'] = $data['username'] = $data['password'] = "";

        if ($this->input->post()) {
            $data = $this->input->post();
            // print_r($data);
            $this->createBkoUser($data);
        }

        $mysql = 'SELECT b.firstname,b.lastname,b.username,bl.added,bl.action,bl.loc FROM bko_users_log bl LEFT JOIN bko_users b ON b.id=bl.user_id ORDER by bl.id desc LIMIT 25';
        $this->renderAdminPage('bkoadmin/view_bko_newuser', $data);
    }

    public function configure() {
        $data = array();
        $data['page_title'] = "Configurations";
        $this->renderAdminPage('bkoadmin/view_bko_configure', $data);
    }

    public function profile() {
        $data = array();
        $data['log_limit'] = 15;
        $mysql = "SELECT l.added,l.loc,u.firstname,u.lastname,u.email,l.action FROM bko_users_log l LEFT JOIN bko_users u ON u.id=l.user_id ORDER by l.added DESC LIMIT " . $data['log_limit'];
        $q = $this->db->query($mysql);
        $data['recent_logs'] = $q->result();

        $tbq['count_query'] = "SELECT * FROM bko_users";
        $tbq['query'] = "SELECT * FROM bko_users";
        $out = $this->returnAdminTable($tbq, '/bkoadmin/users');
        //   print_r( $out );
        $data['output_table'] = $out['output_table'];
        $data['links'] = $out['links'];

        //print_r($_SESSION);  [username] => medtransgo [firstname] => MedtransGo [lastname] 

        $data['page_title'] = "My User Profile [<b>" . $_SESSION['firstname'] . " " . $_SESSION['lastname'] . "</b>]";
        $this->renderAdminPage('bkoadmin/view_bko_profile', $data);
    }

    public function activity() {
        $data = array();
        $mysql = "SELECT l.added,l.loc,u.firstname,u.lastname,u.email,l.action FROM bko_users_log l LEFT JOIN bko_users u ON u.id=l.user_id ORDER by l.added DESC LIMIT 20";
        $q = $this->db->query($mysql);
        $data['recent_logs'] = $q->result();


        $tbq['count_query'] = "SELECT * FROM bko_users_log";
        $tbq['query'] = "SELECT * FROM bko_users_log ORDER BY id DESC";
        $out = $this->returnAdminTable($tbq, '/bkoadmin/users');
        //   print_r( $out );
        $data['session_table'] = $out['output_table'];


        $this->renderAdminPage('view_bko_session', $data);
    }

    public function permssion() {
        $data = array();

        $data = array();
        $mysql = "SELECT l.added,l.loc,u.firstname,u.lastname,u.email,l.action FROM bko_users_log l LEFT JOIN bko_users u ON u.id=l.user_id ORDER by l.added DESC LIMIT 20";
        $q = $this->db->query($mysql);
        $data['recent_logs'] = $q->result();


        $this->renderAdminPage('view_bko_session', $data);
    }

    private function createBkoUser($data) {

        if ($data['firstname'] != '' && $data['lastname'] != '' && $data['email'] != '' && $data['username'] != '' && $data['password'] != "") {

            $data['action'] = MEDTRANS_BKO_CREATEUSER;
            $out = array();
            $ret = $this->medtrans_api($data, $out);
//print_r($out);
//exit();
            if ($ret == PHP_API_OK) {
                
            }
        } else {
            
        }
    }

}
