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

class Product extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        if($this->session->userdata("level") != 99)
        {
            
            redirect(base_url(),'refresh');
            
        }
    }
    
    public function index()
    {
        $this->load->view('admin/Header');
        $this->load->view('admin/Product');
        $this->load->view('admin/Footer');
    }
    
    public function submit()
    {
        $this->load->helper('string');
        $cate = $this->input->post('cate');
        $namepro = $this->input->post('namepro');
        $price = $this->input->post('price');
        $detail = $this->input->post('detail');


        $this->db->where('name_pro', $namepro);
        $this->db->where('id_cate', $cate);
        $query = $this->db->get('product', 1);
        if($query->num_rows() == 0)
        {
            $filename = random_string('alpha',10);
            $config['file_name'] = $filename;
            $config['upload_path'] = './Products/';
            $config['allowed_types'] = 'jpg';
            $config['width'] = "240px";
            $this->upload->initialize($config);
            
            $this->upload->do_upload("image");
            echo $this->upload->display_errors();
            $object = array(
                'name_pro' =>  $namepro,
                'id_cate'   =>  $cate,
                'price'     =>  $price,
                'image'     =>  $filename,
                'detail'     =>  $detail,
            );
            $this->db->insert('product', $object);
            echo json_encode(['status' => 1, 'msg' => 'success']);
        }else{
            echo json_encode(['status' => 0, 'msg' => 'fail']);
        }
    }

    public function edit()
    {
        $id = $this->input->post("id");
        $text = $this->input->post("text");

        $object = array(
            'name_pro' =>  $text,
        );
        $this->db->where('id_product', $id);
        $this->db->update('product', $object);
        echo json_encode(['status' => 1, 'msg' => 'success']);
    }

    public function delete($id)
    {
        $this->db->where('id_product', $id);
        $this->db->delete('product');
        $this->db->where('id_product', $id);
        $this->db->delete('code');
        echo json_encode(['status' => 1, 'msg' => 'success']);
    }

}

/* End of file ManageCategory.php */
