<template>
  <el-dialog
    custom-class="center-dialog"
    :title="title"
    top="0vh"
    :visible.sync="dialogFormVisible"
    width="600px"
    @close="close"
  >
    <el-form
      ref="form"
      label-width="100px"
      :model="form"
      :rules="rules"
      size="medium"
    >
      <el-form-item :label="__('银行名称')" prop="bank_name">
        <el-input
          v-model="form.bank_name"
          clearable
          :placeholder="__('请输入银行名称')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item :label="__('备注')" prop="bank_remark">
        <el-input
          v-model="form.bank_remark"
          clearable
          :placeholder="__('请输入备注')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item :label="__('排序')" prop="bank_order">
        <el-input
          v-model="form.bank_order"
          clearable
          :placeholder="__('请输入排序')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item label="" prop="bank_enable" required>
        <el-switch
          v-model="form.bank_enable"
          :active-text="__('启用')"
          :active-value="1"
          :inactive-text="__('禁用')"
          :inactive-value="0"
        />
      </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/bank'

  export default {
    name: 'BaseBankEdit',
    data() {
      return {
        form: {},
        rules: {
          bank_name: [
            {
              required: true,
              message: this.__('请输入银行名称'),
              trigger: 'blur',
            },
          ],
          bank_remark: [
            {
              required: true,
              message: this.__('请输入备注'),
              trigger: 'blur',
            },
          ],
          bank_order: [
            {
              required: true,
              message: this.__('请输入排序'),
              trigger: 'blur',
            },
          ],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    methods: {
      __,
      showEdit(row) {
        if (!row) {
          this.title = this.__('添加')
        } else {
          this.title = this.__('编辑')
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.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>
