<?php

use App\Models\User;

if(! function_exists('DataTypeContract')){
    function DataTypeContract($type=null)
    {
        $arrayType=[
            '1'=>'Contrato por obra o labor',
            '2'=>'Contrato término fijo',
            '3'=>'Contrato término indefinido',
        ];
        if($type!=null)
            return $arrayType[$type];
        else
            return $arrayType;
    }
}

if (! function_exists('DataSubTypeContract')) {
    function DataSubTypeContract($sub_type=null)
    {
        $arraySubType=[
            '1'=>'Modalidad salarial',
            '2'=>'Puesto de trabajo',
            '3'=>'Trabajo a distancia',
            '4'=>'Trabajo remoto',
        ];
        if ($sub_type!=null)
            return $arraySubType[$sub_type];
        else
            return $arraySubType;
    }
}

if (! function_exists('DataTypeStatusContract')){
    function DataTypeStatusContract($type=null)
    {
        $arrayType=[
            '0'=>'Pendiente',
            '1'=>'Aprobado',
            '2'=>'Terminado',
            '3'=>'Terminado con incidencia',
        ];
        if($type!=null)
            return $arrayType[$type];
        else
            return $arrayType;
    }
}

if (! function_exists('ColorStatusContract')){
    function ColorStatusContract($type=null)
    {
        $arrayType=[
            '0'=>'bg-warning',
            '1'=>'bg-success',
            '2'=>'bg-success',
            '3'=>'bg-danger',
        ];
        return $arrayType[$type];

    }
}
if (! function_exists('IconStatusContract')){
    function IconStatusContract($type=null)
    {
        $arrayType=[
            '0'=>'fa-exclamation-circle',
            '1'=>'fa-check-circle',
            '2'=>'fa-check-circle',
            '3'=>'fa-times-circle',
        ];
        return $arrayType[$type];

    }
}

if (! function_exists('DataTypeModalityAcademic')) {
    function DataTypeModalityAcademic($type=null)
    {
        $arrayType=[
            '1'=>'Presencial',
            '2'=>'Distancia',
            '3'=>'Virtual',
        ];
        if($type!=null)
            return $arrayType[$type];
        else
            return $arrayType;
    }
}

if (! function_exists('DataTypeStatusAcademic')) {
    function DataTypeStatusAcademic($type=null)
    {
        $arrayType=[
            '1'=>'Culminado',
            '2'=>'Cursando',
            '3'=>'Abandonado / Aplazado',
        ];
        if($type!=null)
            return $arrayType[$type];
        else
            return $arrayType;
    }
}

if (! function_exists('DataTypeMonth')) {
    function DataTypeMonth($type=null)
    {
        $arrayType=[
            '1'=>'Enero',
            '2'=>'Febrero',
            '3'=>'Marzo',
            '4'=>'Abril',
            '5'=>'Mayo',
            '6'=>'Junio',
            '7'=>'Julio',
            '8'=>'Agosto',
            '9'=>'Septiembre',
            '10'=>'Octubre',
            '11'=>'Noviembre',
            '12'=>'Diciembre',
        ];
        if($type!=null)
            return $arrayType[$type];
        else
            return $arrayType;
    }
}

if (! function_exists('DataTypeARL')) {
    function DataTypeARL($type=null)
    {
        $arrayType=[
            '0.005'=>'Riesgo I',
            '0.01'=>'Riesgo II',
            '0.024'=>'Riesgo III',
            '0.044'=>'Riesgo IV',
            '0.07'=>'Riesgo V',
        ];
        if($type!=null)
            return $arrayType[$type];
        else
            return $arrayType;
    };
}

if (! function_exists('DataTypeDocument')) {
    function DataTypeDocument($type=null, $abbreviation=false)
    {
        $arrayTypeDocument=[
            '1'=>'Cédula de ciudadanía',
            '2'=>'Cédula de extranjería',
            '3'=>'Pasaporte',
            '4'=>'Tarjeta de identidad',
        ];
        if ($abbreviation == true) {
            $arrayTypeDocument=[
                '1'=>'CC',
                '2'=>'CE',
                '3'=>'PAS',
                '4'=>'TI',
            ];
        }
        if($type!=null)
            return $arrayTypeDocument[$type];
        else
            return $arrayTypeDocument;
    }
}



if (! function_exists('DataTypeCivilStatus')) {
    function DataTypeCivilStatus($type=null)
    {
        $arrayTypeCivilStatus=[
            '1'=>'Soltero(a)',
            '2'=>'Casado(a)',
            '3'=>'Unión libre',
            '4'=>'Divorciado(a)',
            '5'=>'Viudo(a)',
        ];
        if($type!=null)
            return $arrayTypeCivilStatus[$type];
        else
            return $arrayTypeCivilStatus;
    }
}

if(! function_exists('DataTypeSalary')){
    function DataTypeSalary($type=null)
    {
        $arrayTypeSalary=[
            '1'=>'Salario fijo',
            '2'=>'Salario integral',
            '3'=>'Salario parcial',
        ];
        if($type!=null)
            return $arrayTypeSalary[$type];
        else
            return $arrayTypeSalary;
    }
}

if (! function_exists('DataTypePaymentMethod')) {
    function DataTypePaymentMethod($type=null)
    {
        $arrayTypePaymentMethod=[
            '1'=>'Cheque',
            '2'=>'Efectivo',
            '3'=>'Transferencia',
            '4'=>'Depósito',
        ];
        if($type!=null)
            return $arrayTypePaymentMethod[$type];
        else
            return $arrayTypePaymentMethod;
    }
}

