<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Product;
use App\ProductSub;
use App\User;
use App\User_logs;
use DataTables;
use DB;
use Auth;

class ProductSubController extends Controller
{
	
    public function index()
    {


    }	


    public function store(Request $request)
    {
        
        $prd = Product::find($request['id2']);
        
        $dt = new ProductSub;
        
        $dt->product_id = $request['id2'];
        $dt->satuan     = strtoupper($request['kemas']);
        $dt->volume     = $request['isi'];
        $dt->harga      = $request['harga'];
        $dt->harga_2    = $request['harga2'];
        $dt->harga_lk   = $request['hargalk'];
        $dt->hpp        = $request['hpp'];
        $dt->is_invoice = $request['inv'];
        $dt->save();

	// insert logs
		$usrnm	= Auth::user()->username;
		
		$userlog = New User_logs;
		$userlog->title		= 'Input Detail Product';
		$userlog->content	= 'Detail Product '.$prd->nama_brg.' kemasan '.$request['kemas'].'isi '.$request['isi'].' berhasil ditambah oleh '.$usrnm;
		$userlog->user_id	= Auth::user()->id;
		$userlog->save();


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

    public function edit($id)
    {
        $dt = ProductSub::find($id);
        $prd = Product::find($dt->product_id);
        
        echo json_encode(array_merge(json_decode($prd, true), json_decode($dt, true)));
    }

    public function show($id)
    {

    }

    public function update(Request $request, $id)
    {
        $dt = ProductSub::find($id);
        
        $prd = Product::find($dt->product_id);
        
        $dt->satuan     = strtoupper($request['kemas']);
        $dt->volume     = $request['isi'];
        $dt->harga      = $request['harga'];
        $dt->harga_2    = $request['harga2'];
        $dt->harga_lk   = $request['hargalk'];
        $dt->hpp        = $request['hpp'];
        $dt->is_invoice = $request['inv'];
        $dt->update();

	// insert logs
		$usrnm	= Auth::user()->username;
		
		$userlog = New User_logs;
		$userlog->title		= 'Edit Detail Product';
		$userlog->content	= 'Detail Product '.$prd->nama_brg.' kemasan '.$request['kemas'].'isi '.$request['isi'].' berhasil diedit oleh '.$usrnm;
		$userlog->user_id	= Auth::user()->id;
		$userlog->save();
        
        echo json_encode(['msg'=>'success']);        
    }


}
