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

class ManageProduct extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        if($this->session->userdata("level") != 99)
        {
            
            redirect(base_url(),'refresh');
            
        }
    }
    
    public function index()
    {
        $this->data['id'] = $this->input->get("id"); 
        $this->load->view('admin/Header');
        $this->load->view('admin/ManageProduct',$this->data,false);
        $this->load->view('admin/Footer');
    }

    public function Addcode()
    {
        $fail = false;
        $data = explode("\n",$this->input->post("code"));
        $amount = 0;
            for($j = 0; $j <= count($data)-1; $j++)
            {
            $this->db->where('text', $data[$j]);
            $query = $this->db->get('code', 1);
                if($query->num_rows() == 0)
                {
                    $object[] = array(
                        'text'  => $data[$j],
                        'id_product'  =>  $this->input->post('idpro')
                    );
                    $amount++;
                }else{
                    $fail = true;
                    echo json_encode(['status' => 0, 'msg' => $data[$j]]);
                    die;
                }
            }
            if($fail == false)
            {
                $id = $this->input->post('idpro');
                $this->db->insert_batch('code', $object);
                $this->db->query("UPDATE product SET stock = stock +'$amount' WHERE id_product = $id");
                echo json_encode(['status' => 1, 'msg' => 'success']);
            }
    }

    public function editprice()
    {
        $id = $this->input->post("id");
        $idpro = $this->input->post('idpro');
        $price = $this->input->post("price");

        $object = array(
            'price' =>  $price
        );
        $this->db->where('id_product', $idpro);
        $this->db->update('product', $object);
        
        
        redirect('Admin/ManageProduct?id='.$idpro,'refresh');
        
    }

    
    public function editDetail()
    {
        $id = $this->input->post("id");
        $idpro = $this->input->post('idpro');
        $detail = $this->input->post("detail");

        $object = array(
            'detail' =>  $detail
        );
        $this->db->where('id_product', $idpro);
        $this->db->update('product', $object);
        
        
        redirect('Admin/ManageProduct?id='.$idpro,'refresh');
        
    }

    public function editdiscount()
    {
        $id = $this->input->post("id");
        $idpro = $this->input->post('idpro');
        $discount = $this->input->post("discount");

        $object = array(
            'discount' =>  $discount
        );
        $this->db->where('id_product', $idpro);
        $this->db->update('product', $object);
        
        
        redirect('Admin/ManageProduct?id='.$idpro,'refresh');
        
    }

    public function delete($id)
    {
        $this->db->where('id_code', $id);
        $query = $this->db->get('code', 1);
        
        $this->db->where('id_code', $id);
        $this->db->delete('code');
        $data = $query->row_array();
        $id = $data['id_product'];
        $this->db->query("UPDATE product SET stock = stock-1 WHERE id_product = $id");

        echo json_encode(['status' => 1, 'msg' => 'success']);
        
    }

}

/* End of file ManageProduct.php */