if (! function_exists('DataTypeWorkDay')) {
    function DataTypeWorkDay($type=null)
    {
        $arrayTypeWorkDay=[
            '1'=>'Diurna',
            '2'=>'Nocturna',
            '3'=>'Mixta',
        ];
        if($type!=null)
            return $arrayTypeWorkDay[$type];
        else
            return $arrayTypeWorkDay;
    }
}

if (! function_exists('DataTypeStatusStandard')) {
    function DataTypeStatusStandard($type=null)
    {
        $arrayTypeStatusStandard=[
            '0'=>'Pendiente',
            '1'=>'Aprobado',
            '2'=>'Rechazado',
        ];
        if($type!=null)
            return $arrayTypeStatusStandard[$type];
        else
            return $arrayTypeStatusStandard;
    }
}

if (! function_exists('DataTypeStatusSupportWorkPermit')) {
    function DataTypeStatusSupportWorkPermit($type=null)
    {
        $arrayTypeStatusWorkPermit=[
            '0'=>'Pendiente',
            '1'=>'Aprobado',
            '2'=>'Rechazado',
            '3'=>'Solicitud de corrección',
        ];
        if($type!=null)
            return $arrayTypeStatusWorkPermit[$type];
        else
            return $arrayTypeStatusWorkPermit;
    }
}

if (! function_exists('Request_to_Default')) {
    function Request_to_Default($string)
    {

        if(is_null($string) || trim($string)=='')
            return 'A quien corresponda';
        else
            return $string;
    }
}

if (! function_exists('AdminsCompany')) {
    function AdminsCompany($company_id)
    {
        $admins = User::role('Administrador')
                ->select('users.id as user_id')
                ->join('company_user', 'company_user.user_id', '=', 'users.id')
                ->where('company_user.company_id', $company_id)
                ->get();
        return $admins;
    }
}

if (! function_exists('CompanyUser')){
    function CompanyUser($id) {
        //$id = '2';
        $company_user = DB::table('users')
        ->join('company_user', 'users.id', '=', 'company_user.user_id')
        ->join('companies', 'company_user.company_id', '=', 'companies.id')
        ->select('companies.name as company_name')
        ->where('users.id', $id)
        ->get()->first();
        //dd($company_user);
        if ($company_user != null) {
            return $company_user->company_name;
        } else {
            return 'Sin empresa';
        }

    }
}

if(! function_exists('FullNameUser')){
    function FullNameUser($id) {
        $user = User::find($id);
        if ($user != null) {
            return $user->first_name.' '.$user->last_name;
        } else {
            return 'No existe el usuario';
        }
    }
}

if (! function_exists('GeneratePasswordSecure')) {
    function GeneratePasswordSecure($length = 8)
    {
        $lowercase = range('a', 'z'); // generamos un array con todas las letras minúsculas
        $uppercase = range('A', 'Z'); // generamos un array con todas las letras mayúsculas
        $numbers = range(0, 9); // generamos un array con todos los números
        $special = ['!', '@', '#', '$', '%', '^', '*']; // generamos un array con los caracteres especiales
        $chars = array_merge($lowercase, $uppercase, $numbers, $special);   // unimos los arrays en uno solo

        do{
            $password = array();  // creamos un array vacío
            for($i = 0; $i < $length; $i++){
                $int = rand(0, count($chars) - 1);  // generamos un numero aleatorio para seleccionar un caracter del array
                array_push($password, $chars[$int]);    // agregamos el caracter generado al array en la posición generada
            }
        }while(empty(array_intersect($special, $password))); // si el array no contiene caracteres especiales, volvemos a generar

        return implode('', $password); // convertimos el array en un string y lo retornamos
    }
}

/** generar una contraseña aleatoria y segura */

if (! function_exists('tweak_array_rand')){
    function tweak_array_rand($array){
        if (function_exists('random_int')) {
            return random_int(0, count($array) - 1);
        } elseif(function_exists('mt_rand')) {
            return mt_rand(0, count($array) - 1);
        } else {
            return array_rand($array);
        }
    }
}


if (! function_exists('generateStrongPassword')){
    function generateStrongPassword($length = 15, $add_dashes = false, $available_sets = 'luds')
    {
        $sets = array();
        if(strpos($available_sets, 'l') !== false)
            $sets[] = 'abcdefghjkmnpqrstuvwxyz';
        if(strpos($available_sets, 'u') !== false)
            $sets[] = 'ABCDEFGHJKMNPQRSTUVWXYZ';
        if(strpos($available_sets, 'd') !== false)
            $sets[] = '23456789';
        if(strpos($available_sets, 's') !== false)
            $sets[] = '!@#$%&*?';

        $all = '';
        $password = '';
        foreach($sets as $set)
        {
            $password .= $set[tweak_array_rand(str_split($set))];
            $all .= $set;
        }

        $all = str_split($all);
        for($i = 0; $i < $length - count($sets); $i++)
            $password .= $all[tweak_array_rand($all)];

        $password = str_shuffle($password);

        if(!$add_dashes)
            return $password;

        $dash_len = floor(sqrt($length));
        $dash_str = '';
        while(strlen($password) > $dash_len)
        {
            $dash_str .= substr($password, 0, $dash_len) . '-';
            $password = substr($password, $dash_len);
        }
        $dash_str .= $password;
        return $dash_str;
    }
}

