from django.db import models
from apps.utils.models import CommonADModel, BaseModel, CommonBDModel
from apps.system.models import User
from django.core.validators import RegexValidator
# Create your models here.


MTASK_CREATED = 10
MTASK_ASSGINED = 20
MTASK_STOP = 34
MTASK_SUBMIT = 40
MTASK_STATES = (
        (MTASK_CREATED, '创建中'),
        (MTASK_ASSGINED, '已下达'),
        (MTASK_STOP, '已停止'),
        (MTASK_SUBMIT, '已提交')
    )
phone_validator = RegexValidator(r'^1[3456789]\d{9}$', '手机号码格式不正确')


class Mroom(CommonADModel):
    """TN: 会议室基本信息"""
    name = models.CharField('会议室名称', max_length=50, unique=True)
    location = models.CharField('位置', max_length=100)
    capacity = models.PositiveIntegerField('容纳人数')

class MroomBooking(CommonADModel):
    """TN: 会议室预定信息"""
    title = models.CharField('会议主题', max_length=100)


class MroomSlot(BaseModel):
    """TN: 会议室时段"""
    mroom = models.ForeignKey(Mroom, on_delete=models.CASCADE, related_name="slot_m")
    booking = models.ForeignKey(MroomBooking, on_delete=models.CASCADE, related_name="slot_b")
    mdate = models.DateField('会议日期', db_index=True)
    slot = models.PositiveIntegerField('时段', help_text='0-47')
    
    class Meta:
        unique_together = ('mroom', 'mdate', 'slot')


# class Seal(BaseModel):
#     """TN: 印章类型"""
#     name = models.CharField('印章名称', max_length=50, unique=True)


class LendingSeal(CommonBDModel):
    """TN: 印章外出用印信息"""

    seal = models.JSONField('印章信息',default=list ,help_text='{"seal_name": "印章名称"}')
    filename = models.TextField('文件名称')
    file = models.TextField('文件内容')
    file_count = models.PositiveIntegerField('用印份数')
    is_lending= models.BooleanField('是否借出', default=False)
    contacts = models.CharField('联系方式', max_length=50, validators=[phone_validator], blank=True, null=True)
    lending_date = models.DateField('借出日期', blank=True, null=True)
    return_date = models.DateField('拟归还日期', blank=True, null=True)
    actual_return_date = models.DateField('实际归还日期', blank=True, null=True)
    reason = models.CharField('借用理由', max_length=100,  blank=True, null=True)
    submit_user = models.ForeignKey(
        User, verbose_name='提交人', on_delete=models.CASCADE, null=True, blank=True, related_name='seal_submit_user')
    ticket = models.ForeignKey('wf.ticket', verbose_name='关联工单',
                               on_delete=models.SET_NULL, related_name='seal_ticket', null=True, blank=True, db_constraint=False)
    note = models.TextField('备注', null=True, blank=True)


# class Vehicle(CommonADModel):
#     """TN: 用车申请"""
    
#     submit_user = models.ForeignKey(
#         User, verbose_name='用车人', on_delete=models.CASCADE, null=True, blank=True, related_name='vehicle_submit_user')
#     approval_user = models.ForeignKey(
#          User, verbose_name='审批人', on_delete=models.CASCADE, null=True, blank=True, related_name='vehicle_approval_user'
#     )
#     start_time = models.DateTimeField('出车时间')
#     end_time = models.DateTimeField('还车时间')
#     location = models.CharField('出发地点', null=True, blank=True, max_length=100)
#     destination = models.CharField('到达地点', null=True, blank=True, max_length=100)
#     start_km = models.PositiveIntegerField('出发公里数')
#     end_km = models.PositiveIntegerField('归还公里数')
#     actual_km = models.PositiveIntegerField('实际行驶公里数', editable=False)
#     is_city = models.BooleanField('是否市内用车', default=True)
#     reason = models.CharField('用车事由', max_length=100)
#     ticket = models.ForeignKey('wf.ticket', verbose_name='关联工单',
#                                on_delete=models.SET_NULL, related_name='vehicle_ticket', null=True, blank=True, db_constraint=False)
#     state = models.PositiveIntegerField(
#         '状态', choices=MTASK_STATES, default=MTASK_CREATED, help_text=str(MTASK_STATES))
    
