from django.db import models
from apps.system.models import CommonAModel, Dict, User, CommonBModel
from apps.certset.models import ImplementRule
from simple_history.models import HistoricalRecords

# Create your models here.


class Employee(CommonAModel):
    """
    详细信息表
    """
    gender_choices = (
        ('男', '男'),
        ('女', '女'),
    )
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    code = models.CharField('编号', max_length=30, null=True, blank=True)
    photo = models.CharField(
        '证件照', default='', max_length=1000, null=True, blank=True)
    remember_code = models.CharField(
        '易记码', max_length=30, null=True, blank=True)
    gender = models.CharField('性别', max_length=50,
                               choices=gender_choices, default='男')
    landline = models.CharField('座机', max_length=100, null=True, blank=True)
    email = models.EmailField('邮箱', null=True, blank=True)
    birth = models.DateField('出生日期', null=True, blank=True)
    ID_number = models.CharField('身份证号', max_length=100, null=True, blank=True)
    pro_title = models.ForeignKey(Dict, on_delete=models.SET_NULL, null=True,
                                  blank=True, verbose_name='职称', related_name='userdetail_pro_title')
    is_onjob = models.BooleanField('是否在职', default=True)
    is_fulltime = models.BooleanField('是否专职', default=True)
    is_payinsurance = models.BooleanField('是否缴纳保险', default=True)
    finance_number = models.CharField(
        '财务编号', max_length=30, null=True, blank=True)
    fee_level = models.ForeignKey(Dict, on_delete=models.SET_NULL, null=True,
                                  blank=True, verbose_name='劳务费级别', related_name='userdetail_fee_level')
    region = models.ForeignKey(Dict, on_delete=models.SET_NULL, null=True,
                               blank=True, verbose_name='省市区', related_name='userdetail_region')
    address = models.TextField('详细地址', null=True, blank=True)
    signature = models.CharField('签名图片', max_length=200, null=True, blank=True)

    class Meta:
        verbose_name = '人员详细信息'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.user.name


class Qualification(CommonAModel):
    """
    注册资格表
    """
    employee = models.ForeignKey(Employee, on_delete=models.CASCADE, related_name='qualification_employee')
    cert_field = models.ForeignKey(
        Dict, on_delete=models.SET_NULL, null=True, blank=True, verbose_name='认证领域', related_name='qualification_cert_field')
    cert_number = models.CharField('证书号', max_length=200, unique=True)
    registration_date = models.DateField('注册日期')
    expiration_date = models.DateField('到期日期')
    auditor_type = models.ForeignKey(
        Dict, on_delete=models.SET_NULL, null=True, blank=True, verbose_name='资格类型',related_name='qualification_auditor_type')
    publish_number = models.CharField('文号', max_length=200, null=True, blank=True)
    remark = models.TextField('备注',null=True,blank=True)
    is_paused = models.BooleanField('是否暂停审核资格', default=False)
    is_enabled = models.BooleanField('是否有效', default=True)

    class Meta:
        verbose_name = '注册资格'
        verbose_name_plural = verbose_name
        unique_together = ('employee', 'cert_field', 'is_enabled')

    def __str__(self):
        return self.cert_number


class Ability(CommonAModel):
    """
    能力表
    """
    employee = models.ForeignKey(Employee, on_delete=models.CASCADE, related_name='ability_employee')
    cert_field = models.ForeignKey(
        Dict, on_delete=models.SET_NULL, null=True, blank=True, verbose_name='认证领域', related_name='ability_cert_field')
    afrom = models.ForeignKey(
        Dict, on_delete=models.SET_NULL, null=True, blank=True, verbose_name='能力来源', related_name='ability_afrom')
    access_date = models.DateField('评定时间')
    remark = models.TextField('备注', null=True)
    major = models.ForeignKey(
        Dict, on_delete=models.SET_NULL, null=True, blank=True, verbose_name='专业代码', related_name='ability_major')
    major_rule = models.ForeignKey(
        ImplementRule, on_delete=models.SET_NULL, null=True, blank=True, verbose_name='专业(ccc)', related_name='ability_major_rule')
    auditor_abilitys = models.ManyToManyField(
        Dict, verbose_name='能力项', related_name='ability_auditor_abilitys')
    is_paused = models.BooleanField('是否暂停', default=False)

    class Meta:
        verbose_name = '专业能力'
        verbose_name_plural = verbose_name

    def __str__(self):
        s = ''
        if self.major:
            s = s + self.major.code
        elif self.major_rule:
            s = s + self.major_rule.code
        return s


class Education(CommonAModel):
    """
    教育经历表
    """
    employee = models.ForeignKey(Employee, on_delete=models.CASCADE, related_name='education_employee')
    admission_date = models.DateField('入学时间')
    graduation_date = models.DateField('毕业时间')
    school = models.CharField('毕业院校', max_length=200)
    major = models.CharField('所学专业', max_length=200)
    level = models.ForeignKey(
        Dict, on_delete=models.SET_NULL, null=True, blank=True, verbose_name='文化程度', related_name='education_level')
    remark = models.TextField('备注', null=True)

    class Meta:
        verbose_name = '教育经历'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.school


class Train(CommonAModel):
    """
    培训记录表
    """
    name = models.CharField('培训主题', max_length=200)
    start_time = models.DateTimeField('开始时间')
    end_time = models.DateTimeField('结束时间')
    hours = models.IntegerField('学时', default=1)
    train_org = models.CharField('培训机构', max_length=200)
    teacher = models.CharField('主讲人', max_length=200)
    content = models.TextField('培训内容')
    remark = models.TextField('备注')
    employees = models.ManyToManyField(Employee, verbose_name='参加人员', related_name='train_employees')

    class Meta:
        verbose_name = '培训记录'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name
