<?php

namespace App\Controllers\Admin;

use App\Controllers\BaseController;
use App\Models\SettingModel;
use App\Models\UploadModel;
use App\Models\PartnerModel;

class Setting extends BaseController
{
    
    public function app_setting()
    {
        $setting = new SettingModel();
        $data['app_setting'] = $setting->get_application_setting();
        echo view('admin/include/header', $data);
        echo view('admin/settings/application_setting');
        echo view('admin/include/footer');
    }

    function update_application_setting()
    {
        $setting = new SettingModel();
        $upload = new UploadModel();
         
        $data['application_name'] = $this->request->getVar('application_name');
        $data['meta_description'] = $this->request->getVar('meta_description');
        $data['meta_keywords'] = $this->request->getVar('meta_keywords');
        $data['copywrite'] = $this->request->getVar('copywrite');
        $data['address'] = $this->request->getVar('address');
        $data['mobile'] = $this->request->getVar('mobile');
        $data['email'] = $this->request->getVar('email');
        $data['apiKey'] = $this->request->getVar('apiKey');
        $data['authDomain'] = $this->request->getVar('authDomain');
        $data['projectId'] = $this->request->getVar('projectId');
        $data['storageBucket'] = $this->request->getVar('storageBucket');
        $data['messagingSenderId'] = $this->request->getVar('messagingSenderId');
        $data['appId'] = $this->request->getVar('appId');
        $data['measurementId'] = $this->request->getVar('measurementId');
        $data['captcha'] = $this->request->getVar('captcha');
        $data['captcha_site_key'] = $this->request->getVar('captcha_site_key');
        $data['secret_key'] = $this->request->getVar('secret_key');
        $data['cookies'] = $this->request->getVar('cookies');
        $data['head_code'] = $this->request->getVar('head_code');
        $data['footer_about_us'] = $this->request->getVar('footer_about_us');
        // $data['delivery_charge'] = $this->request->getVar('delivery_charge');
        // $data['flat_charge'] = $this->request->getVar('flat_charge');
        // $data['weight'] = $this->request->getVar('weight');
        // $data['charge'] = $this->request->getVar('charge');
        $data['company_pin_code'] = $this->request->getVar('company_pin_code');
        $data['facebook'] = $this->request->getVar('facebook');
        $data['instagram'] = $this->request->getVar('instagram');
        $data['whatsapp'] = $this->request->getVar('whatsapp');
        $data['twitter'] = $this->request->getVar('twitter');
        $data['linkdin'] = $this->request->getVar('linkdin');
        $data['map'] = $this->request->getVar('map');
        $data['rozar_key_id'] = $this->request->getVar('rozar_key_id');
        $data['rozar_key_secret'] = $this->request->getVar('rozar_key_secret');
        $data['protocol'] = $this->request->getVar('protocol');
        $data['host'] = $this->request->getVar('host');
        $data['username'] = $this->request->getVar('username');
        $data['password'] = $this->request->getVar('password');
        $data['port'] = $this->request->getVar('port');
        $data['crypto'] = $this->request->getVar('crypto');
        $data['invoice_trams'] = $this->request->getVar('invoice_trams');
        $data['invoice_format'] = $this->request->getVar('invoice_format');
        $data['gst_status'] = $this->request->getVar('gst_status');
        $data['gst_no'] = $this->request->getVar('gst_no');
        $data['and_app'] = $this->request->getVar('and_app');
        $data['ios_app'] = $this->request->getVar('ios_app');
        $data['win_app'] = $this->request->getVar('win_app');
        $data['credit_limit'] = $this->request->getVar('credit_limit');
        $data['pin_code_popup'] = $this->request->getVar('pin_code_popup');
        $data['bank_details'] = $this->request->getVar('bank_details');
        if($this->request->getFile('header_logo')->isValid() && !$this->request->getFile('header_logo')->hasMoved())
        {
            $header_logo = $setting->get_application_setting();
            if(file_exists(substr($header_logo->header_logo, 1)))
            {
                unlink(substr($header_logo->header_logo, 1));
            }
            $data['header_logo'] = $upload->upload_image($this->request->getFile('header_logo'));
        }
        if($this->request->getFile('footer_logo')->isValid() && !$this->request->getFile('footer_logo')->hasMoved())
        {
            $footer_logo = $setting->get_application_setting();
            if(file_exists(substr($footer_logo->footer_logo, 1)))
            {
                unlink(substr($footer_logo->footer_logo, 1));
            }
            $data['footer_logo'] = $upload->upload_image($this->request->getFile('footer_logo'));
        }
        if($this->request->getFile('admin_panel_logo')->isValid() && !$this->request->getFile('admin_panel_logo')->hasMoved())
        {
            $admin_panel_logo = $setting->get_application_setting();
            if(file_exists(substr($admin_panel_logo->admin_panel_logo, 1)))
            {
                unlink(substr($admin_panel_logo->admin_panel_logo, 1));
            }
            $data['admin_panel_logo'] = $upload->upload_image($this->request->getFile('admin_panel_logo'));
        }
        if($this->request->getFile('fevicon')->isValid() && !$this->request->getFile('fevicon')->hasMoved())
        {
            $fevicon = $setting->get_application_setting();
            if(file_exists(substr($fevicon->fevicon, 1)))
            {
                unlink(substr($fevicon->fevicon, 1));
            }
            $data['fevicon'] = $upload->upload_image($this->request->getFile('fevicon'));
        }
        if($setting->update_application_setting($data))
        {
            session()->set('success', "Application Setting Successfully Saved");
            return redirect()->route('admin/setting/application_setting');
        }
        else
        {
            session()->set('error', "Application Setting Saved Failed");
            return redirect()->route('admin/setting/application_setting');
        }
    }

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

