<template>
  <div class="base-lang-meta-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="24">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.meta_ori"
              clearable
              :placeholder="__('待翻译')"
            />
          </el-form-item>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.meta_value"
              clearable
              :placeholder="__('已翻译')"
            />
          </el-form-item>
          <el-form-item>
            <el-select
              v-model="queryForm.meta_key"
              clearable
              :placeholder="__('请选择语言')"
            >
              <el-option
                v-for="(item, index) in meta_keyOptions"
                :key="index"
                :disabled="item.disabled"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select
              v-model="queryForm.table_name"
              clearable
              :placeholder="__('请选择分类')"
            >
              <el-option
                v-for="(item, index) in table_nameOptions"
                :key="index"
                :disabled="item.disabled"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              {{ __('查询') }}
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-left-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="meta_key"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('待翻译内容')"
        prop="meta_ori"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('翻译后内容')"
        prop="meta_value"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('分类')"
        prop="table_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('名称')"
        prop="column_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatTime"
        :label="__('时间')"
        prop="meta_time"
        show-overflow-tooltip
        width="180"
      />
    </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/lang/meta'
  import Edit from './components/BaseLangMetaEdit'
  import { renderTime } from '@/utils/date'

  export default {
    name: 'BaseLangMeta',
    components: { Edit },
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        queryForm: {
          pageNum: 1,
          pageSize: 20,
        },
        meta_keyOptions: [
          {
            label: this.__('简中'),
            value: 'zh_CN',
          },
          {
            label: 'U.S.A',
            value: 'en_GB',
          },
          {
            label: 'México',
            value: 'es_MX',
          },
          {
            label: ' عربي ، ',
            value: 'ar_SA',
          },
          {
            label: this.__('日本語'),
            value: 'ja_JP',
          },
        ],
        table_nameOptions: [
          {
            label: this.__('店铺信息'),
            value: 'shop_store_base',
          },
          {
            label: this.__('店铺分类'),
            value: 'shop_base_store_category',
          },
          {
            label: this.__('商品内容'),
            value: 'shop_product_base',
          },
          {
            label: this.__('活动内容'),
            value: 'shop_store_activity_base',
          },
          {
            label: this.__('店铺内容'),
            value: 'default',
          },
        ],
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      formatTime(row) {
        const meta_time = row.meta_time
        return meta_time ? renderTime(meta_time) : ''
      },
      __,
      handleEdit(row) {
        if (row.meta_id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleDelete(row) {
        if (row.id) {
          this.$baseConfirm(this.__('你确定要删除当前项吗'), null, async () => {
            const { msg, status } = await doDelete({ ids: row.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>
