<?php

namespace App\Http\Livewire;

use Livewire\Component;
use Livewire\WithFileUploads;
use App\Models\Company;
use Illuminate\Support\Facades\Auth;

class Company_ConfigSignatureController extends Component
{

    use WithFileUploads;

    public $signature_company;
    public $document_representative_form;
    public $company_id, $pageTitle, $componentName, $customFileName;

    public function mount()
    {
        UserAuthPermission('Company_configuration_add_signature');
        $this->company_id = Company::find(Auth::user()->CompanyId())->id;
        $this->pageTitle = 'Configuración';
        $this->componentName = 'Empresa';
    }

    public function render()
    {
        return view('livewire.company.config.signature.component')
        ->extends('layouts.theme.admin.app')
        ->section('content');
    }

    public function OpenAddSignature()
    {

        $useractions = UserAuthPermissionActions('Company_configuration_add_signature');
        if (!empty($useractions)){
            $this->emit($useractions[0],$useractions[1]);
            return false;
        }
        $company = Company::find($this->company_id);
        if ($this->document_representative_form != $company->document_representative) {
            $this->emit('item-error', 'El número de firma ha cambiado, por favor Guarde los cambios');
            return false;
        }
        $this->emit('show-modal-add-signature','Abrir Modal');
    }

    public function saveSignature(){
        $useractions = UserAuthPermissionActions('Company_configuration_add_signature');
        if (!empty($useractions)){
            $this->emit($useractions[0],$useractions[1]);
            return false;
        }

        $company = Company::find($this->company_id);
        $currentFileNameSignature = $company->signature; //Nombre del archivo de la firma almacenado en la base de datos
        $document_representative_current = $company->document_representative; //Documento del representante legal almacenado en la base de datos
        $image_parts = explode(";base64,", $this->signature_company);
        $image_type_aux = explode("image/", $image_parts[0]);
        $image_type = $image_type_aux[1];
        $image_base64 = base64_decode($image_parts[1]);
        //$this->signature_file->storeAs('storage/users/' .$user_id. '/signature_contracts/' ,$signatureName);
        $folderPath = public_path('storage/companies/' .$this->company_id. '/');
        if (!file_exists($folderPath)) {
            mkdir($folderPath, 0777, true);
        }
        $signatureName = 'signature_'.$this->company_id.'_'.$this->document_representative_form.'.'.$image_type;
        /*
        if ($currentFileNameSignature != NULL) {
            if ($document_representative_current == $this->document_representative_form) { //Si el documento del representante legal almacenado en la base de datos es igual al documento del representante legal ingresado
                if (file_exists($folderPath.$currentFileNameSignature)) { //Si existe el archivo de la firma almacenado en la base de datos
                    unlink($folderPath.$currentFileNameSignature);//Elimina el archivo de la firma
                }
            }
        }
        */
        $file = $folderPath . $signatureName;
        file_put_contents($file, $image_base64);
        $company->signature = $signatureName;
        if ($company->save()) {
            $this->emit('item-updated-add-signature', 'Firma guardada');
        }

    }

}