    function color($color='')
    {
        $hex = str_replace('#','', $color);
        if(strlen($hex) == 3):
           $rgbArray['r'] = hexdec(substr($hex,0,1).substr($hex,0,1));
           $rgbArray['g'] = hexdec(substr($hex,1,1).substr($hex,1,1));
           $rgbArray['b'] = hexdec(substr($hex,2,1).substr($hex,2,1));
        else:
           $rgbArray['r'] = hexdec(substr($hex,0,2));
           $rgbArray['g'] = hexdec(substr($hex,2,2));
           $rgbArray['b'] = hexdec(substr($hex,4,2));
        endif;

        return $rgbArray['r'].','.$rgbArray['g'].','.$rgbArray['b'];
    }

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

    function active_delivery_system()
    {
        $setting = new SettingModel();
        $data['own_delivery'] =  $this->request->getVar('status');
        echo $setting->active_delivery_system($data);
    }

    function active_national_delivery_system()
    {
        $setting = new SettingModel();
        $data['national_delivery'] =  $this->request->getVar('status');
        echo $setting->active_delivery_system($data);
    }

    function active_state_delivery_system()
    {
        $setting = new SettingModel();
        $data['state_delivery'] =  $this->request->getVar('status');
        echo $setting->active_delivery_system($data);
    }

    function active_dist_delivery_system()
    {
        $setting = new SettingModel();
        $data['dist_delivery'] =  $this->request->getVar('status');
        echo $setting->active_delivery_system($data);
    }

    function active_local_delivery_system()
    {
        $setting = new SettingModel();
        $data['local_delivery'] =  $this->request->getVar('status');
        echo $setting->active_delivery_system($data);
    }

    function local_delivery_details()
    {
        $setting = new SettingModel();
        $data['local_delivery_details'] =  json_encode($this->request->getVar('local_delivery_details'));
        if($setting->active_delivery_system($data))
        {
            session()->set('success', "Local Delivery Details Successfully Updated");
            return redirect()->route('admin/setting/delivery_setting');
        }
        else
        {
            session()->set('error', "Local Delivery Details Update Failed");
            return redirect()->route('admin/setting/delivery_setting');
        }
    }


    function dist_delivery_details()
    {
        $setting = new SettingModel();
        $data['dist_delivery_details'] =  json_encode($this->request->getVar('dist_delivery_details'));
        if($setting->active_delivery_system($data))
        {
            session()->set('success', "Dist Delivery Details Successfully Updated");
            return redirect()->route('admin/setting/delivery_setting');
        }
        else
        {
            session()->set('error', "Dist Delivery Details Update Failed");
            return redirect()->route('admin/setting/delivery_setting');
        }
    }

