<?php
namespace App\Controllers\Backend;

use App\Controllers\BackendController;
use Models\BlogsModel;
use Models\CandidateJobsModel;
use Models\CandidatesModel;
use Models\EmployeesModel;
use Models\JobEnquiriesModel;
use Models\JobsModel;
use Models\KeySkillsModel;
use Models\QualificationsModel;
use Models\StatesModel;
use Models\CitiesModel;
use Models\UsersModel;
use Models\UserTypesModel;

class AdminPortalController extends BackendController {
    
    public function actionViewAgreement() {

        $arrmixRequestData = $this->request->getGet();        

        $arrmixCandidateEnquiryDetails   = [];
        $strBreadCrumbFirstArrayHrefLink = 'job-enquiries/list';
        
        if( true == isset( $arrmixRequestData['candidate_job_id'] ) && true == isValId( $arrmixRequestData['candidate_job_id'] ) ) {
            
            $arrmixCandidateEnquiryDetails = CandidateJobsModel::createService()->findCandidateJobByCandidateJobId( $arrmixRequestData['candidate_job_id'] );

            $strBreadCrumbFirstArrayName     = 'Candidate Jobs';
            $strBreadCrumbFirstArrayHrefLink = 'candidate-jobs/list';

        } else if( true == isset( $arrmixRequestData['job_enquiry_id'] ) && true == isValId( $arrmixRequestData['job_enquiry_id'] ) ) {
            
            $arrmixCandidateEnquiryDetails = CandidateJobsModel::createService()->findCandidateJobByJobEnquiryId( $arrmixRequestData['job_enquiry_id'] );

            $strBreadCrumbFirstArrayName     = 'Job Enquiries';
            $strBreadCrumbFirstArrayHrefLink = 'job-enquiries/list';
        } 
        
        if( false == isValArr( $arrmixCandidateEnquiryDetails ) ) {
            
            $this->session->setFlashdata( 'error', 'Invalid Agreement details.' );
            return redirect()->route( $strBreadCrumbFirstArrayHrefLink );
        }
        
        $arrmixQualificationsList = rekeyArray( QualificationsModel::createService()->findAll(), 'qualification_id' );
        $arrmixKeySkillsList      = rekeyArray( KeySkillsModel::createService()->findAll(), 'key_skill_id' );
        $arrmixStateList          = rekeyArray( StatesModel::createService()->where( 'country_id', COUNTRY_INDIA )->findAll(), 'state_id');
        $arrmixCitiesList         = rekeyArray( CitiesModel::createService()->findAll(), 'city_id' );
        
        $arrmixBreadCrumbData['strContentHeader']     = 'View Agreement';
        $arrmixBreadCrumbData['arrmixBreadCrumbList'] = [
            ['name' => $strBreadCrumbFirstArrayName, 'is_active' => false, 'href' => getBaseUrl() . $strBreadCrumbFirstArrayHrefLink ],
            ['name' => $arrmixCandidateEnquiryDetails['first_name'] . ' ' . $arrmixCandidateEnquiryDetails['last_name'] . ' Agreement', 'is_active' => true]
        ];

        $arrmixData                                  = [];
        $arrmixData['strTitle']                      = 'Agreement';
        $arrmixData['strCardTitle']                  = 'Agreement';
        $arrmixData['view']                          = 'admin-portal/view-agreement';
        $arrmixData['arrmixBreadCrumbData']          = $arrmixBreadCrumbData;
        $arrmixData['arrmixCandidateEnquiryDetails'] = $arrmixCandidateEnquiryDetails;
        $arrmixData['arrmixQualificationsList']      = $arrmixQualificationsList;
        $arrmixData['arrmixKeySkillsList']           = $arrmixKeySkillsList;
        $arrmixData['arrmixStateList']               = $arrmixStateList;
        $arrmixData['arrmixCitiesList']              = $arrmixCitiesList;
        
        return $this->render( $arrmixData );
    }

    public function actionViewCandidateDetails() {
        
        $arrmixRequestData = $this->request->getPost();

        $arrmixResponseData = [];
        if( false == isset( $arrmixRequestData['candidate_id'] ) || false == \isValId( $arrmixRequestData['candidate_id'] ) ) {
            
            $arrmixResponseData['status']                 = false;
            $arrmixResponseData['view_candidate_details'] = '';

            $this->response( $arrmixResponseData );
            return true;
        }

        $arrmixCandidateDetails = CandidatesModel::createService()->findCandidateByCandidateId( $arrmixRequestData['candidate_id'] );

        $arrmixData['view']                   = 'ajax-view/view-candidate-details';
        $arrmixData['is_return_view_content'] = true;
        $arrmixData['arrmixCandidateDetails'] =  $arrmixCandidateDetails;

        $arrmixResponseData['status']                 = true;
        $arrmixResponseData['view_candidate_details'] = $this->renderPartial( $arrmixData );

        $this->response( $arrmixResponseData );
    } 

}
