myLogger = \Config\Services::mylogger(); $this->insurerModel = new ClientModel(); $this->userModel = new UserModel(); // $this->insurerBranchModel = new ClientBranchModel(); $this->insurerKYCDocsModel = new ClientKYCDocsModel(); $this->insurerPolicyModel = new ClientPolicyModel(); $this->insurerRMModel = new ClientRMModel(); $this->insurerBranchModel = new InsurerBranchModel(); $this->insurerModel = new InsurerModel(); $this->kycDocsModel = new KYCDocsModel(); $this->kycEntityTypeModel = new KYCEntityTypeModel(); $this->levelContactModel = new LevelContactModel(); $this->policesModel = new PolicesModel(); $this->tpaBranchModel = new TPABranchModel(); $this->tpaModel = new TPAModel(); $this->stateModel = new StateModel(); $this->policyTypeModel = new PolicyTypeModel(); } public function insurerList() { $this->myLogger->logme('error','Insurer list function called'); $headerData['page_name'] = 'Insurer List'; $data['insurerList'] = $this->insurerModel->where('is_active',1)->findAll(); $data['insurer_rm'] = $this->insurerRMModel->getAllClientRM(); // echo '
';
        // print_r($data['insurerList']); die;

        echo view('layout/header', $headerData);
        echo view('insurer_list', $data);
        echo view('layout/footer');

        // $this->loadLayout('insurer_onboarding', $data);
    }

    public function insurerRemove($id = null)
    {   
        $this->myLogger->logme('error','Insurer Remove function called');
        // $id                 = $this->request->getPost('PrimaryKey');
        // $data               = $this->request->getPost();
        $data['updated_by'] = get_session_userid();
        $data['is_active'] = 0;
        $update             = $this->insurerModel->update($id,$data);
        if($update){
            return $this->respond(['status' => true,'code' => 200], 200);
        }else{
            return $this->respond(['status' => false,'code' => 404], 200);
        }
    }


    public function removeInsurerBranch($id = null)
    {   
        $this->myLogger->logme('error','Insurer Branch Remove function called');
        // $id                 = $this->request->getPost('PrimaryKey');
        // $data               = $this->request->getPost();
        $data['updated_by'] = get_session_userid();
        $data['is_active'] = 0;
        $update             = $this->insurerBranchModel->update($id,$data);
        if($update){
            return $this->respond(['status' => true,'code' => 200], 200);
        }else{
            return $this->respond(['status' => false,'code' => 404], 200);
        }
    }




    public function editInsurerOnboarding($id = null)
    {

        $this->myLogger->logme('error','Edit Insurer Onboarding function called');
        $headerData['page_name'] = 'Edit Insurer Master';

        $types = array(
            (object) array('id' => 'pvt', 'name' => 'PVT'),
            (object) array('id' => 'psu', 'name' => 'PSU')
        );
    
        $editData['types'] = $types;
        $editData['RM']            = $this->userModel->findAll();
        $editData['tpa']           = $this->tpaBranchModel ->getTpaBranchesWithTpaNames();
        $editData['state']         = $this->stateModel->getAllStates();
        $editData['insurer']       = $this->insurerBranchModel ->getInsurerBranchesWithInsurerNames();


        $editData['insurer']           = $this->insurerModel->where(['id' => $id, 'is_active' => 1])->first();
        $editData['insuer_branch']    = $this->insurerBranchModel->where(['insurer_id' => $id, 'is_active' => 1])->findAll();


        // echo "
";
        // print_r($editData); die;

        echo view('layout/header', $headerData);
        echo view('insurer_onboarding', $editData);
        echo view('layout/footer');

    }


    public function insurerOnboarding()
    {
        
        $this->myLogger->logme('error','Insurer Onboarding function called');
        $headerData['page_name'] = 'Insurer Master';

        $types = array(
            (object) array('id' => 'pvt', 'name' => 'PVT'),
            (object) array('id' => 'psu', 'name' => 'PSU')
        );
    
        $data['types'] = $types;
        // print_r($data['types']);die();
        $data['entity']        = $this->kycEntityTypeModel->findAll();
        $data['kyc_docs']      = $this->kycDocsModel->findAll();
        $data['police']        = $this->policesModel->findAll();
        $data['insurer']       = $this->insurerBranchModel ->getInsurerBranchesWithInsurerNames();
        $data['tpa']           = $this->tpaBranchModel ->getTpaBranchesWithTpaNames();
        $data['state']         = $this->stateModel->getAllStates();
        $data['RM']            = $this->userModel->findAll();
                
        // echo "
";
        // print_r($data); die;

        echo view('layout/header', $headerData);
        echo view('insurer_onboarding', $data);
        echo view('layout/footer');

    } 


    public function createInsurerGeneralInfo()
    {

        $this->myLogger->logme('error','Insurer general info function called');
        $data   = $this->request->getPost();
        $data['created_by'] = get_session_userid();
        
        $insert = $this->insurerModel->insert($data);
        if($insert){
            $insurer_data = $this->insurerModel->where(['id' => $insert, 'is_active' => 1])->first();
            echo json_encode(array("status" => true , 'data' => $insurer_data));
        }else{
            echo json_encode(array("status" => false));
        }
    }

    public function createInsurerBranch()
    {

        $this->myLogger->logme('error','Client branch CREATE function called');
        $data   = $this->request->getPost();
        $data['created_by'] = get_session_userid();
        $insert = $this->insurerBranchModel->insert($data);
        
        if($insert){
            for ($i = 0; $i < count($this->request->getPost('name')); $i++) {
                // Prepare data to insert
                $data = [
                    'contact_type' => 'insurer',
                    'ref_id'       => $insert,
                    'created_by'   => get_session_userid(),
                    'name'         => $this->request->getPost('name')[$i],
                    'email'        => $this->request->getPost('email')[$i],
                    'mobile'       => $this->request->getPost('mobile')[$i],
                    'designation'  => $this->request->getPost('designation')[$i]
                ];
                $contacts = $this->levelContactModel->insert($data);
            }
        }

        if($insert){
            $branchData = $this->insurerBranchModel->where('insurer_id', $this->request->getPost('insurer_id'))->findAll();
            echo json_encode(array("status" => true , 'data' => $branchData));
        }else{
            echo json_encode(array("status" => false));
        }
    }
    

    public function insurerBranchList($id = null)
    {
        $this->myLogger->logme('error','Edit Insurer Onboarding function called');

        $editData['insuer_branch'] = $this->insurerBranchModel->where(['insurer_id' => $id, 'is_active' => 1])->findAll();

        // Return JSON response
        header('Content-Type: application/json');
        echo json_encode($editData);
        exit();
    }


    

    public function editInsurerGeneralInfo()
    {
        $this->myLogger->logme('error','edit Insurer general info function called');
        $id                 = $this->request->getPost('PrimaryKey');
        $data               = $this->request->getPost();
        $data['updated_by'] = get_session_userid();
        $update             = $this->insurerModel->update($id,$data);
        if($update){
            echo json_encode(array("status" => true , 'data' => $data));
        }else{
            echo json_encode(array("status" => false));
        }
    }


    public function editInsurerGet($id = null)
    {
        $this->myLogger->logme('error','Edit TPA Onboarding function called');

        
        $editData['insurerbranch'] = $this->insurerBranchModel->where(['id' => $id, 'is_active' => 1])->first();
        $editData['levelcontact'] = $this->levelContactModel->where(['ref_id' => $id, 'contact_type' => 'insurer' , 'is_active' => 1])->findAll();
        // print_r($editData['tpa']);
        // Return JSON response
        header('Content-Type: application/json');
        echo json_encode($editData);
        exit();
    }

    public function editInsurerBranch()
    {
        $this->myLogger->logme('error','Insurer branch CREATE function called');
        $id     = $this->request->getPost('PrimaryKey');
        $data   = $this->request->getPost();
        $update = $this->insurerBranchModel->update($id, $data);

        if($update){
            $contactsToDelete = $this->levelContactModel->where(['ref_id' => $id,'contact_type' => 'insurer', 'is_active' => 1])->get()->getResult();
            foreach ($contactsToDelete as $contact) {
                $this->levelContactModel->delete($contact->id);
            }
            for ($i = 0; $i < count($this->request->getPost('name')); $i++) {
                // Prepare data to update
                $data = [
                    'contact_type' => 'insurer',
                    'ref_id'       => $id,
                    'created_by'   => get_session_userid(),
                    'name'         => $this->request->getPost('name')[$i],
                    'email'        => $this->request->getPost('email')[$i],
                    'mobile'       => $this->request->getPost('mobile')[$i],
                    'designation'  => $this->request->getPost('designation')[$i]
                ];
                $contacts = $this->levelContactModel->insert($data);
            }
        }

        if($update){
            $branchData = $this->insurerBranchModel->where('insurer_id', $this->request->getPost('insurer_id'))->findAll();
            echo json_encode(array("status" => true , 'data' => $branchData, 'edit'));
        }else{
            echo json_encode(array("status" => false, 'edit'));
        }
    }



