<?php

namespace Modules\Lpj\Http\Controllers;

use Carbon\Carbon;
use App\Http\Controllers\Controller;
use Barryvdh\DomPDF\Facade\Pdf; // https://github.com/barryvdh/laravel-dompdf
use Exception;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
// use Modules\Lpj\Exports\TujuanPenilaianExport;
// use Modules\Lpj\Http\Requests\TujuanPenilaianRequest;
// use Modules\Lpj\Models\TujuanPenilaian;
use Modules\Lpj\Models\PenawaranTender;
use Illuminate\Support\Facades\DB;
use Modules\Lpj\Models\Permohonan;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Auth;

    class SpkController extends Controller
    {
        public $user;

        public function index()
        {
            return view('lpj::spk.index');
        }

        public function dataForDatatables(Request $request)
        {
            if (is_null($this->user) || !$this->user->can('debitur.view')) {
                //abort(403, 'Sorry! You are not allowed to view users.');
            }

            // Retrieve data from the database
            // $query =Permohonan::query()->with(['penawaran','penawaran.tujuanPenilaianKjpp'])->where('permohonan.status','=','spk');
            // $data = $query->with(['user', 'debiture', 'branch', 'tujuanPenilaian','penilaian'])->get();
            //$query =Permohonan::query()->with(['user', 'debiture', 'branch', 'tujuanPenilaian','penilaian','penawaran','penawaran.tujuanPenilaianKjpp'])->where('permohonan.status','=','spk');

            $query = PenawaranTender::query()->with(['permohonan.user', 'permohonan.debiture', 'permohonan.branch', 'permohonan.tujuanPenilaian','permohonan.penilaian','tujuanPenilaianKjpp']);


            // Apply search filter if provided
            if ($request->has('search') && !empty($request->get('search'))) {
                $search = $request->get('search');
                $query->where(function ($q) use ($search) {
                    $q->whereRelation('permohonan','nomor_registrasi', 'LIKE', '%' . $search . '%');
                    //$q->orWhere('tanggal_permohonan', 'LIKE', '%' . $search . '%');
                    //$q->orWhere('status', 'LIKE', '%' . $search . '%');
                });
            }

            // Apply sorting if provided
            if ($request->has('sortOrder') && !empty($request->get('sortOrder'))) {
                $order  = $request->get('sortOrder');
                $column = $request->get('sortField');
                $query->orderBy($column, $order);
            }

            // Get the total count of records
            $totalRecords = $query->count();

            // Apply pagination if provided
            if ($request->has('page') && $request->has('size')) {
                $page   = $request->get('page');
                $size   = $request->get('size');
                $offset = ($page - 1) * $size; // Calculate the offset

                $query->skip($offset)->take($size);
            }

            // Get the filtered count of records
            $filteredRecords = $query->count();

            // Get the data for the current page
            //$data = $query->with(['user', 'debiture', 'branch', 'tujuanPenilaian'])->get();

            $data = $query->get();

            $i=0;
            foreach($data as $obj)
            {
                // tanggal_permohonan
                if ($obj->permohonan->tanggal_permohonan) {
                    $data[$i]->permohonan->tanggal_permohonan = Carbon::parse($obj->permohonan->tanggal_permohonan)->format('d M Y');
                }

                if($obj->tanggal_penilaian_sebelumnya)
                {
                    $data[$i]->tanggal_penilaian_sebelumnya = Carbon::parse($obj->tanggal_penilaian_sebelumnya)->format('d F Y H:i:s');
                }

                if($obj->biaya_kjpp_sebelumnya)
                {
                    $data[$i]->biaya_kjpp_sebelumnya = formatRupiah($obj->biaya_kjpp_sebelumnya);

                }

                // date_range
                if($obj->start_date && $obj->end_date)
                {
                    $data[$i]->date_range = Carbon::parse($obj->start_date)->format('d M Y').' - '.
                                            Carbon::parse($obj->end_date)->format('d M Y');

                }

                // data spk_dokumen_path
                if($obj->spk_dokumen_path)
                {
                    $spk_dokumen_path = Storage::url($obj->spk_dokumen_path);
                    $data[$i]->spk_dokumen_path = $spk_dokumen_path;
                }

                $i++;
            }

            // Calculate the page count
            $pageCount = ceil($totalRecords / $request->get('size'));

            // Calculate the current page number
            $currentPage = 0 + 1;

            // Return the response data as a JSON object
            return response()->json([
                'draw'            => $request->get('draw'),
                'recordsTotal'    => $totalRecords,
                'recordsFiltered' => $filteredRecords,
                'pageCount'       => $pageCount,
                'page'            => $currentPage,
                'totalCount'      => $totalRecords,
                'data'            => $data
            ]);
        }

        public function viewSpk()
        {
            // return Excel::download(new TujuanPenilaianExport, 'tujuan_penilaian.xlsx');

            return view('lpj::spk.view');
        }

        public function show($id)
        {
            $penawaran  = PenawaranTender::find($id);
            // return view('lpj::spk.show', compact('id','permohonan'));
        }

        public function edit($id)
        {
            $penawaran = PenawaranTender::with(['jenisLaporan','tujuanPenilaianKjpp','detail.kjpp','penilaian','persetujuan_penawaran'])->where('id',$id)->first();
            $penawaran->attachmentku = $penawaran->detail->attachment;
            $penawaran->detail_penawaran_no_proposal = $penawaran->detail->no_proposal;
            $penawaran->detail_penawaran_tgl_proposal = $penawaran->detail->tgl_proposal;
            $penawaran->detail_penawaran_biaya_penawaran = $penawaran->detail->biaya_penawaran;
            $penawaran->kjpp_name = $penawaran->detail->kjpp->name;
            $penawaran->kjpp_address = $penawaran->detail->kjpp->address;
            $penawaran->jenis_laporan_name = $penawaran->jenisLaporan->name;
            $penawaran->jenis_laporan_code = $penawaran->jenisLaporan->code;
            $penawaran->tujuan_penilaian_kjpp_name = $penawaran->tujuanPenilaianKjpp->name;
            $penawaran->penilaian_waktu_penilain = $penawaran->penilaian->waktu_penilaian ?? "";

            $permohonan  = Permohonan::where('nomor_registrasi','=',$penawaran->nomor_registrasi)
                                        ->leftJoin('dokumen_jaminan', 'dokumen_jaminan.permohonan_id','=','permohonan.id')
                                        ->leftJoin('jenis_jaminan', 'jenis_jaminan.id','=','dokumen_jaminan.jenis_jaminan_id')
                                        ->select('permohonan.*', 'jenis_jaminan.name as jenis_jaminan_name',
                                                 'dokumen_jaminan.address as dokumen_jaminan_address');

            $data = $permohonan->with(['user', 'debiture', 'branch', 'tujuanPenilaian','dokumenjaminan.detail'])->first();

            if($penawaran->detail_penawaran_tgl_proposal)
                $penawaran->detail_penawaran_tgl_proposal = Carbon::parse($penawaran->detail_penawaran_tgl_proposal)->format('d F Y');

            // generate no spk
            $spk_no_last=$penawaran->spk_no;
            if(!$spk_no_last)
            {
                $spk_no_last = onLastnumberCodePenawaranSPK($penawaran->jenis_laporan_code);
                $penawaran->spk_no = $spk_no_last;
            }

            // pengecekan perubahan jenis report
            $array_no_last = explode("/",$spk_no_last);
            $jenis_report_old=trim($array_no_last[4]);
            if($jenis_report_old!=$penawaran->jenis_laporan_code)
            {
                $penawaran->spk_no=str_replace($jenis_report_old,$penawaran->jenis_laporan_code,$spk_no_last);
            }
            // pengecekan perubahan jenis report
            // generate no spk

            // Jangka Waktu
            // date_start (penilaian.waktu_penilain + 1 day) - date_end (persetujuan_penawaran.sla_final)
            $jangka_waktu='';
            // Jangka Waktu
            $persetujuan_no_proposal = $penawaran->detail_penawaran_no_proposal;
            $persetujuan_tgl_proposal = $penawaran->detail_penawaran_tgl_proposal;
            $persetujuan_sla_resume = '...';
            $persetujuan_sla_final = '...';
            if(null !==$penawaran->persetujuan)
            {
                $sla_resume_text = ceil($data->sla/2);
                $sla_final_text = $data->sla;

                $sla_resume_text_terbilang = ucfirst(terbilang($sla_resume_text));
                $sla_final_text_terbilang = ucfirst(terbilang($sla_final_text));

                $persetujuan_no_proposal = $penawaran->persetujuan->nomor_proposal_penawaran;
                $persetujuan_tgl_proposal = Carbon::parse($penawaran->persetujuan->tanggal_proposal_penawaran)->format('d F Y');
                $persetujuan_sla_resume = $sla_resume_text.' ('.$sla_resume_text_terbilang.')';
                $persetujuan_sla_final = $sla_final_text.' ('.$sla_final_text_terbilang.')';

                if($penawaran->penilaian_waktu_penilain)
                {
                    $jangka_waktu_date_start=Carbon::parse($penawaran->penilaian_waktu_penilain)->addDays(1)->format('d F Y');
                    $jangka_waktu_date_end=Carbon::parse($penawaran->persetujuan->sla_final)->format('d F Y');
                    // Jangka Waktu
                    // date_start (penilaian.waktu_penilain + 1 day) - date_end (persetujuan_penawaran.sla_final)
                    $jangka_waktu=$jangka_waktu_date_start.' - '.$jangka_waktu_date_end;
                    // Jangka Waktu

                }
            }

            return view('lpj::spk.edit', compact('data', 'penawaran', 'persetujuan_no_proposal', 'persetujuan_tgl_proposal', 'persetujuan_sla_resume', 'persetujuan_sla_final', 'jangka_waktu'));
        }

        public function update(Request $request, $id): JsonResponse
        {

            // init
            $data1 = [];
            // $dataPermohonan = array();
            $dataPenawaran = [];
            // data

            $penawaran = PenawaranTender::with(['jenisLaporan','tujuanPenilaianKjpp','detail.kjpp','penilaian','persetujuan_penawaran'])->where('id',$id)->first();
            $penawaran->attachmentku = $penawaran->detail->attachment;
            $penawaran->detail_penawaran_no_proposal = $penawaran->detail->no_proposal;
            $penawaran->detail_penawaran_tgl_proposal = $penawaran->detail->tgl_proposal;
            $penawaran->detail_penawaran_biaya_penawaran = $penawaran->detail->biaya_penawaran;
            $penawaran->kjpp_name = $penawaran->detail->kjpp->name;
            $penawaran->kjpp_address = $penawaran->detail->kjpp->address;
            $penawaran->jenis_laporan_name = $penawaran->jenisLaporan->name;
            $penawaran->jenis_laporan_code = $penawaran->jenisLaporan->code;
            $penawaran->tujuan_penilaian_kjpp_name = $penawaran->tujuanPenilaianKjpp->name;
            $penawaran->penilaian_waktu_penilain = $penawaran->penilaian->waktu_penilaian ?? "";

            $permohonan  = Permohonan::where('nomor_registrasi','=',$penawaran->nomor_registrasi)
                                        ->leftJoin('dokumen_jaminan', 'dokumen_jaminan.permohonan_id','=','permohonan.id')
                                        ->leftJoin('jenis_jaminan', 'jenis_jaminan.id','=','dokumen_jaminan.jenis_jaminan_id')
                                        ->select('permohonan.*', 'jenis_jaminan.name as jenis_jaminan_name');

            $data = $permohonan->with(['user', 'debiture', 'branch', 'tujuanPenilaian','dokumenjaminan'])->first();

            // Jangka Waktu
            // date_start (penilaian.waktu_penilain + 1 day) - date_end (persetujuan_penawaran.sla_final)
            $jangka_waktu='';
            // Jangka Waktu

            if($penawaran->detail_penawaran_tgl_proposal)
                $penawaran->detail_penawaran_tgl_proposal = Carbon::parse($penawaran->detail_penawaran_tgl_proposal)->format('d F Y');

            $folderPath = 'uploads/spk/';
            $extension = '.pdf';
            $newFileName = "SPK_".$penawaran->nomor_registrasi."_".Auth::user()->id."_".time(). $extension;
            $newFileNameWithPath = $folderPath . $newFileName;

            // update table permohonan
            // $dataPermohonan=['dokumen' => $newFileNameWithPath];
            // $data->update($dataPermohonan);
            // update table permohonan

            // update table penawaran
            $dataPenawaran['spk_dokumen_path'] = $newFileNameWithPath;
            // $spk_no_last=$penawaran->spk_no;
            if(!$penawaran->spk_no)
            {
                $spk_no_last = onLastnumberCodePenawaranSPK($penawaran->jenis_laporan_code);
                // $penawaran->no_spk = $no_spk_last;
                $date_now = Carbon::now()->format('Ymd');// 20240124
                $spk_number = substr ($spk_no_last, 0, 3);
                $dataPenawaran['spk_no'] = $spk_no_last;
                $dataPenawaran['spk_no_core'] = $date_now.'_'.$spk_number;

                $penawaran->spk_no = $spk_no_last;

            }

            // pengecekan perubahan jenis report
            $spk_no_old=$penawaran->spk_no;
            $array_no_last = explode("/",$spk_no_old);
            $jenis_report_old=trim($array_no_last[4]);
            if($jenis_report_old!=$penawaran->jenis_laporan_code)
            {
                $penawaran_spk_no_new=str_replace($jenis_report_old,$penawaran->jenis_laporan_code,$spk_no_old);
                $penawaran->spk_no=$penawaran_spk_no_new;
                $dataPenawaran['spk_no'] = $penawaran_spk_no_new;
            }
            // pengecekan perubahan jenis report

            $penawaranM = PenawaranTender::find($penawaran->id);
            $penawaranM->update($dataPenawaran);
            // update table penawaran

            // pdf path
            $spkpenawaran_path = Storage::url($newFileNameWithPath);

            $persetujuan_no_proposal = $penawaran->detail_penawaran_no_proposal;
            $persetujuan_tgl_proposal = $penawaran->detail_penawaran_tgl_proposal;
            $persetujuan_sla_resume = '...';
            $persetujuan_sla_final = '...';
            if(null !==$penawaran->persetujuan)
            {
                //$sla_resume_text = $penawaran->persetujuan->sla_resume;
                //$sla_final_text = $penawaran->persetujuan->sla_final;

                $sla_resume_text = ceil($data->sla/2);
                $sla_final_text = $data->sla;

                $sla_resume_text_terbilang = ucfirst(terbilang($sla_resume_text));
                $sla_final_text_terbilang = ucfirst(terbilang($sla_final_text));

                $persetujuan_no_proposal = $penawaran->persetujuan->nomor_proposal_penawaran;
                $persetujuan_tgl_proposal = Carbon::parse($penawaran->persetujuan->tanggal_proposal_penawaran)->format('d F Y');
                $persetujuan_sla_resume = $sla_resume_text.' ('.$sla_resume_text_terbilang.')';
                $persetujuan_sla_final = $sla_final_text.' ('.$sla_final_text_terbilang.')';

                if($penawaran->penilaian_waktu_penilain)
                {
                    $jangka_waktu_date_start=Carbon::parse($penawaran->penilaian_waktu_penilain)->addDays(1)->format('d F Y');
                    $jangka_waktu_date_end=Carbon::parse($penawaran->persetujuan->sla_final)->format('d F Y');
                    // Jangka Waktu
                    // date_start (penilaian.waktu_penilain + 1 day) - date_end (persetujuan_penawaran.sla_final)
                    $jangka_waktu=$jangka_waktu_date_start.' - '.$jangka_waktu_date_end;
                    // Jangka Waktu

                }


            }

            $pdf =Pdf::loadView('lpj::spk.documentSPK', compact('data', 'penawaran', 'persetujuan_no_proposal', 'persetujuan_tgl_proposal', 'persetujuan_sla_resume', 'persetujuan_sla_final', 'jangka_waktu'));
            $pdf->setPaper('A4', 'portrait');
            $content = $pdf->download()->getOriginalContent();
            Storage::put('public/'.$newFileNameWithPath,$content);

            $permohonanModel = Permohonan::where('nomor_registrasi', $penawaran->nomor_registrasi)->first();
            if ($permohonanModel) {
                $permohonanModel->status = 'registrasi-final';
                $permohonanModel->save();
            }

            $persetujuanPenawaran = PenawaranTender::where('id', $penawaran->id)->first();
            if ($persetujuanPenawaran) {
                $persetujuanPenawaran->status = 'registrasi-final';
                $persetujuanPenawaran->save();
            }


            $data1['status'] = 'success';
            $data1['spkpenawaran_path'] = $spkpenawaran_path;
            $data1['message']['message_success'] = array('Generate SPK PDF successfully');

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

        public function dokumennya()
        {
            // return view('lpj::spk.dokumennya');
            $id="3";
            $penawaran = PenawaranTender::leftJoin('detail_penawaran', 'detail_penawaran.penawaran_id','=','penawaran.id')
                                     ->leftJoin('jenis_laporan', 'jenis_laporan.id','=','penawaran.jenis_laporan_id')
                                     ->leftJoin('kjpp', 'kjpp.id','=','detail_penawaran.kjpp_rekanan_id')
                                     ->where('detail_penawaran.status','=',1)
                                     ->where('penawaran.id','=', $id)
                                     ->select('penawaran.*', 'detail_penawaran.attachment as attachmentku',
                                                'detail_penawaran.biaya_penawaran as detail_penawaran_biaya_penawaran',
                                                'kjpp.name as kjpp_name',
                                                'kjpp.address as kjpp_address',
                                                'jenis_laporan.name as jenis_laporan_name'
                                    )->first();

            $permohonan  = Permohonan::where('nomor_registrasi','=',$penawaran->nomor_registrasi)
                                        ->leftJoin('dokumen_jaminan', 'dokumen_jaminan.permohonan_id','=','permohonan.id')
                                        ->leftJoin('jenis_jaminan', 'jenis_jaminan.id','=','dokumen_jaminan.jenis_jaminan_id')
                                        ->select('permohonan.*', 'jenis_jaminan.name as jenis_jaminan_name',
                                                 'dokumen_jaminan.address as dokumen_jaminan_address');

            $data = $permohonan->with(['user', 'debiture', 'branch', 'tujuanPenilaian'])->first();

            return view('lpj::spk.dokumennya', compact('data', 'penawaran'));
        }

        public function download($id) {
            // dokumen pdf diambil dari penawaran.spk_dokumen_path
            $document = PenawaranTender::find($id);

            return response()->download(storage_path('app/public/' .$document->spk_dokumen_path));
        }

        public function updateSla(Request $request, $id): JsonResponse
        {

            $request->validate([
                'sla' => 'required|integer|min:1',
            ]);


            $penawaran = PenawaranTender::find($id);
            $permohonan = Permohonan::where('nomor_registrasi','=',$penawaran->nomor_registrasi)->first();

            $permohonan->sla = $request->sla;
            $permohonan->save();

            return response()->json(['message' => 'SLA updated successfully']);
        }
    }
