<?php

namespace App\Controllers;
use App\Models\UserModel;


class CredentialController extends BaseController
{
  // Password hasing password_hash($password, PASSWORD_BCRYPT, 14);
    
    
    public function Login()
    {
        $header['title']='Login';
        echo view('Login.php',$header);
 
    }

    public function Masteruser()
    {
        $header['title']='Master user';
        echo view('Master/User.php',$header);
    
        
    }
    public function Simpan()
    {
        $pwdhas = ['cost' => 8, ];
        $datenow = date("Y-m-d H:i:s");
        $pass = $this->request->getPost('password');
        $xpassword = password_hash($pass, PASSWORD_BCRYPT, $pwdhas);
        $UserParam = new UserModel;
        $data = [
            'USER_NAME' => $this->request->getPost('username'),
            'USER_PWD' => $xpassword,
            'USER_LEVEL' => $this->request->getPost('level'),
            'IS_ACTIVE' => $this->request->getPost('active'),
            'CRTBY' => "Admin",
            'UPDBY' => "Admin",
            'CRTDTM' => $datenow,
            'UPDDTM' => $datenow

        ];
        $UserParam->save($data);
        $data = ['status' => 'data berhasil disimpan'];
        return $this->response->setJson($data);    
        
    }

    public function delete()
    {
        $id = $this->request->getPost('user_id');
        $datenow = date("Y-m-d H:i:s");
        $UserParam = new UserModel;
        $data = [
            'IS_DELETE' => 1,
            'CRTBY' => "Admin",
            'UPDBY' => "Admin",
            'IS_ACTIVE' => 0,
            'CRTDTM' => $datenow,
            'UPDDTM' => $datenow

        ];
        $UserParam->update($id,$data);
        $data = ['status' => 'data berhasil dihapus'];
        return $this->response->setJson($data);       
        
    }

     public function Update_data()
    {
        $pwdhas = ['cost' => 8, ];
        $datenow = date("Y-m-d H:i:s");
        $pass = $this->request->getPost('password');
        $id = $this->request->getPost('user_id');
        if($pass != "")
        {
            $xpassword = password_hash($pass, PASSWORD_BCRYPT, $pwdhas);
            $data = [
            
            'USER_PWD' => $xpassword,
            'USER_LEVEL' => $this->request->getPost('level'),
            'IS_ACTIVE' => $this->request->getPost('active'),
            'UPDBY' => "Admin",          
            'UPDDTM' => $datenow

        ];
       
        }
        else
        {
        $data = [
            
            
            'USER_LEVEL' => $this->request->getPost('level'),
            'IS_ACTIVE' => $this->request->getPost('active'),
            'UPDBY' => "Admin",          
            'UPDDTM' => $datenow

        ];
        }
       
        $UserParam = new UserModel;
        $UserParam->update($id,$data);
        $data = ['status' => 'data berhasil diupdate'];
        return $this->response->setJson($data);    
        
    }

    public function fetch()
    {
             $User = new UserModel();
             $data['data'] = $User->where('IS_DELETE',0)->findAll();
             return $this->response->setJson($data); 

    }


    public function Wherecon()
    {
             $User = new UserModel();
             $where= $this->request->getPost('KONDISI');
           
             $data['data'] = $User->where($where)->where('IS_ACTIVE',1)->where('IS_DELETE',0)->orderby('USER_NAME','ASC')->findAll();
             return $this->response->setJson($data); 

    }

    public function get_detail()
    {
             $User = new UserModel();
             $id= $this->request->getPost('id');
             $data['data'] = $User->find($id);
             return $this->response->setJson($data); 

    }
   
   
}
