<?php

namespace App\Http\Controllers;

use App\Audit;
use App\Polis;
use App\Cawangan;
use App\LookupAlasan;
use Illuminate\Http\Request;

use Carbon\Carbon;

class PolisController extends Controller
{
    public function getPolis(Request $request){

        $cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$request->kodcaw)->first();

        $polis = Polis::on($cawangan['mysql'])
                 ->where('norujukan','=',$request->norujukan)
                 ->get();
        
        return response()->json($polis);
    }

    public function PolischangeTebus(Request $request){

        $cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$request->kodcaw)->first();

        $polis = Polis::on($cawangan['mysql'])
                 ->where('norujukan','=',$request->norujukan)
                 ->update(['tebus' => $request->tebus]);
        
        return response()->json($polis);
    }

    public function createPolis(Request $request)
    {
        $harini = Carbon::now();
        $harini = new Carbon;
  
        $cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$request->kodcaw)->first();
        
        $polis = Polis::on($cawangan['mysql'])
                 ->create([ 
                    'tarikh' => $harini->toDateString(),
                    'kodcaw' => $request->kodcaw,
                    'norujukan' => $request->norujukan,
                    'komen1' => $request->komen1,
                    'komen2' => $request->komen2,
                    'tebus' => 0,
                    'teller' => $request->teller,
                    'caj' => 0.00,
                    'kodnegeri' => $request->kodnegeri,
                    'koddaerah' => $request->koddaerah
        ]);

        $audit = new Audit();
        $audit->norujukan = $request->norujukan;
        $audit->users = $request->teller;
        $audit->actions = "Cetak SAG Hilang";
        $audit->new_data = json_encode($polis);
        $audit->kodcaw = $request->kodcaw;
        $audit->created_at =  $harini;
        $audit->updated_at =  $harini;
        $audit->save();
        
        return response()->json($polis);

    }

    public function ulangcetakPolis(Request $request){

        $harini = new Carbon();

        $audit = new Audit();
        $audit->norujukan = $request->norujukan;
        $audit->users = $request->teller;
        $audit->actions = "Ulang Cetak SAG Hilang";
        $audit->new_data = null;
        $audit->kodcaw = $request->kodcaw;
        $audit->created_at =  $harini;
        $audit->updated_at =  $harini;
        $audit->save();

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

    public function editPolis(Request $request)
    {
        $harini = Carbon::now();
        $harini = new Carbon;
  
        $cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$request->kodcaw)->first();
        
        $polis = Polis::on($cawangan['mysql'])
                ->where('norujukan','=',$request->norujukan)
                 ->update([ 
                    'komen1' => $request->komen1,
                    'komen2' => $request->komen2,
                    'teller' => $request->teller,
                    'kodnegeri' => $request->kodnegeri,
                    'koddaerah' => $request->koddaerah
                ]);
        
        return response()->json($polis);

    }

    public function LookupAlasan(Request $request)
    {
        $harini = Carbon::now();
        $harini = new Carbon;
  
        
        $alasan = LookupAlasan::on('mysql7')
                ->get();
        
        return response()->json($alasan);

    }

    public function LaporanPolis(Request $request){

    // $request->validate([
    //     'kodcaw' => 'required|string',
    //     'tarikh_mula' => 'required|date_format:Y-m-d',
    //     'tarikh_akhir' => 'required|date_format:Y-m-d',
    //     'alasan' => 'nullable|string'
    // ]);
    
        $cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$request->kodcaw)->first();

        // dd($cawangan);
        if(!$cawangan){
            return response()->json(['error' => 'Invalid kodcaw'], 403);
        }

        if($request->alasan == 'all'){
            $polis = Polis::on($cawangan['mysql'])
            ->select('gadai.sttebus','gadai.t_update','gadai.norujukan','gadai.nokp','polis.komen1','polis.komen2','polis.tarikh','polis.teller','polis.caj','polis.kodnegeri','polis.koddaerah','polis.tebus')
            ->join('gadai','gadai.norujukan','=','polis.norujukan')
            ->whereBetween('polis.tarikh', [$request->tarikh_mula, $request->tarikh_akhir])
            ->get();
        }else{
            $polis = Polis::on($cawangan['mysql'])
            ->select('gadai.sttebus','gadai.t_update','gadai.norujukan','gadai.nokp','polis.komen1','polis.komen2','polis.tarikh','polis.teller','polis.caj','polis.kodnegeri','polis.koddaerah','polis.tebus')
            ->join('gadai','gadai.norujukan','=','polis.norujukan')
            ->where('komen1','=',$request->alasan)
           //  ->where('tarikh','=',$request->tarikh)
            ->whereBetween('polis.tarikh', [$request->tarikh_mula, $request->tarikh_akhir])
            ->get();
        }
          
        return response()->json($polis);
    }
    
}