#     def save(self, *args, **kwargs):
#         if self.end_km and self.start_km:
#             self.actual_km = self.end_km - self.start_km
#         return super().save(*args, **kwargs)


# class FileRecord(CommonBDModel):
#     """TN: 档案台账"""
#     name = models.CharField('资料名称', max_length=100)
#     number = models.CharField('档案编号', max_length=50)
#     files = models.CharField('文件份数', max_length=10)
#     location = models.CharField('存放位置', max_length=100)
#     contacts = models.CharField('存档人电话', max_length=50)
#     location = models.CharField('存档位置', max_length=100)
#     reciver = models.CharField('接收人（综合办）', max_length=50)
#     remark = models.CharField('备注', max_length=100)


# class BorrowRecord(CommonBDModel):
#     """TN: 借阅、复印、查阅记录"""
#     files = models.ManyToManyField(FileRecord, related_name="borrow_records")
#     borrow_user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="borrow_user")
#     borrow_date = models.DateField('借阅日期')
#     return_date = models.DateField('归还日期')
#     contacts = models.CharField('借阅人电话', max_length=50, validators=[phone_validator], null=True, blank=True)
#     remark = models.CharField('用途', max_length=100)


# class Publicity(CommonBDModel):
#     """TN: 公示栏"""
#     CHANNEL_CHOICES = [
#     ('internet', '互联网'),
#     ('platform', '信息平台'),
#     ('official', '官微'),
#     ('publication', '公开发行物'),
#     ('other', '其它'),
#     ]
#     SECRET_LEVELS = (
#         (0, '重要'),
#         (1, '一般'),
#         (2, '非涉密'),
#     )
#     CONTENT_CHOICES = [
#     ('device', '武器装备科研生产综合事项'),
#     ('other', '其他'),
#     ]
#     number = models.CharField('记录编号', max_length=50)
#     title = models.CharField('送审稿件标题', max_length=100)
#     participants = models.CharField('所有撰稿人', max_length=50)
#     level = models.PositiveBigIntegerField('涉密等级', choices=SECRET_LEVELS, default=2, help_text=str(SECRET_LEVELS))
#     content = models.CharField('稿件内容涉及', max_length=50, choices=CONTENT_CHOICES, default='device', blank=True)
#     report_purpose = models.CharField('宣传报道目的', max_length=100, blank=True, null=True)
#     channel = models.CharField('发布渠道', max_length=50, choices=CHANNEL_CHOICES, default='platform')
#     other_channel = models.CharField('其它渠道', max_length=50, blank=True, null=True)
#     review = models.CharField('第一撰稿人自审', max_length=100, blank=True, null=True)
#     dept_opinion = models.CharField('部门负责人意见', max_length=100, blank=True, null=True)
#     manager_opinion = models.CharField('总经理', max_length=100, blank=True, null=True)


# class PatentInfo(CommonBDModel):
#     """TN: 专利申密审批表单样式"""
#     PATENT_TYPE_CHOICES = (
#     ('invention', '发明专利'),
#     ('utility', '实用新型专利'),
#     ('design', '外观设计专利'),
#     )
#     APPLY_AREAS = (
#         ('Domestic', '国内申请'),
#         ('Foreign', '国外申请'),
#         (' PCT', 'PCT申请'),
#     )
#     name = models.CharField('拟申请专利名称', max_length=100)
#     author = models.CharField('发明人（设计人）', max_length=100)
#     type = models.CharField('专利类型', max_length=50, choices=PATENT_TYPE_CHOICES, default='invention')
#     is_public = models.BooleanField('是否公开', default=False)
#     area = models.CharField('拟申请地域', max_length=50, choices=APPLY_AREAS, default='Domestic')
#     identified = models.BooleanField('是否进行过科技成果鉴定', default=False)
#     published_article = models.BooleanField('是否发表过文章', default=False)
#     exhibited = models.BooleanField('是否参与过展会展出', default=False)
#     applied_to_production = models.BooleanField('是否参与应用于生产/销售', default=False)
#     participated_in_exchange = models.BooleanField('是否参与过技术交流', default=False)
#     tech_background_pages = models.PositiveIntegerField('技术背景材料页数', null=True, blank=True)
#     tech_disclosure_pages = models.PositiveIntegerField('技术交底材料页数', null=True, blank=True)
#     novelty_report_pages = models.PositiveIntegerField('查新检索报告页数', null=True, blank=True)
#     diagrams_or_photos_pages = models.PositiveIntegerField('图/照片页数或张数', null=True, blank=True)