    function state_delivery_details()
    {
        $setting = new SettingModel();
        $data['state_delivery_details'] =  json_encode($this->request->getVar('state_delivery_details'));
        if($setting->active_delivery_system($data))
        {
            session()->set('success', "State Delivery Details Successfully Updated");
            return redirect()->route('admin/setting/delivery_setting');
        }
        else
        {
            session()->set('error', "State Delivery Details Update Failed");
            return redirect()->route('admin/setting/delivery_setting');
        }
    }

    function national_delivery_details()
    {
        $setting = new SettingModel();
        $data['national_delivery_details'] =  json_encode($this->request->getVar('national_delivery_details'));
        if($setting->active_delivery_system($data))
        {
            session()->set('success', "State Delivery Details Successfully Updated");
            return redirect()->route('admin/setting/delivery_setting');
        }
        else
        {
            session()->set('error', "State Delivery Details Update Failed");
            return redirect()->route('admin/setting/delivery_setting');
        }
    }

    function update_visual_setting()
    {
        $setting = new SettingModel();
        
        $data['color_1'] = $this->request->getVar('color_1');
        $data['color_2'] = $this->request->getVar('color_2');
        $data['color_1_rgb'] = $this->color($this->request->getVar('color_1'));
        $data['color_2_rgb'] = $this->color($this->request->getVar('color_2'));
        if($setting->update_visual_setting($data))
        {
            session()->set('success', "Visual Setting Successfully Saved");
            return redirect()->route('admin/setting/visual_setting');
        }
        else
        {
            session()->set('error', "Visual Setting Saved Failed");
            return redirect()->route('admin/setting/visual_setting');
        }
    }

    public function update_profile()
    {
        $setting = new SettingModel();
        $data['app_setting'] = $setting->get_application_setting();
        $data['user'] = $setting->get_user_by_id(user()->user_id);
        echo view('admin/include/header', $data);
        echo view('admin/profile/update_profile_details');
        echo view('admin/include/footer');
    }

    function update_profile_details()
    {
        $setting = new SettingModel();
        $upload = new UploadModel();
        if($this->request->getFile('image')->isValid() && !$this->request->getFile('image')->hasMoved())
        {
            $image = $setting->get_admin_profile_details(user()->user_id);
            if(file_exists(substr($image->user_image, 1)))
            {
                unlink(substr($image->user_image, 1));
            }
            $data['user_image'] = $upload->upload_image($this->request->getFile('image'));
        }
        $data['user_name'] = $this->request->getVar('user_name');
        $data['user_mobile'] = $this->request->getVar('user_mobile');

        if($setting->update_profile_details($data, user()->user_id))
        {
            session()->set('success', "Profile Successfully Saved");
            return redirect()->route('admin/update_profile');
        }
        else
        {
            session()->set('error', "Profile Saved Failed");
            return redirect()->route('admin/update_profile');
        }
    }

    public function check_current_password($password)
    {
        $setting = new SettingModel();
        echo $setting->check_current_password($password);
    }

    public function update_password()
    {
        $setting = new SettingModel();
        $data['app_setting'] = $setting->get_application_setting();
        $data['user'] = $setting->get_admin_profile_details(user()->user_id);
        echo view('admin/include/header', $data);
        echo view('admin/profile/update_profile_password');
        echo view('admin/include/footer');
    }

    function update_profile_details_password()
    {
        $setting = new SettingModel();
        $data['user_password'] = hash("sha256", $this->request->getVar('password'));
        if($setting->update_profile_details_password($data, user()->user_id))
        {
            session()->set('success', "Password Successfully Saved");
           return redirect()->route('admin/update_password');
        }
        else
        {
            session()->set('error', "Password Saved Failed");
          return redirect()->route('admin/update_password');
        }
    }

