<?php

namespace App\Http\Controllers;

use App\Models\User;
use App\Models\Paket;
use App\Models\Parfum;
use App\Models\Pelanggan;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use App\Models\Metode_Pembayaran;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;

class kasirController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $kasir = [];
        $pelanggan = [];
        $parfum = [];
        $paket = [];
        $metode = [];
        $email_pelanggan = '';
        $username_pelanggan = '';
        $password_pelanggan = '';
        // cek apakah ada kasir yang aktif dengan user_id yang sama

        $pegawai = \App\Models\Pegawai::where('users_id', Auth::user()->id)->first();
        $kasir = \App\Models\kasir::where('laundry_id', $pegawai->laundry_id)->where('is_active', 1)->first();
        if ($kasir) {
            $pelanggan = Pelanggan::where('laundry_id', $kasir->laundry_id)->orderBy('id', 'desc')->get();
            $parfum = Parfum::where('laundry_id', $kasir->laundry_id)->get();
            $paket = Paket::where('laundry_id', $kasir->laundry_id)->get();
            $metode = Metode_Pembayaran::where('laundry_id', $kasir->laundry_id)->get();
            $email_pelanggan = 'Pelanggan-' . count($pelanggan) + 1 . '@gmail.com';
            $username_pelanggan = 'Pelanggan-' . count($pelanggan) + 1;
            $password_pelanggan = 'password';
            return view(
                'kasir.index_new',
                compact(
                    'kasir',
                    'pelanggan',
                    'parfum',
                    'paket',
                    'metode',
                    'email_pelanggan',
                    'username_pelanggan',
                    'password_pelanggan'
                )
            );
        }
        return view('kasir.index_new', compact(
            'kasir',
            'pelanggan',
            'parfum',
            'paket',
            'metode',
            'email_pelanggan',
            'username_pelanggan',
            'password_pelanggan'
        ));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

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

        if ($validate->fails()) {
            return redirect()
                ->back()
                ->with('error', $validate->errors()->first());
        }

        $kasir = new \App\Models\kasir();
        $kasir->users_id = Auth::user()->id;
        $kasir->laundry_id = $request->laundry_id;
        $kasir->opening_balance = $request->opening_balance;
        $kasir->tgl_buka = date('Y-m-d');
        $kasir->jam_buka = Carbon::now()->format('H:i:s'); //
        $kasir->is_active = 1;
        // $kasir->keterangan = $request->keterangan;
        $kasir->save();

        return redirect()->route('kasir.index')->with('success', 'Kasir Berhasil Dibuka');
    }

    /**
     * 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)
    {
        //
    }

    public function tutup($id, $total_transaksi, Request $request)
    {
        $kasir = \App\Models\kasir::find($id);
        $kasir->tgl_tutup = date('Y-m-d');
        $kasir->jam_tutup = Carbon::now()->format('H:i:s');
        $kasir->closing_balance = $request->closing_balance;
        $kasir->total_transaksi = $total_transaksi;
        $kasir->is_active = 0;

        $kasir->save();
        return redirect()->route('kasir.index')->with('success', 'Kasir Berhasil Ditutup');
    }

    public function pelanggan_baru(Request $request)
    {

        $validate = Validator::make($request->all(), [
            'username' => 'required',
            'email' => 'required',
            'password' => 'required',
            'nama' => 'required',
            'alamat' => 'required',
            'no_telp'  => 'required',
        ]);

        if ($validate->fails()) {
            return redirect()
                ->back()
                ->with('error', $validate->errors()->first());
        }

        $akun = new User();
        $akun->username = $request->username;
        $akun->email = $request->email;
        $akun->password = bcrypt($request->password);
        $akun->role = 'pelanggan';

        $akun->save();

        $pelanggan = new Pelanggan();
        $pelanggan->users_id = $akun->id;
        $pelanggan->laundry_id = $request->laundry_id;
        $pelanggan->nama = $request->nama;
        $pelanggan->alamat = $request->alamat;
        $pelanggan->no_telp = $request->no_telp;
        $pelanggan->status = 'aktif';
        $pelanggan->no_telp = $request->no_telp;

        if ($request->hasFile('foto')) {
            $file = $request->file('foto');
            $extension = $file->getClientOriginalExtension();
            $filename = 'profile-' . time() . '.' . $extension;
            $file->storeAs('public/img', $filename);
            $pelanggan->foto = $filename;
        } else {
            $pelanggan->foto = 'default.png';
        }
        $pelanggan->save();
        if ($akun && $pelanggan) {
            return redirect()->route('kasir.index')->with('success', 'Pelanggan Berhasil Ditambahkan');
        }
    }
}
