from django.db import models
from django.contrib.auth.models import User
import os
from tinymce import models as tinymce_models
from model_utils import Choices
from inventario.models import invequipo
from django.utils.timezone import now


# Create your models here.

class tiposervicio(models.Model):
    nombre = models.CharField(max_length=250, null=False, unique=True, verbose_name='Nombre')
    def __str__(self):
        return self.nombre
    
    class Meta:
        verbose_name = 'Tipo de Servicio'
        verbose_name_plural = 'Tipos de Servicios'
        ordering = ['nombre']


class falla(models.Model):
    nombre = models.CharField(max_length=250, null=False, unique=True, verbose_name='Nombre')
    def __str__(self):
        return self.nombre
    
    class Meta:
        verbose_name = 'Origen Falla'
        verbose_name_plural = 'Origen Falla'
        ordering = ['nombre']


class ordenservicio(models.Model):
    folio = models.IntegerField(unique=True, null=False, verbose_name='Folio')
    fecha = models.DateField(auto_now_add=True, verbose_name='Fecha')
    turno = models.CharField(max_length=50, null=False, verbose_name='Turno Hora')
    reporta = models.CharField(max_length=250, null=False, verbose_name='Reporta')
    ib1 = models.ForeignKey(User, on_delete=models.CASCADE, null=False, blank=False, related_name='IB1', verbose_name='Atiende IB1')
    ib2 = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True, related_name='IB2', verbose_name='Atiende IB2')
    orden_n_control = models.IntegerField(null=False, blank=False, verbose_name='No Control del Equipo')
    n_reporte = models.CharField(max_length=250, null=False, blank=False, verbose_name='NUMERO DE REPORTE')
    tipo_servicio = models.ForeignKey(tiposervicio, on_delete=models.CASCADE, null=False, blank=False, verbose_name='Tipo de Servicio')
    origen_falla = models.ForeignKey(falla, on_delete=models.CASCADE, null=False, blank=False, verbose_name='Origen de la Falla')
    origen_falla_otra = models.CharField(max_length=250, null=True, blank=True, verbose_name='Especificar solo si selecciona: otra')
    falla_detectada = models.CharField(max_length=250, null=False, verbose_name='Falla Detectada')
    STATUS = Choices('SI', 'NO')
    materiales = models.CharField(choices=STATUS, default=STATUS.NO, max_length=11, verbose_name='Materiales (electronico/Solvente/Adhesivo/Limpieza/Lubricantes/Surgistein)')
    articulos = models.CharField(choices=STATUS, default=STATUS.NO, max_length=11, verbose_name='Articulos de  limpieza y protección (Gasas/Guantes de latex/cubrebocas ) ')
    equipos = models.CharField(choices=STATUS, default=STATUS.NO, max_length=11, verbose_name='Equipos de Medicion (Multimetro/Tacometro/Termometro/Manometro/Corriente de Fuga)')
    patrones = models.CharField(choices=STATUS, default=STATUS.NO, max_length=11, verbose_name='Patrones para validacion ( Probador de descargas/Simulador de ECG,RESP,SP02, GASTO CARDIACO/Marco de Pesas)')
    herramienta = models.CharField(choices=STATUS, default=STATUS.NO, max_length=11, verbose_name='Herramienta (General, Electrica, Neumatica)')
    estatus = models.CharField(choices=STATUS, default=STATUS.SI, max_length=11, verbose_name='Estatus CONCLUIDO?')
    no_concluido = tinymce_models.HTMLField(null=True, blank=True, verbose_name='Especificar si no es Concluido:')
    externo_empresa = models.CharField(max_length=250, null=True, blank=True, verbose_name='Nombre de la Empresa')
    externo_orden = models.CharField(max_length=250, null=True, blank=True, verbose_name='No. de la Orden de Servicio')
    TIPOSERVICIO = Choices('Ninguno', 'Contrato', 'Subrrogado', 'Evento')
    externo_servicio = models.CharField(choices=TIPOSERVICIO, default=TIPOSERVICIO.Ninguno, max_length=13, verbose_name='Tipo de Servicio')
    horas_ib1 = models.IntegerField(null=False, blank=False, default=0, verbose_name='Horas Ingeniero IB1')
    id_ib1 = models.ForeignKey(User, on_delete=models.CASCADE, null=False, blank=False, related_name='+', verbose_name='ID IB1')
    horas_ib2 = models.IntegerField(null=True, blank=True, verbose_name='Horas Ingeniero IB2')
    id_ib2 = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True, related_name='+', verbose_name='ID IB2')
    firma_ib1 = models.ForeignKey(User, on_delete=models.CASCADE, null=False, blank=False, related_name='+', verbose_name='FIRMA IB1')
    firma_ib2 = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True, related_name='+', verbose_name='FIRMA IB2')
    firma_area = models.CharField(max_length=250, null=True, blank=True, verbose_name='Nombre del Personal que Acepta')
    VALSERV = Choices('Excelente', 'Regular', 'Malo')
    val_serv = models.CharField(choices=VALSERV, default=VALSERV.Excelente, max_length=13, null=True, blank=True, verbose_name='Valoracion de Servicio')
    observ_area = models.CharField(max_length=250,  null=True, blank=True, verbose_name='OBSERVACIONES DE PERSONAL DEL AREA.')
    def __str__(self):
        return str(self.folio)
    
    class Meta:
        verbose_name = 'Ordenes de Servicio'
        verbose_name_plural = 'Ordenes de Servicios'
        ordering = ['-folio']


