<template>
  <div class="detail-wrap">
    <el-form ref="editForm" :model="languageSetting">
      <div class="d-header clearfix">
        <div class="fl">
          <i class="el-icon-info"/>
          {{ dataId ? $t1('查看语种') : $t1('新增语种') }}
        </div>
        <div class="fr">
          <el-button type="primary" plain class="button-sty" @click="$baseReload()" icon="el-icon-refresh-right">
            {{ $t1('重置') }}
          </el-button>
          <el-button type="primary" class="button-sty" icon="el-icon-check" @click="saveData">{{ $t1('保存') }}
          </el-button>
        </div>
      </div>
      <baseTabs>
        <baseTabPane :label="$t1('基本信息')">
          <template #default>
            <table class="table-detail">
              <tbody>
              <tr>
                <th>
                  <em class="f-red">*</em>
                  {{ $t1('语言名称') }}
                </th>
                <td>
                  <el-form-item prop="languageName" :rules="[{ required: true, trigger: 'blur' }]">
                    <el-input type="text" autocomplete="off" v-model="languageSetting.languageName" clearable/>
                  </el-form-item>
                </td>
                <th>
                  <em class="f-red">*</em>
                  {{ $t1('对应翻译字段') }}
                </th>
                <td>
                  <el-form-item prop="translationField" :rules="[{ required: true, trigger: 'blur' }]">
                    <el-input type="text" autocomplete="off" v-model="languageSetting.translationField" clearable/>
                  </el-form-item>
                </td>
                <th>
                  <em class="f-red">*</em>
                  {{ $t1('语言编码') }}
                </th>
                <td>
                  <el-form-item prop="languageCode" :rules="[{ required: true, trigger: 'blur' }]">
                    <el-input type="text" autocomplete="off" v-model="languageSetting.languageCode" clearable/>
                  </el-form-item>
                </td>
                <th>
                  {{ $t1('是否启用') }}
                </th>
                <td>
                  <el-form-item prop="enabled" :rules="[{ required: false, trigger: 'blur' }]">
                    <el-radio-group v-model="languageSetting.enabled">
                      <el-radio :label="true">{{ $t1('启用') }}</el-radio>
                      <el-radio :label="false">{{ $t1('禁用') }}</el-radio>
                    </el-radio-group>
                  </el-form-item>
                </td>
              </tr>
              <tr>
                <th>{{ $t1('备注') }}</th>
                <td colspan="5">
                  <el-input type="textarea" :rows="2" :placeholder="$t1('请输入内容')" size="small"
                            v-model="languageSetting.remark"
                            clearable></el-input>
                </td>
              </tr>
              <tr>
                <th>{{ $t1('创建人') }}</th>
                <td>{{ languageSetting.createBy }}</td>
                <th>{{ $t1('创建时间') }}</th>
                <td>{{ languageSetting.createDate }}</td>
                <th>{{ $t1('更新人') }}</th>
                <td>{{ languageSetting.modifyBy }}</td>
                <th>{{ $t1('更新时间') }}</th>
                <td>{{ languageSetting.modifyDate }}</td>
              </tr>
              </tbody>
            </table>
          </template>
        </baseTabPane>
      </baseTabs>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'languageSettingEdit',
  props: {
    _dataId: [String, Number]
  },
  components: {},
  data() {
    return {
      isEdit: false,
      tabIndex: 'first',
      dataId: '',
      languageSetting: {
        enabled: true
      }
    };
  },
  created() {
    if (this._dataId && !isNaN(this._dataId)) this.dataId = this._dataId;
  },
  mounted() {
    this.getData();
  },
  methods: {
    getData() {
      if (this.dataId && !isNaN(this.dataId)) {
        this.isEdit = true;
        this.$http({
          url: USER_PREFIX + `/language_setting/get`,
          method: `post`,
          data: {
            id: this.dataId
          },
          isLoading: true,
          modalStrictly: true,
          success: res => {
            this.languageSetting = res.objx || {};
          }
        });
      } else {
        this.languageSetting = {
          enabled: true
        };
      }
    },
    saveData() {
      this.$refs.editForm.validate(valid => {
        if (valid) {
          this.$baseConfirm(this.$t1('您确定要保存吗？')).then(() => {
            var url = USER_PREFIX + (this.isEdit ? `/language_setting/update` : `/language_setting/save`);
            this.$http({
              url: url,
              method: `post`,
              data: this.languageSetting,
              isLoading: true,
              success: res => {
                this.$message({
                  message: res.content,
                  type: 'success',
                  duration: 500,
                  onClose: t => {
                    if (this.isEdit) {
                      this.$baseReload();
                    } else {
                      this.$baseReload({
                        updateParam: {
                          _dataId: res.objx
                        }
                      });
                    }
                  }
                });
              }
            });
          });
        }
      });
    }
  }
};
</script>
