# _*_ coding: utf-8 _*_
# Author: Monica( nikochild@gmail.com)
from odoo import models, fields, api
from odoo.exceptions import UserError


class JptipBatchUpdateWizard(models.Model):
    _name = 'jt.batch.update.wizard'

    class_name = fields.Many2one(comodel_name='jt.batch', string='班級名稱')
    teacher_id = fields.Many2one('jt.teacher', string='講師')
    course_id = fields.Many2one(comodel_name='jt.course', string='所屬課程', required=True)
    limit = fields.Integer(string='每次上課堂數')
    reserved = fields.Integer(string='保留堂數')
    start_class_course = fields.Many2one('jt.course.line', string='開始級數')
    start_class_course_num = fields.Integer(string='開始堂數', default=1)
    end_class_course = fields.Many2one('jt.course.line', string='結束級數')
    end_class_course_num = fields.Integer(string='結束堂數')
    time_of_class = fields.Many2one(comodel_name='jt.timing', string='上課開始時間')
    class_time = fields.Many2many(comodel_name='jt.time.session.manager', string='課堂時間')
    start_date = fields.Date(string='開始日期', default=fields.Date.today())
    week = fields.Char(string='上課星期')
    class_room = fields.Many2one(comodel_name='jptip.class.room', string='教室')
    # company_id = fields.Many2one('res.company', 'Company', string='公司',
    #                              default=lambda self: self.env['res.company']._company_default_get())

    @api.model
    def default_get(self, fields):
        if len(self.env.context.get('active_ids', list())) > 1:
            raise UserError("一次只能更改一堂課")
        res = super(JptipBatchUpdateWizard, self).default_get(fields)
        batch = self.env['jt.batch'].browse(self.env.context.get('active_id'))
        if batch:
            res.update({
                'class_name': batch.id,
                'course_id': batch.course_id.id,
                'week': batch.week,
                'start_date': batch.start_date,
                # 'company_id': batch.company_id.id,
                # 'limit': batch.limit,
                'reserved': batch.reserved,
                'time_of_class': batch.time_of_class,
                'class_time': [(6, 0, batch.class_time.ids)],
            })
        return res

    @api.multi
    def get_update_timetable(self):
        act_close = {'type': 'ir.actions.act_window_close'}
        batch = self.env['jt.batch']
        start =  self.start_date
        end = ''
        week = self.week
        class_name = self.class_name
        time_list, holiday = batch.remove_holiday(start, end, week)
        timetable = batch._get_time_table(time_list, holiday, self)
        get_team = []
        if any(timetable):
            for new_t in timetable:
                new = self.env['class.timetable'].create(new_t)
                get_team.append(new)
            class_name.update({
                'end_class_course': self.end_class_course.id,
                'end_class_course_num': self.end_class_course_num
            })
            self.env['class.timetable.team'].get_team(get_team)
            self.env['jt.batch.outline']._get_batch_outline(class_name.id)
        return act_close

    @api.onchange('start_class_course')
    def end_course_onchange(self):
        self.end_class_course = self.start_class_course.id
        self.end_class_course_num = self.start_class_course.class_num


