<?php

namespace App\Http\Controllers\Tools;

use App\Http\Controllers\Controller;
use App\Models\TransaksiStiker;
use App\Services\MakeStikerServices;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class StikerExtendedController extends Controller
{
    
    // protected function ;
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $jenisLangganan = DB::table('jenis_langganan')->get();

        // dd($jenisLangganan);
        return view ('tools.stiker-extend', compact('jenisLangganan'));
    }

    /**
     * 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, MakeStikerServices $makeStiker)
    {
         $request->validate([
                'jenis_langganan'   => 'required|array|min:1',
                'jenis_langganan.*' => 'string',
                'tanggal_mulai'     => 'required|date',
                'tanggal_selesai'   => 'required|date|after:tanggal_mulai',
            ]);

        $blnKemarin = Carbon::now()->subMonth()->format('Y-m');
        $start = Carbon::now()->subMonth()->startOfMonth();
        $end   = Carbon::now()->subMonth()->endOfMonth();
        $tanggalMulai   = Carbon::parse($request->tanggal_mulai);
        $tanggalSelesai = Carbon::parse($request->tanggal_selesai);
        $sticker = TransaksiStiker::select('notrans')
        ->whereIn('jenis_langganan', $request->jenis_langganan)
        // ->whereRaw("to_char(tgl_edited, 'YYYY-MM') = ?", [$blnKemarin])
        ->whereBetween('tgl_edited', [$start, $end])
        ->get();
        // $sticker = DB::table('mergetransaksistikerdetail')->whereIn('jenis_langganan', $request->jenis_langganan)->get();
        $jumlah = $sticker->count();
        foreach ($sticker as $row) {
            // $makeStiker->stikerExtend($sticker);
            $makeStiker->stikerExtend(
                $row,
                $tanggalMulai,
                $tanggalSelesai
            );
        }

        return response()->json([
            'status' => true,
            'message' => 'Stiker berhasil diproses',
            'jumlah'  => $jumlah,
        ]);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id, Request $request)
    {
        Log::info($id);
        $start = Carbon::now()->subMonth()->startOfMonth();
        $end   = Carbon::now()->subMonth()->endOfMonth();
        Log::info($start);
        Log::info($end);

        $blnKemarin = Carbon::now()->subMonth()->format('Y-m');
        Log::info($blnKemarin);
        // $selected = $request->pilihannya;
        $selected = $id;
        $count = DB::table('transaksi_stiker')
            ->where('jenis_langganan', $selected)
            // ->whereRaw("to_char(tgl_edited, 'YYYY-MM') = ?", [$blnKemarin])
            ->whereBetween('tgl_edited', [$start, $end])
            ->get();
        
        $jumlah = $count->count();
        
        return response()->json([
            'jumlah' => $jumlah
        ]);
    }

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