<template>
  <el-dialog :title="$t('returnPayFee.fillRefundReason')" :visible.sync="visible" width="50%" @close="handleClose">
    <el-form ref="form" :model="formData" label-width="120px" label-position="left">
      <el-form-item :label="$t('returnPayFee.payment') + 'ID'">
        <el-input v-model="formData.detailId" readonly />
      </el-form-item>

      <el-form-item :label="$t('returnPayFee.cycle') + $t('returnPayFee.monthUnit')">
        <el-input v-model="formData.cycles" readonly />
      </el-form-item>

      <el-form-item :label="$t('returnPayFee.receivableAmount')">
        <el-input v-model="formData.receivableAmount" readonly />
      </el-form-item>

      <el-form-item :label="$t('returnPayFee.receivedAmount')">
        <el-input v-model="formData.receivedAmount" readonly />
      </el-form-item>

      <el-form-item :label="$t('returnPayFee.paymentTime')">
        <el-input v-model="formData.payTime" readonly />
      </el-form-item>

      <el-form-item :label="$t('returnPayFee.refundReason')" prop="reason" :rules="[
        { required: true, message: $t('returnPayFee.reasonRequired'), trigger: 'blur' },
        { max: 200, message: $t('returnPayFee.reasonMaxLength'), trigger: 'blur' }
      ]">
        <el-input v-model="formData.reason" type="textarea" :rows="3"
          :placeholder="$t('returnPayFee.reasonPlaceholder')" />
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">{{ $t('common.cancel') }}</el-button>
      <el-button type="primary" @click="submitReturnPayFee" :loading="loading">
        {{ $t('returnPayFee.submitRefund') }}
      </el-button>
    </span>
  </el-dialog>
</template>

<script>
import { saveReturnPayFee } from '@/api/fee/refundDepositFeeApi'

export default {
  name: 'ReturnPayFee',
  data() {
    return {
      visible: false,
      loading: false,
      formData: {
        detailId: '',
        cycles: '',
        receivableAmount: '',
        receivedAmount: '',
        payTime: '',
        reason: '',
        communityId: '',
        feeId: '',
        feeTypeCd: '',
        configId: ''
      }
    }
  },
  methods: {
    open(data) {
      this.resetForm()
      Object.assign(this.formData, data)
      this.formData.payTime = data.createTime
      this.visible = true
    },
    resetForm() {
      this.$refs.form && this.$refs.form.resetFields()
      this.formData = {
        detailId: '',
        cycles: '',
        receivableAmount: '',
        receivedAmount: '',
        payTime: '',
        reason: '',
        communityId: '',
        feeId: '',
        feeTypeCd: '',
        configId: ''
      }
    },
    handleClose() {
      this.resetForm()
    },
    async submitReturnPayFee() {
      try {
        const valid = await this.$refs.form.validate()
        if (!valid) return

        this.loading = true
        await saveReturnPayFee(this.formData)

        this.$message.success(this.$t('common.operateSuccess'))
        this.visible = false
        this.$emit('success')
      } catch (error) {
        console.error(error)
      } finally {
        this.loading = false
      }
    }
  }
}
</script>