# -*- coding: utf-8 -*-
from odoo import api, fields, models
class ScheduleReport(models.AbstractModel):
    _name = 'report.jptip_core.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')
        doc = []
        for line in object.jt_holiday_line:
            if line.class_name.id not in doc:
                doc.append(line.class_name.id)
        class_name_id = self.env['jt.batch'].browse(doc)
        class_timetable = self.env['class.timetable'].search([('class_name', 'in', doc)])

        for line in class_name_id:
            first, tag = True, True
            j = 0
            sheet = workbook.add_worksheet(u'%s 課表' % line.name)
            sheet.set_column('A:A', 20)
            sheet.set_column('B:B', 10)
            sheet.set_column('C:D', 30)
            sheet.set_row(1, 45)
            res = []
            for o in class_timetable.filtered(lambda r: r.class_name.id == line.id):
                if o.date not in res:
                    res.append(o.date)
            for top in object.jt_holiday_line.filtered(lambda r: r.class_name.id == line.id):
                if tag == True:
                    for only_date in res:
                        for row in class_timetable.filtered(lambda x: x.course_line_id.id == top.timetable.course_line_id.id and x.class_name.id == line.id and x.date == only_date).sorted(key=lambda y: y.id):
                            if first == True:
                                start_date, end_date, week, begin_time, end_time = '', '', '', '', ''
                                for f in class_timetable.filtered(lambda t: t.course_line_id.id == top.timetable.course_line_id.id and (t.case_day_str == u'開課日' or t.case_day_str == u'修了日')).sorted(lambda p: p.id):
                                    if f.case_day_str == u'開課日':
                                        start_date = f.date
                                    elif f.case_day_str == u'修了日':
                                        end_date = f.date
                                for f in row.class_name.week.split(','):
                                    if f == '1':
                                        week += u'月'
                                    elif f == '2':
                                        week += u'火'
                                    elif f == '3':
                                        week += u'水'
                                    elif f == '4':
                                        week += u'木'
                                    elif f == '5':
                                        week += u'金'
                                    elif f == '6':
                                        week += u'土'
                                    elif f == '7':
                                        week += u'日'
                                one_temp = True
                                for f in row.class_name.class_time.sorted(key=lambda r: r.id, reverse=False):
                                    if one_temp == True:
                                        begin_time = f.begin_time
                                        one_temp = False
                                one_temp = True
                                for f in row.class_name.class_time.sorted(key=lambda r: r.id, reverse=True):
                                    if one_temp == True:
                                        end_time = f.end_time
                                        one_temp = False
                                sheet.merge_range(j, 0, j, 3, u'JPTIP日本語學校【%s】' % (row.class_name.company_id.name),format21)
                                j += 1
                                if row.class_name.cultural_experience:
                                    sheet.merge_range(j, 0, j, 3, u'【%s】%s %s\n ' % (row.class_name.code, row.class_name.name, top.timetable.course_line_id.code) + u'開講日：%s     週期：%s\n 修了日：%s     時間：%s～%s     文化體驗日：%s' % (start_date, week, end_date, begin_time, end_time, row.class_name.cultural_experience or ''), format22)
                                elif not row.class_name.cultural_experience:
                                    sheet.merge_range(j, 0, j, 3, u'【%s】%s %s\n ' % (row.class_name.code, row.class_name.name,top.timetable.course_line_id.code) + u'開講日：%s     週期：%s\n 修了日：%s     時間：%s～%s' % (start_date, week, end_date, begin_time, end_time), format22)
                                j += 1
                                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
                            if row.day_of_week:
                                if row.day_of_week == 'Mon':
                                    row.day_of_week = '一'
                                elif row.day_of_week == 'Tue':
                                    row.day_of_week = '二'
                                elif row.day_of_week == 'Wed':
                                    row.day_of_week = '三'
                                elif row.day_of_week == 'Thu':
                                    row.day_of_week = '四'
                                elif row.day_of_week == 'Fri':
                                    row.day_of_week = '五'
                                elif row.day_of_week == 'Sat':
                                    row.day_of_week = '六'
                                elif row.day_of_week == 'Sun':
                                    row.day_of_week = '日'
                            sheet.write(j, 0, '%s (%s)' % (row.date, row.day_of_week), format21)
                            class_data = self.env['class.timetable'].search([('class_name', '=', row.class_name.id),('course_line_id', '=',top.timetable.course_line_id.id),('date', '=', only_date)], order='id asc')
                            res = []
                            for w in class_data:
                                if w.course_seq:
                                    str = w.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 u in sub.course_line_id.num_line.filtered(lambda x: x.name in res):
                                    if u.number_of_times != 0:
                                        if u.number_of_times not in class_outline1:
                                            class_outline1.append(u.number_of_times)
                                    if u.outline:
                                        if u.outline not in class_outline2:
                                            class_outline2.append(u.outline)
                                    if u.learning_goal:
                                        if u.learning_goal not in class_outline3:
                                            class_outline3.append(u.learning_goal)
                                for w in class_outline1:
                                    temp1 += ('第 %s 回' % w) + '\n'
                                for w in class_outline2:
                                    temp2 += w + '\n'
                                for w in class_outline3:
                                    temp3 += w + '\n'
                            sheet.write(j, 1, temp1 or '', format22)
                            sheet.write(j, 2, temp2 or '', format22)
                            sheet.write(j, 3, temp3 or '', format22)
                            if row.class_name.cultural_experience == top and temp2 == '':
                                sheet.write(j, 3, u'文化體驗日', format22)
                            if not row.course_seq or row.course_seq == '':
                                sheet.write(j, 0, '%s (%s)' % (row.date, row.day_of_week), format21)
                                sheet.merge_range(j, 1, j, 3, '停課', format21)
                            break
                        j += 1
                    tag = False

class JPScheduleReport(models.AbstractModel):
    _name = 'report.jptip_core.jt_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.class_name.name)
        sheet.set_column('A:A', 20)
        sheet.set_column('B:B', 10)
        sheet.set_column('C:D', 30)
        sheet.set_row(1, 45)
        class_timetable = self.env['class.timetable'].search([('class_name', '=', object.class_name.id),('course_line_id','=',object.course_line_id.id)])
        doc = []
        for line in class_timetable:
            if line.date not in doc:
                doc.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.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 or ''), 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.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 doc:
            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
                if line.day_of_week:
                    if line.day_of_week == 'Mon':
                        line.day_of_week = '一'
                    elif line.day_of_week == 'Tue':
                        line.day_of_week = '二'
                    elif line.day_of_week == 'Wed':
                        line.day_of_week = '三'
                    elif line.day_of_week == 'Thu':
                        line.day_of_week = '四'
                    elif line.day_of_week == 'Fri':
                        line.day_of_week = '五'
                    elif line.day_of_week == 'Sat':
                        line.day_of_week = '六'
                    elif line.day_of_week == 'Sun':
                        line.day_of_week = '日'
                sheet.write(j, 0, u'%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.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 += u'第 %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, u'文化體驗日', format22)
                if not line.course_seq or line.course_seq == '':
                    sheet.write(j, 0, u'%s (%s)' % (line.date, line.day_of_week), format21)
                    sheet.merge_range(j, 1, j, 3, u'停課', format21)
                break
            j += 1