    function all_state_dist_taluka()
    {
        $setting = new SettingModel();
        $data['app_setting'] = $setting->get_application_setting();
        if(user()->user_role == 'Admin')
        {
            $data['all_state'] = $setting->all_state();
            $data['all_dist'] = $setting->all_dist_with_state();
            $data['all_taluka'] = $setting->get_all_taluka_with_dist_and_state();
        }
        elseif(user()->user_role == 'State Head')
        {
            $data['all_dist'] = $setting->all_dist_by_state_id(user()->user_work_state);
        }
        elseif(user()->user_role == 'Dist Head')
        {
            $data['all_taluka'] = $setting->get_all_taluka_by_dist_id_and_state(user()->user_work_dist);
        }
        echo view('admin/include/header', $data);
        echo view('admin/settings/all_state_dist_taluka');
        echo view('admin/include/footer');
    }

    function add_new_state()
    {
        $setting = new SettingModel();
        $data['state_title'] = $this->request->getVar('state_title');

        if($setting->add_new_state($data))
        {
            session()->set('success', "State Successfully Saved");
            return redirect()->route('admin/setting/all_state_dist_taluka');
        }
        else
        {
            session()->set('error', "State Saved Failed");
            return redirect()->route('admin/setting/all_state_dist_taluka');
        }
    }

    function update_state($id)
    {
        $setting = new SettingModel();
        $data['app_setting'] = $setting->get_application_setting();
        $data['state'] = $setting->get_state_by_id($id);
        echo view('admin/include/header', $data);
        echo view('admin/settings/update_state');
        echo view('admin/include/footer');
    }

    function edit_state($id)
    {
        $setting = new SettingModel();
        $data['state_title'] = $this->request->getVar('state_title');
        if($setting->edit_state($data, $id))
        {
            session()->set('success', "State Successfully Updated");
            return redirect()->route('admin/setting/all_state_dist_taluka');
        }
        else
        {
            session()->set('error', "State Updated Failed");
            return redirect()->route('admin/setting/all_state_dist_taluka');
        }
    }

    function delete_state($id)
    {
        $setting = new SettingModel();
        if($setting->delete_state($id))
        {
            session()->set('success', "State Successfully Deleted");
            return redirect()->route('admin/setting/all_state_dist_taluka');
        }
        else
        {
            session()->set('error', "State Deleted Failed");
            return redirect()->route('admin/setting/all_state_dist_taluka');
        }
    }

    function add_new_dist()
    {
        $setting = new SettingModel();
        $data['district_title'] = $this->request->getVar('district_title');
        $data['state_id'] = $this->request->getVar('state_id');
        $data['district_status'] = 'Active';
        if(user()->user_role != 'Admin')
        {
            $data['district_description'] = user()->user_id;
        }
        if($setting->add_new_dist($data))
        {
            session()->set('success', "District Successfully Saved");
            return redirect()->route('admin/setting/all_state_dist_taluka');
        }
        else
        {
            session()->set('error', "District Saved Failed");
            return redirect()->route('admin/setting/all_state_dist_taluka');
        }
    }

    function delete_dist($id)
    {
        $setting = new SettingModel();
        if($setting->delete_dist($id))
        {
            session()->set('success', "District Successfully Deleted");
            return redirect()->route('admin/setting/all_state_dist_taluka');
        }
        else
        {
            session()->set('error', "District Deleted Failed");
            return redirect()->route('admin/setting/all_state_dist_taluka');
        }
    }

    function update_dist($id)
    {
        $setting = new SettingModel();
        $data['app_setting'] = $setting->get_application_setting();
        $data['dist'] = $setting->get_dist_by_id($id);
        $data['all_state'] = $setting->all_state();
        echo view('admin/include/header', $data);
        echo view('admin/settings/update_dist');
        echo view('admin/include/footer');
    }

    function edit_dist($id)
    {
        $setting = new SettingModel();
        $data['district_title'] = $this->request->getVar('district_title');
        $data['state_id'] = $this->request->getVar('state_id');
        $data['district_status'] = 'Active';
        if($setting->edit_dist($data, $id))
        {
            session()->set('success', "District Successfully Updated");
            return redirect()->route('admin/setting/all_state_dist_taluka');
        }
        else
        {
            session()->set('error', "District Saved Failed");
            return redirect()->route('admin/setting/all_state_dist_taluka');
        }
    }

