<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Attribute extends MY_Controller {

    function __construct() {
        parent::__construct();
    }

    public function index() {
       
         $data['template'] = 'inventory/productattributes_view';
          $this->load->view('template',$data);
    }

    // GET PRODUCT ATTRIBUTES
    public function getproductattributes() {
         $data['data'] = $this->common->getAll('ATTRIBUTE');
         echo json_encode($data);
    }


      function addproductattribute()
    {
    
         $id = $this->input->post('id');
        $data = array(    
            'ID' => uniqid(),
            'NAME' => $this->input->post('NAME')
        );
       if($id == '')
       { 
          $existdata = $this->common->existdata('ATTRIBUTE',$data);
          // print_r( $existdata);die();
           if(!empty($existdata))
           {
           
             $this->session->set_flashdata('success_msg', 'The record is already exist');
           }
           else
           {

              $this->common->insert('ATTRIBUTE',$data);
              $this->session->set_flashdata('success_msg', 'The record is added successfuly.');
           }
         
       }
        else
       {
          $where = array('ID' => $id);
          $this->session->set_flashdata('success_msg', 'The record has been updated successfuly.');
          $this->common->update('ATTRIBUTE',$where,$data);
       }
        redirect('inventory/attribute');
        
    }

       public function editattribute($id) {
        $where = array('ID' => $id,);
        $data = $this->common->getWhere('ATTRIBUTE',$where);

        echo json_encode($data);
 
    }


     function deleteattributes($id)
    { 
         $where = array('ID' => $id);  
        $this->common->delete('ATTRIBUTE',$where);
        $this->session->set_flashdata('success_msg', 'The record has been deleted successfuly.');
        redirect('inventory/attribute');
    }

    public function productattributesets() {
       
         $data['template'] = 'inventory/productattributesets_view';
          $this->load->view('template',$data);
    }

     // GET PRODUCT ATTRIBUTESETS
    public function getproductattributesets() {
         $data['data'] = $this->common->getAll('ATTRIBUTESET');
         echo json_encode($data);
    }
    
     function addproductattributeset()
    {
    
         $id = $this->input->post('id');
        $data = array( 
               'ID' => uniqid(),   
            'NAME' => $this->input->post('NAME')
        );
       if($id == '')
       { 
          $existdata = $this->common->existdata('ATTRIBUTESET',$data);
          // print_r( $existdata);die();
           if(!empty($existdata))
           {
           
             $this->session->set_flashdata('success_msg', 'The record is already exist');
           }
           else
           {

              $this->common->insert('ATTRIBUTESET',$data);
              $this->session->set_flashdata('success_msg', 'The record is added successfuly.');
           }
         
       }
        else
       {
          $where = array('ID' => $id);
          $this->session->set_flashdata('success_msg', 'The record has been updated successfuly.');
          $this->common->update('ATTRIBUTESET',$where,$data);
       }
        redirect('inventory/attribute/productattributesets');
        
    }

       public function editattributeset($id) {
        $where = array('ID' => $id,);
        $data = $this->common->getWhere('ATTRIBUTESET',$where);

        echo json_encode($data);
 
    }

    function deleteattributeset($id)
    { 
          $where = array('ID' => $id);  
        $this->common->delete('ATTRIBUTESET',$where);
        $this->session->set_flashdata('success_msg', 'The record has been deleted successfuly.');
        redirect('inventory/attribute/productattributesets');
 
    }


    public function attributevalues() {
          
          $data['attribute'] = $this->common->getAll('ATTRIBUTE');
          $data['template'] = 'inventory/attributevalues_view';
          $this->load->view('template',$data);
    }

   
    
    

    // GET PRODUCT ATTRIBUTEVALUES
    public function getattributevalues() {
        $sql = "SELECT `ATTRIBUTEVALUE`.*, `ATTRIBUTE`.NAME FROM ATTRIBUTEVALUE INNER JOIN ATTRIBUTE ON `ATTRIBUTEVALUE`.ATTRIBUTE_ID=`ATTRIBUTE`.ID";
         $data['data'] = $this->common->customquery($sql);
         echo json_encode($data);
    }
      
       function addattributevalues()
    {
    
         $id = $this->input->post('id');
        $data = array(   
              'ID' => uniqid(), 
            'ATTRIBUTE_ID' => $this->input->post('ATTRIBUTE'),
            'VALUE' => $this->input->post('VALUE')
        );
       if($id == '')
       { 
          $existdata = $this->common->existdata('ATTRIBUTEVALUE',$data);
          // print_r( $existdata);die();
           if(!empty($existdata))
           {
           
             $this->session->set_flashdata('success_msg', 'The record is already exist');
           }
           else
           {

              $this->common->insert('ATTRIBUTEVALUE',$data);
              $this->session->set_flashdata('success_msg', 'The record is added successfuly.');
           }
         
       }
        else
       {
          $where = array('ID' => $id);
          $this->session->set_flashdata('success_msg', 'The record has been updated successfuly.');
          $this->common->update('ATTRIBUTEVALUE',$where,$data);
       }
        redirect('inventory/attribute/attributevalues');
        
    }

       public function editattributevalues($id) {
        $where = array('ID' => $id,);
        $data = $this->common->getWhere('ATTRIBUTEVALUE',$where);

        echo json_encode($data);
 
    }

    function deleteattributevalues($id)
    { 
          $where = array('ID' => $id);  
        $this->common->delete('ATTRIBUTEVALUE',$where);
        $this->session->set_flashdata('success_msg', 'The record has been deleted successfuly.');
        redirect('inventory/attribute/attributevalues');
 
    }


    // GET ATTRIBUTE NAMES IN COMBO
    function getattributescombo($id = NULL) {
        $this->load->model('inventory/attribute_model');
        $catarr = $this->attribute_model->productattributedropdown();
        $data = '<option value="">None</option>';
        for ($i = 0; $i < count($catarr); $i++) {
            if ($id != NULL && $id == $catarr[$i]->ID)
                $data .= "<option id=\"option_custom\" selected=\"yes\" value=\"" . $catarr[$i]->ID . "\">" . $catarr[$i]->NAME . "</option>\n";
            else
                $data .= "<option id=\"option_custom\" value=\"" . $catarr[$i]->ID . "\">" . $catarr[$i]->NAME . "</option>\n";
        }
        return $data;
    }

    private function getattributeid($name) {
        $this->load->model('inventory/attribute_model');
        return $this->attribute_model->getattributeid($name);
    }
      
    //FOR AUTOCOMPLETE
    function attributeautocomplete() {
         $term = $this->input->post('term', TRUE);
            $this->load->model('inventory/attribute_model');
            $rows = $this->attribute_model->attributeautocomplete($term);
            $keywords = array();
            foreach ($rows as $row)
            array_push($keywords, array(
            'label' => $row->NAME,
            'value' => $row->ID
            ));

            echo json_encode($keywords);
            } 
    function attributesautocomplete() {
            $term = $this->input->post('term', TRUE);
            $this->load->model('inventory/attribute_model');
            $rows = $this->attribute_model->attributesautocomplete($term);

            $keywords = array();
            foreach ($rows as $row)
                array_push($keywords, $row->NAME);
            echo json_encode($keywords);
    }
    
    //ATTRIBUTEVALUE AUTOCOMPLETE
    function attributevalueautocomplete() {
        $term = $this->input->post('term', TRUE);
        $this->load->model('inventory/attribute_model');
        $rows = $this->attribute_model->attributevalueautocomplete($term);
        
        $keywords = array();
        foreach ($rows as $row)
            array_push($keywords, $row->VALUE);
        echo json_encode($keywords);
    }
    
    //ATTRIBUTESET AUTOCOMPLETE
     function attributesetautocomplete() {
        $term = $this->input->post('term', TRUE);
        $this->load->model('inventory/attribute_model');
        $rows = $this->attribute_model->attributesetautocomplete($term);
        
        $keywords = array();
        foreach ($rows as $row)
            array_push($keywords, $row->NAME);
        echo json_encode($keywords);
    }
    //ADD-EDIT-DELETE Product AttributeSet
    function record()
    {
        $this->load->model('inventory/attribute_model');
        $oper = $this->input->post('oper');
        $id = uniqid();
        /*if($oper=='del')
        {
            $id = $this->input->post('id');
            if($id)
            {
                $this->attribute_model->delete($id);
                $fdata = array('msgt' => 'S', 'msg' => 'Attribute deleted successfully.');
                $this->session->set_flashdata($fdata);
                redirect(site_url() . 'inventory/attribute');
            }
            else {
                $fdata = array('msgt' => 'E', 'msg' => 'Cannot delete attribute.');
                $this->session->set_flashdata($fdata);
                redirect(site_url() . 'inventory/attribute');
            }
        }
        else
        {*/
            $form_data = array(
                            'ID' => $id,
                            'NAME' => $this->input->post('NAME')
            );
            
            var_dump($form_data);
        
            if($oper=='add')
            {
                if ($this->attribute_model->add($form_data) == TRUE) {
                    $fdata = array('msgt' => 'S', 'msg' => 'Role added successfully.');
                    $this->session->set_flashdata($fdata);
                    return true;
                }
            }
            elseif($oper=='edit')
            {
                $id = $this->input->post('ID');
                if($id)
                {
               $this->attribute_model->update($id, $form_data);   
            }
        
        }
    }
   
    //ADD-EDIT-DELETE Product Attribute
   
   
    //ADD-EDIT-DELETE Product AttributeValue
    function record1()
    {   
        $oper = $this->input->post('oper');
        $id = uniqid();
        
        $this->load->model('inventory/attribute_model');
        /*if($oper=='del')
        {
            $id = $this->input->post('id');
            if($id)
            {
                $this->attribute_model->deleteattributevalue($id);
                $fdata = array('msgt' => 'S', 'msg' => 'Product deleted successfully.');
                $this->session->set_flashdata($fdata);
                redirect(site_url() . 'inventory/attribute/attributevalues');
            }
            else {
                $fdata = array('msgt' => 'E', 'msg' => 'Cannot delete product.');
                $this->session->set_flashdata($fdata);
                redirect(site_url() . 'inventory/attribute/attributevalues');
            }
        }
        else
        {*/
            $form_data = array(
                            'ID' => $id,
                            'ATTRIBUTE_ID' => $this->input->post('ATTRIBUTE_ID'),
                            'VALUE' => $this->input->post('VALUE')
                          
            );
            var_dump($form_data);
            if($oper=='add')
            {
                    if ($this->attribute_model->addattributevalue($form_data) == TRUE) {
                            $fdata = array('msgt' => 'S', 'msg' => 'Product added successfully.');
                            $this->session->set_flashdata($fdata);
                            return true;
                    }
            }
            elseif($oper=='edit')
            {
                    $id = $this->input->post('id');
                    if($id)
                    {
                            $this->attribute_model->updateattributevalue($id, $form_data);   
                    }       
            }
        
     }
      
    function deleteattributevalue()
    { $this->load->model('inventory/attribute_model');
        if ($this->uri->segment(4)) 
            {
           
            $this->attribute_model->deleteattributevalue($this->uri->segment(4));
            $fdata = array('msgt' => 'S', 'msg' => 'Attributevalue deleted successfully.');
            $this->session->set_flashdata($fdata);
            redirect(site_url() . 'inventory/attribute/attributevalues');
        } else{
            $fdata = array('msgt' => 'E', 'msg' => 'Cannot delete Attributevalue.');
            $this->session->set_flashdata($fdata);

            redirect(site_url() . 'inventory/attribute/attributevalues');
        }
    }
    
}