<template>
  <div>
    <div class="container q-mt-md">
      <q-breadcrumbs class="q-mb-md" active-color="black">
        <template v-slot:separator>
          <q-icon size="1.5em" name="chevron_right" color="black" />
        </template>
        <q-breadcrumbs-el label="Laca" icon="home" to="/" />
        <q-breadcrumbs-el :label="$t('breadcrumbs.historyBooking')" />
      </q-breadcrumbs>
      <div class="q-mt-md">
        <q-table
          flat
          bordered
          :rows="bookings"
          :columns="columns"
          row-key="index"
          v-model:pagination="pagination"
          @request="onRequest"
        >
          <template v-slot:body-cell-index="props">
            <q-td :props="props">
              <div>
                {{
                  props.pageIndex +
                  1 +
                  (pagination.page - 1) * pagination.rowsPerPage
                }}
              </div>
            </q-td>
          </template>
          <template v-slot:body-cell-name="props">
            <q-td :props="props">
              <div>
                {{ props.row.file_name }}
              </div>
            </q-td>
          </template>

          <template v-slot:body-cell-pricing="props">
            <q-td :props="props">
              <div>
                {{
                  `${(props.row.pricing || 0).toLocaleString('it-IT', {
                    style: 'currency',
                    currency: 'VND',
                  })}`
                }}
              </div>
            </q-td>
          </template>
          <template v-slot:body-cell-end_at="props">
            <q-td :props="props">
              <div>
                {{ convertDay(props.row.end_at) }}
              </div>
            </q-td>
          </template>
          <template v-slot:body-cell-start_at="props">
            <q-td :props="props">
              <div>
                {{ convertDay(props.row.start_at) }}
              </div>
            </q-td>
          </template>
          <template v-slot:body-cell-status="props">
            <q-td :props="props">
              <div>
                <q-badge
                  rounded
                  :color="`${
                    props.row.status == 'done' ? 'secondary' : 'warning'
                  }`"
                  :label="props.row.status"
                />
              </div>
            </q-td>
          </template>

          <template v-slot:body-cell-action="props">
            <q-td :props="props">
              <div>
                <q-btn
                  v-if="props.row.status !== 'done'"
                  icon="check"
                  flat
                  round
                  color="secondary"
                  size="md"
                  @click="submitHandle(+props.row.id)"
                >
                  <q-tooltip> Xác Nhận </q-tooltip>
                </q-btn>
                <q-btn
                  icon="delete"
                  flat
                  round
                  color="negative"
                  size="md"
                  @click="deleteBookingHandle(+props.row.id)"
                >
                  <q-tooltip> Huỷ </q-tooltip></q-btn
                >
              </div>
            </q-td>
          </template>
        </q-table>
      </div>
    </div>
  </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
import { mapGetters, mapActions } from 'vuex';
import dayjs from 'dayjs';

export default defineComponent({
  name: 'HistoryBooking',
  data() {
    return {
      bookings: [],
      columns: [
        {
          name: 'index',
          required: true,
          label: this.$t('table.order'),
          align: 'left',
          field: 'index',
        },
        {
          name: 'owner',
          required: true,
          label: this.$t('table.ownerName'),
          align: 'left',
          field: 'owner',
        },
        {
          name: 'license_plates',
          required: true,
          label: this.$t('table.licensePlates'),
          align: 'left',
          field: 'license_plates',
        },
        {
          name: 'pricing',
          required: true,
          label: this.$t('table.estimatedPrice'),
          align: 'left',
          field: 'pricing',
        },
        {
          name: 'status',
          required: true,
          label: this.$t('table.status'),
          align: 'left',
          field: 'status',
        },
        {
          name: 'start_at',
          label: this.$t('table.startTime'),
          field: 'start_at',
          align: 'left',
        },
        {
          name: 'end_at',
          label: this.$t('table.endTime'),
          field: 'end_at',
          align: 'left',
        },
        { name: 'action', label: this.$t('table.options'), align: 'center' },
      ],
      pagination: {
        page: 1,
        rowsPerPage: 5,
      },
      pagesNumber: 1,
    };
  },
  computed: {
    ...mapGetters({
      getUser: 'userModule/getUser',
    }),
  },
  async created() {
    if (this.getUser) await this.loadListSlot();
  },
  methods: {
    ...mapActions({
      historyBookingSlot: 'slotModule/historyBookingSlot',
      submitBookingSlot: 'slotModule/submitBookingSlot',
      outBookingSlot: 'slotModule/outBookingSlot',
    }),
    showError() {
      this.$q.notify({
        badgeClass: 'hide-badge',
        message: 'Có lỗi xảy ra. Vui lòng kiếm tra lại!',
        color: 'negative',
        multiLine: true,
        position: 'top',
        html: true,
        classes: 'notify-error',
        timeout: 1000,
      });
    },
    showSucsess() {
      this.$q.notify({
        badgeClass: 'hide-badge',
        message: 'Thành công',
        color: 'secondary',
        multiLine: true,
        position: 'top',
        html: true,
        timeout: 1000,
      });
    },
    async loadListSlot() {
      const params = {
        search: this.$route.query?.name || '',
        pageSize: this.pagination.rowsPerPage,
        pageNumber: this.pagination.page,
      };
      const res = await this.historyBookingSlot(params);
      this.bookings = res.data.bookings;
      this.pagesNumber = Math.ceil(
        res.data.total / this.pagination.rowsPerPage
      );
    },
    showMap(id: number) {
      this.$router.push({ name: 'map', query: { id: id } });
    },
    convertDay(day) {
      return dayjs(day).format('YYYY/MM/DD hh:mm');
    },
    async submitHandle(id: number) {
      try {
        await this.submitBookingSlot({ id });
        this.showSucsess();
        await this.loadListSlot();
      } catch (error) {}
    },
    async deleteBookingHandle(id: number) {
      try {
        await this.outBookingSlot({ id });
        this.showSucsess();
        await this.loadListSlot();
      } catch (error) {
        this.showError();
      }
    },
  },
});
</script>

<style lang="scss" scoped>
.prop {
  border-top: 1px solid #e6ecf0;
  .prop-info {
    .prop-addr {
      margin-top: 2px;
      font-size: 12px;
      line-height: 18px;
    }
    .link-overlay {
      text-decoration: none;
      .prop-title {
        font-size: 14px;
        line-height: 20px;
        color: #3c4146;
        font-weight: 600;
      }
    }
    .prop-attr {
      margin: 0;
      padding: 0;
      list-style: none;
      font-weight: normal;
      color: #3c4146;
      li {
        display: inline-block;
        margin-right: 16px;
      }
    }
    .price {
      font-size: 16px;
      line-height: 30px;
      font-weight: 700;
      color: #0499a8;
    }
  }
}
</style>
