# -*- 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.controls'
    _description = "企业微信审批模板控件"

    template_id = fields.Many2one('wxwork.approval.template', string='模板')
    property_id = fields.One2many('property.model', 'control_id', string='控件属性')
    config_id = fields.One2many('config.model', 'control_id', string='控件配置')


# 控件属性模型
class PropertyModel(models.Model):
    _name = 'property.model'
    _rec_name = "title"

    CONTROL_TYPE_SELECTION = [
        ('Text', '文本'),
        ('Textarea', '多行文本'),
        ('Number', '数字'),
        ('Money', '金额'),
        ('Date', '日期/日期+时间'),
        ('Selector', '单选/多选'),
        ('Contact', '成员/部门'),
        ('Tips', '说明文字'),
        ('File', '附件'),
        ('Table', '明细'),
        ('Attendance', '假勤控件'),
        ('Vacation', '请假控件'),
        ('Location', '位置'),
        ('RelatedApproval', '关联审批单'),
        ('Formula', '公式'),
        ('DateRange', '时长')
    ]
    control_type = fields.Selection(CONTROL_TYPE_SELECTION, string='控件类型')
    control_id = fields.Many2one('wxwork.approval.template.controls', string='控件')
    unique_control_id = fields.Char(string='控件ID')
    title = fields.Char(string='控件名称', translate=True)
    placeholder = fields.Char(string='控件说明', translate=True)
    require = fields.Boolean(string='是否必填')
    un_print = fields.Boolean(string='是否参与打印')
    un_replace = fields.Boolean(string='不替换')
    display = fields.Boolean(string='显示')
    child_control_id = fields.Many2one('wxwork.approval.table.config', string='明细子控件')


# 控件配置模型
class ConfigModel(models.Model):
    _name = 'config.model'

    control_id = fields.Many2one('wxwork.approval.template.controls', string='控件')
    # # 在此添加其他字段以存储不同控件类型的附加类型和属性
    # date_type = fields.Selection([('day', '日期'), ('hour', '日期+时间')], string='日期类型')
    #
    # selector_type = fields.Selection([('single', '单选'), ('multi', '多选')], string='选择器类型')
    # options = fields.One2many('config.option', 'config_id', string='选项')
    #
    # contact_type = fields.Selection(
    #     [('single', '单选'), ('multi', '多选')],
    #     string='联系人类型')
    # contact_mode = fields.Selection(
    #     [('user', '成员'), ('department', '部门')],
    #     string='联系人模式')
    #
    # # table_children = fields.One2many('config.child', 'config_id', string='明细子控件')
    # table_children = fields.One2many('property.model', 'child_control_id', string='明细子控件')
    #
    # attendance_type = fields.Selection([('1', '请假'), ('3', '出差'), ('4', '外出'), ('5', '加班')], string='假勤组件类型')
    # attendance_date_range_type = fields.Selection([('hour', '分钟'), ('halfday', '上午/下午')], string='假勤时间刻度')
    #
    # vacation_list = fields.One2many('config.option', 'config_id', string='假期类型列表')
    # 增加一个wxwork.approval.date.config的关联字段
    date_type = fields.One2many('wxwork.approval.date.config', 'config_id', string='日期控件配置')
    # 增加一个与wxwork.approval.selector.config的关联字段
    selector_type = fields.One2many('wxwork.approval.selector.config', 'config_id', string='选择器控件配置')
    # 增加一个与wxwork.approval.contact.config的关联字段
    contact_type = fields.One2many('wxwork.approval.contact.config', 'config_id', string='联系人控件配置')
    # 增加一个与wxwork.approval.table.config的关联字段
    table_type = fields.One2many('wxwork.approval.table.config', 'config_id', string='明细控件配置')
    # 增加一个与wxwork.approval.attendance.config的关联字段
    attendance_type = fields.One2many('wxwork.approval.attendance.config', 'config_id', string='假勤控件配置')
    # 增加一个与wxwork.approval.file.config的关联字段
    file_type = fields.One2many('wxwork.approval.file.config', 'config_id', string='附件控件配置')
    # 增加一个与wxwork.approval.table.config的Many2one关联字段
    table_child = fields.Many2one('wxwork.approval.table.config', string='明细子控件配置')


class ConfigOption(models.Model):
    _name = 'config.option'

    key = fields.Char(string='选项Key')
    text = fields.Char(string='选项Text')
    lang = fields.Char(string='语言')
    config_id = fields.Many2one('config.model', string='配置')


