<?php

namespace App\Controllers;
use App\Models\BrandModel;
use App\Models\TalentModel;
use App\Models\UserModel;
use App\Models\StafModel;

class MasterController extends BaseController
{
    public function index()
    {
       
       
    }

    public function Talent()
    {
        $header['title']='Master Talent';
        echo view('Master/Talent.php',$header);
    
        
    }

    public function Talent_simpan()
    {
       
        $datenow = date("Y-m-d H:i:s");

        
        // $User = new UserModel();
        // $username =  $this->request->getPost('USER_NAME');
        
        // $userid= $User->query("select * from ref_user where USER_NAME = '" . $username . "' AND IS_DELETE = 0")->getRow();
      
        $BIRTHDATE = str_replace('/', '-',  $this->request->getPost('BIRTHDATE'));
        $CONTRACT_START_DT = str_replace('/', '-',  $this->request->getPost('CONTRACT_START_DT'));
        $CONTRACT_END_DT = str_replace('/', '-',  $this->request->getPost('CONTRACT_END_DT'));
        $TalentModel = new TalentModel;
        $data = [
            'XNAME' => $this->request->getPost('XNAME'),
            'NICKNAME' =>$this->request->getPost('NICKNAME'),
            'PHONE_NUMBER' => $this->request->getPost('PHONE_NUMBER'),
            'EMAIL' => $this->request->getPost('EMAIL'),
            'ADRESS' => $this->request->getPost('ADRESS'),
            'BIRTHDATE' => $this->request->getPost('BIRTHDATE'),
            'IG' => $this->request->getPost('IG'), 
            'TWITTER' => $this->request->getPost('TWITTER'),
            'YOUTUBE' => $this->request->getPost('YOUTUBE'),
            'TIKTOK' => $this->request->getPost('TIKTOK'),
            'NOTES' => $this->request->getPost('NOTES'),
            'CONTRACT_START_DT' => date('Y-m-d', strtotime($CONTRACT_START_DT)),
            'CONTRACT_END_DT' => date('Y-m-d', strtotime($CONTRACT_END_DT)),
            'PARENT_USERNAME' => $this->request->getPost('PARENT_ID'),
            'BIRTHDATE' => date('Y-m-d', strtotime($BIRTHDATE)), 
            'USER_NAME' => $this->request->getPost('USER_NAME'),
            'CRTBY' => "Admin",
            'UPDBY' => "Admin",
            'CRTDTM' => $datenow,
            'UPDDTM' => $datenow

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


    public function Talent_update()
    {
       
        $datenow = date("Y-m-d H:i:s");

        
        // $User = new UserModel();
        // $username =  $this->request->getPost('USER_NAME');
        
        // $userid= $User->query("select * from ref_user where USER_NAME = '" . $username . "' AND IS_DELETE = 0")->getRow();
        $id = $this->request->getPost('TALENT_ID');
        $BIRTHDATE = str_replace('/', '-',  $this->request->getPost('BIRTHDATE'));
        $CONTRACT_START_DT = str_replace('/', '-',  $this->request->getPost('CONTRACT_START_DT'));
        $CONTRACT_END_DT = str_replace('/', '-',  $this->request->getPost('CONTRACT_END_DT'));
        $TalentModel = new TalentModel;
        $data = [
            'XNAME' => $this->request->getPost('XNAME'),
            'NICKNAME' =>$this->request->getPost('NICKNAME'),
            'PHONE_NUMBER' => $this->request->getPost('PHONE_NUMBER'),
            'EMAIL' => $this->request->getPost('EMAIL'),
            'ADRESS' => $this->request->getPost('ADRESS'),
            'BIRTHDATE' => $this->request->getPost('BIRTHDATE'),
            'IG' => $this->request->getPost('IG'), 
            'TWITTER' => $this->request->getPost('TWITTER'),
            'YOUTUBE' => $this->request->getPost('YOUTUBE'),
            'TIKTOK' => $this->request->getPost('TIKTOK'),
            'NOTES' => $this->request->getPost('NOTES'),
            'CONTRACT_START_DT' => date('Y-m-d', strtotime($CONTRACT_START_DT)),
            'CONTRACT_END_DT' => date('Y-m-d', strtotime($CONTRACT_END_DT)),
            'PARENT_USERNAME' => $this->request->getPost('PARENT_ID'),
            'BIRTHDATE' => date('Y-m-d', strtotime($BIRTHDATE)), 
            'USER_NAME' => $this->request->getPost('USER_NAME'),
         
            'UPDBY' => "Admin",
          
            'UPDDTM' => $datenow

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


    public function Talent_fetch()
    {
             $TalentModel = new TalentModel();
             $data['data'] = $TalentModel->Orderby('XNAME','ASC')->findAll();
             return $this->response->setJson($data); 

    }

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

    }
# Staf
    public function Staff()
    {
        $header['title']='Employee';
        echo view('Master/Staff.php',$header);
    
        
    }
    public function Staf_simpan()
    {
       
        $datenow = date("Y-m-d H:i:s");

        
       
        $BIRTHDATE = str_replace('/', '-',  $this->request->getPost('BIRTHDATE'));
        $JOIN_DT = str_replace('/', '-',  $this->request->getPost('JOIN_DT'));
        $TERMINATION_DT = str_replace('/', '-',  $this->request->getPost('TERMINATION_DT'));
        $StafModel = new StafModel;
        $data = [
            'XNAME' => $this->request->getPost('XNAME'),
            'POSITION' =>$this->request->getPost('POSITION'),
            'NIK' => $this->request->getPost('NIK'),
            'JOIN_DT' => date('Y-m-d', strtotime($JOIN_DT)),
            'TERMINATION_DT' => date('Y-m-d', strtotime($TERMINATION_DT)),
            'PHONE_NUMBER' => $this->request->getPost('PHONE_NUMBER'),
            'EMAIL' => $this->request->getPost('EMAIL'), 
            'BANK_NAME' => $this->request->getPost('BANK_NAME'),
            'ACCOUNT_NUMBER' => $this->request->getPost('ACCOUNT_NUMBER'),
            'USER_NAME' => $this->request->getPost('USER_NAME'),
            'BIRTHDATE' => date('Y-m-d', strtotime($BIRTHDATE)),
            'ADDRESS' =>  $this->request->getPost('ADDRESS'),
            'CRTBY' => "Admin",
            'UPDBY' => "Admin",
            'CRTDTM' => $datenow,
            'UPDDTM' => $datenow

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

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

    }
     public function Staf_detail()
    {
             $StafModel = new StafModel();
             $id= $this->request->getPost('id');
             $data['data'] = $StafModel->find($id);
             return $this->response->setJson($data); 

    }
    public function Staf_update()
    {
       
        $datenow = date("Y-m-d H:i:s");
        $id = $this->request->getPost('STAFF_ID');
        $StafModel = new StafModel;

        $BIRTHDATE = str_replace('/', '-',  $this->request->getPost('BIRTHDATE'));
        $JOIN_DT = str_replace('/', '-',  $this->request->getPost('JOIN_DT'));
        $TERMINATION_DT = str_replace('/', '-',  $this->request->getPost('TERMINATION_DT'));
        $data = [
            'XNAME' => $this->request->getPost('XNAME'),
            'POSITION' =>$this->request->getPost('POSITION'),
            'NIK' => $this->request->getPost('NIK'),
            'JOIN_DT' => date('Y-m-d', strtotime($JOIN_DT)),
            'TERMINATION_DT' => date('Y-m-d', strtotime($TERMINATION_DT)),
            'PHONE_NUMBER' => $this->request->getPost('PHONE_NUMBER'),
            'EMAIL' => $this->request->getPost('EMAIL'), 
            'BANK_NAME' => $this->request->getPost('BANK_NAME'),
            'ACCOUNT_NUMBER' => $this->request->getPost('ACCOUNT_NUMBER'),
            'USER_NAME' => $this->request->getPost('USER_NAME'),
            'BIRTHDATE' => date('Y-m-d', strtotime($BIRTHDATE)),
            'ADDRESS' =>  $this->request->getPost('ADDRESS'),
            'CRTBY' => "Admin",
            'UPDBY' => "Admin",
            'CRTDTM' => $datenow,
            'UPDDTM' => $datenow
        ];
        $StafModel->update($id,$data);
        $data = ['status' => 'data berhasil diupdate'];
        return $this->response->setJson($data);  
        
    }

#brand 

      public function Brand()
    {
        $header['title']='Brand';
        echo view('Master/Brand.php',$header);
    
        
    }

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

    }

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

    }

    public function Brand_simpan()
    {
       
        $datenow = date("Y-m-d H:i:s");
       
        $BrandModel = new BrandModel;
        $data = [
            'XNAME' => $this->request->getPost('XNAME'),
            'IG' =>$this->request->getPost('IG'),
            'PIC_NAME' => $this->request->getPost('PIC_NAME'),
            'IG_PIC' => $this->request->getPost('IG_PIC'),
            'PHONE_NUMBER' => $this->request->getPost('PHONE_NUMBER'),
            'PIC_ADDRESS' => $this->request->getPost('PIC_ADDRESS'), 
            'CRTBY' => "Admin",
            'UPDBY' => "Admin",
            'CRTDTM' => $datenow,
            'UPDDTM' => $datenow

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

    public function Brand_update()
    {
       
        $datenow = date("Y-m-d H:i:s");
        $id = $this->request->getPost('BRAND_ID');
        $BrandModel = new BrandModel;
        $data = [
            'XNAME' => $this->request->getPost('XNAME'),
            'IG' =>$this->request->getPost('IG'),
            'PIC_NAME' => $this->request->getPost('PIC_NAME'),
            'IG_PIC' => $this->request->getPost('IG_PIC'),
            'PHONE_NUMBER' => $this->request->getPost('PHONE_NUMBER'),
            'PIC_ADDRESS' => $this->request->getPost('PIC_ADDRESS'), 
            'UPDBY' => "Admin",       
            'UPDDTM' => $datenow

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

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

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

#end of brand

       public function Finance()
    {
        $header['title']='Finance rate';
        echo view('Master/Finance.php',$header);
    
        
    }
}
