<template>
  <el-dialog
    custom-class="center-dialog"
    :title="title"
    top="0vh"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-form-item :label="__('中文')" prop="zh_CN">
        <el-input v-model="form.zh_CN" :placeholder="__('中文')" />
      </el-form-item>
      <el-form-item :label="__('繁体中文')" prop="zh_TW">
        <el-input v-model="form.zh_TW" :placeholder="__('繁体中文')" />
      </el-form-item>
      <el-form-item :label="__('英文')" prop="en_GB">
        <el-input v-model="form.en_GB" :placeholder="__('英文')" />
      </el-form-item>
      <el-form-item :label="__('泰语')" prop="th_TH">
        <el-input v-model="form.th_TH" :placeholder="__('泰语')" />
      </el-form-item>
      <el-form-item :label="__('西班牙')" prop="es_MX">
        <el-input v-model="form.es_MX" :placeholder="__('西班牙')" />
      </el-form-item>
      <el-form-item :label="__('阿拉伯')" prop="ar_SA">
        <el-input v-model="form.ar_SA" :placeholder="__('阿拉伯')" />
      </el-form-item>
      <el-form-item :label="__('越南语')" prop="vi_VN">
        <el-input v-model="form.vi_VN" :placeholder="__('越南语')" />
      </el-form-item>
      <el-form-item :label="__('土耳其')" prop="tr_TR">
        <el-input v-model="form.tr_TR" :placeholder="__('土耳其')" />
      </el-form-item>
      <el-form-item :label="__('日语')" prop="ja_JP">
        <el-input v-model="form.ja_JP" :placeholder="__('日语')" />
      </el-form-item>
      <el-form-item :label="__('印尼语')" prop="id_ID">
        <el-input v-model="form.id_ID" :placeholder="__('印尼语')" />
      </el-form-item>
      <el-form-item :label="__('德语')" prop="de_DE">
        <el-input v-model="form.de_DE" :placeholder="__('德语')" />
      </el-form-item>
      <el-form-item :label="__('法语')" prop="fr_FR">
        <el-input v-model="form.fr_FR" :placeholder="__('法语')" />
      </el-form-item>
      <el-form-item :label="__('葡萄牙')" prop="pt_PT">
        <el-input v-model="form.pt_PT" :placeholder="__('葡萄牙')" />
      </el-form-item>
      <el-form-item :label="__('意大利')" prop="it_IT">
        <el-input v-model="form.it_IT" :placeholder="__('意大利')" />
      </el-form-item>
      <el-form-item :label="__('俄罗斯')" prop="ru_RU">
        <el-input v-model="form.ru_RU" :placeholder="__('俄罗斯')" />
      </el-form-item>
      <el-form-item :label="__('罗马尼亚')" prop="ro_RO">
        <el-input v-model="form.ro_RO" :placeholder="__('罗马尼亚')" />
      </el-form-item>

      <el-form-item :label="__('希腊语')" prop="el_GR">
        <el-input v-model="form.el_GR" :placeholder="__('希腊语')" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">{{ __('取消') }}</el-button>
      <el-button type="primary" @click="save">{{ __('确定') }}</el-button>
    </div>
  </el-dialog>
