<?php

namespace App\Http\Controllers;
use App\Cawangan;
use App\DenoPendSer;
use App\GlDetail;


use Carbon\Carbon;

use Illuminate\Http\Request;

class PendahuluanSerahanController extends Controller
{

   public function createDenoPendSer($kodcaw,Request $request){
       $current = Carbon::now();
       $current = new Carbon();

       $denopendser = DenoPendSer::on('mysql7')->create([
            'tarikh' => $current->toDateString(),
            'masa' => $current->toTimeString(),
            'kodcaw' => $kodcaw,
            'kodlaluan' => $request->kodlaluan,
            'amaun' => $request->amaun,
            'bd1' => $request->bd1,
            'bd5' => $request->bd5,
            'bd10' => $request->bd10,
            'bd20' => $request->bd20,
            'bd50' => $request->bd50,
            'bd100' => $request->bd100,
            'txtring1' => $request->txtring1,
            'txtring5' => $request->txtring5,
            'txtring10' => $request->txtring10,
            'txtring20' => $request->txtring20,
            'txtring50' => $request->txtring50,
            'txtring100' => $request->txtring100,
            'txtring1000' => $request->txtring1000,
            'txtsen1' => $request->txtsen1,
            'txtsen5' => $request->txtsen5,
            'txtsen10' => $request->txtsen10,
            'txtsen20' => $request->txtsen20,
            'txtsen50' => $request->txtsen50,
            'jenis' =>$request->jenis,
            'recno' =>$request->recno,
            'update' => '0'
       ]);

       return response()->json($denopendser,200);
   }

   public function getDenoPendSer($kodcaw,Request $request){
       $current = Carbon::now();
       $current = new Carbon();


       $denopendser = DenoPendSer::on('mysql7')->where('kodcaw','=', $kodcaw)->where('recno','=', $request->recno)->get();

       return response()->json($denopendser,200);
   }

   public function updateDenoPendSer($kodcaw,Request $request){
       $current = Carbon::now();
       $current = new Carbon();


       $denopendser = DenoPendSer::on('mysql7')
                     ->where('recno','=', $request->recno)
                     ->update(array(
                        'pelulus' => $request->pelulus,
                        'masalulus'=> $harini->toTimeString()
                     ));

       return response()->json($denopendser,200);
    }

    public function delete(REQUEST $request,$kodcaw){
        $cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$kodcaw)->first();

        GlDetail::on($cawangan['mysql'])->where('recno','=',$request->recno)->delete();

        return response('Batal Berjaya', 200);
    }

    public function listpendahuluanserahan($kodcaw,Request $request)
    {
       $cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$kodcaw)->first();
 
       if($request->jenis == 'pendahuluan')
       {
            $gldetail=GlDetail::on($cawangan['mysql'])
                ->where('kodcaw','=',$kodcaw)
                ->where([['tarikh','>=',$request->mula],['tarikh','<=',$request->hingga]])
                ->where('pendahuluan','>',0)
                ->orderby('tarikh','desc')
                ->get();
        }else if($request->jenis == 'serahan'){
                $gldetail = GlDetail::on($cawangan['mysql'])
                    ->where('kodcaw', '=', $kodcaw)
                    ->whereBetween('tarikh', [$request->mula, $request->hingga])
                    ->where(function ($query) {
                        $query->where('descpt', 'NOT LIKE', 'SERAHAN BAYAR ONLINE%')
                              ->where('descpt', 'NOT LIKE', 'SERAHAN LELONG%'); // Using AND instead of OR
                    })
                    ->where('serahan', '>', 0)
                    ->orderBy('tarikh', 'desc')
                    ->get();
 
        }else if($request->jenis == 'online'){
            $gldetail=GlDetail::on($cawangan['mysql'])
                ->where('kodcaw','=',$kodcaw)
                ->where([['tarikh','>=',$request->mula],['tarikh','<=',$request->hingga]])
                ->where('descpt','LIKE','SERAHAN BAYAR ONLINE%')
                ->where('serahan','>',0)
                ->orderby('tarikh','desc')
                ->get();
        }else if($request->jenis == 'serahanlelong'){
            $gldetail=GlDetail::on($cawangan['mysql'])
                ->where('kodcaw','=',$kodcaw)
                ->where([['tarikh','>=',$request->mula],['tarikh','<=',$request->hingga]])
                ->where('descpt','LIKE','SERAHAN LELONG%')
                ->where('serahan','>',0)
                ->orderby('tarikh','desc')
                ->get();
        }else if($request->jenis == 'serahankepadav3'){
            $gldetail=GlDetail::on($cawangan['mysql'])
                ->where('kodcaw','=',$kodcaw)
                ->where([['tarikh','>=',$request->mula],['tarikh','<=',$request->hingga]])
                ->where('descpt','LIKE','Serahan Kepada V3%')
                ->where('teller','LIKE','%AUTO%')
                ->where('serahan','>',0)
                ->orderby('tarikh','desc')
                ->get();

        }else if($request->jenis == 'serahanonline'){
            $gldetail = GlDetail::on($cawangan['mysql'])
                ->where('kodcaw', '=', $kodcaw)
                ->whereBetween('tarikh', [$request->mula, $request->hingga])
                ->where(function ($query) {
                    $query->where('descpt', 'LIKE', 'SERAHAN%')
                        ->orWhere('descpt', 'LIKE', 'Serahan%')
                        ->orWhere('descpt', 'LIKE', 'serahan%'); // Add more variations if needed
                })
                ->where('serahan', '>', 0)
                ->orderBy('tarikh', 'desc')
                ->get();
        }else{
            $gldetail = GlDetail::on($cawangan['mysql'])
                ->where('kodcaw', '=', $kodcaw)
                ->whereBetween('tarikh', [$request->mula, $request->hingga])
                ->orderBy('tarikh', 'desc')
                ->get();
        }
      
 
       return response()->json($gldetail,200);
    }
}
