<?php

namespace App\Controllers\Admin;

use App\Controllers\BaseController;
use App\Models\SettingModel;
use App\Models\UploadModel;
use App\Models\UserModel;
use App\Models\SharnarthiModel;

class Sharnarthi extends BaseController
{

    public function view_all_camp()
    {
        $setting = new SettingModel();
        $sharnarthi = new SharnarthiModel();
        $data['app_setting'] = $setting->get_application_setting();
        if(user()->user_role == 'Admin')
        {
            $data['all_camp'] = $sharnarthi->get_all_camp();
        }
        else
        {
            $data['all_camp'] = $sharnarthi->get_all_camp_by_added_by(user()->user_id);
        }
        
        echo view('admin/include/header', $data);
        echo view('admin/sharnarthi/view_all_camp');
        echo view('admin/include/footer');
    }

    public function add_new_camp()
    {
        $setting = new SettingModel();
        $data['app_setting'] = $setting->get_application_setting();
        echo view('admin/include/header', $data);
        echo view('admin/sharnarthi/add_new_camp');
        echo view('admin/include/footer');
    }

    public function add_camp()
    {
        $sharnarthi = new SharnarthiModel();
        $upload = new UploadModel();

        $data['camp_name']          = $this->request->getVar('camp_name');
        $data['camp_head_name']     = $this->request->getVar('camp_head_name');
        $data['camp_address']       = $this->request->getVar('camp_address');
        $data['camp_pin_code']      = $this->request->getVar('camp_pin_code');
        $data['camp_post_office']   = $this->request->getVar('camp_post_office');
        $data['camp_dist']          = $this->request->getVar('camp_dist');
        $data['camp_state']         = $this->request->getVar('camp_state');
        $data['camp_added_by']      = user()->user_id;
        $data['camp_created_at']    = date('Y-m-d');


        if($sharnarthi->add_camp($data))
        {
            session()->set('success', "Camp Successfully Added");
            return redirect()->to('admin/sharnarthi/view_all_camp');
        }
        else
        {
            session()->set('error', "Camp Added Failed");
            return redirect()->to('admin/sharnarthi/view_all_camp');
        }
    }

    public function edit_camp($id)
    {
        $sharnarthi = new SharnarthiModel();
        $setting = new SettingModel();
        $data['app_setting'] = $setting->get_application_setting();
        $data['camp'] = $sharnarthi->get_camp_by_id($id);
        echo view('admin/include/header', $data);
        echo view('admin/sharnarthi/edit_camp');
        echo view('admin/include/footer');
    }

    public function update_camp($id)
    {
        $sharnarthi = new SharnarthiModel();
        $upload = new UploadModel();

        $data['camp_name']          = $this->request->getVar('camp_name');
        $data['camp_head_name']     = $this->request->getVar('camp_head_name');
        $data['camp_address']       = $this->request->getVar('camp_address');
        $data['camp_pin_code']      = $this->request->getVar('camp_pin_code');
        $data['camp_post_office']   = $this->request->getVar('camp_post_office');
        $data['camp_dist']          = $this->request->getVar('camp_dist');
        $data['camp_state']         = $this->request->getVar('camp_state');


        if($sharnarthi->update_camp($data, $id))
        {
            session()->set('success', "Camp Successfully Updated");
            return redirect()->to('admin/sharnarthi/view_all_camp');
        }
        else
        {
            session()->set('error', "Camp Updated Failed");
            return redirect()->to('admin/sharnarthi/view_all_camp');
        }
    }

    public function delete_camp($id)
    {
        $sharnarthi = new SharnarthiModel();

        $image = $sharnarthi->get_camp_by_id($id);
        if (file_exists(substr($image->camp_image_1, 1))) {
            unlink(substr($image->camp_image_1, 1));
        }
        if (file_exists(substr($image->camp_image_2, 1))) {
            unlink(substr($image->camp_image_2, 1));
        }
        if (file_exists(substr($image->camp_image_2, 1))) {
            unlink(substr($image->camp_image_2, 1));
        }

        if($sharnarthi->delete_camp($id))
        {
            session()->set('success', "Camp Successfully Deleted");
            return redirect()->to('admin/sharnarthi/view_all_camp');
        }
        else
        {
            session()->set('error', "Camp Deleted Failed");
            return redirect()->to('admin/sharnarthi/view_all_camp');
        }
    }

