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

class Product_stock extends CI_Controller {
	// Session Authoriesed
	public function __construct() {
		parent::__construct();
		
		$admin = $this->session->userdata('admin');
		$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().'master/login/index');
		}
		
	   // loading common model
	    $this->load->model('Common_model');
	}

	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 Stock';
		
		$table = 'tbl_product';
        // we are getting results from common modal
		$result = $this->Common_model->Results($table);
		
		$data['product'] = $result;
		
		
		$table1 = 'tbl_product_size';
        // we are getting results from common modal
		$result1 = $this->Common_model->Results($table1);
		
		$data['product_size'] = $result1;
		
		
		
		$table2 = 'tbl_size';
        // we are getting results from common modal
		$result2 = $this->Common_model->Results($table2);
		
		$data['size'] = $result2;
		
        // we are loading admin dashboard
		$this->load->view('master/product_stock/list',$data);	
	}


	public function all()
	{
	    if(isset($_POST['p_id']) && isset($_POST['p_qty'])){
	        foreach($_POST['p_qty'] as $key => $value){
	            $column2['p_id'] = $_POST['p_id'][$key];
	            $column2['id'] = $_POST['row_id'][$key];
	            $formdata['p_old_price'] = $_POST['p_old_price'][$key];
	            $formdata['p_current_price'] = $_POST['p_current_price'][$key];
	            $formdata['p_pv'] = $_POST['p_pv'][$key];
	            $formdata['p_qty'] = $_POST['p_qty'][$key];
	            $formdata['p_min_qty'] = $_POST['p_min_qty'][$key];
	            
	            $data2[$key]['$formdata'] = $formdata;
	            $data2[$key]['$column2'] = $column2;
	            
	            $table2="tbl_product_size";
	            
                // we are updating data from tbl_p_type
                $result  = $this->Common_model->Edit($column2,$formdata,$table2);
        
	        }
	        
	        if($result == true){
                $this->session->set_flashdata('success','SUCCESS!! Data is Updated Successfully.');
    		    redirect(base_url('master/product_stock/all'));
    		    exit();
            } else{
                $this->session->set_flashdata('error','Something went Wrong .');
    		    redirect(base_url('master/product_stock/all'));
    		    exit();
            }
        
	       // echo '<pre>';
        //     print_r($data2);
        //     exit();
        }
        // 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 Stock';
		
		$table = 'tbl_product';
        // we are getting results from common modal
		$result = $this->Common_model->Results($table);
		
		$data['product'] = $result;
		
		
		$table1 = 'tbl_product_size';
        // we are getting results from common modal
		$result1 = $this->Common_model->Results($table1);
		
		$data['product_size'] = $result1;
		
		
		
		$table2 = 'tbl_size';
        // we are getting results from common modal
		$result2 = $this->Common_model->Results($table2);
		
		$data['size'] = $result2;
		
        // we are loading admin dashboard
		$this->load->view('master/product_stock/all',$data);	
	}


	public function edit($id)
	{
        // 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 Stock';
		
		$table = 'tbl_product';
        // we are getting results from common modal
		$result = $this->Common_model->Results($table);
		
		$data['product'] = $result;
		
		
		$table1 = 'tbl_product_size';
		$column1['id'] = $id;
        // we are getting results from common modal
		$result1 = $this->Common_model->Result($column1,$table1);
		
		$data['product_size'] = $result1;
		
		
		
		$table2 = 'tbl_size';
        // we are getting results from common modal
		$result2 = $this->Common_model->Results($table2);
		
		$data['size'] = $result2;
		
        // we are loading admin dashboard
		$this->load->view('master/product_stock/edit',$data);	
	}
	
	public function edit_content($id){
	    
        $data = [];
        
        foreach ($_POST as $key => $value) {
            
            if($key == 'table'){$table = $value;}
            
            if($key == 'page'){$page = $value;}
            
            if($key != 'status'){
                if (empty($value)) {
                    $this->session->set_flashdata('error',''.strtoupper("$key").' Can not be Empty.');
    			    redirect(base_url('master/'.$page));
    			    exit();
                }
            }
            
            if($key != 'table' && $key != 'page' && $key != 'column'){
                $data[$key] = $value;
            }
            
        }
        
        $column['id'] = $id;
                
        // we are updating data from tbl_p_type
        $result  = $this->Common_model->Edit($column,$data,$table);
        if($result == true){
            $this->session->set_flashdata('success','SUCCESS!! Data is Updated Successfully.');
		    redirect(base_url('master/'.$page));
		    exit();
        } else{
            $this->session->set_flashdata('error','Something went Wrong .');
		    redirect(base_url('master/'.$page));
		    exit();
        }
           
	}
	
}

?>