# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import fields, models, api


class Lead(models.Model):
    _inherit = 'crm.lead'

    partner_id = fields.Many2one(
        'res.partner', string='Client', index=True, tracking=10,
        domain="['|', ('company_id', '=', False), ('company_id', '=', company_id)]",
        help="Linked partner (optional). Usually created when converting the lead. You can find a partner by its Name, TIN, Email or Internal Reference.")
    lead_no = fields.Char(string='Lead ID')
    scope = fields.Char(string='Scope (NIS)')
    professional_support = fields.Char(string='Professional Support')
    ref_summary_status = fields.Char(string='Referral Summary status')
    contract = fields.Selection([('Yes','Yes'),('No','No')], string='Contract')
    project_name = fields.Char(string='Project Name')
    project_scope = fields.Char(string='The scope of the project')
    act_project_manager_id = fields.Many2one('res.users', string='Actual Project Manager')
    project_manager_id = fields.Many2one('res.users', string='Project Manager')
    client_folder = fields.Char(string='Client Folder')
    start_date = fields.Date(string='Start Date')
    close_date = fields.Date(string='Close Date')
    admin_user = fields.Boolean(compute='get_admin_login')
    #user_id = fields.Many2one('res.users', string='Salesperson', index=True, tracking=True, default=lambda self: self.env.user)
    #set_readonly = fields.Boolean('Set Readonly', compute='_domain_check_user_group')

    _sql_constraints = [
        ('lead_no_uniq', 'unique(lead_no)', "Lead ID already exists !"),
        ('phone_uniq', 'unique(phone)', "Phone No already exists !"),
        ('email_from_uniq', 'unique(email_from)', "Email ID already exists !"),
    ]
    @api.onchange('type')
    def get_admin_login(self):
        #print('11111111111', self.env.context.get('uid'))
        group_list = self.env.ref('base.group_erp_manager')
        if (self.env.context.get('uid') in group_list.users.ids):
            self.admin_user = True
        else:
            self.admin_user = False

    """def _domain_check_user_group(self):
        print(self.user_has_groups('sales_team.group_sale_salesman'), self.user_has_groups('sales_team.group_sale_salesman_all_leads'), self.user_has_groups('group_sale_manager'))
        if self.user_has_groups('sales_team.group_sale_salesman') and \
                (not self.user_has_groups('sales_team.group_sale_salesman_all_leads') or not self.user_has_groups('group_sale_manager')):
            self.set_readonly = True
        return domain"""
