<template>
  <div class="base-currency-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="4">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">
          {{ __('添加') }}
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="20">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">
          {{ __('添加') }}
        </el-button>
        <el-button icon="el-icon-refresh" type="primary" @click="queryData">
          {{ __('刷新') }}
        </el-button>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      border
      :data="list"
      :height="tableConfig.height"
    >
      <el-table-column align="center" :label="__('操作')" width="120">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">
            {{ __('编辑') }}
          </el-button>
          <el-button type="text" @click="handleDelete(row)">
            {{ __('删除') }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('编号')"
        prop="currency_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('货币名称')"
        prop="currency_title"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('左符号')"
        prop="currency_symbol_left"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('右符号')"
        prop="currency_symbol_right"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('小数位')"
        prop="currency_decimal_place"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('汇率')"
        prop="currency_exchange_rate"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatStatus"
        :label="__('是否启用')"
        prop="currency_status"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('修改时间')"
        prop="currency_time"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatDedault"
        :label="__('是否默认')"
        prop="currency_is_default"
        show-overflow-tooltip
      />
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'

  import { getList, doDelete } from '@/api/base/currency'
  import Edit from './components/BaseCurrencyEdit'

  export default {
    name: 'BaseCurrency',
    components: { Edit },
    data() {
      return {
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNum: 1,
          pageSize: 20,
          title: '',
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,
      formatStatus(row) {
        return row.currency_status === 1 ? this.__('启用') : this.__('停用')
      },
      formatDedault(row) {
        return row.currency_is_default === 1 ? this.__('是') : this.__('否')
      },

      handleEdit(row) {
        if (row.currency_id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleDelete(row) {
        this.$baseConfirm(this.__('你确定要删除当前项吗'), null, async () => {
          const { msg, status } = await doDelete({
            currency_id: row.currency_id,
          })
          if (200 == status) {
            this.$baseMessage(msg, 'success')
          } else {
            this.$baseMessage(msg, 'error')
          }
          await this.fetchData()
        })
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
      },
    },
  }
</script>
