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


class JptipCloseHoliday(models.Model):
    _name = 'jt.close.holiday.wizard'

    name = fields.Char(string='Name')
    timetable_lines = fields.One2many('jt.close.holiday.lines', 'wizard_id', string='課程')
    all = fields.Boolean(string='All')
    holiday = fields.Many2one('jt.holiday', string='假日')
    company_id = fields.Many2one('res.company', 'Company')

    @api.model
    def default_get(self, fields):
        res = super(JptipCloseHoliday, self).default_get(fields)
        lines = []
        holiday = self.env['jt.holiday'].browse(self.env.context.get('active_id'))
        res.update({'holiday': holiday.id})
        class_calc = self.env['class.timetable'].search(
            [('date', '=', holiday.schedule_date), ('state', '=', 'open'), ('company_id', '=', holiday.company_id.id)])
        for x in class_calc:
            lines.append((0, 0, {
                'timetable': x.id,
                'class_name': x.class_name.id,
                'date': x.date,
                'time': x.class_time_begin.id,
                'selected':1,
                'company_id': holiday.company_id.id
            }))
        res.update({'timetable_lines': lines})
        return res

    @api.multi
    def close_button(self, vals):
        batch = self.env['jt.batch']
        close_tt = []
        lines = []
        for line in self.timetable_lines.filtered('selected'):
            close_tt.append((line.timetable))
        class_calc = self.env['class.timetable'].search([('date', '=', self.holiday.schedule_date), ('state', '=', 'open'), ('company_id', '=', self.holiday.company_id.id)], order ='id asc')
        doc =[]
        flag = False
        for x in self.holiday.jt_holiday_line:
            lines.append((0, 0, {
                'flag': x.flag,
                'timetable': x.timetable.id,
                'class_name': x.class_name.id,
                'date': x.date,
                'time': x.time.id,
                'company_id': self.holiday.company_id.id
            }))
        for x in class_calc:
            if x.class_name.id not in doc:
                doc.append(x.class_name.id)
                flag = True
            elif x.class_name.id in doc:
                flag = False
            lines.append((0, 0, {
                'flag': flag,
                'timetable': x.id,
                'class_name': x.class_name.id,
                'date': x.date,
                'time': x.class_time_begin.id,
                'company_id': self.holiday.company_id.id
            }))
        self.holiday.update({'jt_holiday_line': lines})
        return batch.close_replace_timetable(close_tt,self.holiday.schedule_date)

class JptipCloseHolidayLines(models.Model):
    _name = 'jt.close.holiday.lines'

    wizard_id = fields.Many2one('jt.close.holiday.wizard')
    timetable = fields.Many2one('class.timetable', string='課表')
    class_name = fields.Many2one('jt.batch', string='課程')
    time = fields.Many2one('jt.time.session.manager', string='上課時間')
    date = fields.Date(string='日期')
    teacher = fields.Many2one('jt.teacher', string='老師', related='timetable.teacher')
    class_room = fields.Many2one('jptip.class.room', string='教室', related='timetable.class_room')
    selected = fields.Boolean(string='Select')
    company_id = fields.Many2one('res.company', 'Company')