    public function add_camp_review()
    {
        $sharnarthi = new SharnarthiModel();
        $upload = new UploadModel();

        $data['camp_rating']        = $this->request->getVar('camp_rating');
        $data['camp_review']        = $this->request->getVar('camp_review');

        if($this->request->getFile('camp_image_1')->isValid() && !$this->request->getFile('camp_image_1')->hasMoved())
        {
            $data['camp_image_1'] = $upload->upload_image($this->request->getFile('camp_image_1'));
        }

        if($this->request->getFile('camp_image_2')->isValid() && !$this->request->getFile('camp_image_2')->hasMoved())
        {
            $data['camp_image_2'] = $upload->upload_image($this->request->getFile('camp_image_2'));
        }

        if($this->request->getFile('camp_image_3')->isValid() && !$this->request->getFile('camp_image_3')->hasMoved())
        {
            $data['camp_image_3'] = $upload->upload_image($this->request->getFile('camp_image_3'));
        }

        if($sharnarthi->add_camp_review($data, $this->request->getVar('camp_id')))
        {
            session()->set('success', "Camp Review Successfully Added");
            return redirect()->to('admin/sharnarthi/view_all_camp');
        }
        else
        {
            session()->set('error', "Camp Review Added Failed");
            return redirect()->to('admin/sharnarthi/view_all_camp');
        }
    }

    public function check_camp_availability($id)
    {
        $sharnarthi = new SharnarthiModel();
        echo $sharnarthi->check_camp_avaliablity($id);
    }



    public function view_all_sharnarthi()
    {
        $setting = new SettingModel();
        $sharnarthi = new SharnarthiModel();
        $data['app_setting'] = $setting->get_application_setting();
        if(user()->user_role == 'Admin')
        {
            $data['all_sharnarthi'] = $sharnarthi->get_all_sharnarthi();
        }
        else
        {
            $data['all_sharnarthi'] = $sharnarthi->get_all_sharnarthi_by_added_by(user()->user_id);
        }
        
        echo view('admin/include/header', $data);
        echo view('admin/sharnarthi/view_all_sharnarthi');
        echo view('admin/include/footer');
    }

    public function add_new_sharnarthi()
    {
        $setting = new SettingModel();
        $sharnarthi = new SharnarthiModel();
        $data['app_setting'] = $setting->get_application_setting();
        $data['all_state'] = $sharnarthi->get_all_state();
        $data['all_camp'] = $sharnarthi->get_all_camp();
        echo view('admin/include/header', $data);
        echo view('admin/sharnarthi/add_new_sharnarthi');
        echo view('admin/include/footer');
    }

