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


class JptipAttendance(models.Model):
    _name = 'jt.attendance'

    name = fields.Char('點名', size=256)
    batch_id = fields.Many2one(
        'jt.batch', '課程', store=True, required=True)
    course_line_id = fields.Many2one(
        'jt.course.line', '級數', store=True)
    timetable_id = fields.Many2one(
        'class.timetable', '課堂', store=True, required=True)
    start_time = fields.Many2one(
        'jt.time.session.manager', related='timetable_id.class_time_begin')
    # start_time = fields.Many2many('jt.time.session.manager', string='上課時間')
    lines_id = fields.One2many(
        'jt.attendance.lines', 'attendance_id')
    company_id = fields.Many2one(related='batch_id.company_id', string='Company', store=True, readonly=True)
    curriculum_id = fields.Many2one('curriculum')

    @api.multi
    def exam_lines(self):
        lines = []
        for exam in self:
            if any(exam.batch_id.st_batch_id):
                for st in exam.batch_id.st_batch_id.filtered(lambda x: x.state == 'ing' and x.course_line_id.id == self.course_line_id.id):
                    line = self.env['jt.attendance.lines'].create({
                        'attendance_id': exam.id,
                        'student_id': st.student_id.id,
                        'attendance_category': 'b',
                        'attendance_time': self.timetable_id.class_time_begin.id,
                    })
                    lines.append(line.id)
        return lines

    # @api.onchange('batch_id')
    # def onchange_student(self):
    #     if not self.batch_id:
    #         self.lines_id.unlink()
    #     else:
    #         self.lines_id.unlink()
    #         lines = self.exam_lines()
    #         self.update({
    #             'timetable_id': False,
    #             'start_time': [(6, 0, self.batch_id.class_time.ids)],
    #             'lines_id': [(6, 0, lines)]
    #         })

    @api.onchange('timetable_id')
    def onchange_name(self):
        if not self.timetable_id:
            self.name = False
        else:
            self.name = '%s' % self.timetable_id.name
            self.course_line_id = self.timetable_id.course_line_id.id
            lines = self.exam_lines()
            self.update({
                # 'timetable_id': False,
                # 'start_time': [(6, 0, self.timetable_id.class_time_begin)],
                'lines_id': [(6, 0, lines)]
            })

    @api.model
    def create(self, values):
        if not values.get('name', False) or values['name'] == _('New'):
            values['name'] = '%s' % self.env['class.timetable'].browse(values.get('timetable_id')).name
        if not values.get('lines_id', False):
            values['lines_id'] = [(6, 0, self.exam_lines())]
        Attendance = super(JptipAttendance, self).create(values)
        return Attendance


class JptipAttendanceLines(models.Model):
    _name = 'jt.attendance.lines'

    attendance_id = fields.Many2one('jt.attendance', ondelete="cascade")
    student_id = fields.Many2one('jt.student', string='學生')
    attendance_category = fields.Selection([('a', '出席'), ('b', '未出席'), ('c', '遲到'), ('d', '早退')], default='b',
                                           string='出席狀況')
    attendance_time = fields.Many2one('jt.time.session.manager', string='上課時間')
    note = fields.Text('Note')
    is_makeup_class = fields.Boolean(string='補課')
    company_id = fields.Many2one(related='attendance_id.company_id', string='Company', store=True, readonly=True)

    def registration_makeup(self):
        if not self.is_makeup_class:
            self.is_makeup_class = True
        self.env['jt.makeup'].create({
            'date': datetime.date.today(),
            'student_id': self.student_id.id,
            'class_date': self.attendance_id.timetable_id.date,
            'class_time': self.attendance_id.timetable_id.class_time_begin.name,
            'class_name': self.attendance_id.batch_id.id,
            'course_line_id': self.attendance_id.course_line_id.id,
            'teacher_id': self.attendance_id.timetable_id.teacher.id or self.attendance_id.timetable_id.teacher1.id,
            'room_id': self.attendance_id.timetable_id.class_room.id,
        })

    @api.multi
    def unlink(self):
        return super(JptipAttendanceLines, self).unlink()
