<?php

namespace App\Controllers\Admin;

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

class Venue extends BaseController
{

    public function view_all_booking()
    {
        $setting = new SettingModel();
        $venue = new VenueModel();
        $data['app_setting'] = $setting->get_application_setting();
        if(user()->user_role == 'Admin')
        {
            $data['all_venue'] = $venue->get_all_booking();
        }
        else
        {
            $data['all_venue'] = $venue->get_all_booking_by_added_by(user()->user_id);
        }
        
        echo view('admin/include/header', $data);
        echo view('admin/venue/view_all_booking');
        echo view('admin/include/footer');
    }

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

    public function add_booking()
    {
        $venue = new VenueModel();
        $upload = new UploadModel();
        $setting = new SettingModel();
        $data['venue_wedding_name'] = $this->request->getVar('venue_wedding_name');
        $data['venue_wedding_is_bride_or_groom'] = $this->request->getVar('venue_wedding_is_bride_or_groom');
        $data['venue_wedding_age'] = $this->request->getVar('venue_wedding_age');
        $data['venue_wedding_date'] = $this->request->getVar('venue_wedding_date');
        $data['venue_wedding_guardian_name'] = $this->request->getVar('venue_wedding_guardian_name');
        $data['venue_wedding_annual_income'] = $this->request->getVar('venue_wedding_annual_income');
        $data['venue_wedding_address'] = $this->request->getVar('venue_wedding_address');
        $data['venue_wedding_pin_code'] = $this->request->getVar('venue_wedding_pin_code');
        $data['venue_wedding_post_office'] = $this->request->getVar('venue_wedding_post_office');
        $data['venue_wedding_dist'] = $this->request->getVar('venue_wedding_dist');
        $data['venue_wedding_state'] = $this->request->getVar('venue_wedding_state');

        $data['venue_wedding_who_booking_name'] = $this->request->getVar('venue_wedding_who_booking_name');
        $data['venue_wedding_price_status'] = $this->request->getVar('venue_wedding_price_status') == 1 ? 1 : 0;;
        $data['venue_wedding_price'] = $this->request->getVar('venue_wedding_price');
        $data['venue_added_by'] = user()->user_id;
        $data['venue_wedding_created_at'] = date('Y-m-d');

        if($this->request->getFile('venue_wedding_image')->isValid() && !$this->request->getFile('venue_wedding_image')->hasMoved())
        {
            $data['venue_wedding_image'] = $upload->upload_image($this->request->getFile('venue_wedding_image'));
        }
        if($this->request->getFile('venue_wedding_card_image')->isValid() && !$this->request->getFile('venue_wedding_card_image')->hasMoved())
        {
            $data['venue_wedding_card_image'] = $upload->upload_image($this->request->getFile('venue_wedding_card_image'));
        }
        if($this->request->getFile('venue_wedding_application_form')->isValid() && !$this->request->getFile('venue_wedding_application_form')->hasMoved())
        {
            $data['venue_wedding_application_form'] = $upload->upload_image($this->request->getFile('venue_wedding_application_form'));
        }
        
        if($venue->add_booking($data))
        {
            if($this->request->getVar('venue_wedding_price_status') == 1)
            {
                $price = $this->request->getVar('venue_wedding_price');
                
                $in_data['expense_amount']         = $this->request->getVar('venue_wedding_price');
                $in_data['expense_date']           = date('Y-m-d');
                $in_data['expense_note']           = 'Rs.'.$price.' Income From Venue Booking';
                $in_data['expense_category']       = 'Credit';
                $in_data['expense_added_by']       = user()->user_id;
                $setting->add_new_expense($in_data);
            }
            session()->set('success', "Booking Successfully Complete");
            return redirect()->to('admin/venue/view_all_booking');
        }
        else
        {
            session()->set('error', "Booking Failed");
            return redirect()->to('admin/venue/view_all_booking');
        }
    }

    public function edit_venue($id)
    {
        $setting = new SettingModel();
        $venue = new VenueModel();
        $data['app_setting'] = $setting->get_application_setting();
        $data['vanue'] = $venue->get_venue_by_id($id);
        echo view('admin/include/header', $data);
        echo view('admin/venue/edit_venue');
        echo view('admin/include/footer');
    }

