<!--
/**
 * OrangeHRM is a comprehensive Human Resource Management (HRM) System that captures
 * all the essential functionalities required for any enterprise.
 * Copyright (C) 2006 OrangeHRM Inc., http://www.orangehrm.com
 *
 * OrangeHRM is free software: you can redistribute it and/or modify it under the terms of
 * the GNU General Public License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 *
 * OrangeHRM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with OrangeHRM.
 * If not, see <https://www.gnu.org/licenses/>.
 */
 -->

<template>
  <div class="orangehrm-background-container">
    <div class="orangehrm-paper-container">
      <div class="orangehrm-header-container">
        <oxd-text tag="h6" class="orangehrm-main-title">{ACTION_VIEW_TITLE}</oxd-text>
        <div>
          <oxd-button
              label="Add"
              iconName="plus"
              displayType="secondary"
              @click="onClickAdd"
          />
        </div>
      </div>
      <table-header
          :selected="checkedItems.length"
          :total="total"
          :loading="isLoading"
          @delete="onClickDeleteSelected"
      ></table-header>
      <div class="orangehrm-container">
        <oxd-card-table
            :headers="headers"
            :items="items"
            :selectable="true"
            :clickable="false"
            :loading="isLoading"
            v-model:selected="checkedItems"
            rowDecorator="oxd-table-decorator-card"
        />
      </div>
      <div class="orangehrm-bottom-container">
        <oxd-pagination
            v-if="showPaginator"
            :length="pages"
            v-model:current="currentPage"
        />
      </div>
    </div>

    <delete-confirmation ref="deleteDialog"></delete-confirmation>
  </div>
</template>

<script>
import {navigate} from '@orangehrm/core/util/helper/navigation';
import DeleteConfirmationDialog from '@orangehrm/components/dialogs/DeleteConfirmationDialog.vue';

export default {
  data() {
    return {
      headers: [
        {name: 'title', slot: 'title', title: 'Title', style: {flex: 2}},
        {name: 'description', title: 'Description', style: {flex: 4}},
        {
          name: 'actions',
          title: 'Actions',
          slot: 'action',
          style: {flex: 1},
          cellType: 'oxd-table-cell-actions',
          cellConfig: {
            delete: {
              onClick: this.onClickDelete,
              component: 'oxd-icon-button',
              props: {
                name: 'trash',
              },
            },
            edit: {
              onClick: this.onClickEdit,
              props: {
                name: 'pencil-fill',
              },
            },
          },
        },
      ],
        items: [
          {
            title: 'item name one',
            description: "item description one",
          },
          {
            title: 'item name two',
            description: "item description two",
          },
        ],
      checkedItems: [],
    };
  },

  components: {
    'delete-confirmation': DeleteConfirmationDialog,
  },

  setup() {
    // call Api here
  },

  methods: {
    onClickAdd() {
      navigate('/todo/{SAVE_ACTION}');
    },
    onClickEdit(item) {
      navigate('/todo/{SAVE_ACTION}/{id}', {id: item.id});
    },
    onClickDeleteSelected() {
      const ids = [];
      this.checkedItems.forEach(index => {
        ids.push(this.items?.data[index].id);
      });
      this.$refs.deleteDialog.showDialog().then(confirmation => {
        if (confirmation === 'ok') {
          this.deleteItems(ids);
        }
      });
    },
    onClickDelete(item) {
      this.$refs.deleteDialog.showDialog().then(confirmation => {
        if (confirmation === 'ok') {
          this.deleteItems([item.id]);
        }
      });
    },
    deleteItems(items) {
      if (items instanceof Array) {
        this.isLoading = true;
        this.http
            .deleteAll({
              ids: items,
            })
            .then(() => {
              return this.$toast.deleteSuccess();
            })
            .then(() => {
              this.isLoading = false;
              this.resetDataTable();
            });
      }
    },
    async resetDataTable() {
      this.checkedItems = [];
      await this.execQuery();
    },
  },
};
</script>
