<?php

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


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

    public function Job_payment()
    {
        $header['title']='Deal Job & Payment';
        echo view('Transactional/DealJobPayment.php',$header);
    
        
    }

    public function Display_talent()
    {
             $session = session();
             $username = $session->USER_NAME;
             $userlevel = $session->USER_LEVEL;
             $TalentModel = new TalentModel();
             $UserModel = new UserModel();
            
             if($userlevel == "TALENT_MANAGER"){
                $data['data'] = $TalentModel->where('PARENT_USERNAME',$username)->orderby('XNAME','ASC')->findAll();
             }else{
                $data['data'] = $TalentModel->orderby('XNAME','ASC')->findAll();
             }
             
           
             
             return $this->response->setJson($data); 

    }
    public function Display_brand()
    {
             $session = session();
             $username = $session->USER_NAME;
             $userlevel = $session->USER_LEVEL;
             $BrandModel = new BrandModel();
             $UserModel = new UserModel();
             $data['data'] = $BrandModel->orderby('XNAME','ASC')->findAll();
             return $this->response->setJson($data); 

    }

    public function Job_payment_save_manager()
    {
        $session = session();
        $username = $session->USER_NAME;
        $datenow = date("Y-m-d H:i:s");

        $DealjobModel = new DealjobModel;
        $data = [
            'DEAL_YEAR' => $this->request->getPost('DEAL_YEAR'),
            'DEAL_MONTH' =>$this->request->getPost('DEAL_MONTH'),
            'TALENT_ID' => $this->request->getPost('TALENT_ID'),
           
            'BRAND_ID' => $this->request->getPost('BRAND_ID'),
            'DEAL_BY' => $this->request->getPost('DEAL_BY'), 
            'DEAL_VAL' => str_replace(',', '', $this->request->getPost('DEAL_VAL')),
            'CRTBY' => $username,
            'UPDBY' => $username,
            'CRTDTM' => $datenow,
            'UPDDTM' => $datenow,
            'DEAL_VAL_TAX' => str_replace(',', '', $this->request->getPost('DEAL_VAL_TAX'))

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


    public function Job_payment_update()
    {
        $session = session();
        $username = $session->USER_NAME;
        $datenow = date("Y-m-d H:i:s");

        $id = $this->request->getPost('DEAL_ID');


        $DTM_PAID_CLIENT =  $this->request->getPost('DTM_PAID_CLIENT');
        $DTM_PAID_TALENT = str_replace('/', '-',  $this->request->getPost('DTM_PAID_TALENT'));
        $DTM_PAID_MANAGER = str_replace('/', '-',  $this->request->getPost('DTM_PAID_MANAGER'));

        

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

        if($DTM_PAID_MANAGER != ""){
            $DTM_PAID_MANAGER = date('Y-m-d', strtotime($DTM_PAID_MANAGER));
        }else{
            $DTM_PAID_MANAGER = null;
        }
        
        $DealjobModel = new DealjobModel;
        $data = [
            'DEAL_YEAR' => $this->request->getPost('DEAL_YEAR'),
            'DEAL_MONTH' =>$this->request->getPost('DEAL_MONTH'),
            'TALENT_ID' => $this->request->getPost('TALENT_ID'),
            'BRAND_ID' => $this->request->getPost('BRAND_ID'),
            'DEAL_BY' => $this->request->getPost('DEAL_BY'), 
            'DEAL_VAL' => str_replace(',', '', $this->request->getPost('DEAL_VAL')),   
            'DEAL_VAL_TAX' => str_replace(',', '', $this->request->getPost('DEAL_VAL_TAX')),   

            'STAT_PAID_CLIENT' => $this->request->getPost('STAT_PAID_CLIENT'), 
            'DTM_PAID_CLIENT' => $DTM_PAID_CLIENT,
            'DTL_STAT_CLIENT' => $this->request->getPost('DTL_STAT_CLIENT'), 
            'STAT_PAID_TALENT' => $this->request->getPost('STAT_PAID_TALENT'), 
            'DTM_PAID_TALENT' => $DTM_PAID_TALENT, 
            'DTL_STAT_TALENT' => $this->request->getPost('DTL_STAT_TALENT'), 
            'INV' => $this->request->getPost('INV'), 
            'INV_NUMBER' => $this->request->getPost('INV_NUMBER'), 
            'STAT_TAX_PAID' => $this->request->getPost('STAT_TAX_PAID'), 
            'FAKTUR_PAJAK' => $this->request->getPost('FAKTUR_PAJAK'), 
            'CONTRACT' => $this->request->getPost('CONTRACT'), 
            'UPDBY' => $username,       
            'UPDDTM' => $datenow,
            'DTM_PAID_MANAGER' => $DTM_PAID_MANAGER,
            'STAT_PAID_MANAGER' => $this->request->getPost('STAT_PAID_MANAGER')

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


      public function Dealjob_fetch()
    {
        $header['title']='Deal Job & Payment';
         $DealjobModel = new DealjobModel();
        $session = session();
        $username = $session->USER_NAME;
        $level = $session->USER_LEVEL;
        if($level == "TALENT_MANAGER" || $level == "TALENT" )
            {
        
         $data['data']= $DealjobModel->query("SELECT d.DEAL_ID,
                    d.DEAL_YEAR,
                    d.DEAL_MONTH,
                    t.XNAME as talent_name,
                    d.BRAND_ID as brand_name,
                    d.DEAL_BY,
                    d.DEAL_VAL,
                    d.DEAL_VAL_TAX,
                    COALESCE(d.STAT_PAID_CLIENT, '') as STAT_PAID_CLIENT,
                    COALESCE(d.DTM_PAID_CLIENT, '') as DTM_PAID_CLIENT,
                    COALESCE(d.DTL_STAT_CLIENT, '') as DTL_STAT_CLIENT,
                    d.STAT_PAID_TALENT,
                    COALESCE(d.DTM_PAID_TALENT, '') as DTM_PAID_TALENT,
                    COALESCE(d.DTL_STAT_TALENT, '') as DTL_STAT_TALENT,
                    COALESCE(d.INV, '') as INV,
                    d.STAT_PAID_MANAGER,
                    COALESCE(d.DTM_PAID_MANAGER, '') as DTM_PAID_MANAGER,
                    COALESCE(d.INV_NUMBER, '') as INV_NUMBER,
                    d.STAT_TAX_PAID,
                    COALESCE(d.FAKTUR_PAJAK, '') as FAKTUR_PAJAK,
                    COALESCE(d.CONTRACT, '') as CONTRACT
                    FROM `tran_dealjob_payment` d
                    inner join ref_talent t on t.TALENT_ID = d.TALENT_ID
                    
                    where t.PARENT_USERNAME = '".$username."' or t.USER_NAME = '".$username."';")->getResult();
            }else{
                 $data['data']= $DealjobModel->query("SELECT d.DEAL_ID,
                 d.DEAL_YEAR,
                 d.DEAL_MONTH,
                 t.XNAME as talent_name,
                 d.BRAND_ID as brand_name,
                 d.DEAL_BY,
                 d.DEAL_VAL,
                 d.DEAL_VAL_TAX,
                 COALESCE(d.STAT_PAID_CLIENT, '') as STAT_PAID_CLIENT,
                 COALESCE(d.DTM_PAID_CLIENT, '') as DTM_PAID_CLIENT,
                 COALESCE(d.DTL_STAT_CLIENT, '') as DTL_STAT_CLIENT,
                 d.STAT_PAID_TALENT,
                 d.STAT_PAID_MANAGER,
                 COALESCE(d.DTM_PAID_MANAGER, '') as DTM_PAID_MANAGER,
                 COALESCE(d.DTM_PAID_TALENT, '') as DTM_PAID_TALENT,
                 COALESCE(d.DTL_STAT_TALENT, '') as DTL_STAT_TALENT,
                 COALESCE(d.INV, '') as INV,
                 COALESCE(d.INV_NUMBER, '') as INV_NUMBER,
                 d.STAT_TAX_PAID,
                 COALESCE(d.FAKTUR_PAJAK, '') as FAKTUR_PAJAK,
                 COALESCE(d.CONTRACT, '') as CONTRACT
                 FROM `tran_dealjob_payment` d
                 inner join ref_talent t on t.TALENT_ID = d.TALENT_ID;
                 ")->getResult();
            }
        return $this->response->setJson($data); 
    
        
    }
    public function Deal_detail()
    {
             $DealjobModel = new DealjobModel();
             $id= $this->request->getPost('id');
             $data['data'] = $DealjobModel->find($id);
             return $this->response->setJson($data); 

    }

  

    public function draf_post()
    {
        $header['title']='Schedule draft & post';
        echo view('Transactional/Schedule_draft_post.php',$header);
    
        
    }



 public function draft_save()
    {
        $session = session();
        $username = $session->USER_NAME;
        $datenow = date("Y-m-d H:i:s");

         $DRAFT_DTM = str_replace('/', '-',  $this->request->getPost('DRAFT_DTM'));

        $DraftpostModel = new DraftpostModel;
        $data = [
            'TALENT_ID' => $this->request->getPost('TALENT_ID'),
            'DRAFT_DTM' => date('Y-m-d', strtotime($DRAFT_DTM)),
            'MEDIA_TYPE' => $this->request->getPost('MEDIA_TYPE'),           
            'DETAIL_DRAFT' => $this->request->getPost('DETAIL_DRAFT'),
            'CRTBY' => $username,
            'UPDBY' => $username,
            'CRTDTM' => $datenow,
            'UPDDTM' => $datenow

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

    public function update_schedule()
    {
        $session = session();
        $username = $session->USER_NAME;
        $datenow = date("Y-m-d H:i:s");
        $id = $this->request->getPost('id');

        $DRAFT_DTM = str_replace('/', '-',  $this->request->getPost('DRAFT_DTM'));

        $DraftpostModel = new DraftpostModel;
        $data = [
            'TALENT_ID' => $this->request->getPost('TALENT_ID'),
            'DRAFT_DTM' => date('Y-m-d', strtotime($DRAFT_DTM)),
            'MEDIA_TYPE' => $this->request->getPost('MEDIA_TYPE'),           
            'DETAIL_DRAFT' => $this->request->getPost('DETAIL_DRAFT'),
            'UPDBY' => $username,
            'UPDDTM' => $datenow

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


    public function event_detail()
    {
        $DraftpostModel = new DraftpostModel();
           
        $uri = service('uri');
        $id = $uri->getSegment('4');
        $header['title']='Deal Job & Payment: '.$id;
        $header['id'] = $id;
        echo view('Transactional/Schedule_draft_post.php',$header);   
        
    }

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

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


     public function performance_fetch()
    {
        $header['title']='Performance';
        $PerformanceModel = new PerformanceModel();
       
        
         $data['data']= $PerformanceModel->query("SELECT 
            a.PERFORM_ID,
            t.XNAME as talent_name,
            a.PERFORM_YEAR,
            a.PERFORM_MONTH,
            a.FW_IG,
            a.PROG_IG,
            a.FW_TIKTOK,
            a.PROG_TIKTOK,
            a.FW_TWITTER,
            a.PROG_TWITTER,
            a.FW_YOUTUBE,
            a.PROG_YOUTUBE,
            a.ER_IG,
            a.ER_PROG_IG,
            a.ER_TIKTOK,
            a.ER_PROG_TIKTOK,
            a.ER_TWITTER,
            a.ER_PROG_TWITTER,
            a.ER_YOUTUBE,
            a.ER_PROG_YOUTUBE
            FROM tran_performance a
            inner join ref_talent t on t.TALENT_ID = a.TALENT_ID;")->getResult();
        return $this->response->setJson($data); 
    
        
    }


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

    }

    public function performance_save()
    {
        $session = session();
        $username = $session->USER_NAME;
        $datenow = date("Y-m-d H:i:s");

        $PerformanceModel = new PerformanceModel;
        $data = [
            'TALENT_ID' => $this->request->getPost('TALENT_ID'),
            'PERFORM_YEAR' =>$this->request->getPost('PERFORM_YEAR'),
            'PERFORM_MONTH' => $this->request->getPost('PERFORM_MONTH'),           
            'FW_IG' => $this->request->getPost('FW_IG'),
            'PROG_IG' => $this->request->getPost('PROG_IG'), 
            'FW_TIKTOK' => $this->request->getPost('FW_TIKTOK'), 
            'PROG_TIKTOK' => $this->request->getPost('PROG_TIKTOK'), 
            'FW_TWITTER' => $this->request->getPost('FW_TWITTER'), 
            'PROG_TWITTER' => $this->request->getPost('PROG_TWITTER'), 
            'FW_YOUTUBE' => $this->request->getPost('FW_YOUTUBE'), 
            'PROG_YOUTUBE' => $this->request->getPost('PROG_YOUTUBE'), 
            'ER_IG' => $this->request->getPost('ER_IG'), 
            'ER_PROG_IG' => $this->request->getPost('ER_PROG_IG'), 
            'ER_TIKTOK' => $this->request->getPost('ER_TIKTOK'), 
            'ER_PROG_TIKTOK' => $this->request->getPost('ER_PROG_TIKTOK'), 
            'ER_TWITTER' => $this->request->getPost('ER_TWITTER'), 
            'ER_PROG_TWITTER' => $this->request->getPost('ER_PROG_TWITTER'), 
            'ER_YOUTUBE' => $this->request->getPost('ER_YOUTUBE'), 
            'ER_PROG_YOUTUBE' => $this->request->getPost('ER_PROG_YOUTUBE'), 
            'CRTBY' => $username,
            'UPDBY' => $username,
            'CRTDTM' => $datenow,
            'UPDDTM' => $datenow

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

     public function Performance_update()
    {
        $session = session();
        $username = $session->USER_NAME;
        $datenow = date("Y-m-d H:i:s");

        $id = $this->request->getPost('PERFORM_ID');
        $PerformanceModel = new PerformanceModel;
        $data = [
            'TALENT_ID' => $this->request->getPost('TALENT_ID'),
            'PERFORM_YEAR' =>$this->request->getPost('PERFORM_YEAR'),
            'PERFORM_MONTH' => $this->request->getPost('PERFORM_MONTH'),           
            'FW_IG' => $this->request->getPost('FW_IG'),
            'PROG_IG' => $this->request->getPost('PROG_IG'), 
            'FW_TIKTOK' => $this->request->getPost('FW_TIKTOK'), 
            'PROG_TIKTOK' => $this->request->getPost('PROG_TIKTOK'), 
            'FW_TWITTER' => $this->request->getPost('FW_TWITTER'), 
            'PROG_TWITTER' => $this->request->getPost('PROG_TWITTER'), 
            'FW_YOUTUBE' => $this->request->getPost('FW_YOUTUBE'), 
            'PROG_YOUTUBE' => $this->request->getPost('PROG_YOUTUBE'), 
            'ER_IG' => $this->request->getPost('ER_IG'), 
            'ER_PROG_IG' => $this->request->getPost('ER_PROG_IG'), 
            'ER_TIKTOK' => $this->request->getPost('ER_TIKTOK'), 
            'ER_PROG_TIKTOK' => $this->request->getPost('ER_PROG_TIKTOK'), 
            'ER_TWITTER' => $this->request->getPost('ER_TWITTER'), 
            'ER_PROG_TWITTER' => $this->request->getPost('ER_PROG_TWITTER'), 
            'ER_YOUTUBE' => $this->request->getPost('ER_YOUTUBE'), 
            'ER_PROG_YOUTUBE' => $this->request->getPost('ER_PROG_YOUTUBE'), 
            'UPDBY' => $username,       
            'UPDDTM' => $datenow

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

    public function calendarView() {
    $DraftpostModel = new DraftpostModel;
	$start 	= $_POST['start'];
	//$sdate	= date("Y-m-d\TH:i\Z", time($start));
	$end 	= $_POST['end'];
    $session = session();
    $username = $session->USER_NAME;
    $level = $session->USER_LEVEL;
	//$edate	= date("Y-m-d\TH:i\Z", time($end));
	$bookings = array();
	$sql	= "SELECT u.DETAIL_DRAFT, u.DRAFT_ID as id, r.XNAME as u_name, u.TALENT_ID as user_id , u.DRAFT_DTM as rdate, 'PENDING' as status FROM `tran_draft_post` u 
    inner join ref_talent r on r.TALENT_ID = u.TALENT_ID
			   AND (u.DRAFT_DTM BETWEEN '$start' AND '$end') 
               where r.PARENT_USERNAME = '".$username."' or r.USER_NAME = '".$username."'";
	//AND r.status = 'APPROVED'
	$result = $DraftpostModel->query($sql);
    foreach($result->getResultArray() as $row){
    	extract($row);
		$book = new Booking();
		$book->title = $u_name.'/'.$DETAIL_DRAFT;
		$book->start = $rdate; 
		$bgClr = '#f39c12';//pending
		if($status == 'DENIED') {$bgClr = '#ff0000';}
		else if($status == 'APPROVED') {$bgClr = '#00cc00';}
		$book->backgroundColor = $bgClr; //#7FFF00 -> green, #ff0000 red, #f39c12 -> pending 
		$book->borderColor = $bgClr;
		$book->url = site_url('tran/get/event/' . $id);
		$bookings[] = $book; 
	}
	
	echo json_encode($bookings);
}



    
    
}

class Booking {
	
	public $title;
	public $start;
	public $allDay = false;
	public $backgroundColor;
	public $borderColor;
	public $url;
	public $className;
	public $block = false;
		
	public function __constructor($t, $s) {
		$this->title = $t;
		$this->start = $s;
	}

}
	