    function add_new_taluka()
    {
        $setting = new SettingModel();
        $data['taluka_name'] = $this->request->getVar('taluka_name');
        $data['taluka_dist'] = $this->request->getVar('taluka_dist');
        $data['taluka_state'] = $this->request->getVar('taluka_state');
        if(user()->user_role != 'Admin')
        {
            $data['added_by'] = user()->user_id;
        }
        if($setting->add_new_taluka($data))
        {
            session()->set('success', "Taluka Successfully Saved");
            return redirect()->route('admin/setting/all_state_dist_taluka');
        }
        else
        {
            session()->set('error', "Taluka Saved Failed");
            return redirect()->route('admin/setting/all_state_dist_taluka');
        }
    }

    function get_dist_for_add_taluka($id)
    {
        $setting = new SettingModel();
        $data = $setting->get_dist_for_add_taluka($id);

        $option = '';

        foreach ($data as $value) {
             $option .= '<option value='.$value->districtid.'>'.$value->district_title.'</option>';
        }
        echo $option;
    }


    function update_taluka($id)
    {
        $setting = new SettingModel();
        $data['app_setting'] = $setting->get_application_setting();
        $data['all_state'] = $setting->all_state();
        $data['taluka'] = $setting->get_taluka_by_id($id);
        echo view('admin/include/header', $data);
        echo view('admin/settings/update_taluka');
        echo view('admin/include/footer');
    }

    function get_dist_for_add_taluka_for_edit($state, $dist)
    {
        $setting = new SettingModel();
        $data = $setting->get_dist_for_add_taluka($state);

        $option = '';

        foreach ($data as $value) {
             $option .= '<option value="'.$value->districtid.'" '.(($value->districtid==$dist)?'selected="selected"':"").'>'.$value->district_title.'</option>';
        }
        echo $option;
    }

    function edit_taluka($id)
    {
        $setting = new SettingModel();
        $data['taluka_name'] = $this->request->getVar('taluka_name');
        $data['taluka_dist'] = $this->request->getVar('taluka_dist');
        $data['taluka_state'] = $this->request->getVar('taluka_state');
        if($setting->edit_taluka($data, $id))
        {
            session()->set('success', "Taluka Successfully Updated");
            return redirect()->route('admin/setting/all_state_dist_taluka');
        }
        else
        {
            session()->set('error', "Taluka Updated Failed");
            return redirect()->route('admin/setting/all_state_dist_taluka');
        }
    }

    function delete_taluka($id)
    {
        $setting = new SettingModel();
        if($setting->delete_taluka($id))
        {
            session()->set('success', "Taluka Successfully Deleted");
            return redirect()->route('admin/setting/all_state_dist_taluka');
        }
        else
        {
            session()->set('error', "Taluka Deleted Failed");
            return redirect()->route('admin/setting/all_state_dist_taluka');
        }
    }

    function get_dist_with_leader_name_by_state_id($state_id,  $option_d = null)
    {
        $partner = new PartnerModel();
        $user_and_dist_data = $partner->get_dist_with_leader_name_by_state_id($state_id);
        $option = '';
        $option = '<option value="">Select Now</option>';
        foreach ($user_and_dist_data as $value) {
             $option .= '<option '.(($value->districtid == $option_d)?'selected="selected"':"").' value='.$value->districtid.'>'.$value->district_title.'</option>';
        }
        echo $option;
    }

    function get_dist_for_add_partner($user_id, $option_d = null)
    {
        $setting = new SettingModel();
        $partner = new PartnerModel();
        $user_data = $partner->get_partner_by_id_for_state_id($user_id);
        $data = $setting->get_dist_for_add_taluka($user_data->user_work_state);

        $option = '';
        $option = '<option value="">Select Now</option>';
        foreach ($data as $value) {
             $option .= '<option '.(($value->districtid.'|'.$value->state_id == $option_d)?'selected="selected"':"").' value='.$value->districtid.'|'.$value->state_id.'>'.$value->district_title.'</option>';
        }
        echo $option;
    }

    function get_taluka_by_dist_id($dist_id, $option_d = null)
    {
        $setting = new SettingModel();
        $data = $setting->get_taluka_by_dist_id($dist_id);

        $option = '';
        if($data)
        {
            $option = '<option value="">Select Now</option>';
            foreach ($data as $value) {
                 $option .= '<option '.(($value->taluka_id==$option_d)?'selected="selected"':"").' value='.$value->taluka_id.'>'.$value->taluka_name.'</option>';
            }
        }
        else
        {
            $option = '<option value="">No Taluka Found. Create New Taluka</option>';
        }
        
        echo $option;
    }

