<?php

class Ayam_pedaging extends CI_Controller{

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

	if($this->session->userdata('role_id') != '2'){
			$this->session->set_flashdata('pesan','<div class="alert alert-danger alert-dismissible fade show" role="alert">
				  Anda Belum Login!
				  <button type="button" class="close" data-dismiss="alert" aria-label="Close">
				  <span aria-hidden="true">&times; </span>
				  </button>
				</div>');
			redirect('auth/login');
		}
	}

	
	public function tambah_ke_keranjang($id)
	{
		$ayam_pedaging = $this->model_aym_pedaging->find($id);

		$data = array(

        'id'      => $ayam_pedaging->id_brg,
        'qty'     => 1,
        'price'   => $ayam_pedaging->harga,
        'keuntungan'   => $ayam_pedaging->keuntungan,
        'name'    => $ayam_pedaging->nama_brg
		);

		$this->cart->insert($data);
		redirect('kategori/ayam_pedaging');
	}
	public function detail_keranjang()
	{
		$data = [
		'str'	=> '',
		'method' =>''];
		$this->load->view('templates/header');
		$this->load->view('templates/sidebar',$data);
		$this->load->view('keranjang');
		$this->load->view('templates/footer');
	}
	
	public function pesanan()
	{
		$id = $this->session->userdata('id_user');
		$invoice = $this->db->query("SELECT * FROM tb_invoice WHERE id_user=$id ")->result_array();
		foreach($invoice as $i => $in){ 	
			$in['harga'] = 0;
			$in['keuntungan'] = 0;
			$pesanan = $this->model_invoice->ambil_id_pesanan($in['id']);
			// var_dump($pesanan->jumlah);die;
			if($pesanan){
				foreach($pesanan as $k => $p){
							// $pesanan['harga'];
						$in['harga']=$in['harga'] + ($p->harga * $p->jumlah);
						$in['keuntungan']=$in['keuntungan'] + ($p->keuntungan);
						}
					}
		$invoice[$i]['harga'] = $in['harga'];
		$invoice[$i]['hargaa'] = $in['harga'] + $in['keuntungan']; 
		$invoice[$i]['keuntungan'] = $in['keuntungan']; 
		}
		$data = [
		'str'	=> '',
		'inv'	=> $invoice,
		'method' =>''];
		$this->load->view('templates/header');
		$this->load->view('templates/sidebar',$data);
		$this->load->view('pesanan',$data);
		$this->load->view('templates/footer');
	}

	public function bukti($id){
		// var_dump($this->input->post());
		// var_dump($this->input->file());die;
		$gambar		= $_FILES['gambar']['name'];
		if ($gambar	!= null){
			$config ['upload_path'] = './uploads/kwitansi/';
			$config ['allowed_types'] = 'jpg|jpeg|png';

			$this->load->library('upload', $config);
			if (!$this->upload->do_upload('gambar')){
				$this->session->set_flashdata('pesan','<div class="alert alert-warning">
				<strong>Upload Gagal!</strong> coba upload kembali file gambar anda.
			  </div>');
			}else {
				$gambar = $this->upload->data('file_name');
				$this->session->set_flashdata('pesan','<div class="alert alert-success">
				<strong>Upload Berhasil!</strong> Periksa kembali file gambar anda.
			  </div>');
			  $data = [
				  'img' => $gambar,
				  'ket' => -1
			  ];
			  $this->model_invoice->update('tb_invoice',(['id'=>$id]),$data);
			}
		}else{
			$this->session->set_flashdata('pesan','<div class="alert alert-warning">
				<strong>Upload Gagal!</strong> Periksa kembali file gambar anda.
			  </div>');
		}
		redirect(base_url('dashboard/pesanan'));
	}
	
	public function hapus_keranjang()
	{
		$this->cart->destroy();
		redirect('ayam_pedaging');
	}

	public function pembayaran()
	{
		$data = [
		'str'	=> '',
		'method' =>''];
		$this->load->view('templates/header');
		$this->load->view('templates/sidebar',$data);
		$this->load->view('pembayaran');
		$this->load->view('templates/footer');
	}

	public function proses_pesanan()
	{
		$is_processed = $this->model_invoice->index();
		$data = ['str'=>'','method'=>''];
		if($is_processed){
		$this->cart->destroy();
		$this->load->view('templates/header');
		$this->load->view('templates/sidebar',$data);
		$this->load->view('proses_pesanan');
		$this->load->view('templates/footer');
	}else {
		echo "Maaf, Pesanan Anda Gagal diproses";
	}
}

	public function detail($id_brg)
	{
		$data = [
		'ayam_pedaging' => $this->model_aym_pedaging->detail_brg($id_brg),
		'str'	=> '',
		'method' =>''
	];
		$data['ayam_pedaging'] = $this->model_aym_pedaging->detail_brg($id_brg);
		$this->load->view('templates/header');
		$this->load->view('templates/sidebar', $data);
		$this->load->view('detail_barang', $data);
		$this->load->view('templates/footer');
	}
}