<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="currency_title">
        <el-input v-model="form.currency_title" :placeholder="__('货币名称')" />
      </el-form-item>
      <el-form-item :label="__('左符号')" prop="currency_symbol_left">
        <el-input
          v-model="form.currency_symbol_left"
          :placeholder="__('左符号')"
        />
      </el-form-item>
      <el-form-item :label="__('右符号')" prop="currency_symbol_right">
        <el-input
          v-model="form.currency_symbol_right"
          :placeholder="__('右符号')"
        />
      </el-form-item>
      <el-form-item :label="__('小数位')" prop="currency_decimal_place">
        <el-input
          v-model="form.currency_decimal_place"
          :placeholder="__('小数位')"
        />
      </el-form-item>
      <el-form-item :label="__('汇率')" prop="currency_exchange_rate">
        <el-input
          v-model="form.currency_exchange_rate"
          :placeholder="__('汇率')"
        />
      </el-form-item>
      <el-form-item :label="__('是否启用')" prop="currency_status">
        <el-radio v-model="form.currency_status" :label="1">
          {{ __('启用') }}
        </el-radio>
        <el-radio v-model="form.currency_status" :label="0">
          {{ __('停用') }}
        </el-radio>
      </el-form-item>
      <el-form-item prop="currency_is_default">
        <el-switch
          v-model="form.currency_is_default"

          :active-text="__('默认')"
          :active-value="1"

          :inactive-text="__('非默认')"
          :inactive-value="0"
          style="display: block"
        />
      </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/currency'

  export default {
    name: 'BaseCurrencyEdit',
    data() {
      return {
        form: {
          currency_status: 1,
        },
        rules: {},
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    mounted() {
      this.rules = {
        currency_id: [
          { required: true, trigger: 'blur', message: this.__('编号') },
        ],
        currency_title: [
          { required: true, trigger: 'blur', message: this.__('货币名称') },
        ],
        currency_symbol_left: [
          { required: true, trigger: 'blur', message: this.__('左符号') },
        ],
        currency_symbol_right: [
          { required: true, trigger: 'blur', message: this.__('右符号') },
        ],
        currency_decimal_place: [
          { required: true, trigger: 'blur', message: this.__('小数位') },
        ],
        currency_exchange_rate: [
          { required: true, trigger: 'blur', message: this.__('汇率') },
        ],
        currency_status: [
          { required: true, trigger: 'blur', message: this.__('是否启用') },
        ],
        currency_time: [
          { required: true, trigger: 'blur', message: this.__('修改时间') },
        ],
        currency_is_default: [
          { 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 = {
            currency_id: obj.currency_id,
            currency_title: obj.currency_title,
            currency_symbol_right: obj.currency_symbol_right,
            currency_symbol_left: obj.currency_symbol_left,
            currency_decimal_place: obj.currency_decimal_place,
            currency_exchange_rate: obj.currency_exchange_rate,
            currency_status: obj.currency_status,
            currency_is_default: obj.currency_is_default,
          }
        }
        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>
