# _*_ coding: utf-8 _*_
from odoo import models, fields, api
from datetime import *
import datetime
import collections

class WizardCurriculum(models.TransientModel):
    _name = 'wizard.curriculum'

    today = fields.Date(string="今日日期", default=fields.Date.today)
    company_id = fields.Many2one('res.company', 'Company', default=lambda self: self.env.user.company_id.id)

    def expand_curriculum(self):
        class_data = self.env['jt.batch'].search([('start_date','<=',self.today),('end_date','>=',self.today),('company_id','=',self.company_id.id)])
        for line in class_data:
            class_time = ''
            course_line_id = 0
            teacher = 0
            class_room = 0
            i = 0
            r = []
            s = collections.Counter()
            morning, afternoon, night = False, False, False
            for row in line.class_timetable.filtered(lambda x: x.date == self.today).sorted(key=lambda w: w.sequence):
                s[row.course_line_id.id] += 1
            course_list = list(s.items())
            for j in range(len(course_list)):
                for row in line.class_timetable.filtered(lambda x: x.date == self.today and x.course_line_id.id == int(course_list[j][0])).sorted(key=lambda w: w.sequence):
                    if i == 0:
                        class_time = row.class_time_begin.begin_time
                    elif i == len(line.class_timetable.filtered(lambda x: x.date == self.today).sorted(key=lambda w: w.sequence)) - 1:
                        class_time += '-' + row.class_time_begin.end_time
                    i += 1
                    r.append([0, 0, {
                        'company_id': self.company_id.id,
                        'batch_id':line.id,
                        'timetable_id': row.id,
                    }])
                    course_line_id = row.course_line_id.id
                    class_room = row.class_room.id
                    teacher = row.teacher.id
                    if row.class_time_begin.name[1] == u'早':
                        morning = True
                    elif row.class_time_begin.name[1] == u'午':
                        afternoon = True
                    elif row.class_time_begin.name[1] == u'晚':
                        night = True
                self.env['curriculum'].create({
                    'event_batch':line.event_batch,
                    'company_id': self.company_id.id,
                    'batch_id':line.id,
                    'date': self.today,
                    'class_time': class_time,
                    'course_line_id': course_line_id,
                    'teacher': teacher,
                    'class_room': class_room,
                    'morning': morning,
                    'afternoon': afternoon,
                    'night': night,
                    'details':r
                })
        return {'type': 'ir.actions.act_window_close'}