<template>
  <div>
    <v-toolbar dark>
      <v-spacer />
      <v-toolbar-title>캔 충전 현황</v-toolbar-title>
      <v-spacer />
    </v-toolbar>

    <br>

    <v-container>
      <v-row>
        <v-spacer />
        <v-col cols="2">
          <datetime
            v-model="start_date"
            class="datepicker"
            format="YYYY-MM-DD"
          />
        </v-col>

        <v-col cols="1">
          ~
        </v-col>

        <v-col cols="2">
          <datetime
            v-model="end_date"
            class="datepicker"
            format="YYYY-MM-DD"
          />
        </v-col>

        <v-col cols="1" />

        <v-col cols="2">
          <v-btn
            block
            color="#9970ff"
            dark
            depressed
            @click="getChargeStatus"
          >
            조회
          </v-btn>
        </v-col>
      </v-row>

      <v-row>
        <v-col>
          <v-data-table
            :headers="headers"
            :items="items"
            :loading="is_loading"
            :items-per-page="-1"
            class="elevation-1"
            hide-default-footer
            @click:row="getChargeStatusDetail"
          >
            <template v-slot:item.date="{ item }">
              {{ item.date }}
            </template>

            <template v-slot:item.chargeAmount="{ item }">
              {{ item.chargeAmount.toLocaleString() }} 원
            </template>

            <template v-slot:item.chargeCount="{ item }">
              {{ item.chargeCount }}
            </template>

            <template v-slot:item.pg="{ item }">
              {{ item.pg }}
            </template>
          </v-data-table>
        </v-col>
      </v-row>
      <v-row>
        <v-dialog
          v-model="show_popup_dialog"
          max-width="1000px"
          persistent
        >
          <v-card>
            <v-data-table
              :headers="detail_headers"
              :items="detail_items"
              :loading="is_loading"
              :items-per-page="-1"
              class="elevation-1"
              hide-default-footer
            >
              <template v-slot:item.accountId="{ item }">
                {{ item.accountId }}
              </template>

              <template v-slot:item.nickname="{ item }">
                {{ item.nickname }}
              </template>

              <template v-slot:item.method="{ item }">
                {{ item.method }}
              </template>

              <template v-slot:item.amount="{ item }">
                {{ item.amount.toLocaleString() }} 원
              </template>

              <template v-slot:item.datetime="{ item }">
                {{ item.datetime }}
              </template>
            </v-data-table>
            <v-card-actions v-show="!is_loading">
              <v-spacer />
              <v-btn
                color="blue darken-1"
                text
                @click="cancel"
              >
                확인
              </v-btn>
            </v-card-actions>
          </v-card>
        </v-dialog>
      </v-row>
    </v-container>
  </div>
</template>

<script>
import * as api from "@/api/charge_status";
import datetime from 'vuejs-datetimepicker';

export default {
  name: "CanStatus",
  components: {datetime},

  data() {
    return {
      is_loading: false,
      start_date: null,
      end_date: null,
      items: [],
      detail_items: null,
      show_popup_dialog: false,
      detail_headers: [
        {
          text: 'no',
          align: 'center',
          sortable: false,
          value: 'accountId',
        },
        {
          text: '닉네임',
          align: 'center',
          sortable: false,
          value: 'nickname',
        },
        {
          text: '결제수단',
          align: 'center',
          sortable: false,
          value: 'method',
        },
        {
          text: '가격',
          align: 'center',
          sortable: false,
          value: 'amount',
        },
        {
          text: '날짜',
          align: 'center',
          sortable: false,
          value: 'datetime',
        },
      ],
      headers: [
        {
          text: '날짜',
          align: 'center',
          sortable: false,
          value: 'date',
        },
        {
          text: '충전금액',
          align: 'center',
          sortable: false,
          value: 'chargeAmount',
        },
        {
          text: '충전횟수',
          align: 'center',
          sortable: false,
          value: 'chargeCount',
        },
        {
          text: 'PG',
          sortable: false,
          value: 'pg',
        }
      ]
    }
  },

  async created() {
    const date = new Date();
    const firstDate = new Date(date.getFullYear(), date.getMonth(), 1);
    const lastDate = new Date(date.getFullYear(), date.getMonth() + 1, 0);

    let firstDateMonth = (firstDate.getMonth() + 1).toString()
    if (firstDateMonth.length < 2) {
      firstDateMonth = '0' + firstDateMonth
    }


    let lastDateMonth = (lastDate.getMonth() + 1).toString()
    if (lastDateMonth.length < 2) {
      lastDateMonth = '0' + lastDateMonth
    }

    this.start_date = firstDate.getFullYear() + '-' + firstDateMonth + '-0' + firstDate.getDate()
    this.end_date = lastDate.getFullYear() + '-' + lastDateMonth + '-' + lastDate.getDate()

    await this.getChargeStatus()
  },

  methods: {
    notifyError(message) {
      this.$dialog.notify.error(message)
    },

    notifySuccess(message) {
      this.$dialog.notify.success(message)
    },

    cancel() {
      this.show_popup_dialog = false
    },

    async getChargeStatus() {
      this.is_loading = true

      try {
        const res = await api.getChargeStatus(this.start_date, this.end_date)
        if (res.status === 200 && res.data.success === true) {
          this.items = res.data.data
        } else {
          this.notifyError(res.data.message || '알 수 없는 오류가 발생했습니다. 다시 시도해 주세요.')
        }

        this.is_loading = false
      } catch (e) {
        this.notifyError('알 수 없는 오류가 발생했습니다. 다시 시도해 주세요.')
        this.is_loading = false
      }
    },

    async getChargeStatusDetail(value) {
      if (value.date !== '합계') {
        this.is_loading = true

        try {
          const res = await api.getChargeStatusDetail(value.date, value.pg)
          if (res.status === 200 && res.data.success === true) {
            this.detail_items = res.data.data
            this.show_popup_dialog = true
          } else {
            this.notifyError(res.data.message || '알 수 없는 오류가 발생했습니다. 다시 시도해 주세요.')
          }

          this.is_loading = false
        } catch (e) {
          this.notifyError('알 수 없는 오류가 발생했습니다. 다시 시도해 주세요.')
          this.is_loading = false
        }
      }
    }
  }
}
</script>

<style scoped>

</style>
