<template>
  <div class="app-container">
    <div v-loading="loading" class="white-box">
      <el-table :data="conversionsList" stripe style="width: 100%;">
        <el-table-column prop="NAME" :label="$t('exchangeRateConfig.currencyType')" />
        <el-table-column prop="PRODUCT_RATE" :label="$t('exchangeRateConfig.ProductExchangeRate')" min-width="90px;" />
        <el-table-column prop="BONUSES_RATE" :label="$t('exchangeRateConfig.BonusExchangeRate')" min-width="90px;" />
<!--        <el-table-column :label="$t('common.createdAt')" min-width="100px;">-->
<!--          <template slot-scope="{row}">-->
<!--            {{ row.CREATED_AT | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}-->
<!--          </template>-->
<!--        </el-table-column>-->
<!--        <el-table-column :label="$t('common.updatedAt')" min-width="100px;">-->
<!--          <template slot-scope="{row}">-->
<!--            {{ row.UPDATED_AT | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}-->
<!--          </template>-->
<!--        </el-table-column>-->
        <el-table-column :label="$t('common.action')" min-width="100px;">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" icon="el-icon-edit" plain @click="edit(scope.row)">{{ $t('common.edit') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 编辑	-->
    <el-dialog v-loading="editLoading" :title="$t('config.modifyExchangeRate')" :visible.sync="dialog" :width="screenWidth" style="margin-top: -80px">
      <el-form ref="editForm" :model="editForm" :label-position="labelPosition" label-width="130px" style="width: 100%; margin-top: -30px; margin-bottom: -15px;">
        <el-row :gutter="3">
          <el-col :xs="24" :sm="24" :lg="12">
            <el-input v-show="false" v-model="editForm.ID" size="small" type="text" />
            <el-form-item :label="$t('exchangeRateConfig.ProductExchangeRate')" prop="Product_Exchange_Rate" style="margin-bottom: 10px; width: 100%;">
              <el-input-number v-model.trim="editForm.product_rate" :precision="6" :min="0" size="small" type="text" style="min-width: 300px;" readonly />
            </el-form-item>
            <el-form-item :label="$t('exchangeRateConfig.BonusExchangeRate')" prop="Bonus_Exchange_Rate" style="margin-bottom: 10px; width: 100%;">
              <el-input-number v-model="editForm.bonuses_rate" :precision="6" :min="0" size="small" type="text" style="min-width: 300px;" />
            </el-form-item>
            <el-form-item :label="$t('config.refreshShopPrice')" style="min-width: 420px;" >
              <el-switch v-model="editForm.synchronize" /> <div class="synchronize break-word">( {{$t('config.refreshProductPricePrompt')}} )</div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item style="margin-bottom: 15px;">
          <el-button type="warning" size="mini" @click="dialog = false">{{ $t('table.cancel') }}</el-button>
          <el-button type="primary" size="mini" @click="editSubmit">{{ $t('table.confirm') }}</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

  </div>
</template>

<script>
import {
  fetchCurrencyConversionsList, setCurrencyConversions, fetchCurrenciesList
} from '@/api/config'
import { getScreenWidth } from '@/utils'

export default {
  name: 'Currencies',
  data() {
    return {
      form: {
        CONFIG_NAME: '',
        VALUE: 0,
        TITLE: ''
      },
      loading: true,
      submitButtonStat: false,
      synchronize: false,
      screenWidth: getScreenWidth() > 600 ? '500px' : getScreenWidth() + 'px',
      labelPosition: getScreenWidth() > 600 ? 'right' : 'top',
      conversionsList: [],
      editLoading: false,
      editForm: {
        from_currency_id: '149',
        to_currency_id: '',
        product_rate: '',
        bonuses_rate: '',
        synchronize: false,
      },
      dialog: false,

    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.loading = true
      fetchCurrencyConversionsList().then(response => {
        this.conversionsList = response
        setTimeout(() => {
          this.loading = false
        }, 0.5 * 1000)
      })
    },
    edit(data){
      this.editForm = {
            from_currency_id: '149',
            to_currency_id: data.ID,
            product_rate: data.PRODUCT_RATE,
            bonuses_rate: data.BONUSES_RATE,
            synchronize: false,
      }
      this.dialog = true
    },
    editSubmit(){
      setCurrencyConversions(this.editForm).then(response => {
        this.$message({
          message: response.data,
          type: 'success'
        })
        this.dialog = false
        this.fetchData()
      }).catch(error => {
        this.$message({
          message: error,
          type: 'warning'
        })
      })
    },
    onSubmit() {
      this.$confirm(this.$t('config.confirmToChangeExchangeRate'), this.$t('common.hint'), {
        confirmButtonText: this.$t('common.confirm'),
        cancelButtonText: this.$t('common.cancel'),
        type: 'warning'
      }).then(() => {
        this.submitButtonStat = true

        const param = this.form
        param.synchronize = this.synchronize
        updateExchangeRateConfig(param).then(response => {
          this.$message({
            message: response.data,
            type: 'success'
          })

          setTimeout(() => {
            this.submitButtonStat = false
          }, 0.5 * 1000)

          this.fetchData()
        }).catch(error => {
          this.$message({
            message: error,
            type: 'warning'
          })

          this.submitButtonStat = false
          this.fetchData()
        })
      }).catch(() => {
        this.submitButtonStat = false
      })
    }
  }
}
</script>

<style scoped>
.app-main {
	padding: 15px;
}
.app-container {
	padding: 0;
}
.white-box {
	padding: 15px;
}
.form-page {
	width: 100%;
}
.synchronize{
  color: #F56C6C;
}

::v-deep .el-form-item__label{
  word-wrap:break-word;
  word-break: break-word;
  overflow-wrap: break-word;
}

</style>
