<template>
  <div>
    <v-toolbar dark>
      <v-spacer />
      <v-toolbar-title>캔 환율관리</v-toolbar-title>
      <v-spacer />
    </v-toolbar>

    <br>

    <v-row>
      <v-dialog
        v-model="show_dialog"
        max-width="400px"
        persistent
      >
        <template v-slot:activator="{ on, attrs }">
          <v-container>
            <v-row>
              <v-col cols="10" />
              <v-col>
                <v-btn
                  block
                  color="#9970ff"
                  dark
                  depressed
                  v-bind="attrs"
                  v-on="on"
                >
                  캔등록
                </v-btn>
              </v-col>
            </v-row>
            <v-row>
              <v-col>
                <v-data-table
                  :headers="headers"
                  :items="cans"
                  class="elevation-1"
                  hide-default-footer
                >
                  <template v-slot:item.price="{ item }">
                    {{ item.price.toLocaleString('en-US') }} 원
                  </template>

                  <template v-slot:item.can="{ item }">
                    {{ item.can.toLocaleString('en-US') }} 캔
                  </template>

                  <template v-slot:item.rewardCan="{ item }">
                    {{ item.rewardCan.toLocaleString('en-US') }} 캔
                  </template>

                  <template v-slot:item.management="{ item }">
                    <v-btn
                      :disabled="isLoading"
                      @click="deleteCan(item)"
                    >
                      삭제
                    </v-btn>
                  </template>
                </v-data-table>
              </v-col>
            </v-row>
          </v-container>
        </template>

        <v-card>
          <v-card-title>캔 등록</v-card-title>
          <v-card-text>
            <v-text-field
              v-model="price"
              label="원화"
              required
            />
          </v-card-text>
          <v-card-text>
            <v-text-field
              v-model="can"
              label="캔"
              required
            />
          </v-card-text>
          <v-card-text>
            <v-text-field
              v-model="reward_can"
              label="리워드 캔"
              required
            />
          </v-card-text>
          <v-card-actions v-show="!isLoading">
            <v-spacer />
            <v-btn
              color="blue darken-1"
              text
              @click="cancel"
            >
              취소
            </v-btn>
            <v-btn
              color="blue darken-1"
              text
              @click="submit"
            >
              등록
            </v-btn>
          </v-card-actions>
        </v-card>
      </v-dialog>
    </v-row>
  </div>
</template>

<script>
import * as api from '@/api/can'

export default {
  name: "CanView",

  data() {
    return {
      isLoading: false,
      show_dialog: false,
      selected_can: null,
      price: null,
      can: null,
      reward_can: null,
      headers: [
        {
          text: '원화(VAT포함)',
          align: 'center',
          sortable: false,
          value: 'price',
        },
        {
          text: '충전캔',
          align: 'center',
          sortable: false,
          value: 'can',
        },
        {
          text: '리워드캔',
          align: 'center',
          sortable: false,
          value: 'rewardCan',
        },
        {
          text: '관리',
          align: 'center',
          sortable: false,
          value: 'management'
        },
      ],
      cans: [],
    }
  },

  async created() {
    await this.getCans()
  },

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

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

    cancel() {
      this.show_dialog = false
      this.can = null
      this.price = null
      this.reward_can = null
      this.selected_can = null
    },

    async getCans() {
      this.isLoading = true
      try {
        let res = await api.getCans();
        this.cans = res.data.data
      } catch (e) {
        this.notifyError("알 수 없는 오류가 발생했습니다. 다시 시도해 주세요.")
      } finally {
        this.isLoading = false
      }
    },

    async deleteCan(item) {
      this.isLoading = true

      let res = await api.deleteCan(item.id)
      if (res.status === 200 && res.data.success === true) {
        this.notifySuccess(res.data.message || '삭제되었습니다.')
        this.cans = []
        await this.getCans()
      } else {
        this.notifyError(res.data.message || '알 수 없는 오류가 발생했습니다. 다시 시도해 주세요.')
      }
    },

    async submit() {
      this.isLoading = true

      const res = await api.insertCan(this.can, this.reward_can, this.price)

      if (res.status === 200 && res.data.success === true) {
        this.show_dialog = false
        this.can = null
        this.price = null
        this.reward_can = null
        this.selected_can = null
        this.notifySuccess(res.data.message || '등록되었습니다.')

        this.cans = []
        await this.getCans()
      } else {
        this.notifyError(res.data.message || '알 수 없는 오류가 발생했습니다. 다시 시도해 주세요.')
      }

      this.isLoading = false
    },
  }
}
</script>

<style scoped>

</style>