# class PaperOfm(CommonADModel):
#     """TN: 论文申密审批表单"""
#     PAPER_TYPE_CHOICES = (
#     ('research', '研究论文'),
#     ('comprehensive', '综合'),
#     )
#     name = models.CharField('拟申请专利名称', max_length=100)
#     author = models.CharField('发明人（设计人）', max_length=100)
#     paper_type = models.CharField('论文类型', max_length=50, choices=PAPER_TYPE_CHOICES, default='research')
#     is_chinese_core = models.BooleanField('是否为中文核心', default=False)
#     is_sci = models.BooleanField('是否被SCI/EI收录', default=False)
#     has_appraisal = models.BooleanField('是否进行过科技成果鉴定', default=False)
#     has_published_article = models.BooleanField('是否发表过文章', default=False)
#     has_exhibited = models.BooleanField('是否参与过展会展出', default=False)
#     has_applied_in_production = models.BooleanField('是否参与应用于生产/销售', default=False)
#     has_technical_exchange = models.BooleanField('是否参与过技术交流', default=False)
#     paper_page_count = models.PositiveIntegerField('论文页数', null=True, blank=True)
#     image_count = models.PositiveIntegerField('图/照片张数', null=True, blank=True)


# class Platform(CommonADModel):
#     name = models.CharField(max_length=100)

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

# class Project(CommonADModel):
#     name = models.CharField(max_length=100)

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

# class PatentRecord(CommonADModel):
#     """TN: 专利台账登记"""
#     volume_number = models.CharField(max_length=50, null=True, blank=True, verbose_name="卷号")
#     application_number = models.CharField(max_length=50, verbose_name="申请号（交局后补登）")
#     title = models.CharField(max_length=255, verbose_name="名称")
    
#     patent_type = models.CharField(
#         max_length=20,
#         choices=[
#             ("invention", "发明"),
#             ("utility_model", "实用新型"),
#             ("design", "外观设计")
#         ],
#         verbose_name="专利类型"
#     )
#     organization = models.CharField(max_length=100, verbose_name="单位")
#     inventors = models.CharField(max_length=255, verbose_name="发明人")
#     agent = models.CharField(max_length=255, null=True, blank=True, verbose_name="代理人")
#     affiliated_platforms = models.ManyToManyField('Platform', blank=True, verbose_name="归属平台")
#     affiliated_projects = models.ManyToManyField('Project', blank=True, verbose_name="归属项目")
#     application_date = models.DateField(null=True, blank=True, verbose_name="申请日")
#     authorization_date = models.DateField(null=True, blank=True, verbose_name="授权日")
#     validity_years = models.IntegerField(null=True, blank=True, verbose_name="有效年限（年）")
#     annuity_paid = models.DecimalField(max_digits=10,decimal_places=2, null=True,blank=True,verbose_name="年费缴纳")

#     status = models.CharField(
#         max_length=20,
#         choices=[
#             ("not_disclosed", "未公开"),
#             ("under_examination", "实审中"),
#             ("first_office_action", "一通"),
#             ("second_office_action", "二通"),
#             ("rejected", "驳回"),
#             ("reexamination", "复审"),
#             ("authorized", "授权")
#         ],
#         verbose_name="状态"
#     )
#     award_info = models.TextField(null=True, blank=True, verbose_name="报奖情况")
#     bonus_amount = models.DecimalField(max_digits=10,decimal_places=2, null=True,blank=True,verbose_name="奖金金额（元）")


