var VPTimelineUjian = (function () {
    return {
        name: "timeline-ujian",
        props: ['id'],
        template: `
            <div ref="timelineujian"></div>
        `,
        data() {
            return {
                calendar: {
                    instance: null,
                    events: [],
                    resources: [],
                },


            }
        },
        mounted() {
            this.loadJadwal();
        },
        methods: {
            initCalendar() {
                this.calendar.instance = new FullCalendar.Calendar(this.$refs.timelineujian, {
                    timeZone: timezone,
                    schedulerLicenseKey: 'CC-Attribution-NonCommercial-NoDerivatives',
                    // the initial locale. of not specified, uses the first one
                    locale: 'id',
                    initialView: 'resourceTimelineDay',
                    events: this.events,
                    resourceAreaHeaderContent: 'Ruang',
                    resources: this.resources,
                });
                this.calendar.instance.render();
            },
            loadJadwal() {
                axios({
                    method: 'get',
                    url: this.$store.state.config.base_url + "/panel/ujian/jadwal/all?startDate=" + mm().subtract(1, 'w').hours(0).minutes(0).seconds(0).unix() + "&endDate=" + mm().add(1, 'w').hours(23).minutes(59).seconds(59).unix(),
                }).then((response) => {
                    if (!isObject(response.data)) return Swal.fire('Error', gaem({ response }, response.data), 'error');
                    let ruang = [];
                    let jadwal = [];
                    response.data.data.forEach(v => {
                        if (v.status != 1) return;
                        jadwal.push({
                            url: this.$store.state.config.base_url + "/panel/ujian/jadwal/" + btoa(v.id),
                            resourceId: v.ruang_id,
                            title: `${v.kode_mapel} - ${v.kode_jenis} - ${v.nama_draft}`,
                            start: mm({ unix: v.startDateTime }).format("YYYY-MM-DD"),
                            end: mm({ unix: v.endDateTime }).format("YYYY-MM-DD"),
                        });
                        if (ruang.findIndex(e => e.id == v.ruang_id) == -1) {
                            ruang.push({ id: v.ruang_id, title: "[" + v.kode_ruang + "]" + v.nama_ruang })
                        }
                    });
                    this.resources = ruang;
                    this.events = jadwal;
                    this.initCalendar();
                }, (error) => {
                    Swal.fire('Gagal', gaem(error), 'error')
                });
            }
        },
        computed: {
            events: {
                get() {
                    return this.calendar.events;
                },
                set(v) {
                    this.calendar.events = v;
                    if (!this.calendar.instance) return;
                    this.calendar.instance.render();
                }
            },
            resources: {
                get() {
                    return this.calendar.resources;
                },
                set(v) {
                    this.calendar.resources = v;
                    if (!this.calendar.instance) return;
                    this.calendar.instance.render();
                }
            },
        }
    }
})();