// Insurer Ends




// TPA Start


    public function tpaList()
    {   
        $this->myLogger->logme('error','TPA list function called');
        $headerData['page_name'] = 'TPA List';
        $data['tpaList'] = $this->tpaModel->where('is_active',1)->findAll();
        // $data['insurer_rm'] = $this->insurerRMModel->getAllClientRM();
        // echo '
';
        // print_r($data['insurerList']); die;

        echo view('layout/header', $headerData);
        echo view('tpa_list', $data);
        echo view('layout/footer');

        // $this->loadLayout('insurer_onboarding', $data);
    }
    

    public function tpaRemove($id = null)
    {   
        $this->myLogger->logme('error','TPA Remove function called');
        $data               = $this->request->getPost();
        $data['updated_by'] = get_session_userid();
        $data['is_active'] = 0;
        $update             = $this->tpaModel->update($id,$data);
        if($update){
            return $this->respond(['status' => true,'code' => 200], 200);
        }else{
            return $this->respond(['status' => false,'code' => 404], 200);
        }
    }


    
    public function removeTPABranch($id = null)
    {   
        $this->myLogger->logme('error','TPA Branch Remove function called');
        // $id                 = $this->request->getPost('PrimaryKey');
        // $data               = $this->request->getPost();
        $data['updated_by'] = get_session_userid();
        $data['is_active'] = 0;
        $update             = $this->tpaBranchModel->update($id,$data);
        if($update){
            return $this->respond(['status' => true,'code' => 200], 200);
        }else{
            return $this->respond(['status' => false,'code' => 404], 200);
        }
    }

    public function tpaOnboarding()
    {
        
        $this->myLogger->logme('error','TPA Onboarding function called');
        $headerData['page_name'] = 'TPA Master';


        // print_r($data['types']);die();
        $data['entity']        = $this->kycEntityTypeModel->findAll();
        $data['kyc_docs']      = $this->kycDocsModel->findAll();
        $data['police']        = $this->policesModel->findAll();
        $data['insurer']       = $this->insurerBranchModel ->getInsurerBranchesWithInsurerNames();
        $data['tpa']           = $this->tpaBranchModel ->getTpaBranchesWithTpaNames();
        $data['state']         = $this->stateModel->getAllStates();
        $data['RM']            = $this->userModel->findAll();
                
        // echo "
";
        // print_r($data); die;

        echo view('layout/header', $headerData);
        echo view('tpa_onboarding', $data);
        echo view('layout/footer');

    } 

    

    public function createTPAGeneralInfo()
    {

        $this->myLogger->logme('error','TPA general info function called');
        $data   = $this->request->getPost();
        $data['created_by'] = get_session_userid();
        
        $insert = $this->tpaModel->insert($data);
        if($insert){
            $tpa_data = $this->tpaModel->where(['id' => $insert, 'is_active' => 1])->first();
            echo json_encode(array("status" => true , 'data' => $tpa_data));
        }else{
            echo json_encode(array("status" => false));
        }
    }







    public function createTPABranch()
    {

        $this->myLogger->logme('error','TPA branch CREATE function called');
        $data   = $this->request->getPost();
        $data['created_by'] = get_session_userid();
        $insert = $this->tpaBranchModel->insert($data);
        
        if($insert){
            for ($i = 0; $i < count($this->request->getPost('name')); $i++) {
                // Prepare data to insert
                $data = [
                    'contact_type' => 'tpa',
                    'ref_id'       => $insert,
                    'created_by'   => get_session_userid(),
                    'name'         => $this->request->getPost('name')[$i],
                    'email'        => $this->request->getPost('email')[$i],
                    'mobile'       => $this->request->getPost('mobile')[$i],
                    'designation'  => $this->request->getPost('designation')[$i]
                ];
                $contacts = $this->levelContactModel->insert($data);
            }
        }

        if($insert){
            $branchData = $this->tpaBranchModel->where('tpa_id', $this->request->getPost('tpa_id'))->findAll();
            echo json_encode(array("status" => true , 'data' => $branchData));
        }else{
            echo json_encode(array("status" => false));
        }
    }



    public function editTPAOnboarding($id = null)
    {

        $this->myLogger->logme('error','Edit TPA Onboarding function called');
        $headerData['page_name'] = 'Edit TPA Master';

        $editData['tpa']           = $this->tpaModel->where(['id' => $id, 'is_active' => 1])->first();
        $editData['tpa_branch']    = $this->tpaBranchModel->where(['tpa_id' => $id, 'is_active' => 1])->findAll();
        $editData['state']         = $this->stateModel->getAllStates();


        // echo "
";
        // print_r($editData); die;

        echo view('layout/header', $headerData);
        echo view('tpa_onboarding', $editData);
        echo view('layout/footer');

    }


    

    public function editTPAGeneralInfo()
    {
        $this->myLogger->logme('error','edit TPA general info function called');
        $id                 = $this->request->getPost('PrimaryKey');
        $data               = $this->request->getPost();
        $data['updated_by'] = get_session_userid();
        $update             = $this->tpaModel->update($id,$data);
        if($update){
            echo json_encode(array("status" => true , 'data' => $data));
        }else{
            echo json_encode(array("status" => false));
        }
    }

    
    public function editTPAGet($id = null)
    {
        $this->myLogger->logme('error','Edit TPA Onboarding function called');

        
        $editData['tpabranch'] = $this->tpaBranchModel->where(['id' => $id, 'is_active' => 1])->first();
        $editData['levelcontact'] = $this->levelContactModel->where(['ref_id' => $id, 'contact_type' => 'tpa' , 'is_active' => 1])->findAll();
        // print_r($editData['tpa']);
        // Return JSON response
        header('Content-Type: application/json');
        echo json_encode($editData);
        exit();
    }



    

    public function editTPABranch()
    {
        $this->myLogger->logme('error','TPA branch CREATE function called');
        $id     = $this->request->getPost('PrimaryKey');
        $data   = $this->request->getPost();
        $update = $this->tpaBranchModel->update($id, $data);

        // print_r($this->request->getPost('name[]'));
        // print_r($this->request->getPost('email[]'));
        // print_r($this->request->getPost('mobile[]'));
        // print_r($this->request->getPost('designation[]'));

        if($update){
            $contactsToDelete = $this->levelContactModel->where(['ref_id' => $id,'contact_type' => 'tpa', 'is_active' => 1])->get()->getResult();
            foreach ($contactsToDelete as $contact) {
                $this->levelContactModel->delete($contact->id);
            }
            for ($i = 0; $i < count($this->request->getPost('name')); $i++) {
                // Prepare data to update
                $data = [
                    'contact_type' => 'tpa',
                    'ref_id'       => $id,
                    'created_by'   => get_session_userid(),
                    'name'         => $this->request->getPost('name')[$i],
                    'email'        => $this->request->getPost('email')[$i],
                    'mobile'       => $this->request->getPost('mobile')[$i],
                    'designation'  => $this->request->getPost('designation')[$i]
                ];
                $contacts = $this->levelContactModel->insert($data);
            }
        }

        if($update){
            $branchData = $this->tpaBranchModel->where('tpa_id', $this->request->getPost('tpa_id'))->findAll();
            echo json_encode(array("status" => true , 'data' => $branchData));
        }else{
            echo json_encode(array("status" => false));
        }
    }



    
    public function tpaBranchList($id = null)
    {
        $this->myLogger->logme('error','TPA List function called');

        $editData['tpa_branch'] = $this->tpaBranchModel->where(['tpa_id' => $id, 'is_active' => 1])->findAll();

        // Return JSON response
        header('Content-Type: application/json');
        echo json_encode($editData);
        exit();
    }




    //Ends TPA Models
    
    //Start KYC 


    public function kycList()
    {   
        $this->myLogger->logme('error','KYC list function called');
        $headerData['page_name'] = 'KYC List';
        $data['kycList'] = $this->kycEntityTypeModel->where('is_active',1)->findAll();
        // $data['insurer_rm'] = $this->insurerRMModel->getAllClientRM();
        // echo '
';
        // print_r($data['insurerList']); die;

        echo view('layout/header', $headerData);
        echo view('kyc_list', $data);
        echo view('layout/footer');

        // $this->loadLayout('insurer_onboarding', $data);
    }

    public function kycRemove($id = null)
    {   
        $this->myLogger->logme('error','KYC Remove function called');
        // $data               = $this->request->getPost();
        $data['updated_by'] = get_session_userid();
        $data['is_active'] = 0;
        $update             = $this->kycEntityTypeModel->update($id,$data);
        if($update){
            return $this->respond(['status' => true,'code' => 200], 200);
        }else{
            return $this->respond(['status' => false,'code' => 404], 200);
        }
    }


    public function removeKYCDocs($id = null)
    {   
        $this->myLogger->logme('error','KYC Docs Remove function called');
        // $id                 = $this->request->getPost('PrimaryKey');
        // $data               = $this->request->getPost();
        $data['updated_by'] = get_session_userid();
        $data['is_active'] = 0;
        $update             = $this->kycDocsModel->update($id,$data);
        if($update){
            return $this->respond(['status' => true,'code' => 200], 200);
        }else{
            return $this->respond(['status' => false,'code' => 404], 200);
        }
    }

    public function kycOnboarding()
    {
        
        $this->myLogger->logme('error','KYC Onboarding function called');
        $headerData['page_name'] = 'KYC Master';


        // print_r($data['types']);die();
        $data['entity']        = $this->kycEntityTypeModel->findAll();
        $data['kyc_docs']      = $this->kycDocsModel->findAll();
        $data['police']        = $this->policesModel->findAll();
        $data['insurer']       = $this->insurerBranchModel ->getInsurerBranchesWithInsurerNames();
        $data['tpa']           = $this->tpaBranchModel ->getTpaBranchesWithTpaNames();
        $data['state']         = $this->stateModel->getAllStates();
        $data['RM']            = $this->userModel->findAll();
                
        // echo "
";
        // print_r($data); die;

        echo view('layout/header', $headerData);
        echo view('kyc_onboarding', $data);
        echo view('layout/footer');

    }


    

    public function createKYCInfo()
    {

        $this->myLogger->logme('error','KYC Entity Type general info function called');
        $data   = $this->request->getPost();
        $data['created_by'] = get_session_userid();
        
        $insert = $this->kycEntityTypeModel->insert($data);
        if($insert){
            $kyc_data = $this->kycEntityTypeModel->where(['id' => $insert, 'is_active' => 1])->first();
            echo json_encode(array("status" => true , 'data' => $kyc_data));
        }else{
            echo json_encode(array("status" => false));
        }
    }


    

    public function createKycDocs()
    {

        $this->myLogger->logme('error','Kyc Docs CREATE function called');
        $data   = $this->request->getPost();
        // print_r($data);die;

        if($data['kyc_type_id'] == ''){
            // PrimaryKey
            $data['kyc_type_id'] =$data['PrimaryKey'];
        }
        // print_r($data);die;

        $data['created_by'] = get_session_userid();
        $insert = $this->kycDocsModel->insert($data);

        if($insert){
            $kycDocsData = $this->kycDocsModel->where('kyc_type_id', $data['kyc_type_id'])->where('is_active',1)->findAll();
            echo json_encode(array("status" => true , 'data' => $kycDocsData));
        }else{
            echo json_encode(array("status" => false));
        }
    }

    
    public function editKYCOnboarding($id = null)
    {

        $this->myLogger->logme('error','Edit KYC Onboarding function called');
        $headerData['page_name'] = 'Edit KYC Master';

        $editData['kyc']           = $this->kycEntityTypeModel->where(['id' => $id, 'is_active' => 1])->first();
        $editData['kyc_docs']    = $this->kycDocsModel->where(['kyc_type_id' => $id, 'is_active' => 1])->findAll();


        // echo "
";
        // print_r($editData); die;

        echo view('layout/header', $headerData);
        echo view('kyc_onboarding', $editData);
        echo view('layout/footer');

    }


    

    public function kycDocsList($id = null)
    {

        $this->myLogger->logme('error','Edit KYC Docs Onboarding function called');


        $editData['kyc_docs'] = $this->kycDocsModel->where(['kyc_type_id' => $id, 'is_active' => 1])->findAll();

        // Return JSON response
        header('Content-Type: application/json');
        echo json_encode($editData);
        exit();
    }


    
    public function editKYCInfo()
    {
        $this->myLogger->logme('error','edit KYC general info function called');
        $id                 = $this->request->getPost('PrimaryKey');
        $data               = $this->request->getPost();
        $data['updated_by'] = get_session_userid();
        $update             = $this->kycEntityTypeModel->update($id,$data);
        if($update){
            echo json_encode(array("status" => true , 'data' => $data));
        }else{
            echo json_encode(array("status" => false));
        }
    }

    
    public function editKYCGet($id = null)
    {
        $this->myLogger->logme('error','Edit KYC Onboarding function called');

        $editData['policies'] = $this->kycDocsModel->where(['id' => $id, 'is_active' => 1])->first();

        // Return JSON response
        header('Content-Type: application/json');
        echo json_encode($editData);
        exit();
    }


    
    public function editKYCDocs()
    {
        $this->myLogger->logme('error','edit KYC Docs function called');
        $id                 = $this->request->getPost('PrimaryKey');
        $data['file_name']  = $this->request->getPost('file_name');
        $data['updated_by'] = get_session_userid();
        $update             = $this->kycDocsModel->update($id,$data);
        if($update){
            echo json_encode(array("status" => true , 'data' => $data));
        }else{
            echo json_encode(array("status" => false));
        }
    }





    //Ends KYC 

    //Start Policy  


    public function policyTypeList()
    {   
        $this->myLogger->logme('error','Policy Type list function called');
        $headerData['page_name'] = 'Policy Type List';
        $data['policyList'] = $this->policyTypeModel->where('is_active',1)->findAll();
        // $data['insurer_rm'] = $this->insurerRMModel->getAllClientRM();
        // echo '
';
        // print_r($data['insurerList']); die;

        echo view('layout/header', $headerData);
        echo view('policy_type_list', $data);
        echo view('layout/footer');

        // $this->loadLayout('insurer_onboarding', $data);
    }


    public function policyTypeRemove($id = null)
    {   
        $this->myLogger->logme('error','Policy Type Remove function called');
        // $data               = $this->request->getPost();
        $data['updated_by'] = get_session_userid();
        $data['is_active'] = 0;
        $update             = $this->policyTypeModel->update($id,$data);
        if($update){
            return $this->respond(['status' => true,'code' => 200], 200);
        }else{
            return $this->respond(['status' => false,'code' => 404], 200);
        }
    }
    
    public function removePolicyPolicies($id = null)
    {   
        $this->myLogger->logme('error','Policies Remove function called');
        // $id                 = $this->request->getPost('PrimaryKey');
        // $data               = $this->request->getPost();
        $data['updated_by'] = get_session_userid();
        $data['is_active'] = 0;
        $update             = $this->policesModel->update($id,$data);
        if($update){
            return $this->respond(['status' => true,'code' => 200], 200);
        }else{
            return $this->respond(['status' => false,'code' => 404], 200);
        }
    }

    public function policyTypeOnboarding()
    {
        
        $this->myLogger->logme('error','Policy Type Onboarding function called');
        $headerData['page_name'] = 'Policy Type Master';


        // print_r($data['types']);die();
        $data['entity']        = $this->kycEntityTypeModel->findAll();
        $data['kyc_docs']      = $this->kycDocsModel->findAll();
        $data['police']        = $this->policesModel->findAll();
        // $data['insurer']       = $this->insurerBranchModel ->getInsurerBranchesWithInsurerNames();
        $data['tpa']           = $this->tpaBranchModel ->getTpaBranchesWithTpaNames();
        $data['state']         = $this->stateModel->getAllStates();
        $data['RM']            = $this->userModel->findAll();
        $data['policy_type']   = $this->policyTypeModel->findAll();
        $data['insurer']       = $this->insurerModel ->findAll();
        // echo "
";

        echo view('layout/header', $headerData);
        echo view('policy_type_onboarding', $data);
        echo view('layout/footer');

    }



    
    public function createPolicies()
    {
        $this->myLogger->logme('error','Policies CREATE function called');
        $data   = $this->request->getPost();
        // print_r($data);
        // die();
        $data['created_by'] = get_session_userid();
        $insert = $this->policesModel->insert($data);
        if($insert){
            $policiesData = $this->policesModel->select('policies.*, insurers.name as insurer_name')
                ->join('insurers', 'insurers.id = policies.insurer_id')
                ->where('policies.policy_type_id', $this->request->getPost('policy_type_id'))
                ->where('policies.is_active', 1)
                ->findAll();
            // $policiesData = $this->policesModel->where('policy_type_id', $this->request->getPost('policy_type_id'))->findAll();
            echo json_encode(array("status" => true , 'data' => $policiesData));
        }else{
            echo json_encode(array("status" => false));
        }
    }



    
    public function createPolicyType()
    {
        $this->myLogger->logme('error','Policy Type CREATE function called');
        $data   = $this->request->getPost();

        $data['created_by'] = get_session_userid();
        $insert = $this->policyTypeModel->insert($data);

        if($insert){
            $policyTypeData = $this->policyTypeModel->where('id', $insert)->first();
            echo json_encode(array("status" => true , 'data' => $policyTypeData));
        }else{
            echo json_encode(array("status" => false));
        }
    }

    
    public function editPolicyTypeOnboarding($id = null)
    {

        $this->myLogger->logme('error','Edit KYC Onboarding function called');
        $headerData['page_name'] = 'Edit Policy Type';

        $editData['policytype']   = $this->policyTypeModel->where(['id' => $id, 'is_active' => 1])->first();
        $editData['policies']     = $this->policesModel->select('policies.*, insurers.name as insurer_name')
                                                        ->join('insurers', 'insurers.id = policies.insurer_id')
                                                        ->where('policies.policy_type_id', $id)
                                                        ->where('policies.is_active', 1)
                                                        ->findAll();
        // $editData['policies']     = $this->policesModel->where(['policy_type_id' => $id, 'is_active' => 1])->findAll();
        $editData['insurer']      = $this->insurerModel-> findAll();
        echo view('layout/header', $headerData);
        echo view('policy_type_onboarding', $editData);
        echo view('layout/footer');

    }


    public function editPolicyType()
    {
        $this->myLogger->logme('error','edit Policy general info function called');
        $id                 = $this->request->getPost('PrimaryKey');
        $data               = $this->request->getPost();
        $data['updated_by'] = get_session_userid();
        $update             = $this->policyTypeModel->update($id,$data);
        if($update){
            echo json_encode(array("status" => true , 'data' => $data));
        }else{
            echo json_encode(array("status" => false));
        }
    }


    public function policesList($id = null)
    {
        $this->myLogger->logme('error','Edit Policies Onboarding function called');

        // $editData['policies'] = $this->policesModel->where(['policy_type_id' => $id, 'is_active' => 1])->findAll();

        $policies = $this->policesModel->where(['policy_type_id' => $id, 'is_active' => 1])->findAll();

        // print_r($id);die;
            foreach ($policies as $policy) {
                // Retrieve the insurer information
                $insurer = $this->insurerModel->find($policy['insurer_id']);

                // print_r($insurer);die;
                // Add the insurer information to the editData array
                if ($insurer) {
                    $editData['policies'][$policy['id']] = [
                        'policy' => $policy,
                        'insurer' => $insurer
                    ];
                }
            }

        
        // Return JSON response
        // header('Content-Type: application/json');
        // echo json_encode($editData);
        exit();
    }


    
    public function editPoliciesGet($id = null)
    {
        $this->myLogger->logme('error','Edit Policies Onboarding function called');
        $editData['policies'] = $this->policesModel->where(['id' => $id, 'is_active' => 1])->first();
        // Return JSON response
        header('Content-Type: application/json');
        echo json_encode($editData);
        exit();
    }


    public function editPolicies()
    {
        $this->myLogger->logme('error','edit Policy general info function called');
        $id                 = $this->request->getPost('PrimaryKey');
        $data               = $this->request->getPost();
        $data['updated_by'] = get_session_userid();
        $update             = $this->policesModel->update($id,$data);
        if($update){
            $policyData = $this->policesModel->where(['id' => $id, 'is_active' => 1])->findAll();
            echo json_encode(array("status" => true , 'data' => $policyData));
        }else{
            echo json_encode(array("status" => false));
        }
    }

}