from odoo.exceptions import ValidationError
from odoo import models, api, fields, _

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

    visa = fields.Many2one(comodel_name='visa.type', string='簽證', track_visibility='onchange')
    visa1 = fields.Char(string='簽證', track_visibility='onchange')
    remarks = fields.Text(string='備註', track_visibility='onchange')
    registration_date = fields.Date(string='報名日期', track_visibility='onchange')
    school_confirmation = fields.Selection(selection=[(1, '是'), (2, '否')], string='學校確認', track_visibility='onchange')
    request_object = fields.Many2one(comodel_name='request.object', string='請求對象', track_visibility='onchange')
    departure_month = fields.Date(string='出發月份', track_visibility='onchange')
    during_school = fields.Many2one(comodel_name='during.school', string='就學期間', track_visibility='onchange')
    request_summary_ids = fields.One2many(comodel_name='request.summary', inverse_name='crm_id',string='學生總表')
    student_len = fields.Integer(compute='_compute_student_count')
    crm_register_id = fields.Many2one(comodel_name='crm.register', string='關聯商機暫存')
    order_id = fields.Many2one(comodel_name='sale.order', string='銷售訂單編號')
    seminar_id = fields.Many2one(comodel_name='seminar', string='說明會梯次')
    margin = fields.Text(string='保證金處理備註')
    is_return_margin = fields.Boolean(string='保證金已退回')
    selected_materials_jpy= fields.Integer(string='已收選考料(日幣)', track_visibility='onchange')
    is_registration_system = fields.Boolean(string='已轉入請求書總表', track_visibility='onchange', store = True)
    stage_id = fields.Many2one(track_visibility='onchange')

    hope_service = fields.Char(string='希望服務項目', track_visibility='onchange')
    hope_location = fields.Char(string='希望諮詢地點')
    hope_departure = fields.Date(string='預定出發年月')
    hope_area = fields.Char(string='想去的地區')
    hope_jobcondition = fields.Text(string='其他希望工作條件')
    other_issue = fields.Text(string='其他欲詢問的問題')
    country = fields.Char(string='現居地')
    # visa = fields.Char(string='所持簽證')
    jp_level = fields.Char(string='日文程度')
    hope_job = fields.Char(string='期望職缺')
    event_plan_id = fields.Many2one(comodel_name='event.plan', string='關聯的活動', track_visibility='onchange')
    crm_source_id = fields.Many2one(comodel_name='utm.source', string='商機來源')
    date_deadline = fields.Date(string='預期結束', track_visibility='onchange')
    select_location = fields.Char(string='地區')
    select_kind = fields.Char(string='分類')

    gender = fields.Char(string='性別')
    highest_education = fields.Char(string='最高學歷')
    status = fields.Char(string='學籍')
    school_name = fields.Char(string='校名')
    department = fields.Char(string='科系')
    birth_date = fields.Char(string='出生年')
    new1 = fields.Char(string='預設1', track_visibility='onchange')
    new2 = fields.Char(string='預設2', track_visibility='onchange')
    new3 = fields.Char(string='預設3', track_visibility='onchange')
    new4 = fields.Char(string='預設4', track_visibility='onchange')
    new5 = fields.Char(string='預設5', track_visibility='onchange')

    reservation_date = fields.Date(string='預約日期', default=fields.Date.today())
    auditing_date = fields.Date(string='試聽日期')
    course_id = fields.Many2one(comodel_name='jt.course', string='試聽課程')
    class_id = fields.Many2one(comodel_name='jt.batch', string='試聽班級')
    class_course = fields.Many2one('jt.course.line', string='試聽級數')
    class_timetable_id = fields.Many2one(comodel_name='class.timetable', string='所屬課堂')
    number_of_students = fields.Integer(string='上課人數', compute='compute_students_num')
    auditing_number_of_students = fields.Integer(string='試聽人數', compute='compute_students_num')
    class_capacity = fields.Integer(string='可容納人數', compute='compute_students_num')
    auditing_ids = fields.One2many(comodel_name='crm.auditing', inverse_name='crm_id', string='試聽紀錄')
    priority = fields.Selection(track_visibility='onchange')
    tag_ids = fields.Many2many(track_visibility='onchange')
    team_id = fields.Many2one(track_visibility='onchange')
    active = fields.Boolean(track_visibility='onchange')

    @api.depends('class_course','class_timetable_id')
    def compute_students_num(self):
        for line in self:
            line.number_of_students = len(line.class_id.st_batch_id.filtered(lambda x: x.course_line_id == line.class_course.id))
            num = line.env['crm.auditing'].search([('class_id','=',line.class_id.id),('class_timetable_id','=',line.class_timetable_id.id)])
            line.auditing_number_of_students = len(num)
            line.class_capacity = line.class_timetable_id.class_room.class_member

    def confirm(self):
        if not self.class_timetable_id:
            raise ValidationError(u'請選擇課堂')
        auditing_id = self.env['crm.auditing'].search([('crm_id','=',self.id),('course_id','=',self.course_id.id),('class_id','=',self.class_id.id),('class_course','=',self.class_course.id),('class_timetable_id','=',self.class_timetable_id.id)])
        if not auditing_id:
            self.env['crm.auditing'].create({
                'crm_id': self.id,
                'reservation_date': self.reservation_date,
                'auditing_date': self.auditing_date,
                'course_id': self.course_id.id,
                'class_id': self.class_id.id,
                'class_course': self.class_course.id,
                'class_timetable_id': self.class_timetable_id.id
            })
            self.reservation_date, self.auditing_date, self.course_id, self.class_id, self.class_course, self.class_timetable_id = None, None, None, None, None, None
        elif auditing_id:
            raise ValidationError(u'此課程已試聽過，無法再試聽')
    # @api.multi
    # def price_list(self):
    #     action = self.env.ref('sale_order_request_view_action').read()[0]
    #     action['context'] = {'default_partner_id': self.partner_id.id, 'default_team_id': self.team_id.id}
    #     return action
    #     if self.team_id.name == '留遊學長期-台中' or self.team_id.name == '留遊學長期-台北' or self.team_id.name == '留遊學長期-高雄':
    #         action = self.env.ref('jptip_request.study_tour_long_term_form_action').read()[0]
    #         action['context'] = {'default_partner_id': self.partner_id.id, 'default_team_id': self.team_id.id}
    #         return action
    #     elif self.team_id.name == '留遊學短期-台中' or self.team_id.name == '留遊學短期-台北' or self.team_id.name == '留遊學短期-高雄':
    #         action = self.env.ref('jptip_request.study_tour_short_term_form_action').read()[0]
    #         action['context'] = {'default_partner_id': self.partner_id.id, 'default_team_id': self.team_id.id}
    #         return action
    #     elif self.team_id.name == '住宿服務-台中' or self.team_id.name == '住宿服務-台北' or self.team_id.name == '住宿服務-高雄':
    #         action = self.env.ref('jptip_request.lodging_view_form_action').read()[0]
    #         action['context'] = {'default_partner_id': self.partner_id.id, 'default_team_id': self.team_id.id}
    #         return action
    #     elif self.team_id.name == '打工工作-台中' or self.team_id.name == '打工工作-台北' or self.team_id.name == '打工工作-高雄' or self.team_id.name == '正職工作-台中' or self.team_id.name == '正職工作-台北' or self.team_id.name == '正職工作-高雄':
    #         action = self.env.ref('jptip_request.work_plan_view_form_action').read()[0]
    #         action['context'] = {'default_partner_id': self.partner_id.id, 'default_team_id': self.team_id.id}
    #         return action
    #     elif self.team_id.name == '3.0 補習班-台北':
    #         action = self.env.ref('jptip_request.work_holiday_view_form_action').read()[0]
    #         action['context'] = {'default_partner_id': self.partner_id.id,'default_team_id': self.team_id.id}
    #         return action
    #     elif self.team_id.name == '3.0 補習班-高雄':
    #         action = self.env.ref('jptip_request.work_holiday_view_form_action').read()[0]
    #         action['context'] = {'default_partner_id': self.partner_id.id,'default_team_id': self.team_id.id}
    #         return action

    # 計算產生的學生總表有幾張
    def _compute_student_count(self):
        for partner in self:
            partner.student_len = len(partner.request_summary_ids)

    # 查看學生總表
    def open_student_list(self):
        action = self.env.ref('jptip_request.request_summary_view_action').read()[0]
        action['domain'] = [('crm_id', '=', self.id)]
        return action
    
# 20191224依需求調整自動填入內容Smith
    def wizard_registration_system(self):
        action = self.env.ref('jptip_request.registration_system_action').read()[0]
        action['context'] = {'default_partner_id': self.partner_id.id,
                            'default_registration_date': self.registration_date,
                            'default_departure_month': self.departure_month,
                            'default_start_date': self.departure_month,
                            'default_request_object': self.request_object.id,
                            'default_selected_materials_jpy':self.selected_materials_jpy,
                            'default_remarks': self.remarks,
                            'default_visa': self.visa.id,
                            'default_study_period': self.during_school.name,
                            'default_during_school': self.during_school.id}
        return action


class CrmAuditing(models.Model):
    _name = 'crm.auditing'

    crm_id = fields.Many2one(comodel_name='crm.lead')
    reservation_date = fields.Date(string='預約日期', default=fields.Date.today())
    auditing_date = fields.Date(string='試聽日期')
    course_id = fields.Many2one(comodel_name='jt.course', string='試聽課程')
    class_id = fields.Many2one(comodel_name='jt.batch', string='試聽班級')
    class_course = fields.Many2one('jt.course.line', string='試聽級數')
    class_timetable_id = fields.Many2one(comodel_name='class.timetable', string='所屬課表')