    function all_coupon_code()
    {
        $setting = new SettingModel();
        $data['app_setting'] = $setting->get_application_setting();
        $data['all_coupon'] = $setting->get_all_coupon_code();
        echo view('admin/include/header', $data);
        echo view('admin/offer_setting/all_coupon_code');
        echo view('admin/include/footer');
    }

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

    function add_coupon()
    {
        $setting = new SettingModel();
        $data['coupon_code'] = $this->request->getVar('coupon_code');
        $data['coupon_discount'] = $this->request->getVar('coupon_discount');
        $data['coupon_start_date'] = $this->request->getVar('coupon_start_date');
        $data['coupon_end_date'] = $this->request->getVar('coupon_end_date');
        $data['coupon_count'] = $this->request->getVar('coupon_count');
        $data['coupon_status'] = 1;
        if($setting->add_coupon($data))
        {
            session()->set('success', "Coupon Successfully Added");
            return redirect()->route('admin/offer_setting/all_coupon_code');
        }
        else
        {
            session()->set('error', "Coupon Added Failed");
            return redirect()->route('admin/offer_setting/all_coupon_code');
        }
    }

    function delete_coupon($id)
    {
        $setting = new SettingModel();
        if($setting->delete_coupon($id))
        {
            session()->set('success', "Coupon Successfully Deleted");
            return redirect()->route('admin/offer_setting/all_coupon_code');
        }
        else
        {
            session()->set('error', "Coupon Deleted Failed");
            return redirect()->route('admin/offer_setting/all_coupon_code');
        }
    }

    function edit_coupon($id)
    {
        $setting = new SettingModel();
        $data['app_setting'] = $setting->get_application_setting();
        $data['coupon'] = $setting->get_coupon_by_id($id);
        echo view('admin/include/header', $data);
        echo view('admin/offer_setting/edit_coupon');
        echo view('admin/include/footer');
    }

    function update_coupon($id)
    {
        $setting = new SettingModel();
        $data['coupon_code'] = $this->request->getVar('coupon_code');
        $data['coupon_discount'] = $this->request->getVar('coupon_discount');
        $data['coupon_start_date'] = $this->request->getVar('coupon_start_date');
        $data['coupon_end_date'] = $this->request->getVar('coupon_end_date');
        $data['coupon_count'] = $this->request->getVar('coupon_count');
        if($setting->update_coupon($data, $id))
        {
            session()->set('success', "Coupon Successfully Updated");
            return redirect()->route('admin/offer_setting/all_coupon_code');
        }
        else
        {
            session()->set('error', "Coupon Updated Failed");
            return redirect()->route('admin/offer_setting/all_coupon_code');
        }
    }

    function all_tickets()
    {
        $setting = new SettingModel();
        $data['app_setting'] = $setting->get_application_setting();
        $data['all_support_ticket'] = $setting->get_all_support_ticket();
        echo view('admin/include/header', $data);
        echo view('admin/ticket/support_ticket');
        echo view('admin/include/footer');
    }

    function change_support_status()
    {
        $setting = new SettingModel();
        $data['status'] = $this->request->getVar('val');
        echo $setting->change_support_status($data, $this->request->getVar('id'));
    }

    function view_ticket_details($ticket)
    {
        $setting = new SettingModel();
        $data['app_setting'] = $setting->get_application_setting();
        $data['all_support_ticket_comment'] = $setting->all_support_ticket_comment($ticket);
        $data['support_ticket']     = $setting->support_ticket_by_ticket_id($ticket);
        echo view('admin/include/header', $data);
        echo view('admin/ticket/view_details');
        echo view('admin/include/footer');
    }

    function view_all_package()
    {
        $setting = new SettingModel();
        $data['app_setting']        = $setting->get_application_setting();
        $data['all_packages']       = $setting->get_all_packages();
        echo view('admin/include/header', $data);
        echo view('admin/package/view_all_package');
        echo view('admin/include/footer');
    }

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