class ConfigChild(models.Model):
    _name = 'config.child'

    control = fields.Char(string='控件类型')
    control_id = fields.Char(string='控件ID')
    # 其他属性...


# Date控件config
class DateConfig(models.Model):
    _name = 'wxwork.approval.date.config'

    # 增加一个与config.model的关联字段
    config_id = fields.Many2one('config.model', string='控件')
    date_type = fields.Selection([('day', '日期'), ('hour', '日期+时间')], string='日期类型')


# Selector控件（单选/多选控件）config
class SelectorConfig(models.Model):
    _name = 'wxwork.approval.selector.config'

    # 增加一个与config.model的关联字段
    config_id = fields.Many2one('config.model', string='控件')
    selector_type = fields.Selection([('single', '单选'), ('multi', '多选')], string='选择器类型')
    selector_exp_type = fields.Integer(string='选择器表达式类型', default=0)
    options = fields.One2many('selector.option', 'config_id', string='选项')


# Selector控件（单选/多选控件）选项
class SelectorOption(models.Model):
    _name = 'selector.option'

    key = fields.Char(string='选项Key')
    text = fields.Char(string='选项Text')
    lang = fields.Char(string='语言')
    config_id = fields.Many2one('wxwork.approval.selector.config', string='配置')


# Contact控件（成员/部门控件）config
class ContactConfig(models.Model):
    _name = 'wxwork.approval.contact.config'

    # 增加一个与config.model的关联字段
    config_id = fields.Many2one('config.model', string='控件')
    contact_type = fields.Selection([('single', '单选'), ('multi', '多选')], string='选择器类型')
    contact_mode = fields.Selection([('user', '成员'), ('department', '部门')], string='联系人类型')


# Table控件（明细控件）config
class TableConfig(models.Model):
    _name = 'wxwork.approval.table.config'

    # 增加一个与config.model的关联字段
    config_id = fields.Many2one('config.model', string='控件')
    # table_children = fields.One2many('config.child', 'config_id', string='明细子控件')
    table_children = fields.One2many('property.model', 'child_control_id', string='明细属性控件')
    # 增加一个与config.model的One2many关联字段
    table_children_config = fields.One2many('config.model', 'table_child', string='明细配置控件')
    # stat_field = fields.Many2one('config.model', string='统计字段')


# Attendance控件（假勤控件）config
class AttendanceConfig(models.Model):
    _name = 'wxwork.approval.attendance.config'

    # 增加一个与config.model的关联字段
    config_id = fields.Many2one('config.model', string='控件')
    attendance_type = fields.Selection([('1', '请假'), ('3', '出差'), ('4', '外出'), ('5', '加班')], string='假勤组件类型')
    attendance_date_range_type = fields.Selection([('hour', '分钟'), ('halfday', '上午/下午')], string='假勤时间刻度')


# Vacation控件（假勤控件）config
class VacationConfig(models.Model):
    _name = 'wxwork.approval.vacation.config'

    # 增加一个与wxwork.approval.template的关联字段
    template_id = fields.Many2one('wxwork.approval.template', string='模板')
    # control_id = fields.Many2one('wxwork.approval.template.controls', string='控件')
    # vacation_list = fields.One2many('config.option', 'config_id', string='假期类型列表')
    vacation_id = fields.Integer(string='假期ID')
    name = fields.Char(string='假期名称')
    lang = fields.Char(string='语言')

# file控件（文件控件）config
class FileConfig(models.Model):
    _name = 'wxwork.approval.file.config'

    # 增加一个与config.model的关联字段
    config_id = fields.Many2one('config.model', string='控件')
    # control_id = fields.Many2one('wxwork.approval.template.controls', string='控件')
    # vacation_list = fields.One2many('config.option', 'config_id', string='假期类型列表')
    # file_id = fields.Integer(string='文件ID')
    # name = fields.Char(string='文件名称')
    # lang = fields.Char(string=
    is_only_photo = fields.Boolean(string='是否只能上传图片')


class ApproverLine(models.Model):
    _name = 'wxwork.approval.approver_line'

    main_id = fields.Many2one('wxwork.approval.template', string='主模型')
    userid = fields.Many2many('res.users', string='审批人userid列表')
    attr = fields.Selection([('1', '或签'), ('2', '会签')], string='节点审批方式')


class ResUserAddShow(models.Model):
    _inherit = 'res.users'
    _rec_name = 'name'
