<?php

namespace App\Http\Controllers;

use App\Gadai;
use App\Marhun;
use App\Cawangan;
use App\Transaction;
use App\TransaksiKeuntungan;
use App\TransactionOnline;
use App\batalGadai;
use App\TuntutBaki;
use App\AnsRugi;
use App\Lelong;
use App\WTD;
use App\HapusKira;
use App\Tebus;
use App\Vault;
use App\Tunai;
use App\Komuditi;
use App\KawalanStokLama;
use App\KawalanStok;
use App\KadarUpah;
use App\Moratorium;
use App\Audit;
use App\Customer;
use App\KomuditiPurchase;
use App\KomuditiTransaksi;
use App\Advansur;
use App\BlastWasap;
use App\Notis;
use App\CawanganNotis;
use App\NotisPeringatan;
use App\Jobs\WhatsappPelangganSAGLAMA;
use App\Jobs\SMSPelangganSAGLAMA;
use Illuminate\Http\Request;
use App\CawanganDetail;
use App\BlastSMS;
use App\Jobs\ARCCWhatsappPelangganSAGLAMAbysag;
use App\Jobs\ARCCSMSPelangganSAGLAMAbysag;
use App\Services\MasterServices;
use App\Daerah;
use App\Poskod;
use App\Negeri;
use Log;

use App\Services\OnePercentServices;

use Illuminate\Support\Facades\DB;

use Carbon\Carbon;

class CawanganCallCenterController extends Controller
{
    public function senarainotisperingatan($kodcaw)
    {
        $current = Carbon::now();
        $current = new Carbon();

        $arraybatch = [];
        $batchnotis = CawanganNotis::on('mysql7')
        ->join('notis', 'notis.id', '=', 'cawangan_notis.notis_id')
        ->where('kodcaw',$kodcaw)->get();
        return response()->json($batchnotis);
    }

    public function getcawnotis($kodcaw,$notisid)
    {
        $current = Carbon::now();
        $current = new Carbon();

        $arraybatch = [];
        $batchnotis = Notis::on('mysql7')->where('id',$notisid)->get();
        $cawangan_all = Cawangan::on('mysql7')->get();
        $cawnotis_all = [];
        foreach ($batchnotis as $index => $cawangan){
            $notis = CawanganNotis::on('mysql7')
            ->join('arrahn_master_db', 'arrahn_master_db.kodcaw', '=', 'cawangan_notis.kodcaw')
            ->where('notis_id',$notisid)->where('cawangan_notis.kodcaw',$kodcaw)->get();
            if(sizeof($notis) != 0){
                array_push($cawnotis_all,$notis);
            }
        }
        return response()->json($cawnotis_all);
    }

    public function getcawnotisall(Request $request)
    {
        $kodcaw = $request->kodcaw;
        $current = Carbon::now();
        $current = new Carbon();

        $arraybatch = [];
        $cawangan_all = Cawangan::on('mysql7')->get();
        $cawnotis_all = [];

        $notis = CawanganNotis::on('mysql7')
            ->join('notis', 'notis.id', '=', 'cawangan_notis.notis_id')
            ->join('arrahn_master_db', 'arrahn_master_db.kodcaw', '=', 'cawangan_notis.kodcaw')
            ->where('cawangan_notis.kodcaw',$kodcaw)->get();
        foreach ($notis as $index => $cawangan){
            // dd($cawangan['kodcaw']);
                // dd($cawangan);
                $notisbycaw = NotisPeringatan::on('mysql7')
                ->where('notisperingatan.notis_id',$cawangan['notis_id'])
                ->where('notisperingatan.kodcaw',$cawangan['kodcaw'])
                // ->where('notisperingatan.tarikhnotis1','=', NULL)
                ->distinct()
                ->get();

                $notispertamabycaw = NotisPeringatan::on('mysql7')
                ->where('notisperingatan.notis_id',$cawangan['notis_id'])
                ->where('notisperingatan.kodcaw',$cawangan['kodcaw'])
                ->where('notisperingatan.peringatan','=', 1)
                ->distinct()
                ->get();

                $notiskeduabycaw = NotisPeringatan::on('mysql7')
                ->where('notisperingatan.notis_id',$cawangan['notis_id'])
                ->where('notisperingatan.kodcaw',$cawangan['kodcaw'])
                ->where('notisperingatan.peringatan','=', 2)
                ->distinct()
                ->get();
                
                $totalsag = $notisbycaw->count('norujukan');
                $totalsagpertama = $notispertamabycaw->count('norujukan');
                $totalsagkedua = $notiskeduabycaw->count('norujukan');
                $totalbakipjm = $notisbycaw->sum('pinjaman');
                
                // if(sizeof($notis) != 0){
                    array_push($cawnotis_all,['namacaw' => $notis, 'detailbycaw' => $notisbycaw,'totalsag' =>$totalsag,'totalsagpertama' =>$totalsagpertama,'totalsagkedua' =>$totalsagkedua,'totalbakipjm' =>$totalbakipjm]);
                    // }
                }
                // dd($cawnotis_all);
        return response()->json($cawnotis_all);
    }

    public function bycawnotisperingatan($kodcaw,$notisid)
    {
        $cawnotis_all = [];
        $cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$kodcaw)->first();
        $notisbycaw = NotisPeringatan::on('mysql7')
        ->where('notisperingatan.notis_id',$notisid)
        ->where('notisperingatan.kodcaw',$kodcaw)
        // ->where('notisperingatan.peringatan', '=', 1)
        ->distinct()
        ->get();

        $notisbycawarray = $notisbycaw->toArray();

        foreach($notisbycawarray as $index=>$cal){
            $customer =  Customer::where('kpbaru','=',$cal['nokp'])
            ->orWhere('kplama','=',$cal['nokp'])
            ->first();

            $datagadai =  Gadai::on($cawangan['mysql'])
            ->where('norujukan','=',$cal['norujukan'])
            ->first();
            // dd( $datagadai);

            if($datagadai->tagbaru == '0'){
                $keuntungan = 'asal';
                $jeniskeuntungan = ($keuntungan == 'onepercent') ? 'onepercent' : 'asal';
                $master = new MasterServices($kodcaw);
                $response = $master->kiraUpahTawarruq($kodcaw,$cal['norujukan'],$jeniskeuntungan);
            }elseif($datagadai->tagbaru == '1'){
                $keuntungan = 'onepercent';
                $jeniskeuntungan = ($keuntungan == 'onepercent') ? 'onepercent' : 'asal';
                $master = new MasterServices($kodcaw);
                $response = $master->kiraupahUjrahOnePercent($kodcaw,$cal['norujukan']);
            }
            
            $notisbycawarray[$index]['namacust'] = $customer;
            $notisbycawarray[$index]['datagadai'] = $datagadai;
            $notisbycawarray[$index]['keuntungan'] = $response;
        }

        array_push($notisbycawarray);
        return response()->json($notisbycawarray);
    }
}
