# -*- coding: utf-8 -*-
from odoo import api, fields, models
class ScheduleReport(models.AbstractModel):
    _name = 'report.jptip_core.sub_curriculum_report.xlsx'
    _inherit = 'report.report_xlsx.abstract'

    def generate_xlsx_report(self,workbook, data, object):
        # 文件網址: https://xlsxwriter.readthedocs.io/format.html#set_align
        format21 = workbook.add_format({'font_size': 10, 'align': 'vcenter', 'text_wrap': 1, 'bold': True, 'top': 1, 'left': 1, 'right': 1, 'bottom': 1})
        format22 = workbook.add_format({'font_size': 10, 'align': 'vcenter', 'text_wrap': 1, 'bold': True, 'top': 1, 'left': 1, 'right': 1, 'bottom': 1})
        format21.set_align('center')
        format22.set_align('left')
        sheet = workbook.add_worksheet(u'%s 停課課表' % object.jt_holiday_id.type_date)
        sheet.set_column('A:A', 20)
        sheet.set_column('B:B', 10)
        sheet.set_column('C:D', 30)
        sheet.set_row(1, 45)
        doc = []
        for line in object:
            if line.class_name.id not in doc:
                doc.append(line.class_name.id)
        class_timetable = self.env['class.timetable'].search([('class_name', 'in', doc),('course_line_id','=',object.timetable.course_line_id.id)])
        res =[]
        for line in class_timetable:
            if line.date not in res:
                res.append(line.date)
        j = 0
        first = True
        start_date, end_date, week, begin_time, end_time = '', '', '', '', ''
        for line in class_timetable.filtered(lambda x: x.case_day_str == u'開課日' or x.case_day_str == u'修了日').sorted(lambda y: y.id):
            if line.case_day_str == u'開課日':
                start_date = line.date
            elif line.case_day_str == u'修了日':
                end_date = line.date
        for line in object.class_name.week.split(','):
            if line == '1':
                week += u'月'
            elif line == '2':
                week += u'火'
            elif line == '3':
                week += u'水'
            elif line == '4':
                week += u'木'
            elif line == '5':
                week += u'金'
            elif line == '6':
                week += u'土'
            elif line == '7':
                week += u'日'
        for line in object.class_name.class_time.sorted(key=lambda r: r.id, reverse=False):
            begin_time = line.begin_time
            break
        for line in object.class_name.class_time.sorted(key=lambda r: r.id, reverse=True):
            end_time = line.end_time
            break
        sheet.merge_range(j, 0, j, 3, u'JPTIP日本語學校【%s】' % (object.class_name.company_id.name), format21)
        j += 1
        if object.class_name.cultural_experience:
            sheet.merge_range(j, 0, j, 3, u'【%s】%s %s\n ' % (object.class_name.code, object.class_name.name,object.timetable.course_line_id.code) + u'開講日：%s     週期：%s\n 修了日：%s     時間：%s～%s     文化體驗日：%s' % (start_date, week, end_date, begin_time, end_time, object.class_name.cultural_experience), format22)
        elif not object.class_name.cultural_experience:
            sheet.merge_range(j, 0, j, 3, u'【%s】%s %s\n ' % (object.class_name.code, object.class_name.name,object.timetable.course_line_id.code) + u'開講日：%s     週期：%s\n 修了日：%s     時間：%s～%s' % (start_date, week, end_date, begin_time, end_time), format22)
        j += 1
        for top in res:
            for line in class_timetable.filtered(lambda x: x.date == top).sorted(lambda y: y.id):
                if first == True:
                    sheet.write(j, 0, u'上課日期', format21)
                    sheet.write(j, 1, u'回目', format21)
                    sheet.write(j, 2, u'課綱', format21)
                    sheet.write(j, 3, u'學習目標', format21)
                    j += 1
                    first = False
                sheet.write(j, 0, '%s (%s)' % (line.date, line.day_of_week), format21)
                class_data = self.env['class.timetable'].search([('class_name', '=', object.class_name.id), ('course_line_id', '=', object.timetable.course_line_id.id),('date', '=', top)], order='id asc')
                res = []
                for row in class_data:
                    if row.course_seq:
                        str = row.course_seq.split('：')
                        if str[1] not in res:
                            res.append(str[1])
                for sub in class_data:
                    temp1, temp2, temp3, class_outline1, class_outline2, class_outline3 = '', '', '', [], [], []
                    for row in sub.course_line_id.num_line.filtered(lambda x: x.name in res):
                        if row.number_of_times != 0:
                            if row.number_of_times not in class_outline1:
                                class_outline1.append(row.number_of_times)
                        if row.outline:
                            if row.outline not in class_outline2:
                                class_outline2.append(row.outline)
                        if row.learning_goal:
                            if row.learning_goal not in class_outline3:
                                class_outline3.append(row.learning_goal)
                    for row in class_outline1:
                        temp1 += ('第 %s 回' % row) + '\n'
                    for row in class_outline2:
                        temp2 += row + '\n'
                    for row in class_outline3:
                        temp3 += row + '\n'
                sheet.write(j, 1, temp1 or '', format22)
                sheet.write(j, 2, temp2 or '', format22)
                sheet.write(j, 3, temp3 or '', format22)
                if object.class_name.cultural_experience == top and temp2 == '':
                    sheet.write(j, 3, '文化體驗日', format22)
                if not line.course_seq or line.course_seq == '':
                    sheet.write(j, 0, '%s (%s)' % (line.date, line.day_of_week), format21)
                    sheet.merge_range(j, 1, j, 3, '停課', format21)
                break
            j += 1