</template>

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

  import { doEdit } from '@/api/base/lang/standard'

  export default {
    name: 'BaseLangStandardEdit',
    data() {
      return {
        form: {},
        rules: {},
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    mounted() {
      this.form = {
        unit_name: '',
      }

      this.rules = {
        zh_CN: [{ required: true, trigger: 'blur', message: this.__('中文') }],
        zh_TW: [
          { required: true, trigger: 'blur', message: this.__('繁体中文') },
        ],
        en_GB: [{ required: true, trigger: 'blur', message: this.__('英文') }],
        th_TH: [{ required: true, trigger: 'blur', message: this.__('泰语') }],
        es_MX: [
          { required: true, trigger: 'blur', message: this.__('西班牙') },
        ],
        ar_SA: [
          { required: true, trigger: 'blur', message: this.__('阿拉伯') },
        ],
        vi_VN: [
          { required: true, trigger: 'blur', message: this.__('越南语') },
        ],
        tr_TR: [
          { required: true, trigger: 'blur', message: this.__('土耳其') },
        ],
        ja_JP: [{ required: true, trigger: 'blur', message: this.__('日语') }],
        id_ID: [
          { required: true, trigger: 'blur', message: this.__('印尼语') },
        ],
        de_DE: [{ required: true, trigger: 'blur', message: this.__('德语') }],
        fr_FR: [{ required: true, trigger: 'blur', message: this.__('法语') }],
        pt_PT: [
          { required: true, trigger: 'blur', message: this.__('葡萄牙') },
        ],
        it_IT: [
          { required: true, trigger: 'blur', message: this.__('意大利') },
        ],
        ru_RU: [
          { required: true, trigger: 'blur', message: this.__('俄罗斯') },
        ],
        ro_RO: [
          { required: true, trigger: 'blur', message: this.__('罗马尼亚') },
        ],
        az_AZ: [
          { required: true, trigger: 'blur', message: this.__('阿塞拜疆语') },
        ],
        el_GR: [
          { required: true, trigger: 'blur', message: this.__('希腊语') },
        ],
        fi_FI: [
          { required: true, trigger: 'blur', message: this.__('芬兰语') },
        ],
        lv_LV: [
          { required: true, trigger: 'blur', message: this.__('拉脱维亚语') },
        ],
        nl_NL: [
          { required: true, trigger: 'blur', message: this.__('荷兰语') },
        ],
        da_DK: [
          { required: true, trigger: 'blur', message: this.__('丹麦语') },
        ],
        sr_RS: [
          { required: true, trigger: 'blur', message: this.__('塞尔维亚语') },
        ],
        pl_PL: [
          { required: true, trigger: 'blur', message: this.__('波兰语') },
        ],
        uk_UA: [
          { required: true, trigger: 'blur', message: this.__('乌克兰语') },
        ],
        kk_KZ: [
          { required: true, trigger: 'blur', message: this.__('哈萨克斯坦') },
        ],
        my_MM: [
          { required: true, trigger: 'blur', message: this.__('缅甸语') },
        ],
        ko_KR: [{ required: true, trigger: 'blur', message: this.__('韩语') }],
        time: [{ required: true, trigger: 'blur', message: this.__('时间') }],
        is_imp: [
          { required: true, trigger: 'blur', message: this.__('重要文字') },
        ],
        is_used: [
          { required: true, trigger: 'blur', message: this.__('是否启用') },
        ],
        frontend: [
          { required: true, trigger: 'blur', message: this.__('前端启用') },
        ],
        backend: [
          { required: true, trigger: 'blur', message: this.__('后端启用') },
        ],
      }

      this.$forceUpdate()
    },
    methods: {
      __,
      showEdit(row) {
        if (!row) {
          this.title = this.__('添加')
        } else {
          this.title = this.__('编辑')
          const obj = Object.assign({}, row)
          this.form = {
            zh_CN: obj.zh_CN,
            zh_TW: obj.zh_TW,
            en_GB: obj.en_GB,
            th_TH: obj.th_TH,
            es_MX: obj.es_MX,
            vi_VN: obj.vi_VN,
            tr_TR: obj.tr_TR,
            ja_JP: obj.ja_JP,
            id_ID: obj.id_ID,
            ar_SA: obj.ar_SA,
            de_DE: obj.de_DE,
            fr_FR: obj.fr_FR,
            pt_PT: obj.pt_PT,
            it_IT: obj.it_IT,
            ru_RU: obj.ru_RU,
            ro_RO: obj.ro_RO,
            az_AZ: obj.az_AZ,
            el_GR: obj.el_GR,
          }
        }
        this.dialogFormVisible = true
      },
      close() {
        this.form = this.$options.data().form
        this.$refs['form'].resetFields()
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg, status } = await doEdit(this.form)
            if (200 == status) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>