    public function add_sharnarthi()
    {
        // error_reporting(0);
        $sharnarthi = new SharnarthiModel();
        $upload = new UploadModel();

        $data['sharnarthi_name']                    = $this->request->getVar('sharnarthi_name');
        $data['sharnarthi_migrated_from']           = $this->request->getVar('sharnarthi_migrated_from');
        $data['sharnarthi_migrated_address']        = $this->request->getVar('sharnarthi_migrated_address');
        $data['sharnarthi_migrated_pin_code']       = $this->request->getVar('sharnarthi_migrated_pin_code');
        $data['sharnarthi_migrated_post_office']    = $this->request->getVar('sharnarthi_migrated_post_office');
        $data['sharnarthi_migrated_dist']           = $this->request->getVar('sharnarthi_migrated_dist');
        $data['sharnarthi_migrated_state']          = $this->request->getVar('sharnarthi_migrated_state');
        $data['sharnarthi_migrated_reason']         = $this->request->getVar('sharnarthi_migrated_reason');
        $data['sharnarthi_religion']                = $this->request->getVar('sharnarthi_religion');
        $data['sharnarthi_living_since']            = $this->request->getVar('sharnarthi_living_since');
        $data['sharnarthi_camp']                    = $this->request->getVar('sharnarthi_camp');
        $data['sharnarthi_earning_source']          = $this->request->getVar('sharnarthi_earning_source');
        $data['sharnarthi_income']                  = $this->request->getVar('sharnarthi_income');
        $data['sharnarthi_no_of_member_family']     = $this->request->getVar('sharnarthi_no_of_member_family');
        $data['sharnarthi_gender']                  = $this->request->getVar('sharnarthi_gender');
        $data['sharnarthi_age']                     = $this->request->getVar('sharnarthi_age');
        $data['sharnarthi_guardian_name']           = $this->request->getVar('sharnarthi_guardian_name');
        $data['is_other_member']                    = $this->request->getVar('is_other_member') == 1 ? 1 : 0;
        $data['is_bank_account']                    = $this->request->getVar('is_bank_account') == 1 ? 1 : 0;
        $data['sharnarthi_added_by']                = user()->user_id;
        $data['sharnarthi_created_at']              = date('Y-m-d');

        if($this->request->getVar('member_name'))
        {
            $members = [];

            // Retrieve member data from the form
            $memberNames = $this->request->getVar('member_name');
            $memberGenders = $this->request->getVar('member_gender');
            $memberAges = $this->request->getVar('member_age');
            $memberRelations = $this->request->getVar('member_relation');

            // Iterate through the member data and create an array of member objects
            for ($i = 0; $i < count($memberNames); $i++) {
                $member = [
                    'member_name'       => $memberNames[$i],
                    'member_gender'     => $memberGenders[$i],
                    'member_age'        => $memberAges[$i],
                    'member_relation'   => $memberRelations[$i]
                ];

                $members[] = $member;
            }

            // Convert the members array to JSON
            $data['other_member_details']               = json_encode($members);
        }

        if($this->request->getPost('children_name'))
        {
            $children = [];

            // Retrieve children data from the form
            $childrenNames = $this->request->getPost('children_name');
            $childrenGenders = $this->request->getPost('children_gender');
            $childrenAges = $this->request->getPost('children_age');
            $childrenIsStudying = $this->request->getPost('children_is_studying');
            $childrenIsWorking = $this->request->getPost('children_is_working') ;
            $schoolNames = $this->request->getPost('school_name');
            $highestEducations = $this->request->getPost('highest_education');
            $incomeAmounts = $this->request->getPost('income_amount');

            // Iterate through the children data and create an array of child objects
            for ($a = 0; $a < count($childrenNames); $a++) {
                $child = [
                    'name' => $childrenNames[$a],
                    'gender' => $childrenGenders[$a],
                    'age' => $childrenAges[$a],
                    'is_studying' => $childrenIsStudying[$a],
                    'is_working' => $childrenIsWorking[$a]
                ];

                if ($childrenIsStudying[$a] == '1') {
                    $child['school_name'] = $schoolNames[$a];
                } else {
                    $child['highest_education'] = $highestEducations[$a];
                }

                if ($childrenIsWorking[$a] == '1') {
                    $child['income_amount'] = $incomeAmounts[$a];
                }

                $children[] = $child;
            }

            // Convert the children array to JSON
            $data['chlidren_details']               = json_encode($children);
        }

        if($this->request->getFile('sharnarthi_identification_doc')->isValid() && !$this->request->getFile('sharnarthi_identification_doc')->hasMoved())
        {
            $data['sharnarthi_identification_doc'] = $upload->upload_image($this->request->getFile('sharnarthi_identification_doc'));
        }

        if($this->request->getFile('sharnarthi_immigration_doc')->isValid() && !$this->request->getFile('sharnarthi_immigration_doc')->hasMoved())
        {
            $data['sharnarthi_immigration_doc'] = $upload->upload_image($this->request->getFile('sharnarthi_immigration_doc'));
        }

        if($sharnarthi->add_sharnarthi($data))
        {
            session()->set('success', "Sharnarthi Successfully Added");
            return redirect()->to('admin/sharnarthi/view_all_sharnarthi');
        }
        else
        {
            session()->set('error', "Sharnarthi Added Failed");
            return redirect()->to('admin/sharnarthi/view_all_sharnarthi');
        }
    }

    public function delete_sharnarthi($id)
    {
        $sharnarthi = new SharnarthiModel();

        $image = $sharnarthi->get_sharnarthi_by_id($id);
        if(file_exists($image->sharnarthi_identification_doc))
        {
            unlink($image->sharnarthi_identification_doc);
        }
        if(file_exists($image->sharnarthi_immigration_doc))
        {
            unlink($image->sharnarthi_immigration_doc);
        }


        if($sharnarthi->delete_sharnarthi($id))
        {
            session()->set('success', "Sharnarthi Successfully Deleted");
            return redirect()->to('admin/sharnarthi/view_all_sharnarthi');
        }
        else
        {
            session()->set('error', "Sharnarthi Deleted Failed");
            return redirect()->to('admin/sharnarthi/view_all_sharnarthi');
        }
    }

    

  
    
}
