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

class Cart extends CI_Controller {

	function __Construct()
    {
        parent ::__construct();
		$this->load->model('Barang');
		$this->load->model('Kategori');
    }
	
	public function index()
	{
		$hd['title'] = "Toko Dzaky Mulya";
		$data['active'] = "None";
		$data['kategori'] = $this->Kategori->kategori_all();
		
		$this->load->view('front/header',$hd);
		$this->load->view('front/shopstep');
		$this->load->view('front/Cart',$data);
		$this->load->view('front/footer');
	}
	
	public function tambah()
	{
		$data = array(
			'id'	=> $_POST['id'],
			'name'	=> $_POST['nama'],
			'price'	=> $_POST['harga'],
			'qty'	=> $_POST['qty'],
		);
		//print_r($data);
		$this->cart->insert($data);
		redirect('product');
	}
	public function update(){
		$i= 0;
		foreach ($_POST as $p){
			$i= $i+1;
			$data = array(
				'rowid'	=> $p['rowid'],
				'qty'	=> $p['qty']
			);
			$this->cart->update($data);
		}
		redirect('product');
	}
	public function check_out(){
		//print_r($this->cart->contents());
		$this->load->view('front/Checkout');
	}
	public function check_out_conv(){
		$data['profile'] = array(
			'nama'	=> $_POST['nama'],
			'email'	=> $_POST['email'],
			'alamat'	=> $_POST['alamat'],
			'kota_kab'	=> $_POST['kota'],
			'provinsi'	=> $_POST['prov'],
			'telp'	=> $_POST['telp']
		);
		$data['transaksi'] = array(
			'tanggal_order' => date('Y-m-d'),
			'total_harga' 	=> $this->cart->total(),
			'cara_bayar' 	=> $_POST['cara_bayar'],
			'type' 			=> $_POST['tipe'],
			'status' 		=> 'PENDING'
		);
		$data['belanjaan'] = $this->cart->contents();
		//echo JSON_encode($data);
		$this->load->model('User');
		$this->load->model('Transaction');
		$idProf = $this->User->tambah_profil($data['profile']);
		if( $idProf != 0){
			$idTrans = $this->Transaction->tambah_trans($data['transaksi']);
			if( $idProf != 0){
				foreach ($data['belanjaan'] as $belanja){
					$sal = array(
						'pembeli'	=> $idProf,
						'transaksi'	=> $idTrans,
						'barang'	=> $belanja['id'],
						'qty'		=> $belanja['qty'],
						'tanggal'	=> date('Y-m-d')						
					);
					$this->Transaction->tambah_sales($sal);
					$this->Barang->barang_sold($sal['barang'],$sal['qty']);	
				}
			} 
		}
		
		
	}
}
?>