class serviciorealizado(models.Model):
    folio = models.IntegerField(null=True, blank=True, verbose_name='Folio')
    ser_s_n_control = models.IntegerField(null=True, blank=True, verbose_name='No Control')
    ib = models.ForeignKey(User, on_delete=models.CASCADE, null=False, blank=False, verbose_name='IB')
    fecha = models.DateField(auto_now_add=True, verbose_name='Fecha')
    descripcion = tinymce_models.HTMLField(null=False, verbose_name='Descripción del Servicio')
    control_bit = models.CharField(max_length=250, null=True, blank=True, verbose_name='Control Bitacora')
    n_reporte = models.CharField(max_length=250, null=True, blank=True, default='', verbose_name='NUMERO DE REPORTE')
    def __str__(self):
        return str(self.folio)
    
    class Meta:
        verbose_name = 'Servicios de las ordenes'
        verbose_name_plural = 'Servicios de las ordenes'
        ordering = ['-id']


class accesorios(models.Model):
    folio = models.IntegerField(null=True, blank=True, verbose_name='Folio')
    ser_a_n_control = models.IntegerField(null=True, blank=True, verbose_name='No Control')
    cantidad = models.IntegerField(verbose_name='Cantidad')
    n_parte = models.CharField(max_length=250, null=False, verbose_name='Número de Parte')
    descripcion = models.CharField(max_length=250, null=False, verbose_name='Descripción')
    control_bit = models.CharField(max_length=250, null=True, blank=True, verbose_name='Control Bitacora')
    n_reporte = models.CharField(max_length=250, null=True, blank=True, default='', verbose_name='NUMERO DE REPORTE')
    def __str__(self):
        return str(self.folio)
    
    class Meta:
        verbose_name = 'Accesorios de Ordenes'
        verbose_name_plural = 'Accesorios de Ordenes'
        ordering = ['-id']


class refacciones(models.Model):
    referencia = models.CharField(max_length=250, null=False, verbose_name='Referencia')
    n_reporte = models.CharField(max_length=250, null=True, blank=True, default='', verbose_name='No. de Reporte')
    cantidad = models.IntegerField(verbose_name='Cantidad')
    fecharegis = models.DateField(auto_now_add=True, null=True, blank=True, verbose_name='Fecha')
    usuario = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True, related_name='+', verbose_name='USUARIO')
    def __str__(self):
        return str(self.n_reporte)
    
    class Meta:
        verbose_name = 'refaccion'
        verbose_name_plural = 'refacciones'
        ordering = ['-id']



class preventivo(models.Model):
    STATUS = Choices('PENDIENTE', 'REALIZADO')
    n_control = models.IntegerField(null=False, blank=False, verbose_name='No Control del Equipo')
    fecha_start = models.DateField(verbose_name='FECHA INICIO')
    fecha_end = models.DateField(verbose_name='FECHA TERMINO')
    usuario = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True, related_name='+', verbose_name='Realizara')
    notas = models.CharField(max_length=250, null=False, verbose_name='Notas')
    fecha_add = models.DateField(auto_now_add=True, null=True, blank=True, verbose_name='Fecha de registro')
    estatus = models.CharField(choices=STATUS, default=STATUS.PENDIENTE, max_length=11, verbose_name='Estatus?')
    def __str__(self):
        return str(self.n_control)
    
    class Meta:
        verbose_name = 'Mantenimiento'
        verbose_name_plural = 'Mantenimientos'
        ordering = ['-id']
