<?php  



class Data_produk extends CI_Controller{

	public function __construct()
	{
		parent::__construct();
		$this->load->model('model_produk');
	}

	public function index()

	{

		$data['produk'] = $this->model_produk->tampil_data()->result();;

		$this->load->view('templates_admin/header');

		$this->load->view('templates_admin/sidebar');

		$this->load->view('adminmm/list_produk', $data);

		$this->load->view('templates_admin/footer');

	}

	public function tambah_produk()

	{

		$this->load->view('templates_admin/header');

		$this->load->view('templates_admin/sidebar');

		$this->load->view('adminmm/tambah_produk');

		$this->load->view('templates_admin/footer');

	}

	public function tambah_aksi()

	{
		$nama_produk		= $this->input->post('nama_produk');

		$deskripsi			= $this->input->post('deskripsi');

		$harga_produk		= $this->input->post('harga_produk');

		$link_produk		= $this->input->post('link_produk');

		$shopee_link		= $this->input->post('shopee_link');

		$tiktok_link		= $this->input->post('tiktok_link');

		$gambar				= isset($_FILES['gambar']['name']) ? $_FILES['gambar']['name'] : '';

		if($gambar == ''){}else{

			$config['upload_path'] 		= './uploads';

			$config['allowed_types']	= 'jpg|jpeg|png|gif';

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

			if(!$this->upload->do_upload('gambar')){

			}else{
				$gambar = $this->upload->data('file_name');
			}

		}

		// Validate required fields
		if(empty($nama_produk) || empty($deskripsi) || empty($harga_produk)) {
			// Redirect back with error - you may want to add session flash data here
			redirect('adminmm/data_produk/tambah_produk');
			return;
		}

		$data = array(

			'nama_produk'			=> $nama_produk,

			'deskripsi'				=> $deskripsi,

			'harga_produk'			=> $harga_produk,

			'link_produk'			=> $link_produk ?: '',

			'shopee_link'			=> $shopee_link ?: '',

			'tiktok_link'			=> $tiktok_link ?: '',

			'gambar'				=> $gambar,

			);



		$this->model_produk->tambah_produk($data, 'produk');

		redirect('adminmm/data_produk/tambah_produk');

	}

}