<template>
  <div class="my-class">
    <group-list
      :name="me.selectedClass.name"
      :active-members="me.selectedClass.members.filter(member => member.active)"
      :inactive-members="me.selectedClass.members.filter(member => !member.active)"
      :show-code="me.isTeacher"
      :show-code-route="showCodeRoute"
      :read-only="me.readOnly"
      :can-edit="me.isTeacher"
      :enable-deactivate="!me.readOnly && !me.selectedClass.readOnly"
      title="Klassenliste"
      class="my-class__class"
      @add="add"
      @edit="editClassName"
      @remove="remove"
      @leave="remove"
    />
  </div>
</template>

<script>
  import GroupList from '@/components/profile/GroupList';

  import MY_SCHOOL_CLASS_QUERY from '@/graphql/gql/queries/mySchoolClass.gql';
  import ADD_REMOVE_MEMBER_MUTATION from '@/graphql/gql/mutations/addRemoveMember.gql';
  import selectedClassMixin from '@/mixins/selected-class';
  import {SHOW_SCHOOL_CLASS_CODE} from '@/router/me.names';

  export default {
    mixins: [selectedClassMixin],

    components: {
      GroupList,
    },

    data() {
      return {
        showCodeRoute: {
          name: SHOW_SCHOOL_CLASS_CODE,
        },
      };
    },

    methods: {
      changeMember(member, active) {
        this.$apollo.mutate({
          mutation: ADD_REMOVE_MEMBER_MUTATION,
          variables: {
            input: {
              member: member.id,
              schoolClass: this.me.selectedClass.id,
              active,
            },
          },
          update(store, {data: {addRemoveMember: {success}}}) {
            if (success) {
              const query = MY_SCHOOL_CLASS_QUERY;
              const data = store.readQuery({query});
              let memberIndex = data.me.selectedClass.members.findIndex(m => m.id === member.id);
              data.me.selectedClass.members = [
                ...data.me.selectedClass.members.slice(0, memberIndex),
                {...member, active},
                ...data.me.selectedClass.members.slice(memberIndex + 1),
              ];
              if (member.isMe) {
                // if we changed the currently selected class of the logged in user, we need to change this property too
                data.me.selectedClass.readOnly = !active;
              }
              store.writeQuery({query, data});
            }
          },
        });
      },
      add(member) {
        this.changeMember(member, true);
      },
      remove(member) {
        const memberId = atob(member.id).split(':')[1];
        const meId = atob(this.me.id).split(':')[1];
        this.$modal.open('deactivate-person', {
            myself: memberId === meId,
            name: `${member.firstName} ${member.lastName}`,
            className: this.me.selectedClass.name,
          })
          .then(() => {
            this.changeMember(member, false);
          })
          .catch(() => {
          });
      },
      editClassName() {
        this.$store.dispatch('editClassName');
      },
    },
  };
</script>

<style scoped lang="scss">
  @import "~styles/helpers";

  .my-class {
    display: grid;
    grid-template-columns: auto auto;
    grid-template-rows: auto auto;
    grid-template-areas: "h b" "c c";

    &__header {
      grid-area: h;
    }

    &__code-link {
      grid-area: b;
      justify-self: end;
      align-self: center;
    }

    &__class {
      grid-area: c;
      width: 100%;
      margin-bottom: $large-spacing;
    }
  }

</style>
