<template>
  <div class="base-currency-container">
    <vab-query-form>
      <vab-query-form-right-panel :span="24">
        <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="__('语言名称')"
        prop="language_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('代码')"
        prop="language_code"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('环境')"
        prop="language_locale"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('图片')"
        prop="language_image"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('文件目录')"
        prop="language_directory"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('排序')"
        prop="language_sort"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('是否启用')"
        prop="language_status"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.language_status"

            :active-value="1"

            :inactive-value="0"
            @change="sChange(scope.row)"
          />
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { languageList, editLanguage } from '@/api/base/config'

  export default {
    name: 'Settings',
    components: {},
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNum: 1,
          pageSize: 20,
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,
      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 languageList(this.queryForm)
        this.list = data.items
        this.total = data.total
        this.listLoading = false
      },
      async sChange(row) {
        const { msg, status } = await editLanguage({
          language_id: row.language_id,
          language_status: row.language_status,
        })

        if (status == 200) {
          this.$baseMessage(msg, 'success')
        } else {
          this.$baseMessage(msg, 'error')
        }
        await this.fetchData()
      },
    },
  }
</script>
