<?php

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

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;

        if($CONTRACT_START_DT != ""){
            $CONTRACT_START_DT = date('Y-m-d', strtotime($CONTRACT_START_DT));
        }else{
             $CONTRACT_START_DT = null;
        }

        if($CONTRACT_END_DT != ""){
            $CONTRACT_END_DT = date('Y-m-d', strtotime($CONTRACT_START_DT));
        }else{
            $CONTRACT_END_DT = null;
        }

        $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' => $CONTRACT_START_DT,
            'CONTRACT_END_DT' => $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;

        if($CONTRACT_START_DT != ""){
            $CONTRACT_START_DT = date('Y-m-d', strtotime($CONTRACT_START_DT));
        }else{
             $CONTRACT_START_DT = null;
        }

        if($CONTRACT_END_DT != ""){
            $CONTRACT_END_DT = date('Y-m-d', strtotime($CONTRACT_START_DT));
        }else{
            $CONTRACT_END_DT = null;
        }

        $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' => $CONTRACT_START_DT,
            'CONTRACT_END_DT' => $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'));

        if($TERMINATION_DT != ""){
            $TERMINATION_DT = date('Y-m-d', strtotime($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' => $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'));

        if($TERMINATION_DT != ""){
            $TERMINATION_DT = date('Y-m-d', strtotime($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' => $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);
    
        
    }

    public function Get_talent()
    {
        $header['title']='Finance rate';
         $TalentModel = new TalentModel();
        $session = session();
        $username = $session->USER_NAME;
        $level = $session->USER_LEVEL;
        
         $data['data']= $TalentModel->query("SELECT t.TALENT_ID, t.XNAME FROM `ref_user` usr
            inner join ref_talent t on t.USER_NAME = usr.USER_NAME
            where t.PARENT_USERNAME = '".$username."' or t.USER_NAME = '".$username."'
            and usr.USER_LEVEL = 'TALENT' order by t.XNAME asc")->getResult();
        return $this->response->setJson($data); 
    
        
    }

     public function Finance_fetch()
    {
        $header['title']='Finance rate';
         $FinanceModel = new FinanceModel();
        $session = session();
        $username = $session->USER_NAME;
        $level = $session->USER_LEVEL;
       
          if($level == "TALENT_MANAGER" || $level == "TALENT" )
            {
         $data['data']= $FinanceModel->query("SELECT t.XNAME,
            fin.RATE_ID,
            fin.R_YEAR,
            CONCAT('', FORMAT(fin.IGS, 0)) as IGS,
            CONCAT('', FORMAT(fin.IG_PHOTO, 0)) as IG_PHOTO,
            CONCAT('', FORMAT(fin.IG_VIDEO, 0)) as IG_VIDEO,
            CONCAT('', FORMAT(fin.IG_LIVE, 0)) as IG_LIVE,
            CONCAT('', FORMAT(fin.TWITTER, 0)) as TWITTER,
            CONCAT('', FORMAT(fin.YOUTUBE, 0)) as YOUTUBE,
            CONCAT('', FORMAT(fin.ECOM, 0)) as ECOM,
            CONCAT('', FORMAT(fin.EVENT_ATT, 0)) as EVENT_ATT
            FROM `finance_rate` fin
            inner join ref_talent t on t.TALENT_ID = fin.TALENT_ID
            where t.PARENT_USERNAME = '".$username."' or t.USER_NAME = '".$username."'            
                        order by t.XNAME asc;")->getResult();
            }else{
             $data['data']= $FinanceModel->query("SELECT t.XNAME,
            fin.RATE_ID,
            fin.R_YEAR,
            CONCAT('', FORMAT(fin.IGS, 0)) as IGS,
            CONCAT('', FORMAT(fin.IG_PHOTO, 0)) as IG_PHOTO,
            CONCAT('', FORMAT(fin.IG_VIDEO, 0)) as IG_VIDEO,
            CONCAT('', FORMAT(fin.IG_LIVE, 0)) as IG_LIVE,
            CONCAT('', FORMAT(fin.TWITTER, 0)) as TWITTER,
            CONCAT('', FORMAT(fin.YOUTUBE, 0)) as YOUTUBE,
            CONCAT('', FORMAT(fin.ECOM, 0)) as ECOM,
            CONCAT('', FORMAT(fin.EVENT_ATT, 0)) as EVENT_ATT
            FROM `finance_rate` fin
            inner join ref_talent t on t.TALENT_ID = fin.TALENT_ID           
            order by t.XNAME asc;")->getResult();    

            }
        return $this->response->setJson($data); 
    
        
    }
    public function Finance_simpan()
    {
       
        $datenow = date("Y-m-d H:i:s");
       
        $FinanceModel = new FinanceModel;
        $data = [
            'TALENT_ID' => $this->request->getPost('TALENT_ID'),
            'R_YEAR' =>$this->request->getPost('R_YEAR'),
            'IGS' => str_replace(',', '', $this->request->getPost('IGS')),
            'IG_PHOTO' => str_replace(',', '', $this->request->getPost('IG_PHOTO')),
            'IG_VIDEO' => str_replace(',', '', $this->request->getPost('IG_VIDEO')),
            'IG_LIVE' => str_replace(',', '', $this->request->getPost('IG_LIVE')), 
            'TWITTER' => str_replace(',', '', $this->request->getPost('TWITTER')),
            'YOUTUBE' => str_replace(',', '', $this->request->getPost('YOUTUBE')), 
            'ECOM' => str_replace(',', '', $this->request->getPost('ECOM')),
            'EVENT_ATT' => str_replace(',', '', $this->request->getPost('EVENT_ATT')), 
            'CRTBY' => "Admin",
            'UPDBY' => "Admin",
            'CRTDTM' => $datenow,
            'UPDDTM' => $datenow

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

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

    }
    public function Finance_update()
    {
       
        $datenow = date("Y-m-d H:i:s");
        $id = $this->request->getPost('RATE_ID');
        $FinanceModel = new FinanceModel;
        $data = [
            'TALENT_ID' => $this->request->getPost('TALENT_ID'),
            'R_YEAR' =>$this->request->getPost('R_YEAR'),
            'IGS' => str_replace(',', '', $this->request->getPost('IGS')),
            'IG_PHOTO' => str_replace(',', '', $this->request->getPost('IG_PHOTO')),
            'IG_VIDEO' => str_replace(',', '', $this->request->getPost('IG_VIDEO')),
            'IG_LIVE' => str_replace(',', '', $this->request->getPost('IG_LIVE')), 
            'TWITTER' => str_replace(',', '', $this->request->getPost('TWITTER')),
            'YOUTUBE' => str_replace(',', '', $this->request->getPost('YOUTUBE')), 
            'ECOM' => str_replace(',', '', $this->request->getPost('ECOM')),
            'EVENT_ATT' => str_replace(',', '', $this->request->getPost('EVENT_ATT')), 
            'UPDBY' => "Admin",           
            'UPDDTM' => $datenow

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