# -*- coding: utf-8 -*-
import logging
from odoo import api, fields, models
from odoo.exceptions import UserError

_logger = logging.getLogger(__name__)


class WxWorkApprovalTemplate(models.Model):
    _name = 'wxwork.approval.template'
    _description = "企业微信审批模板"

    process_code = fields.Char(string='模板id')
    name = fields.Char(string='模板名称')
    # content_ids = fields.Many2many('wxwork.approval.template.controls', string='模板内容')
    content_ids = fields.One2many('wxwork.approval.template.controls', 'template_id', string='模板内容')
    # 增加一个与wxwork.approval.vacation.config的关联字段
    vacation_config_id = fields.One2many('wxwork.approval.vacation.config', 'template_id', string='控件配置')

    company_id = fields.Many2one('res.company', '公司', default=lambda self: self.env.user.company_id.id)
    creator_userid = fields.Many2one('res.users', '创建者', default=lambda self: self.env.user.id)
    use_template_approver = fields.Selection([
        ('0', '自选审批人'),
        ('1', '预设审批人')
    ], string='审批人模式', default='1')
    choose_department = fields.Char(string='提单部门')
    approver = fields.One2many('wxwork.approval.approver_line', 'main_id', string='审批流程')
    notifyer = fields.Many2many('res.users', string='抄送人userid列表')
    notify_type = fields.Selection([
        ('1', '提单时抄送'),
        ('2', '单据通过后抄送'),
        ('3', '提单和单据通过后抄送'),
    ])
    summary_info_ids = fields.One2many('wxwork.approval.template.summary', 'template_id', string='模板摘要')

    related_records_count = fields.Integer(string="关联记录数", compute="_compute_related_records_count")

    @api.depends("summary_info_ids")
    def _compute_related_records_count(self):
        for record in self:
            record.related_records_count = len(record.summary_info_ids)


