<template>
  <el-dialog
    append-to-body
    :title="title"
    :visible.sync="dialogFormVisible"
    width="40%"
    @close="close"
  >
    <el-form ref="form" label-width="100px" :model="form" :rules="rules">
      <el-form-item :label="__('运费金额')" prop="fee_amount">
        <el-input
          v-model="form.fee_amount"
          clearable
          oninput="value=value.replace(/[^0-9.]/g,'')"
          :placeholder="__('请输入运费金额')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">{{ __('取消') }}</el-button>
      <el-button
        v-loading="loading"
        :disabled="loading"
        type="primary"
        @click="save"
      >
        {{ __('确定') }}
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { editFee } from '@/api/order/base'
  export default {
    name: 'PostageEdit',
    data() {
      return {
        loading: false,
        form: { fee_amount: 0 },
        rules: {
          fee_amount: [
            {
              required: true,
              message: '请输入运费金额',
              trigger: 'blur',
            },
          ],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    methods: {
      __,
      showEdit(order_id, fee_amount) {
        this.title = this.__('修改邮费')
        this.form = {
          order_id: order_id,
          fee_amount: fee_amount,
        }
        this.dialogFormVisible = true
      },
      close() {
        this.form = this.$options.data().form
        this.$refs['form'].resetFields()
        this.dialogFormVisible = false
      },
      save() {
        this.loading = true
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg, status } = await editFee(this.form)
            if (200 == status) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
            this.$emit('fetch-data')
            this.loading = false
            this.close()
          } else {
            this.loading = false
            return false
          }
        })
      },
    },
  }
</script>
