<?php

namespace App\Models;

use CodeIgniter\Model;

class SettingModel extends Model
{
    protected $setting = 'application_setting';
    protected $visual_setting = 'visual_setting';
    protected $users = 'users';
    protected $contact_message = 'contact_message';
    protected $home_banner = 'home_banner';
    protected $coupon_code = 'coupon_code';
    protected $db;


    function get_application_setting_with_visual_setign()
    {
        $info = $this->db->table($this->setting)
       ->select('*')
       ->join('visual_setting', 'visual_setting.id = application_setting.id')
       ->get()
       ->getResult();
       
        return $info[0];

    }

    function get_application_setting()
    {
        return $this->db->table($this->setting)
        ->where(['id'=> 1])
        ->get()
        ->getRow();
    }

    function active_delivery_system($data)
    {
        return $this->db->table($this->setting)
       ->where(['id'=> 1])
       ->update($data);
    }

    function update_application_setting($data)
    {
        return $this->db->table($this->setting)
       ->where(['id'=> 1])
       ->update($data);
    }

    function get_visual_setting()
    {
        return $this->db->table($this->visual_setting)
        ->where(['id'=> 1])
        ->get()
        ->getRow();
    }

    function update_visual_setting($data)
    {
        return $this->db->table($this->visual_setting)
       ->where(['id'=> 1])
       ->update($data);
    }

    function get_admin_profile_details($user_id)
    {
        return $this->db->table($this->users)
        ->where('user_id', $user_id)
        ->get()
        ->getRow();
    }

    function get_user_by_id($id)
    {
        return $this->db->table($this->users)
        ->where(['user_id'=> $id])
        ->get()
        ->getRow();
    }

    function update_profile_details($data, $id)
    {
       return $this->db->table($this->users)
       ->where(['user_id'=> $id])
       ->update($data);
    }


    function update_profile($data)
    {
        return $this->db->table($this->users)
        ->where(['user_id'=> user()->user_id])
        ->update($data);
    }

    function check_current_password($password)
    {
        $data = $this->db->table($this->users)
        ->where(['user_id'=> user()->user_id])
        ->get()
        ->getRow();

        if($data->user_password == hash("sha256", $password))
        {
            return TRUE;
        }
        else
        {
            return false;
        }
    }

    function update_profile_details_password($data, $id)
    {
        return $this->db->table($this->users)
        ->where(['user_id'=> $id])
        ->update($data);
    }

    function submit_contact_us_data($data)
    {
        return $this->db->table($this->contact_message)
        ->insert($data);
    }

    function get_all_coupon_code()
    {
        return $this->db->table($this->coupon_code)
        ->get()
        ->getResult();
    }

    function check_coupon_code_by_name($name)
    {
        return $this->db->table($this->coupon_code)
        ->where('coupon_code', $name)
        ->get()
        ->getRow();
    }

    function update_coupon_count($name, $data)
    {
        return $this->db->table($this->coupon_code)
        ->where('coupon_code', $name)
        ->update($data);
    }

    function add_coupon($data)
    {
        return $this->db->table($this->coupon_code)
        ->insert($data);
    }

    function delete_coupon($id)
    {
        return $this->db->table($this->coupon_code)
        ->where('coupon_id', $id)
        ->delete();
    }

    function get_coupon_by_id($id)
    {
        return $this->db->table($this->coupon_code)
        ->where('coupon_id', $id)
        ->get()
        ->getRow();
    }

    function update_coupon($data, $id)
    {
        return $this->db->table($this->coupon_code)
        ->where('coupon_id', $id)
        ->update($data);
    }

    function get_all_support_department()
    {
        return $this->db->table('role_permission')
        ->select('id, role_name')
        ->where('support_ticket', 1)
        ->where('role_name !=', 'Admin')
        ->get()
        ->getResult();
    }

    function add_new_ticket($data)
    {
        return $this->db->table('support_ticket')
        ->insert($data);
    }

    function add_new_support_ticket_comment($data)
    {
        return $this->db->table('support_ticket_comment')
        ->insert($data);
    }

    function get_all_support_ticket_by_user_id($user_id)
    {
        return $this->db->table('support_ticket')
        ->join('role_permission', 'role_permission.id = support_ticket.department')
        ->select('support_ticket.*, role_permission.role_name')
        ->where('support_ticket.user_id', $user_id)
        ->get()
        ->getResult();
    }

    function all_support_ticket_comment($ticket_id)
    {
        return $this->db->table('support_ticket_comment')
        ->where('ticket_id', $ticket_id)
        ->get()
        ->getResult();
    }

    function get_all_support_ticket()
    {
        return $this->db->table('support_ticket')
        ->join('users', 'users.user_id = support_ticket.user_id')
        ->join('role_permission', 'role_permission.id = support_ticket.department')
        ->select('support_ticket.*, role_permission.role_name, users.user_name')
        ->get()
        ->getResult();
    }

