<template>
  <el-dialog
    :title="$t('returnPayFee.refundTitle')"
    :visible.sync="visible"
    width="50%"
    @close="handleClose"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="150px">
      <el-form-item :label="`${$t('returnPayFee.fee')}ID`">
        <el-input v-model="form.detailId" readonly />
      </el-form-item>
      <el-form-item :label="`${$t('returnPayFee.cycle')}(${$t('returnPayFee.month')})`">
        <el-input v-model="form.cycles" readonly />
      </el-form-item>
      <el-form-item :label="$t('returnPayFee.receivableAmount')">
        <el-input v-model="form.receivableAmount" readonly />
      </el-form-item>
      <el-form-item :label="$t('returnPayFee.receivedAmount')">
        <el-input v-model="form.receivedAmount" readonly />
      </el-form-item>
      <el-form-item :label="$t('returnPayFee.payTime')">
        <el-input v-model="form.payTime" readonly />
      </el-form-item>
      <el-form-item 
        :label="`<span style='color:red'>*</span>${$t('returnPayFee.refundReason')}`" 
        prop="reason"
      >
        <el-input
          v-model="form.reason"
          type="textarea"
          :rows="4"
          :placeholder="$t('returnPayFee.inputReason')"
        />
      </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="handleSubmit">{{ $t('returnPayFee.submitRefund') }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { saveReturnPayFee } from '@/api/fee/payFeeAuditManageApi'
import { getCommunityId } from '@/api/community/communityApi'

export default {
  name: 'ReturnPayFeeModal',
  data() {
    return {
      visible: false,
      form: {
        detailId: '',
        cycles: '',
        receivableAmount: '',
        receivedAmount: '',
        payTime: '',
        reason: '',
        communityId: '',
        feeId: '',
        feeTypeCd: '',
        configId: ''
      },
      rules: {
        reason: [
          { required: true, message: this.$t('returnPayFee.reasonRequired'), trigger: 'blur' },
          { min: 1, max: 200, message: this.$t('returnPayFee.reasonLength'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    open(row) {
      this.form = {
        detailId: row.detailId,
        cycles: row.cycles,
        receivableAmount: row.receivableAmount,
        receivedAmount: row.receivedAmount,
        payTime: row.createTime,
        reason: '',
        communityId: row.communityId,
        feeId: row.feeId,
        feeTypeCd: row.feeTypeCd,
        configId: row.configId
      }
      this.visible = true
    },
    handleClose() {
      this.$refs.form.resetFields()
    },
    async handleSubmit() {
      try {
        await this.$refs.form.validate()
        this.form.communityId = getCommunityId()
        await saveReturnPayFee(this.form)
        this.$message.success(this.$t('common.operateSuccess'))
        this.$emit('success')
        this.visible = false
      } catch (error) {
        if (error.message) {
          this.$message.error(error.message)
        }
      }
    }
  }
}
</script>