    function add_package()
    {
        $setting = new SettingModel();
        $upload = new UploadModel();
        $data['pack_name'] = $this->request->getVar('pack_name');
        $data['pack_price'] = $this->request->getVar('pack_price');
        $data['pack_wallet_bal'] = $this->request->getVar('pack_wallet_bal');
         if($this->request->getFile('pack_image')->isValid() && !$this->request->getFile('pack_image')->hasMoved())
        {
            $data['pack_image'] = $upload->upload_image($this->request->getFile('pack_image'));
        }
        if($setting->add_package($data))
        {
            session()->set('success', "Package Successfully Added");
            return redirect()->route('admin/package_details/view_all_package');
        }
        else
        {
            session()->set('error', "Package Added Failed");
            return redirect()->route('admin/package_details/view_all_package');
        }
    }

    function edit_package($id)
    {
        $setting = new SettingModel();
        $data['app_setting']        = $setting->get_application_setting();
        $data['pack']       = $setting->get_package_by_id($id);
        echo view('admin/include/header', $data);
        echo view('admin/package/edit_package');
        echo view('admin/include/footer');
    }

    function update_package($id)
    {
        $setting = new SettingModel();
        $upload = new UploadModel();
        $data['pack_name'] = $this->request->getVar('pack_name');
        $data['pack_price'] = $this->request->getVar('pack_price');
        $data['pack_wallet_bal'] = $this->request->getVar('pack_wallet_bal');
        if($this->request->getFile('pack_image')->isValid() && !$this->request->getFile('pack_image')->hasMoved())
        {
            $pack_image = $setting->get_package_by_id($id);
            if(file_exists(substr($pack_image->pack_image, 1)))
            {
                unlink(substr($pack_image->pack_image, 1));
            }
            $data['pack_image'] = $upload->upload_image($this->request->getFile('pack_image'));
        }
        if($setting->update_package($data, $id))
        {
            session()->set('success', "Package Successfully Updated");
            return redirect()->route('admin/package_details/view_all_package');
        }
        else
        {
            session()->set('error', "Package Updated Failed");
            return redirect()->route('admin/package_details/view_all_package');
        }
    }
    
    function delete_package($id)
    {
        $setting = new SettingModel();
        $pack_image = $setting->get_package_by_id($id);
        if(file_exists(substr($pack_image->pack_image, 1)))
        {
            unlink(substr($pack_image->pack_image, 1));
        }
        if($setting->delete_package($id))
        {
            session()->set('success', "Package Successfully Deleted");
            return redirect()->route('admin/package_details/view_all_package');
        }
        else
        {
            session()->set('error', "Package Deleted Failed");
            return redirect()->route('admin/package_details/view_all_package');
        }
    }

    function level_commission()
    {
        $setting = new SettingModel();
        $data['app_setting'] = $setting->get_application_setting();
        $data['all_commission'] = $setting->get_partner_commission();
        echo view('admin/include/header', $data);
        echo view('admin/offer_setting/partner_commission');
        echo view('admin/include/footer'); 
    }

    function add_new_partner_commission()
    {
        $setting = new SettingModel();
        $partner_details = $setting->get_last_commission();
        if($partner_details)
        {
            $arr['commission_end_date'] = date('Y-m-d');
            $arr['commission_status'] = 0;
            $setting->update_last_commission_details($arr, $partner_details->commission_id );
        }
        $data['commission_se'] = $this->request->getVar('commission_se');
        $data['commission_mp'] = $this->request->getVar('commission_mp');
        $data['commission_mla'] = $this->request->getVar('commission_mla');
        $data['commission_coun'] = $this->request->getVar('commission_coun');
        $data['commission_hur'] = $this->request->getVar('commission_hur');
        $data['commission_sarthi'] = $this->request->getVar('commission_sarthi');
        $data['commission_ss'] = $this->request->getVar('commission_ss');
        $data['commission_drc'] = $this->request->getVar('commission_drc');
        $data['commission_start_date'] = date('Y-m-d');
        $data['commission_status'] = 1;
        if($setting->add_new_partner_commission($data))
        {
            session()->set('success', "Commission Successfully Added");
            return redirect()->route('admin/offer_setting/level_commission');
        }
        else
        {
            session()->set('error', "Commission Added Failed");
            return redirect()->route('admin/offer_setting/level_commission');
        }
    }
    
}
