<?php

namespace App\Models;

use CodeIgniter\Model;

class HomeModel extends Model
{
    protected $db;
    protected $home_slider                  = 'home_slider';
    protected $extra_page                   = 'extra_page';
    protected $home_testimonials            = 'home_testimonials';
    protected $home_banner                  = 'home_banner';

    function get_all_slider()
    {
        return $this->db->table($this->home_slider)->get()->getResult();
    }

    function new_slider_add($data)
    {
        return $this->db->table($this->home_slider)->insert($data);
    }

    function get_slider_by_id($id)
    {
        return $this->db->table($this->home_slider)
        ->where('id', $id)
        ->get()
        ->getRow();
    }

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

    function delete_slider($id)
    {
        return $this->db->table($this->home_slider)
        ->where('id', $id)
        ->delete();
    }

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

    function get_all_slider_frontend()
    {
        return $this->db->table($this->home_slider)
        ->where('status', 1)
        ->get()
        ->getResult();
    }

    function get_all_page()
    {
        return $this->db->table($this->extra_page)
        ->get()
        ->getResult();
    }

    function get_all_active_page()
    {
        return $this->db->table($this->extra_page)
        ->where('status', 1)
        ->get()
        ->getResult();
    }

    function get_all_page_footer_important()
    {
        return $this->db->table($this->extra_page)
        ->where('important', 'Important')
        ->where('status', 1)
        ->get()
        ->getResult();
    }

    function get_all_page_footer_help()
    {
        return $this->db->table($this->extra_page)
        ->where('important', 'Help')
        ->where('status', 1)
        ->get()
        ->getResult();
    }

    function get_all_page_header_top()
    {
        return $this->db->table($this->extra_page)
        ->where('important', 'Top')
        ->where('status', 1)
        ->get()
        ->getResult();
    }

    function delete_page($id)
    {
        return $this->db->table($this->extra_page)
        ->where('id', $id)
        ->delete();
    }

    function get_page_by_id($id)
    {
        return $this->db->table($this->extra_page)
        ->where('id', $id)
        ->get()
        ->getRow();
    }

    function get_page_by_slug($slug)
    {
        return $this->db->table($this->extra_page)
        ->where('slug', $slug)
        ->get()
        ->getRow();
    }

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

    function add_new_page($data)
    {
        return $this->db->table($this->extra_page)
        ->insert($data);
    }
   
    function get_all_banner()
    {
        return $this->db->table($this->home_banner)
        ->get()
        ->getResult();
    }

    function add_banner($data)
    {
        return $this->db->table($this->home_banner)
        ->insert($data);
    }

    function get_banner_by_position($value)
    {
        return $this->db->table($this->home_banner)
        ->where('banner_position', $value)
        ->get()
        ->getResult();
    }

    function get_banner_by_id($id)
    {
        return $this->db->table($this->home_banner)
        ->where('banner_id', $id)
        ->get()
        ->getRow();
    }

    function delete_banner($id)
    {
        return $this->db->table($this->home_banner)
        ->where('banner_id', $id)
        ->delete();
    }

    function update_banner($data, $id)
    {
        return $this->db->table($this->home_banner)
        ->where('banner_id', $id)
        ->update($data);
    }

    function get_prd_puchase_info_by_prd_id($prd_id, $user_id)
    {
        return $this->db->table('order')
        ->where('order_prd_id', $prd_id)
        ->where('dealer_id', $user_id)
        ->select('order_id')
        ->get()
        ->getResult();
    }

    function get_wishlist_prd_count_by_user_id($user_id)
    {
        return $this->db->table('wishlist')
        ->where('user_id', $user_id)
        ->get()
        ->getNumRows();
    }

    function get_wishlist_prd_by_user_id($user_id)
    {
        $info = $this->db->table('wishlist')
        ->join('product', 'product.prd_id = wishlist.product_id')
        ->where('wishlist.user_id', $user_id)
        ->where('product.prd_status', 1)
        ->select('wishlist.*, product.prd_name, product.prd_slug, product.prd_default_image, product.prd_id')
        ->get();
        $final = array();
        if ($info->getNumRows() > 0) 
        {
            foreach ($info->getResult() as $row) 
            {
                $info = $this->db->table('product_varient')
                ->where('prd_id', $row->prd_id)
                // ->where('prd_v_product_qnty >', 0)
                ->where('prd_v_default_varient', 1)
                ->get();
                $row->varient = $info->getResult()[0];
                array_push($final, $row);
            }
            
        }
        return  $final;
    }

    

}
