<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="#3bb9f1"
            dark
            depressed
            @click="getStatistics"
          >
            조회
          </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
          >
            <template slot="body.prepend">
              <tr class="summary">
                <td>
                  합계
                </td>
                <td>
                  {{ total_sign_up_count }}
                </td>
                <td>
                  {{ total_sign_up_email_count }}
                </td>
                <td>
                  {{ total_sign_up_kakao_count }}
                </td>
                <td>
                  {{ total_sign_up_google_count }}
                </td>
                <td>
                  {{ total_auth_count }}
                </td>
                <td>
                  {{ total_sign_out_count }}
                </td>
                <td>
                  {{ total_payment_member_count }}
                </td>
              </tr>
            </template>

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

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

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

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

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

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

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

            <template v-slot:item.paymentMemberCount="{ item }">
              {{ item.paymentMemberCount.toLocaleString() }}
            </template>
          </v-data-table>
        </v-col>
      </v-row>
      <v-row class="text-center">
        <v-col>
          <v-pagination
            v-model="page"
            :length="total_page"
            circle
            @input="getStatistics"
          />
        </v-col>
      </v-row>
    </v-container>
  </div>
</template>

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

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

  data() {
    return {
      is_loading: false,
      start_date: null,
      end_date: null,
      total_auth_count: 0,
      total_sign_up_count: 0,
      total_sign_up_email_count: 0,
      total_sign_up_kakao_count: 0,
      total_sign_up_google_count: 0,
      total_sign_out_count: 0,
      total_payment_member_count: 0,
      page: 1,
      total_page: 0,
      items: [],
      headers: [
        {
          text: '날짜',
          align: 'center',
          sortable: false,
          value: 'date',
        },
        {
          text: '회원가입 수',
          align: 'center',
          sortable: false,
          value: 'signUpCount',
        },
        {
          text: '이메일 가입 수',
          align: 'center',
          sortable: false,
          value: 'signUpEmailCount',
        },
        {
          text: '카카오 가입 수',
          align: 'center',
          sortable: false,
          value: 'signUpKakaoCount',
        },
        {
          text: '구글 가입 수',
          align: 'center',
          sortable: false,
          value: 'signUpGoogleCount',
        },
        {
          text: '본인인증 수',
          align: 'center',
          sortable: false,
          value: 'authCount',
        },
        {
          text: '회원탈퇴 수',
          align: 'center',
          sortable: false,
          value: 'signOutCount',
        },
        {
          text: '결제자 수',
          align: 'center',
          sortable: false,
          value: 'paymentMemberCount',
        },
      ]
    }
  },

  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.getStatistics()
  },

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

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

    async getStatistics() {
      this.is_loading = true

      try {
        const res = await api.getStatistics(this.start_date, this.end_date, this.page);

        if (res.status === 200 && res.data.success === true) {
          const data = res.data.data
          this.total_auth_count = data.totalAuthCount
          this.total_sign_up_count = data.totalSignUpCount
          this.total_sign_up_email_count = data.totalSignUpEmailCount
          this.total_sign_up_kakao_count = data.totalSignUpKakaoCount
          this.total_sign_up_google_count = data.totalSignUpGoogleCount
          this.total_sign_out_count = data.totalSignOutCount
          this.total_payment_member_count = data.totalPaymentMemberCount
          this.items = data.items

          const totalPage = Math.ceil(data.totalCount / 30)
          if (totalPage <= 0)
            this.total_page = 1
          else
            this.total_page = totalPage
        } else {
          this.notifyError('알 수 없는 오류가 발생했습니다. 다시 시도해 주세요.')
        }

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

<style scoped>
.summary {
  background-color: #c4dbf1;
}
</style>
