<?php

namespace App\Http\Controllers\master;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use Yajra\Datatables\Datatables;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Session;

class HargaController extends Controller
{
    //
    public function __construct(){      
        $this->middleware(function ($request, $next) {
        $url = request()->segment(1);
        $kode_group = Session::get('kode_group');
        $d_menu = DB::table("tb_menu as a")->join("tb_role as b","a.kode_menu","b.kode_menu")->where([['a.link_menu',$url],['b.kode_group',$kode_group],['b.status_role','1'],['a.active','1']])->orderBy('a.sort_menu','asc')->count();
        if(Session::get('decode') <> 1)
         {
            Session::flash('message', 'Silahkan login dengan username dan password anda.!!');
             return redirect()->to('login');
         }
         if($d_menu == 0){
            Session::flush();
            Session::flash('message', 'Anda Tidak Punya Akses ke Halaman ini.!!');
             return redirect()->to('login');
         }
        return $next($request); 
        });
    }

    function index(){
        return view('master.HargaList');
    }

    function get_data(){
        $d_data = DB::table("view_hargabarang as v")->select(DB::raw("v.*, DATE_FORMAT(v.tanggal, '%d-%m-%Y') as tgl_input"))->get();

        $arr = array();

        return Datatables::of($d_data)
                
                ->addColumn("aksi", function($d_data){
                    $button = "<div class='btn-group'>";
                        $button .= '<button type="button" class="btn btn-sm btn-warning" onclick="get_edit('.$d_data->id_harga.')"><i class="icon-pencil"></i> </button>';
                        $button .= "<button type='button' class='btn btn-sm btn-danger' onclick='hapus(".$d_data->id_harga.")'><i class='fa fa-trash'></i></button>";
                    $button .= "</div>";
                    return $button;
                })
                ->make("true");
    }

    function get_edit(Request $request){
        $id = $request->get('id');

        $d_data = DB::table("view_hargabarang")->where("id_harga", $id);
        $c_data = $d_data->count();

        $arr = array();

        if($c_data > 0){
            $arr['data'] = $d_data->get()->first();
            $arr['data']->tgl = tgl_full($d_data->get()->first()->tanggal, '');
            $arr['status'] = "1";
        }else{
            $arr["status"] = "0";
        }

        return response()->json($arr);
    }

    function simpan(Request $request){
        $req = $request->all();

        $id = $req['id'];
        $data['tanggal'] = tgl_full($req['popup_tanggal'], '2');
        $data['id_barang'] = $req['popup_barang'];
        $data['id_corak'] = $req['popup_corak'];
        $data['id_motif'] = $req['popup_motif'];
        $data['id_satuan'] = $req['popup_satuan'];
        $data['id_proses'] = $req['popup_proses'];
        
        // $data['id_warna'] = $req['popup_warna'];
        
        $id_data = "";
        $respon = array();
        if($id == ''){
            $d_exist = DB::table("ms_hargabarang")->where($data)->get();
            $d_count = $d_exist->count();

            if($d_count > 0){
                $respon = ["status" => 0, "keterangan" => "Data sudah tersedia, silahkan periksa data anda"];
            }else{
                $data['harga'] = $req['popup_harga'];
                $id_data = DB::table("ms_hargabarang")->insertGetId($data);
                $respon = ["status" => 1, "keterangan" => "Data Tersimpan"];
            }
        }else{
            $d_exist = DB::table("ms_hargabarang")->where($data)->where("id", "!=", $id)->get();
            $d_count = $d_exist->count();
            if($d_count > 0){
                $respon = ["status" => 0, "keterangan" => "Data sudah tersedia, silahkan periksa data anda"];
            }else{
                $data['harga'] = $req['popup_harga'];
                DB::table('ms_hargabarang')->where(array('id' => $id))->update($data);
                $respon = ["status" => 1, "keterangan" => "Data Tersimpan"];
            }
        }

        return response()->json($respon);
    }

    function hapus(Request $request){
        $data['id'] = $request->get('id');
        DB::table('ms_hargabarang')->where($data)->delete();
    }

    function get_proses(){
        $d_data = DB::table('ms_proses')->where('parent', '!=', '0');
        if($d_data->count() > 0){
            $arr['data'] = $d_data->get();
            $arr['status'] = array('1');
        }else{
            $arr['status'] = array('0');
        }

        return response()->json($arr);
    }

    function get_motif(){
        $d_data = DB::table('ms_motif')->orderBy('nama_motif', 'asc');
        if($d_data->count() > 0){
            $arr['data'] = $d_data->get();
            $arr['status'] = array('1');
        }else{
            $arr['status'] = array('0');
        }

        return response()->json($arr);
    }

    function get_corak(){
        $d_data = DB::table('ms_motif_barang')->orderBy('motif_keterangan', 'asc');
        if($d_data->count() > 0){
            $arr['data'] = $d_data->get();
            $arr['status'] = array('1');
        }else{
            $arr['status'] = array('0');
        }

        return response()->json($arr);
    }

    function get_satuan(){
        $d_data = DB::table('ms_satuan')->orderBy('nama_satuan', 'asc');
        if($d_data->count() > 0){
            $arr['data'] = $d_data->get();
            $arr['status'] = array('1');
        }else{
            $arr['status'] = array('0');
        }

        return response()->json($arr);
    }

    function get_barang(){
        DB::statement(DB::raw('set @rownum=0'));
        $d_data = DB::table('ms_barang AS mb')->join('ms_kategori_barang AS mkb', 'mb.id_kb', 'mkb.id_kb')->join('ms_kategori_barang AS mkb2', 'mkb.induk_kb', 'mkb2.id_kb')->join('ms_satuan AS ms', 'mb.id_satuan', 'ms.id_satuan')->select(DB::raw('mb.*, mkb2.id_kb AS id_kategori, mkb2.nama nama_kategori, mkb.id_kb id_sub, mkb.nama nama_sub, ms.id_satuan, ms.nama_satuan'));
        if($d_data->count() > 0){
            $arr['data'] = $d_data->orderBy('nama_kategori', 'asc')->orderBy('nama_sub', 'asc')->orderBy('kode_barang', 'asc')->orderBy('nama_barang', 'asc')->get();
            $arr['status'] = array('1');
        }else{
            $arr['data'] = array();
            $arr['status'] = array('0');
        }

        return response()->json($arr);
    }

    function get_warna(){
        $d_data = DB::table('ms_warnakomposisi')->orderBy('nama', 'asc');
        if($d_data->count() > 0){
            $arr['data'] = $d_data->get();
            $arr['status'] = array('1');
        }else{
            $arr['status'] = array('0');
        }

        return response()->json($arr);
    }
}