<?php

namespace App\Http\Controllers\Admin;

use App\EmailsResponders;
use App\Excesses;
use App\Helpers\EmailTemplates;
use App\Helpers\Functions;
use App\Http\Controllers\AdminController;
use App\Http\Controllers\Controller;
use App\Settings;
use App\SubscriptionFacilities;
use Illuminate\Http\Request;
use App\Transactions;
use App\Packages;
use Dompdf\Dompdf;
use PDF;

use App\SubscriptionEvent;

class TransactionsController extends AdminController
{
    public function __construct()
    {
        $this->middleware('auth');

        $this->breadcrumbs = $this->getBreadcrumbs();
        $this->breadcrumbs[1]['link'] = "admin/transactions";
        $this->breadcrumbs[1]['name'] = "Transactions";
    }

    public function index()
    {

        $model = Transactions::get();

        $data["pageHeader"] = false;
        $data["model"] = $model;
        $data["title"] = "Transactions";
        $data["SubTitle"] = "List";
        $data['breadcrumbs'] = $this->breadcrumbs;
        return view('admin/transactions/index', $data);
    }

    public function show($id)
    {

        $q = new Transactions();
        $data['model'] = $q->find($id);
        //        $package=Packages::where('id',$data['model']->package_id)->first();

        $data["title"] = "Transactions";
        $this->breadcrumbs[2]['link'] = "#_";
        $this->breadcrumbs[2]['name'] = $data['model']->invoiceNumber;

        $data['breadcrumbs'] = $this->breadcrumbs;

        return view('admin/transactions/show', $data);
    }


    public function logs($id)
    {

        $data["pageHeader"] = false;
        $data["model"] = SubscriptionEvent::where('transaction_id', $id)->get();
        $data["title"] = "Logs";
        $data["SubTitle"] = "List";
        $data['breadcrumbs'] = $this->breadcrumbs;

        return view('admin/transactions/logs', $data);


    }

    public function singleLog($id, $logId)
    {

        $q = new SubscriptionEvent();
        $data['model'] = $q->find($logId);

        $data["title"] = "Log";
        $this->breadcrumbs[2]['link'] = "#_";
        $this->breadcrumbs[2]['name'] = $data['model']->id;

        $data['breadcrumbs'] = $this->breadcrumbs;

        return view('admin/transactions/singleLog', $data);


    }

    public function downloadPdf(Request $request, $invoiceNumber)
    {


        /////////////
        $transactions = Transactions::with('package', 'customers')->find($invoiceNumber);
        $package = $transactions->package;
        $customer = $transactions->customers;
        $facilities = SubscriptionFacilities::
            leftjoin('package_facilities', 'package_facilities.facility_id', '=', 'subscription_facilities.facility_id')
            ->leftjoin('facilities', 'package_facilities.facility_id', '=', 'facilities.id')
            ->where("subscription_facilities.transaction_id", $invoiceNumber)
            ->where("facilities.status", 1)
            ->groupBy("facilities.id")
            ->select('subscription_facilities.title')
            ->get();
        //}

        $prices = $transactions->total;
        $emailsResponders = EmailsResponders::where('Title', 'User - Invoice')->first();

        $agreementDate = $transactions->startDate;

        $excess_id = $transactions->excess_id;
        $type = $transactions->type;

        $excess = Excesses::find($excess_id);

        $data["body"] = $emailsResponders->Content;
        $data["title"] = $emailsResponders->Title;
        $data["subject"] = $emailsResponders->Subject;

        $replaces["Title"] = $customer->title;
        $replaces["First_Name"] = $customer->firstName;
        $replaces["Last_Name"] = $customer->lastName;
        $replaces["Phone_Number"] = $customer->phone;
        $replaces["Email"] = $customer->email;
        $replaces['Address1'] = $customer->address->address1 ?? '';
        $replaces['Address2'] = $customer->address->address2 ?? '';
        $replaces['City'] = $customer->address->city ?? '';
        $replaces['Zip'] = $customer->address->postcode ?? '';

        $replaces['Package_Name'] = ucfirst($package['title']) ?? '';
        $replaces["CURRENCY"] = env("CURRENCY_SYMBOL");

        $replaces['Total_Price'] = numberformat($transactions->total);
        $replaces['NET_Amount'] = numberformat($transactions->price);
        $replaces["Plan"] = rtrim($transactions["type"], "ly") ?? '';
        $replaces["VAT_Amount"] = numberformat($transactions->total) - numberformat($transactions->price);
        $replaces["Invoice_Number"] = $invoiceNumber;
        $replaces["Created_Date_Time"] = date("d/m/y", strtotime($agreementDate));
        $replaces["Invoice_Service_Table"] = EmailTemplates::setPackageTemplate($facilities);
        $replaces["CALL_OUT_CHARGE"] = "Your call out charge is " . $excess->title . ".";

        $template = Functions::replaces($data, $replaces);
        // $settings = sett
        $facilities = array_chunk($facilities->toArray(), 3);

        $count = count($facilities);
        $pdf = new Dompdf();
        $pdf->loadHtml(view('front.pdf.invoice', ['data' => $replaces, 'facilities' => $facilities, 'count' => $count]));
        $pdf->setPaper('A4', 'portrait');
        $pdf->render();
        $output = $pdf->stream("$customer->firstName.pdf", array("Attachment" => false));

        $output = $pdf->output();
        return response($output, 200)
            ->header('Content-Type', 'application/pdf')
            ->header('Content-Disposition', 'attachment; filename="' . $customer->firstName . '.pdf"');

    }





}