<template>
  <div class="base-lang-standard-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="18">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.zh_CN"
              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-button icon="el-icon-search" type="primary" @click="queryData">
              {{ __('查询') }}
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="6">
        <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="zh_CN"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('繁体中文')"
        prop="zh_TW"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('英文')"
        prop="en_GB"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('泰语')"
        prop="th_TH"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('西班牙')"
        prop="es_MX"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('阿拉伯')"
        prop="ar_SA"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('越南语')"
        prop="vi_VN"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('土耳其')"
        prop="tr_TR"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('日语')"
        prop="ja_JP"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('印尼语')"
        prop="id_ID"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('德语')"
        prop="de_DE"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('法语')"
        prop="fr_FR"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('葡萄牙')"
        prop="pt_PT"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('意大利')"
        prop="it_IT"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('俄罗斯')"
        prop="ru_RU"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('罗马尼亚')"
        prop="ro_RO"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('阿塞拜疆语')"
        prop="az_AZ"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('希腊语')"
        prop="el_GR"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('芬兰语')"
        prop="fi_FI"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('拉脱维亚语')"
        prop="lv_LV"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('荷兰语')"
        prop="nl_NL"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('丹麦语')"
        prop="da_DK"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('塞尔维亚语')"
        prop="sr_RS"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('波兰语')"
        prop="pl_PL"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('乌克兰语')"
        prop="uk_UA"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('哈萨克斯坦')"
        prop="kk_KZ"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('缅甸语')"
        prop="my_MM"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('韩语')"
        prop="ko_KR"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('时间')"
        prop="time"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('重要文字')"
        prop="is_imp"
        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/lang/standard'
  import Edit from './components/BaseLangStandardEdit'

  export default {
    name: 'BaseLangStandard',
    components: { Edit },
    data() {
      return {
        options: [
          {
            // 1001-正常;1002-下架仓库中;1000-违规禁售
            value: 1001,
            label: this.__('销售中'),
          },
          {
            value: 1002,
            label: this.__('仓库中'),
          },
          {
            value: 1000,
            label: this.__('违规禁售'),
          },
        ],

        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',
          },
        ],
        value: '',
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNum: 1,
          pageSize: 20,
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,

      handleEdit(row) {
        if (row.zh_CN) {
          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({ zh_CN: row.zh_CN })
            if (200 == status) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
            await this.fetchData()
          })
        } else {
          if (this.selectRows.length > 0) {
            const ids = this.selectRows.map((item) => item.id).join()
            this.$baseConfirm(
              this.__('你确定要删除选中项吗'),
              null,
              async () => {
                const { msg, status } = await doDelete({ ids })
                if (200 == status) {
                  this.$baseMessage(msg, 'success')
                } else {
                  this.$baseMessage(msg, 'error')
                }
                await this.fetchData()
              }
            )
          } else {
            this.$baseMessage(this.__('未选中任何行'), 'error')
            return false
          }
        }
      },
      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>