    function get_all_support_ticket_by_role_id_num_rows($staus)
    {
        return $this->db->table('support_ticket')
        ->select('status')
        ->where('status', $staus)
        ->get()
        ->getNumRows();
    }

    function change_support_status($data, $id)
    {
        return $this->db->table('support_ticket')
        ->where('id', $id)
        ->update($data);
    }

    function support_ticket_by_ticket_id($ticket_id)
    {
        return $this->db->table('support_ticket')
        ->where('rand_id', $ticket_id)
        ->get()
        ->getRow();
    }

    function get_all_packages()
    {
        return $this->db->table('package')
        ->get()
        ->getResult();
    }

    function add_package($data)
    {
        return $this->db->table('package')
        ->insert($data);
    }

    function get_package_by_id($id)
    {
        return $this->db->table('package')
        ->where('pack_id', $id)
        ->get()
        ->getRow();
    }

    function update_package($data, $id)
    {
        return $this->db->table('package')
        ->where('pack_id', $id)
        ->update($data);
    }

    function delete_package($id)
    {
        return $this->db->table('package')
        ->where('pack_id', $id)
        ->delete();
    }

    function get_partner_commission()
    {
        return $this->db->table("partner_comission_rate")
        ->orderBy('commission_id ', "DESC")
        ->get()
        ->getResult();
    }

    function get_last_commission()
    {
        return $this->db->table("partner_comission_rate")
        ->orderBy('commission_id ', "DESC")
        ->get()
        ->getRow();
    }

    function update_last_commission_details($data, $id)
    {
        return $this->db->table("partner_comission_rate")
        ->where('commission_id', $id)
        ->update($data);
    }

    function add_new_partner_commission($data)
    {
        return $this->db->table("partner_comission_rate")
        ->insert($data);
    }

    function add_new_donation_amount($data)
    {
        return $this->db->table("donation_payment")
        ->insert($data);
    }

    function get_all_donation_details_by_project_id($id)
    {
        return $this->db->table("donation_payment")
        ->where('donation_project_id', $id)
        ->selectSum('donation_amount')
        ->get()
        ->getResult();
    }

    function all_donation_report()
    {
        return $this->db->table("donation_payment")
        ->get()
        ->getResult();
    }

    public function approve_donation($data, $id)
    {
        return $this->db->table("donation_payment")
        ->where('donation_id', $id)
        ->update($data);
    }

    public function delete_donation($id)
    {
        return $this->db->table("donation_payment")
        ->where('donation_id', $id)
        ->delete();
    }

    function add_subscriber($data)
    {
        return $this->db->table("users")
        ->insert($data);
    }

    function add_subscriber_by_email_id($email)
    {
        return $this->db->table("users")
        ->where('user_email', $email)
        ->get()
        ->getRow();
    }

    function get_all_subscribers_and_volunteer()
    {
        return $this->db->table("users")
        ->select('user_email')
        ->where('user_role', 'Volunteer')
        ->orWhere('user_role', 'Subscriber')
        ->get()
        ->getResult();
    }

    function get_all_subscribers_for_email()
    {
        return $this->db->table("users")
        ->select('user_email')
        ->where('user_role', 'Subscriber')
        ->get()
        ->getResult();
    }

    function get_all_volunteer_for_email()
    {
        return $this->db->table("users")
        ->select('user_email')
        ->where('user_role', 'Volunteer')
        ->get()
        ->getResult();
    }

    /// Expanse Management Start

    public function get_all_expense($month, $year)
    {
        return $this->db->table("expense_management")
        ->where('MONTH(expense_date)', $month)
        ->where('YEAR(expense_date)', $year)
        ->where('expense_category', 'Debit')
        ->orderBy('DATE(expense_date)', "desc")
        ->get()
        ->getResult();
    }

    public function add_new_expense($data)
    {
        return $this->db->table("expense_management")
        ->insert($data);
    }

    public function get_all_income($month, $year)
    {
        return $this->db->table("expense_management")
        ->where('MONTH(expense_date)', $month)
        ->where('YEAR(expense_date)', $year)
        ->where('expense_category', 'Credit')
        ->orderBy('DATE(expense_date)', "desc")
        ->get()
        ->getResult();
    }

    public function get_all_expance_and_income_by_category($cat)
    {
        $month = [];
        for ($i=1; $i < 13; $i++) 
        { 
            $month[] = $this->db->table("expense_management")
            ->where('MONTH(expense_date)', $i)
            ->where('expense_category', $cat)
            ->where('YEAR(expense_date)', date('Y'))
            ->selectSum('expense_amount')
            ->get()
            ->getRow();

        }
        return $month;
    }

    
}
