<?php

namespace App\Http\Controllers;

use App\Models\Admin;
use App\Models\Barang;
use App\Models\Laundry;
use App\Models\Pegawai;
use App\Models\KepalaToko;
use App\Models\Metode_Pembayaran;
use Illuminate\Http\Request;
use App\Models\PembelianBarang;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;

class pembelianBahanBakuController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        if (Auth::user()->role == 'superadmin') {
            $data = PembelianBarang::orderBy('id', 'desc')->get();
            $laundry = Laundry::all();
            $barang = Barang::where('is_bahan_baku', 1)->get();
            $metode = Metode_Pembayaran::all();
        } elseif (Auth::user()->role == 'admin') {
            $akun = Auth::user()->id;
            $admin = Admin::where('users_id', $akun)->first();
            $admin_brand = $admin->brand_id;
            $laundry = Laundry::where('brand_id', $admin_brand)->get();
            $data = PembelianBarang::join('laundry', 'laundry.id', '=', 'pembelian_barang.id_laundry')
                ->where('laundry.brand_id', $admin_brand)
                ->get();
            $barang = Barang::join('laundry', 'laundry.id', '=', 'barang.id_laundry')
                ->where('laundry.brand_id', $admin_brand)
                ->where('is_bahan_baku', 1)->select('barang.*')->get();
            $metode = Metode_Pembayaran::join('laundry', 'laundry.id', '=', 'metode_pembayaran.laundry_id')
                ->where('laundry.brand_id', $admin_brand)->get();
        } else {
            if (Auth::user()->role == 'pegawai') {
                $akun = Auth::user()->id;
                $pegawai = Pegawai::where('users_id', $akun)->first();
                $laundry_id = $pegawai->laundry_id;
                $laundry = Laundry::where('id', $laundry_id)->get();
                $data = PembelianBarang::where('id_laundry', $laundry_id)
                    ->get();
                $barang = Barang::where('id_laundry', $laundry_id)
                    ->where('is_bahan_baku', 1)->get();
                $metode = Metode_Pembayaran::where('laundry_id', $laundry_id)->get();
            } else {
                $akun = Auth::user()->id;
                $kepala_toko = KepalaToko::where('users_id', $akun)->first();
                $laundry_id = $kepala_toko->laundry_id;
                $laundry = Laundry::where('id', $laundry_id)->get();
                $data = PembelianBarang::where('id_laundry', $laundry_id)

                    ->orderBy('id', 'desc')->get();
                $barang = Barang::where('id_laundry', $laundry_id)
                    ->where('is_bahan_baku', 1)->get();
                $metode = Metode_Pembayaran::where('laundry_id', $laundry_id)->get();
            }
        }
        return view('pembelian_bahan.bahanbaku.index', compact('data', 'laundry', 'barang', 'metode'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        // ajax request to get data barang

    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // dd($request->all());
        $validasi = Validator::make($request->all(), [
            'id_laundry' => 'required',
            'barang_id' => 'required',
            'qty' => 'required',
            'total_harga' => 'required',
        ]);

        if ($validasi->fails()) {
            return redirect()->back()->with('error', $validasi->errors()->first());
        } else {
            $data = new pembelianBarang();
            $data->tgl_beli = Carbon::now();
            $data->id_laundry = $request->id_laundry;
            $data->barang_id = $request->barang_id;
            $data->qty = $request->qty;
            $data->satuan = $request->satuan;
            $data->total_harga = $request->total_harga;
            $data->catatan = $request->catatan;
            $data->is_cash = $request->is_cash;
            $data->metode_bayar_id = $request->metode_bayar_id;
            // Konversi satuan berdasarkan satuan barang
            if ($request->satuan == 'kg') {
                // Jika satuan kg, konversi ke gram dan bagi total harga dengan jumlah dalam gram
                $data->satuan_susut = 'gram';
                $data->harga_susut = ($request->total_harga / $request->qty) / 1000;
                $volume_susut = $request->qty * 1000;  // Harga per gram
            } elseif ($request->satuan == 'liter') {
                // Jika satuan liter, konversi ke ml dan bagi total harga dengan jumlah dalam ml
                $data->satuan_susut = 'ml';
                $data->harga_susut = ($request->total_harga / $request->qty) / 1000;
                $volume_susut = $request->qty * 1000;   // Harga per ml
            } elseif ($request->satuan == 'pcs') {
                // Jika satuan pcs, tetap harga per pcs
                $data->satuan_susut = 'pcs';
                $data->harga_susut = $request->total_harga / $request->qty;
                $volume_susut = $request->qty;  // Harga per pcs
            } elseif ($request->satuan == 'meter') {
                // Jika satuan meter, tetap harga per meter
                $data->satuan_susut = 'meter';
                $data->harga_susut = $request->total_harga / $request->qty;
                $volume_susut = $request->qty;  // Harga per meter
            } else {
                // Jika satuan tidak dikenali, beri nilai default
                $data->satuan_susut = '-';
                $data->harga_susut = 0;
                $volume_susut = 0;
            }

            // update stok bahan baku
            $data_barang = Barang::where('id', $request->barang_id)->first();

            // volume awal bahan baku
            $data->volume_awal = $data_barang->volume_total;
            $data->volume_akhir = $data_barang->volume_total + $volume_susut;
            $data_barang->volume_total = $data->volume_akhir;
            $data_barang->satuan = $data->satuan_susut;


            $data->save();
            $data_barang->save();
            return redirect()->back()->with('success', 'Data Pembelian Barang Berhasil Ditambahkan');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
