import { TemplatesApi } from '@openapi/notifier-registry';
const templatesApi = new TemplatesApi();

const columns = [
  {
    name: 'id',
    label: 'id шаблона',
    field: 'id',
    align: 'left',
  },
  {
    name: 'name',
    label: 'Название шаблона',
    field: 'name',
    align: 'left',
  },
];

export default {
  name: 'notification-template',
  data() {
    return {
      templates: [],
      columns: columns,
      loading: false,
      pagination: {
        descending: true,
        rowsPerPage: 10,
        sortBy: 'modified',
        rowsNumber: 0,
        page: 1,
      },
    };
  },
  methods: {
    init(val) {
      if (val?.pagination) {
        this.pagination.descending = val.pagination.descending;
        this.pagination.rowsPerPage = val.pagination.rowsPerPage;
        this.pagination.page = val.pagination.page;
        this.pagination.sortBy =
          val.pagination.sortBy === null
            ? this.sortByDefault
            : val.pagination.sortBy;
      }
      templatesApi
        .pagingMessageTemplates(
          this.pagination.page,
          this.pagination.rowsPerPage
        )
        .then((response) => {
          this.templates = [...response.data.items];
          this.pagination.rowsNumber = response.data.totalRows;
        });
    },
    selectTemplate(templateId) {
      this.$router.push({
        name: 'notificationTemplate',
        params: {
          id: templateId,
        },
      });
    },
  },
  mounted() {
    this.init();
  },
};
