<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Product extends CI_Controller
{
    // Session Authoriesed
    public function __construct()
    {
        parent::__construct();

        $admin = $this
            ->session
            ->userdata('staff');
        $column['id'] = $admin['id'];
        $table = 'tbl_user';

        // loading common model
        $this
            ->load
            ->model('Common_model');
        $result = $this
            ->Common_model
            ->Result($column, $table);
        if ($result['password'] == $admin['password'])
        {
            $login = true;
        }
        else
        {
            $login = false;
        }

        if (empty($admin) || $login == false)
        {
            $this
                ->session
                ->set_flashdata('msg', 'Your Session Has Been Expired');
            redirect(base_url() . 'staff/login');
        }

        // loading common model
        $this
            ->load
            ->model('Common_model');
        $this
            ->load
            ->helper('common_helper');
    }

    public function index()
    {
        // we are setting data for get site data
        $column['id'] = '1';
        $table = 'tbl_settings';

        // we are getting results from common modal
        $result = $this
            ->Common_model
            ->Result($column, $table);

        // we are setting result in data array as $data
        $data['data'] = $result;
        $data['page'] = 'Product';

        // 		getting product data
        $table0 = 'tbl_product';
        $table2 = 'tbl_top_category';
        $table3 = 'tbl_mid_category';
        $table4 = 'tbl_end_category';

        // we are getting product results from common modal
        $result0 = $this
            ->Common_model
            ->Results_product($table0, $table2, $table3, $table4);

        $table5 = 'tbl_size';
        $table7 = 'tbl_product_size';

        // we are getting product_size results from common modal
        $result7 = $this
            ->Common_model
            ->Results_product_size($table5, $table7);

        $table6 = 'tbl_color';
        // we are getting results from common modal
        $result6 = $this
            ->Common_model
            ->Results($table6);

        $data['product'] = $result0;
        $data['color'] = $result6;
        $data['product_size'] = $result7;

        // we are loading admin dashboard
        $this
            ->load
            ->view('staff/product/list', $data);
    }

    public function edit($id) {
        // we are setting data for get site data
        $column['id'] = '1';
        $table = 'tbl_settings';
        $data['page'] = 'Product Edit';

        // we are getting results from common modal
        $result0 = $this
            ->Common_model
            ->Result($column, $table);

        // 		getting product data
        $table0 = 'tbl_product';
        $table2 = 'tbl_top_category';
        $table3 = 'tbl_mid_category';
        $table4 = 'tbl_end_category';

        // we are getting product results from common modal
        $result01 = $this
            ->Common_model
            ->Results_product_id($table0, $table2, $table3, $table4, $id);

        $table2 = 'tbl_top_category';
        // we are getting results from common modal
        $result2 = $this
            ->Common_model
            ->Results($table2);

        $table3 = 'tbl_mid_category';
        $column3['tcat_id'] = $result01['tcat_id'];
        // we are getting results from common modal
        $result3 = $this
            ->Common_model
            ->ResultsWhere2($column3, $table3);

        $table4 = 'tbl_end_category';
        $column4['mcat_id'] = $result01['mcat_id'];
        // we are getting results from common modal
        $result4 = $this
            ->Common_model
            ->ResultsWhere2($column4, $table4);

        $table5 = 'tbl_size';
        // we are getting results from common modal
        $result5 = $this
            ->Common_model
            ->Results($table5);

        $table6 = 'tbl_color';
        // we are getting results from common modal
        $result6 = $this
            ->Common_model
            ->Results($table6);

        $table7 = 'tbl_product_size';
        $p_id = $result01['p_id'];
        
        // we are getting product_size results from common modal
        $result7 = $this
            ->Common_model
            ->Results_product_size_p_id($p_id, $table5, $table7);

        $table8 = 'tbl_product_photo';
        $column8['p_id'] = $result01['p_id'];
        // we are getting results from common modal
        $result8 = $this
            ->Common_model
            ->ResultsWhere2($column8, $table8);

        // $table9 = 'tbl_product_color';
        // $column9['p_id'] = $result01['p_id'];
        // // we are getting results from common modal
        // $result9 = $this
        //     ->Common_model
        //     ->ResultsWhere2($column9, $table9);

        $data['data'] = $result0;
        $data['product'] = $result01;
        $data['top_category'] = $result2;
        $data['mid_category'] = $result3;
        $data['end_category'] = $result4;
        $data['size'] = $result5;
        $data['color'] = $result6;
        $data['product_size'] = $result7;
        $data['p_photo'] = $result8;
        // $data['p_color'] = $result9;
        
        // echo '<pre>';print_r($result9);exit();

        // we are loading admin dashboard
        $this
            ->load
            ->view('staff/product/edit', $data);
    }

    public function create()
    {
        // we are setting data for get site data
        $column['id'] = '1';
        $table = 'tbl_settings';
        $data['page'] = 'Product Add';

        // we are getting results from common modal
        $result0 = $this
            ->Common_model
            ->Result($column, $table);

        $table2 = 'tbl_top_category';
        // we are getting results from common modal
        $result2 = $this
            ->Common_model
            ->Results($table2);

        $table3 = 'tbl_mid_category';
        // we are getting results from common modal
        $result3 = $this
            ->Common_model
            ->Results($table3);

        $table4 = 'tbl_end_category';
        // we are getting results from common modal
        $result4 = $this
            ->Common_model
            ->Results($table4);

        $table5 = 'tbl_size';
        // we are getting results from common modal
        $result5 = $this
            ->Common_model
            ->Results($table5);

        $table6 = 'tbl_color';
        // we are getting results from common modal
        $result6 = $this
            ->Common_model
            ->Results($table6);

        $table7 = 'tbl_product_size';
        // we are getting results from common modal
        $result7 = $this
            ->Common_model
            ->Results($table7);

        $data['data'] = $result0;
        $data['top_category'] = $result2;
        $data['mid_category'] = $result3;
        $data['end_category'] = $result4;
        $data['size'] = $result5;
        $data['color'] = $result6;
        $data['product_size'] = $result7;

        // we are loading admin dashboard
        $this
            ->load
            ->view('staff/product/create', $data);
    }

    public function get_mid_category()
    {
        if (!empty($_POST['tcat_id']))
        {
            $tcat_id = $_POST['tcat_id'];

            $column['tcat_id'] = $tcat_id;
            $table = 'tbl_mid_category';
            // we are getting results from common modal
            $result = $this
                ->Common_model
                ->ResultsWhere2($column, $table);

            echo '<option value="" selected>Select Mid Category </option>';
            foreach ($result as $row)
            {
                echo '<option value="' . $row['mcat_id'] . '">' . $row['mcat_name'] . '</option>';
            }
        }
    }

    public function get_end_category()
    {
        if (!empty($_POST['mcat_id']))
        {
            $mcat_id = $_POST['mcat_id'];

            $column['mcat_id'] = $mcat_id;
            $table = 'tbl_end_category';
            // we are getting results from common modal
            $result = $this
                ->Common_model
                ->ResultsWhere2($column, $table);

            echo '<option value="" selected>Select End Category </option>';
            foreach ($result as $row)
            {
                echo '<option value="' . $row['ecat_id'] . '">' . $row['ecat_name'] . '</option>';
            }
        }
    }

    public function add()
    {
        $user = $this->session->userdata('admin');
        $userid = $user['id'];
        
        $data = [];

        foreach ($_POST as $key => $value)
        {

            if ($key == 'table')
            {
                $table = $value;
            }

            if ($key == 'page')
            {
                $page = $value;
            }

            if ($key != 'p_is_active' && $key != 'todays_best_deal' && $key != 'p_is_featured' && $key != 'p_description' && $key != 'p_feature' && $key != 'p_condition' && $key != 'p_return_policy')
            {
                if (empty($value))
                {
                    $this
                        ->session
                        ->set_flashdata('error', '' . strtoupper("$key") . ' Can not be Empty.');
                    redirect(base_url('staff/' . $page));
                    exit();
                }
            }

            if ($key != 'table' && $key != 'page' && $key != 'photo' && $key != 'p_photo' && $key != 'size' && $key != 'color' && $key != 'p_old_price' && $key != 'p_current_price' && $key != 'p_qty' && $key != 'p_qty2' && $key != 'p_dp_price' && $key != 'p_pv')
            {
                $data[$key] = $value;
            }

            if ($key == 'size' || $key == 'p_old_price' || $key == 'p_current_price' || $key == 'p_qty' || $key == 'p_qty2' || $key == 'color' || $key == 'p_dp_price' || $key == 'p_pv' )
            {
                $data_size[$key] = $value;
            }

            // if ($key == 'color' || $key == 'size')
            // {
            //     $data_color[$key] = $value;
            // }
        }

        $table001 = 'tbl_product';
        $column_name001 = 'p_id';

        // getting last added product id
        $p_id001 = $this
            ->Common_model
            ->Results_last_id($column_name001, $table001);
        $p_id001 = $p_id001 + 1;

        // 		here we are uploading product featured photo
        if (!empty($_FILES['photo']['name']))
        {
            $path = $_FILES['photo']['name'];
            $ext = pathinfo($path, PATHINFO_EXTENSION);

            // $rand = mt_rand(10000,999999);
            $rand = $p_id001;
            $product_name = 'Product_' . $rand . '.' . $ext;
            $product_name2 = 'Product_Temp_' . $rand . '.' . $ext;

            $config['upload_path'] = 'uploads/shop/product/';
            $config['allowed_types'] = 'jpg|jpeg|png';
            $config['file_name'] = $product_name2;

            $this
                ->load
                ->library('upload', $config);

            if ($this
                ->upload
                ->do_upload('photo'))
            {
                $photo = $this
                    ->upload
                    ->data();

                // Resizeing Image Part
                resizeImage($config['upload_path'] . $product_name2, $config['upload_path'] . $product_name, 225, 225, false);

                if (file_exists($config['upload_path'] . $product_name2))
                {
                    unlink($config['upload_path'] . $product_name2);
                }

                $data['p_featured_photo'] = $product_name;

            }
            else
            {
                $this
                    ->session
                    ->set_flashdata('error', 'Something went Wrong Image is Not Uploaded.');
                redirect(base_url('staff/product/create/'));
                exit();
            }

        }

        $d_main['data'] = $data;
        // we are adding product data from tbl_product
        $result = $this
            ->Common_model
            ->Create($data, $table);

        if ($result == true)
        {
            $this
                ->session
                ->set_flashdata('success', 'SUCCESS!! Product is Added Successfully.');
        }
        else
        {
            $this
                ->session
                ->set_flashdata('error', 'Something went Wrong .');
        }

        $table = 'tbl_product';
        $column_name = 'p_id';

        // getting last added product id
        $p_id = $this
            ->Common_model
            ->Results_last_id($column_name, $table);

        // here we are adding product more photo
        if (!empty($_FILES['p_photo']['name']) && count(array_filter($_FILES['p_photo']['name'])) > 0)
        {
            $filesCount = count($_FILES['p_photo']['name']);
            for ($i = 0;$i < $filesCount;$i++)
            {
                $_FILES['file']['name'] = $_FILES['p_photo']['name'][$i];
                $_FILES['file']['type'] = $_FILES['p_photo']['type'][$i];
                $_FILES['file']['tmp_name'] = $_FILES['p_photo']['tmp_name'][$i];
                $_FILES['file']['error'] = $_FILES['p_photo']['error'][$i];
                $_FILES['file']['size'] = $_FILES['p_photo']['size'][$i];

                /*echo '<pre>';
                print_r($_FILES['file']);
                exit;*/

                $ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);

                $rand = rand();
                $product_other_img_name = 'Product_other_img_' . $rand . '.' . $ext;
                $product_other_img_name2 = 'Product_other_img_Temp_' . $rand . '.' . $ext;

                // File upload configuration
                $config2['upload_path'] = 'uploads/shop/product/more/';
                $config2['allowed_types'] = 'jpg|jpeg|png';
                $config2['file_name'] = $product_other_img_name2;
                //$config2['max_size']    = '100';
                //$config2['max_width'] = '1024';
                //$config2['max_height'] = '768';
                // Load and initialize upload library
                $this
                    ->load
                    ->library('upload', $config2);
                $this
                    ->upload
                    ->initialize($config2);

                // Upload file to server
                if ($this
                    ->upload
                    ->do_upload('file'))
                {
                    // Uploaded file data
                    $fileData = $this
                        ->upload
                        ->data();

                    // 	resizeImage($config2['upload_path'].$product_other_img_name2,$config2['upload_path'].$product_other_img_name,750,400,FALSE);
                    // if(file_exists($config2['upload_path'].$product_other_img_name2)){
                    //     unlink($config2['upload_path'].$product_other_img_name2);
                    // }
                    $file = $product_other_img_name2;

                    $data_p_photo = ['p_id' => $p_id, 'photo' => $file];

                    $d_main['data_p_photo'][$i] = $data_p_photo;

                    $table_p_photo = 'tbl_product_photo';
                    $result = $this
                        ->Common_model
                        ->Create($data_p_photo, $table_p_photo);
                }
            }
        }

        if (isset($data_size['size']))
        {
            foreach ($data_size['size'] as $key2 => $val)
            {

                $d_size['size_id'] = $data_size['size'][$key2];
                $d_size['color_id'] = $data_size['color'][$key2];
                $d_size['p_old_price'] = $data_size['p_old_price'][$key2];
                $d_size['p_current_price'] = $data_size['p_current_price'][$key2];
                $d_size['p_dp_price'] = $data_size['p_dp_price'][$key2];
                $d_size['p_pv'] = $data_size['p_pv'][$key2];
                $d_size['p_qty'] = $data_size['p_qty'][$key2];
                $d_size['p_min_qty'] = $data_size['p_qty2'][$key2];
                $d_size['p_id'] = $p_id;
                $d_size['user'] = $userid;

                $d_main['d_size'][$key2] = $d_size;

                $table21 = 'tbl_product_size';

                // we are adding data from tbl_product_size
                $result = $this
                    ->Common_model
                    ->Create($d_size, $table21);

                $table3 = 'tbl_product_size';
                $column_name3 = 'id';

                // getting last added product size id
                $ps_id = $this
                    ->Common_model
                    ->Results_last_id($column_name3, $table3);

                // if (isset($data_color['color'][$key2]))
                // {
                //     foreach ($data_color['color'][$key2] as $key3 => $val2)
                //     {
                //         $d_color['color_id'] = $data_color['color'][$key2][$key3];
                //         $d_color['size_id'] = $data_color['size'][$key2];
                //         $d_color['p_id'] = $p_id;
                //         $d_color['product_size'] = $ps_id;

                //         $d_main['d_color'][$key2][$key3] = $d_color;

                //         $table31 = 'tbl_product_color';
                //         // we are adding data from tbl_product_size
                //         $result = $this
                //             ->Common_model
                //             ->Create($d_color, $table31);
                //     }
                // }

            }
        }

        // echo '<pre>';
        // print_r($d_main);
        // exit;

        		redirect(base_url('staff/' . $page));
        
    }

    public function edit_content($id) {
        $user = $this->session->userdata('admin');
        $userid = $user['id'];
        
        $data = [];

        foreach ($_POST as $key => $value)
        {

            if ($key == 'table')
            {
                $table = $value;
            }

            if ($key == 'page')
            {
                $page = $value;
            }

            if ($key != 'p_is_active' && $key != 'todays_best_deal' && $key != 'p_is_featured' && $key != 'p_description' && $key != 'p_feature' && $key != 'p_condition' && $key != 'p_return_policy')
            {
                if (empty($value))
                {
                    $this
                        ->session
                        ->set_flashdata('error', '' . strtoupper("$key") . ' Can not be Empty.');
                    redirect(base_url('staff/' . $page));
                    exit();
                }
            }

            if ($key != 'table' && $key != 'page' && $key != 'photo' && $key != 'p_photo' && $key != 'size' && $key != 'color' && $key != 'p_old_price' && $key != 'p_current_price' && $key != 'p_qty' && $key != 'p_qty2'  && $key != 'p_dp_price' && $key != 'p_pv')
            {
                $data[$key] = $value;
            }

            if ($key == 'size' || $key == 'p_old_price' || $key == 'p_current_price' || $key == 'p_qty' || $key == 'p_qty2' || $key == 'color' || $key == 'p_dp_price' || $key == 'p_pv')
            {
                $data_size[$key] = $value;
            }

            // if ($key == 'color' || $key == 'size')
            // {
            //     $data_color[$key] = $value;
            // }
        }
       
        $table001 = 'tbl_product';
        $column_name001 = 'p_id';

        // getting last added product id
        $p_id001 = $this
            ->Common_model
            ->Results_last_id($column_name001, $table001);
        $p_id001 = $p_id001 + 1;
        
        $column_p['p_id'] = $id;

        $d_main['data'] = $data;
        // we are adding product data from tbl_product
        $result = $this
            ->Common_model
            ->Edit($column_p,$data,$table);

        if ($result == true)
        {
            $this
                ->session
                ->set_flashdata('success', 'SUCCESS!! Product is Updated Successfully.');
        }
        else
        {
            $this
                ->session
                ->set_flashdata('error', 'Something went Wrong .');
        }

        $table = 'tbl_product';
        $column_name = 'p_id';

        // getting last added product id
        $p_id = $id;

        $columna1['p_id'] = $p_id;
        $result  = $this->Common_model->Del($columna1,'tbl_product_size');
        // $result  = $this->Common_model->Del($columna1,'tbl_product_color');

        if (isset($data_size['size']))
        {
            foreach ($data_size['size'] as $key2 => $val)
            {
                $d_size['color_id'] = $data_size['color'][$key2];
                $d_size['size_id'] = $data_size['size'][$key2];
                $d_size['p_old_price'] = $data_size['p_old_price'][$key2];
                $d_size['p_current_price'] = $data_size['p_current_price'][$key2];
                $d_size['p_dp_price'] = $data_size['p_dp_price'][$key2];
                $d_size['p_pv'] = $data_size['p_pv'][$key2];
                $d_size['p_qty'] = $data_size['p_qty'][$key2];
                $d_size['p_min_qty'] = $data_size['p_qty2'][$key2];
                $d_size['p_id'] = $p_id;
                $d_size['user'] = $userid;

                $d_main['d_size'][$key2] = $d_size;

                $table21 = 'tbl_product_size';

                // we are adding data from tbl_product_size
                $result = $this
                    ->Common_model
                    ->Create($d_size, $table21);

                // $table3 = 'tbl_product_size';
                // $column_name3 = 'id';

                // getting last added product size id
                // $ps_id = $this
                //     ->Common_model
                //     ->Results_last_id($column_name3, $table3);

                // if (isset($data_color['color'][$key2]))
                // {
                //     foreach ($data_color['color'][$key2] as $key3 => $val2)
                //     {
                //         $d_color['color_id'] = $data_color['color'][$key2][$key3];
                //         $d_color['size_id'] = $data_color['size'][$key2];
                //         $d_color['p_id'] = $p_id;
                //         $d_color['product_size'] = $ps_id;

                //         $d_main['d_color'][$key2][$key3] = $d_color;

                //         $table31 = 'tbl_product_color';
                //         // we are adding data from tbl_product_size
                //         $result = $this
                //             ->Common_model
                //             ->Create($d_color, $table31);
                //     }
                // }

            }
        }

        // echo '<pre>';
        // print_r($d_main);
        // exit;

        		redirect(base_url('staff/' . $page));
        
    }
    
    public function edit_img()
    {
        foreach ($_POST as $key => $value)
        {
            if ($key == 'table')
            {
                $table = $value;
            }
            if ($key == 'page')
            {
                $page = $value;
            }
            if ($key == 'p_id')
            {
                $id = $value;
            }

        }

        if (!empty($_FILES['photo']['name']))
        {

            $column['p_id'] = $id;
            $table = 'tbl_product';

            $result = $this
                ->Common_model
                ->Result($column, $table);

            $current_img = $result['photo'];
            $current_img = './uploads/shop/product/' . $current_img;

            if (file_exists($current_img))
            {
                unlink($current_img);
            }

            $path = $_FILES['photo']['name'];
            $ext = pathinfo($path, PATHINFO_EXTENSION);

            $product_pic_name = 'product-' . $id . '.' . $ext;
            $product_pic_name2 = 'product-' . $id . '-' . rand() . '.' . $ext;

            $config['upload_path'] = 'uploads/shop/product/';
            $config['allowed_types'] = 'jpg|jpeg|png';
            $config['file_name'] = $product_pic_name2;

            $this
                ->load
                ->library('upload', $config);
            if ($this
                ->upload
                ->do_upload('photo'))
            {
                $logo = $this
                    ->upload
                    ->data();

                // / Resizeing Image Part
                resizeImage($config['upload_path'] . $product_pic_name2, $config['upload_path'] . $product_pic_name, 750, 400, false);

                if (file_exists($config['upload_path'] . $product_pic_name2))
                {
                    unlink($config['upload_path'] . $product_pic_name2);
                }

                $data['p_featured_photo'] = $product_pic_name;

                // we are updating data from tbl_settings
                $result = $this
                    ->Common_model
                    ->Edit($column, $data, $table);
                if ($result == true)
                {
                    $this
                        ->session
                        ->set_flashdata('success', 'SUCCESS!! Product ' . strtoupper("photo") . ' is Updated Successfully.');
                    redirect(base_url('staff/product/edit/' . $id));
                    exit();
                }
                else
                {
                    $this
                        ->session
                        ->set_flashdata('error', 'Something went Wrong.');
                    redirect(base_url('staff/product/edit/' . $id));
                    exit();
                }
            }
            else
            {
                $this
                    ->session
                    ->set_flashdata('error', 'Something went Wrong Image is Not Uploaded.');
                redirect(base_url('staff/product/edit/' . $id));
                exit();
            }

        }
        else
        {
            $this
                ->session
                ->set_flashdata('error', 'Product ' . strtoupper("photo") . ' can not be Empty.');
            redirect(base_url('staff/product/edit/' . $id));
        }
    }

    public function add_more_img($id)
    {
        $p_id = $id;

        if (!empty($_FILES['p_photo']['name']) && count(array_filter($_FILES['p_photo']['name'])) > 0)
        {
            $filesCount = count($_FILES['p_photo']['name']);
            for ($i = 0;$i < $filesCount;$i++)
            {
                $_FILES['file']['name'] = $_FILES['p_photo']['name'][$i];
                $_FILES['file']['type'] = $_FILES['p_photo']['type'][$i];
                $_FILES['file']['tmp_name'] = $_FILES['p_photo']['tmp_name'][$i];
                $_FILES['file']['error'] = $_FILES['p_photo']['error'][$i];
                $_FILES['file']['size'] = $_FILES['p_photo']['size'][$i];

                $ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);

                $rand = mt_rand(1000000, 99999999);
                $product_other_img_name = 'Product_other_img_' . $rand . '.' . $ext;
                $product_other_img_name2 = 'Product_other_img_Temp_' . $rand . '.' . $ext;

                // File upload configuration
                $uploadPath = 'uploads/shop/product/more/';
                $config['upload_path'] = $uploadPath;
                $config['allowed_types'] = 'jpg|jpeg|png|gif';
                $config['file_name'] = $product_other_img_name2;
                //$config['max_size']    = '100';
                //$config['max_width'] = '1024';
                //$config['max_height'] = '768';
                // Load and initialize upload library
                $this
                    ->load
                    ->library('upload', $config);
                $this
                    ->upload
                    ->initialize($config);

                // Upload file to server
                if ($this
                    ->upload
                    ->do_upload('file'))
                {
                    // Uploaded file data
                    $fileData = $this
                        ->upload
                        ->data();

                    // 	resize image works start here
                    $CI = & get_instance();

                    $config3['image_library'] = 'gd2';
                    $config3['source_image'] = $config['upload_path'] . $product_other_img_name2;
                    $config3['new_image'] = $config['upload_path'] . $product_other_img_name;
                    $config3['create_thumb'] = true;
                    $config3['maintain_ratio'] = false;
                    $config3['width'] = 750;
                    $config3['height'] = 400;
                    $config3['thumb_marker'] = '';

                    $CI
                        ->load
                        ->library('image_lib', $config3);
                    $CI
                        ->image_lib
                        ->resize();
                    $CI
                        ->image_lib
                        ->clear();

                    // 	if(file_exists($config3['source_image'])){
                    //                     unlink($config3['source_image']);
                    //                 }
                    $file = $product_other_img_name2;

                    $data = ['p_id' => $p_id, 'photo' => $file];

                    $table = 'tbl_product_photo';
                    $result = $this
                        ->Common_model
                        ->Create($data, $table);
                }
            }
        }

        $this
            ->session
            ->set_flashdata('success', 'SUCCESS!! Data is Submited Successfully.');
        redirect(base_url('staff/product/edit/' . $id));
    }

    public function test()
    {
        $this
            ->load
            ->view('staff/product/test');
    }
    
    
    
    
    
     public function kush()
    {
       
  $query = $this->db->get('tbl_product');
  if ($query->num_rows() >0)  
  {
    foreach ($query->result() as $rows)
        {
          $ecat_id= $rows->ecat_id;
          $p_id= $rows->p_id;
          
 $querysk11 = $this->db->query("SELECT * FROM tbl_end_category where ecat_id='$ecat_id' ");
 $rowfa111 = $querysk11->row();
 $mcat_id=$rowfa111->mcat_id;
 
 $querysk1 = $this->db->query("SELECT * FROM tbl_mid_category where mcat_id='$mcat_id' ");
 $rowfa11 = $querysk1->row();
 $tcat_id=$rowfa11->tcat_id;
 
 $data=['tcat_id'=>$tcat_id,'mcat_id'=>$mcat_id];
  
  $this->db->where('p_id',$p_id);        
  $this->db->update('tbl_product',$data);        
          
         
        
       }
      
  } 
    }
    
    
    
    
    
    public function delete() {
        $p_id = $_POST['pid'];
        $column['p_id'] = $p_id;
        
         $this->load->model('Common_model');
        
        $result = $this->Common_model->Result($column,'tbl_product');
        $product_pic_name2 = $result['p_featured_photo'];
        $uploadPath = 'uploads/shop/product/';
        
        if (file_exists($uploadPath . $product_pic_name2))
        {
            unlink($uploadPath . $product_pic_name2);
        }
        
        $this->Common_model->Del($column,'tbl_product');
        $this->Common_model->Del($column,'tbl_product_size');
        // $this->Common_model->Del($column,'tbl_product_color');
        $this->Common_model->Del($column,'tbl_product_photo');
    
        $this->session->set_flashdata('success', 'SUCCESS!! Product is Deleted Successfully.');

        redirect(base_url('staff/product'));
        
    }



}