<?php

namespace App\Http\Controllers;

use App\Transaction;
use App\TransaksiKeuntungan;
use App\Pengguna;
use App\Cawangan;
use App\Advansur;

use Illuminate\Http\Request;

use Carbon\Carbon;
use DB;

class TransactionController extends Controller
{
    public function duitMasuk(Request $request){
        $current = Carbon::now();
        $current = new Carbon();

        $transaction = Transaction::on('mysql7')->create([
            'trans_type' => $request->trans_type,
            'norujukan' => $request->norujukan,
            'nokp' => $request->nokp,
            'teller' => $request->teller,
            'kodcaw' => $request->kodcaw,
            'duit_masuk' => $request->duit_masuk,
            'created_at' => $current,
        ]);

        return response()->json($transaction,201);
    }

    public function duitKeluar(Request $request){
        $current = Carbon::now();
        $current = new Carbon();

        $transaction = Transaction::on('mysql7')->create([
            'trans_type' => $request->trans_type,
            'norujukan' => $request->norujukan,
            'nokp' => $request->nokp,
            'teller' => $request->teller,
            'kodcaw' => $request->kodcaw,
            'duit_keluar' => $request->duit_keluar,
            'created_at' => $current,
        ]);

        return response()->json($transaction,201);
    }

    public function showAllTransaction(){
        $transaction = Transaction::on('mysql7')->get();

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

    public function getTransactionByRujukan($norujukan){
        
        $transactions = Transaction::on('mysql7')
            ->where('norujukan', $norujukan)
            ->where('trans_type', 'A')
            ->get();

        foreach ($transactions as $transaction) {
            $createdDate = Carbon::parse($transaction->created_at)->toDateString();

            $keuntungan = \DB::connection('mysql7')
                ->table('transaksi_keuntungan')
                ->whereDate('tarikh_bayaran', $createdDate)
                ->where('norujukan', $transaction->norujukan)
                ->first(); // ambil seluruh rekod

            $transaction->bayaran_keuntungan = number_format($keuntungan->bayaran_keuntungan ?? 0, 2);
            $transaction->bayaran_pembiayaan = number_format($keuntungan->bayaran_pembiayaan  ?? 0, 2);
        }
        return response()->json($transactions);
    }

    public function getTransactionByid($id){
        $transaction = Transaction::on('mysql7')
        ->leftJoin('transaksi_keuntungan as tk', function($join){
            $join->on('tk.norujukan', '=', 'transaction.norujukan');
            $join->on('tk.tarikh_bayaran', '=', 'transaction.created_at');
        })
        ->where('transaction.id','=',$id)->get();

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

    public function getTransactionByRujukanOne($norujukan){
        
        $transaction = Transaction::on('mysql7')
        ->leftJoin('transaksi_keuntungan as tk', function($join){
            $join->on('tk.norujukan', '=', 'transaction.norujukan');
            $join->on('tk.tarikh_bayaran', '=', 'transaction.created_at');
        })
        ->where('transaction.norujukan','=',$norujukan)
        ->orderBy('transaction.created_at','desc')->first();

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

    public function getTotalBayaranAnsuran(Request $request){
        $transaction = Transaction::on('mysql7')
        ->leftJoin('transaksi_keuntungan as tk', function($join){
            $join->on('tk.norujukan', '=', 'transaction.norujukan');
            $join->on('tk.tarikh_bayaran', '=', 'transaction.created_at');
        })
        ->where('transaction.norujukan','=',$request->norujukan)
        ->where('transaction.trans_type','A')
        ->sum('tk.bayaran_pembiayaan');

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

    public function GetPengguna($kodcaw,Request $request)
    {
        $namateller = $request->namateller;

        $cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$kodcaw)->first();
        $pengguna=Pengguna::on($cawangan['mysql'])
                ->where('nama','=',$namateller)->limit(1)->get();

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

    public function getSumBayaran_Ansuran($kodcaw,$norujukan)
    {

        $current = Carbon::now();
        $current = new Carbon();
        $tarikh = $current->toDateString();

        $transaction=DB::connection('mysql7')->select("SELECT  SUM(duit_masuk) AS cash_in  FROM transaction WHERE DATE(created_at)='".$tarikh."' AND kodcaw='".$kodcaw."' AND norujukan ='".$norujukan."'" );     

        return response()->json($transaction);

    }

    public function getTransactionByRujukanAll($norujukan){
        
        $transaction = Transaction::on('mysql7')->where('norujukan','=',$norujukan)->get();

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

    public function TransaksiPelanggan(Request $request){

        $transaction = TransaksiKeuntungan::on('mysql7')
            ->leftJoin('transaction as trans', function($join)
                {
                    $join->on('transaksi_keuntungan.norujukan', '=', 'trans.norujukan');
                    $join->on('transaksi_keuntungan.tarikh_bayaran', '=', 'trans.created_at');
                })
            ->where('trans.norujukan','=',$request->norujukan)
            ->get();

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

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

        $listadvansur = Advansur::on($cawangan['mysql'])
        ->where('norujukan','=',$request->norujukan)
        ->orderBy('tarikh','desc')
        ->get();

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