
from odoo import models, fields, api


class sos_marketing(models.Model):
    _name = 'sos_marketing_leads'
    _description = 'Marketing Leads'
    _rec_name = 'company_name'

    company_name = fields.Char(string="Company Name")
    website_url = fields.Char(string="Website URL")
    country = fields.Many2one(
    'res.country',
    string='Country',
    default=lambda self: self.env['res.country'].search([('code', '=', 'IN')], limit=1)
)
    location = fields.Char(string="City/State")
    expo_option=fields.Boolean(string="Expo",default="True")
    conference_option=fields.Boolean(string="Conference")
    linkedin_groups_option=fields.Boolean(string="LinkedIn Groups")
    linkedin_poll_option=fields.Boolean(string="LinkedIn Poll")
    direct_company=fields.Boolean(string="Direct Company")
    expo_participated_option=fields.Boolean(string="Expo [Paricipated]")
    #Expo
    expo_name = fields.Char(string="Expo Name")
    expo_date_from = fields.Date(string="Expo Date From")
    expo_date_to = fields.Date(string="Expo Date To")
    expo_Location = fields.Char(string="Expo Location")
    #Conference
    conference_name = fields.Char(string="Conference Name")
    conference_date_from = fields.Date(string="Conference Date From")
    conference_date_to = fields.Date(string="Conference Date To")
    conference_Location = fields.Char(string="Conference Location")
    #Linkedin Group
    linkedin_group_name = fields.Char(string="LinkedIn Group Name")
    #Linkedin poll
    linkedin_post_url =  fields.Char(string="LinkedIn Post URL")
    vertical_domain = fields.Many2many('sos_vertical_domain',string="Vertical/Domain", ondelete="cascade")
    line_ids_contacts = fields.One2many('sos_marketing_leads_contact_lines', 'ref_id', string="Contact Details",copy=True)
    move_to_inside_sales = fields.Many2many('res.users',string="Move Lead to", domain=lambda self: [('groups_id', 'in', self.env.ref('sos_inventory.sos_inside_sales_user').ids + self.env.ref('sos_inventory.sos_sales_user').ids)])
    cc_mail = fields.Many2one('res.users',string="CC to", domain=lambda self: [('groups_id', 'in', self.env.ref('sos_inventory.sos_sales_user').ids + self.env.ref('sos_inventory.sos_inside_sales_user').ids)])
    move_div_display = fields.Boolean(default=False,string="Moved to Sales")
    moved_on = fields.Date(string="Moved On")
    source = fields.Selection(
            [
                ('Expo', 'Expo'), 
                ('Conference', 'Conference'), 
                ('LinkedIn Groups', 'LinkedIn Groups'),  
                ('LinkedIn Poll', 'LinkedIn Poll'),
                ('Direct Company', 'Direct Company'),
                ('Expo [Paricipated]', 'Expo [Paricipated]')

            ],
            string="Source",default="Expo")
    @api.onchange('expo_option')
    def _onchange_expo(self):
        if self.expo_option:
            self.source = "Expo"
            self.conference_option = False
            self.linkedin_groups_option = False
            self.linkedin_poll_option = False
            self.direct_company = False
            self.expo_participated_option = False

    @api.onchange('conference_option')
    def _onchange_conference(self):
        if self.conference_option:
            self.source = "Conference"
            self.expo_option = False
            self.linkedin_groups_option = False
            self.linkedin_poll_option = False
            self.direct_company = False
            self.expo_participated_option = False

    @api.onchange('linkedin_groups_option')
    def _onchange_linkedin_groups(self):
        if self.linkedin_groups_option:
            self.source = "LinkedIn Groups"
            self.expo_option = False
            self.conference_option = False
            self.linkedin_poll_option = False
            self.direct_company = False
            self.expo_participated_option = False

    @api.onchange('linkedin_poll_option')
    def _onchange_linkedin_poll(self):
        if self.linkedin_poll_option:
            self.source = "LinkedIn Poll"
            self.expo_option = False
            self.conference_option = False
            self.linkedin_groups_option = False
            self.direct_company = False
            self.expo_participated_option = False

    @api.onchange('direct_company')
    def _onchange_direct_company(self):
        if self.direct_company:
            self.source = "Direct Company"
            self.expo_option = False
            self.conference_option = False
            self.linkedin_groups_option = False
            self.linkedin_poll_option = False
            self.expo_participated_option = False

    @api.onchange('expo_participated_option')
    def _onchange_expo_participated(self):
        if self.expo_participated_option:
            self.source = "Expo [Paricipated]"
            self.expo_option = False
            self.conference_option = False
            self.linkedin_groups_option = False
            self.linkedin_poll_option = False
            self.direct_company = False
    def action_move_to_inside_sales(self):
        if self.move_to_inside_sales:
            for user in self.move_to_inside_sales:
                body_html = f"""
                <p>Below <b>Lead</b> is moved to Inside-Sales</p>
                """
                subject = f"New Lead Received - {self.company_name}"
                send_email = self.env['sos_common_scripts']
                send_email.send_direct_email(self.env,"sos_marketing_leads",self.id,user.login,subject,body_html,self.cc_mail.login)
        new_record = self.env['sos_inside_sales_leads'].create({
            'company_name': self.company_name,
            'location':self.location,
            'website_url':self.website_url,
            'vertical_domain': [(6, 0, self.vertical_domain.ids)]
            })
        if new_record:
            if self.line_ids_contacts:
                for contact in self.line_ids_contacts:
                    self.env['sos_leads_contact_lines'].create({
                    'ref_id': new_record.id,
                    'name': contact.name,
                    'dept': contact.dept,
                    'email': contact.email,
                    'mobile_number': contact.mobile_number,
                    'set_as_primary': contact.set_as_primary,
                    'linkedin_profile': contact.linkedin_profile,
                    'remarks': contact.remarks
                    })
            self.move_div_display = True
            return {
                    'type': 'ir.actions.client',
                    'tag': 'display_notification',
                    'params': {
                        'message': "Moved Successfully",
                        'type': 'success',
                        'sticky': False
                    }
                }
class SOS__Marketing_Leads_Line(models.Model):
    _name = 'sos_marketing_leads_contact_lines'
    _description = 'Marketing Leads Contact Lines'

    ref_id = fields.Many2one('sos_marketing_leads', string="Marketing Leads", ondelete="cascade")
    name = fields.Char(string="Name")
    dept = fields.Char(string="Designation")
    email = fields.Char(string="Email Id")
    mobile_number = fields.Char(string="Contact No")
    set_as_primary = fields.Boolean(string="Set Primary")
    linkedin_profile = fields.Char(string="LinkedIn Profile")
    linkedin_invited_on = fields.Char(string="LinkedIn Conn Invited On")
    linkedin_invitation_status = fields.Selection(
            [
                ('Yet To Accept', 'Yet To Accept'),
                ('Accepted', 'Accepted'),
                ('No Match', 'No Match'),
                ('No Requirement', 'No Requirement')               
            ],
            string="LinkedIn Conn Invitation Status")
    remarks = fields.Text(string="Remarks")