# class PaperRecord(models.Model):
#     """TN: 论文台账登记"""
#     index = models.PositiveIntegerField(verbose_name="序号")
#     paper_code = models.CharField(max_length=100, blank=True, null=True, verbose_name="论文编号（投稿后补登）")
#     title = models.CharField(max_length=255, verbose_name="名称")
#     paper_type = models.CharField(max_length=100, verbose_name="论文类型")
#     affiliation = models.CharField(max_length=255, verbose_name="单位")
#     authors = models.CharField(max_length=255, verbose_name="作者")
#     corresponding_author = models.CharField(max_length=255, blank=True, null=True, verbose_name="通讯作者")
#     affiliated_platforms = models.ManyToManyField('Platform', blank=True, verbose_name="归属平台")
#     affiliated_projects = models.ManyToManyField('Project', blank=True, verbose_name="归属项目")
#     acceptance_date = models.DateField(blank=True, null=True, verbose_name="接受日期")
#     publication_date = models.DateField(blank=True, null=True, verbose_name="发表日期")
#     page_fee_paid = models.DecimalField(
#         max_digits=10,
#         decimal_places=2,
#         blank=True,
#         null=True,
#         verbose_name="版面费缴纳"
#     )
    
#     status = models.CharField(
#         max_length=50,
#         choices=[
#             ("under_review", "审稿中"),
#             ("revise_1", "一修"),
#             ("revise_2", "二修"),
#             ("accepted", "接收"),
#             ("published", "发表")
#         ],
#         default="under_review", verbose_name="状态"
#     )
#     award_status = models.CharField(max_length=255, blank=True, null=True, verbose_name="报奖情况")
#     bonus_amount = models.DecimalField(max_digits=10,decimal_places=2,blank=True,null=True,verbose_name="奖金发放")


# class ProjectApproval(CommonBDModel):
#     """TN: 立项审批表"""
#     project_start_date = models.DateField("立项日期", null=True, blank=True)
#     is_self_initiated = models.BooleanField("自立项目", default=False)
#     is_city_level = models.BooleanField("市级项目", default=False)
#     is_province_level = models.BooleanField("省级项目", default=False)
#     construction_period = models.CharField("建设期", max_length=100, null=True, blank=True)
#     project_members = models.TextField("项目组员", null=True, blank=True)
#     project_budget = models.DecimalField("项目预算(万元)", max_digits=12, decimal_places=2, null=True, blank=True)
#     project_description = models.TextField("项目基本情况", null=True, blank=True)
#     project_performance = models.TextField("目标绩效", null=True, blank=True)


# class ProjectInfo(CommonBDModel):
#     """TN: 项目信息表
    
#     """
#     serial_number = models.CharField("序号", max_length=50, null=True, blank=True)
#     red_head_doc_no = models.CharField("红头发文号/公示页", max_length=100, null=True, blank=True)
#     name = models.CharField("名称", max_length=200, null=True, blank=True)
#     project_type = models.CharField("项目类型", max_length=100, null=True, blank=True)
#     platform = models.CharField("所属平台", max_length=100, null=True, blank=True)
#     project_source = models.CharField("项目来源", max_length=100, null=True, blank=True)
#     construction_period = models.CharField("建设期", max_length=100, null=True, blank=True)
#     project_funding = models.DecimalField("项目资金(财政与自筹)", max_digits=15, decimal_places=2, null=True, blank=True)
#     support_period = models.CharField("项目支持期", max_length=100, null=True, blank=True)
#     undertaking_unit = models.CharField("承担单位", max_length=200, null=True, blank=True)
#     responsible_person = models.CharField("负责人", max_length=50, null=True, blank=True)

#     project_members = models.TextField("项目人员", null=True, blank=True)
#     milestone = models.TextField("里程碑节点", null=True, blank=True)
#     mid_term_status = models.TextField("项目中期情况", null=True, blank=True)
#     acceptance_status = models.TextField("项目验收情况", null=True, blank=True)
#     sci_tech_achievements = models.TextField("科技成果", null=True, blank=True)