    public function update_booking($id)
    {
        $venue = new VenueModel();
        $upload = new UploadModel();

        $data['venue_wedding_name'] = $this->request->getVar('venue_wedding_name');
        $data['venue_wedding_is_bride_or_groom'] = $this->request->getVar('venue_wedding_is_bride_or_groom');
        $data['venue_wedding_age'] = $this->request->getVar('venue_wedding_age');
        $data['venue_wedding_date'] = $this->request->getVar('venue_wedding_date');
        $data['venue_wedding_guardian_name'] = $this->request->getVar('venue_wedding_guardian_name');
        $data['venue_wedding_annual_income'] = $this->request->getVar('venue_wedding_annual_income');
        $data['venue_wedding_address'] = $this->request->getVar('venue_wedding_address');
        $data['venue_wedding_pin_code'] = $this->request->getVar('venue_wedding_pin_code');
        $data['venue_wedding_post_office'] = $this->request->getVar('venue_wedding_post_office');
        $data['venue_wedding_dist'] = $this->request->getVar('venue_wedding_dist');
        $data['venue_wedding_state'] = $this->request->getVar('venue_wedding_state');

        $data['venue_wedding_who_booking_name'] = $this->request->getVar('venue_wedding_who_booking_name');
        $data['venue_wedding_price_status'] = $this->request->getVar('venue_wedding_price_status') == 1 ? 1 : 0;;
        $data['venue_wedding_price'] = $this->request->getVar('venue_wedding_price');


        if($this->request->getFile('venue_wedding_image')->isValid() && !$this->request->getFile('venue_wedding_image')->hasMoved())
        {
            $image_1 = $venue->get_venue_by_id($id);
            if(file_exists(substr($image_1->venue_wedding_image, 1)))
            {
                unlink(substr($image_1->venue_wedding_image, 1));
            }
            $data['venue_wedding_image'] = $upload->upload_image($this->request->getFile('venue_wedding_image'));
        }
        if($this->request->getFile('venue_wedding_card_image')->isValid() && !$this->request->getFile('venue_wedding_card_image')->hasMoved())
        {
            $image_2 = $venue->get_venue_by_id($id);
            if(file_exists(substr($image_2->venue_wedding_card_image, 1)))
            {
                unlink(substr($image_2->venue_wedding_card_image, 1));
            }
            $data['venue_wedding_card_image'] = $upload->upload_image($this->request->getFile('venue_wedding_card_image'));
        }
        if($this->request->getFile('venue_wedding_application_form')->isValid() && !$this->request->getFile('venue_wedding_application_form')->hasMoved())
        {
            $image_3 = $venue->get_venue_by_id($id);
            if(file_exists(substr($image_3->venue_wedding_application_form, 1)))
            {
                unlink(substr($image_3->venue_wedding_application_form, 1));
            }
            $data['venue_wedding_application_form'] = $upload->upload_image($this->request->getFile('venue_wedding_application_form'));
        }
        
        if($venue->update_booking($data, $id))
        {
            session()->set('success', "Booking Successfully Updated");
            return redirect()->to('admin/venue/view_all_booking');
        }
        else
        {
            session()->set('error', "Booking Updated Failed");
            return redirect()->to('admin/venue/view_all_booking');
        }
    }

    public function delete_venue($id)
    {
        $venue = new VenueModel();

        $image = $venue->get_venue_by_id($id);
        if(file_exists(substr($image->venue_wedding_image, 1)))
        {
            unlink(substr($image->venue_wedding_image, 1));
        }
        if(file_exists(substr($image->venue_wedding_card_image, 1)))
        {
            unlink(substr($image->venue_wedding_card_image, 1));
        }
        if(file_exists(substr($image->venue_wedding_application_form, 1)))
        {
            unlink(substr($image->venue_wedding_application_form, 1));
        }

        if($venue->delete_venue($id))
        {
            session()->set('success', "Booking Successfully Delete");
            return redirect()->back();
        }
        else
        {
            session()->set('error', "Booking Deleted Failed");
            return redirect()->back();
        }
    }

    
}
