<template>
  <el-dialog :title="$t('editFee.title')" :visible.sync="visible" width="40%" :before-close="handleClose">
    <el-form :model="editFeeInfo" label-width="120px">
      <el-form-item :label="$t('editFee.startTime')" prop="startTime">
        <el-date-picker v-model="editFeeInfo.startTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"          :placeholder="$t('editFee.startTimePlaceholder')" 
          @change="validateStartTime"></el-date-picker>
      </el-form-item>
      <el-form-item :label="$t('editFee.endTime')" prop="endTime">
        <el-date-picker v-model="editFeeInfo.endTime" type="date" :placeholder="$t('editFee.endTimePlaceholder')"
          value-format="yyyy-MM-dd" @change="validateEndTime"></el-date-picker>
      </el-form-item>
      <el-form-item :label="$t('editFee.maxEndTime')" prop="maxEndTime">
        <el-date-picker v-model="editFeeInfo.maxEndTime" type="date" :placeholder="$t('editFee.maxEndTimePlaceholder')"
          value-format="yyyy-MM-dd" @change="validateMaxEndTime"></el-date-picker>
      </el-form-item>
      <template v-if="editFeeInfo.computingFormula === '1102'">
        <el-form-item :label="$t('editFee.rateCycle')" prop="rateCycle">
          <el-input v-model="editFeeInfo.rateCycle" :placeholder="$t('editFee.rateCyclePlaceholder')"></el-input>
        </el-form-item>
        <el-form-item :label="$t('editFee.rate')" prop="rate">
          <el-input v-model="editFeeInfo.rate" :placeholder="$t('editFee.ratePlaceholder')"></el-input>
        </el-form-item>
        <el-form-item :label="$t('editFee.rateStartTime')" prop="rateStartTime">
          <el-date-picker v-model="editFeeInfo.rateStartTime" type="date"
            :placeholder="$t('editFee.rateStartTimePlaceholder')" value-format="yyyy-MM-dd"
            @change="validateRateStartTime"></el-date-picker>
        </el-form-item>
      </template>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">{{ $t('common.cancel') }}</el-button>
      <el-button type="primary" @click="_doEidtFee">{{ $t('common.submit') }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { updateFee } from '@/api/fee/editFeeApi'
import { getCommunityId } from '@/api/community/communityApi'

export default {
  name: 'EditFee',
  data() {
    return {
      visible: false,
      editFeeInfo: {
        feeId: '',
        startTime: '',
        endTime: '',
        feeFlag: '',
        maxEndTime: '',
        computingFormula: '',
        rateCycle: '',
        rate: '',
        rateStartTime: ''
      }
    }
  },
  methods: {
    open(fee) {
      this.editFeeInfo = {
        feeId: fee.feeId,
        startTime: fee.startTime.indexOf(":") === -1 ? fee.startTime + " 00:00:00" : fee.startTime,
        endTime: fee.endTime.split(' ')[0],
        feeFlag: fee.feeFlag,
        maxEndTime: fee.maxEndTime,
        computingFormula: fee.computingFormula,
        rateCycle: fee.rateCycle,
        rate: fee.rate,
        rateStartTime: fee.rateStartTime
      }
      this.visible = true
    },
    handleClose() {
      this.visible = false
      this.clearAddFeeConfigInfo()
    },
    validateStartTime(value) {
      const start = new Date(value)
      const end = new Date(this.editFeeInfo.endTime)
      if (start >= end) {
        this.$message.error(this.$t('editFee.startTimeError'))
        this.editFeeInfo.startTime = ''
      }
    },
    validateEndTime(value) {
      const start = new Date(this.editFeeInfo.startTime)
      const end = new Date(value)
      if (start >= end) {
        this.$message.error(this.$t('editFee.endTimeError'))
        this.editFeeInfo.endTime = ''
      }
    },
    validateMaxEndTime(value) {
      const start = new Date(this.editFeeInfo.startTime)
      const end = new Date(value)
      if (start >= end) {
        this.$message.error(this.$t('editFee.maxEndTimeError'))
        this.editFeeInfo.maxEndTime = ''
      }
    },
    validateRateStartTime(value) {
      const start = new Date(this.editFeeInfo.startTime)
      const end = new Date(value)
      if (start >= end) {
        this.$message.error(this.$t('editFee.rateStartTimeError'))
        this.editFeeInfo.rateStartTime = ''
      }
    },
    editFeeValidate() {
      if (!this.editFeeInfo.startTime) {
        this.$message.error(this.$t('editFee.startTimeRequired'))
        return false
      }
      if (!this.editFeeInfo.endTime) {
        this.$message.error(this.$t('editFee.endTimeRequired'))
        return false
      }
      return true
    },
    async _doEidtFee() {
      if (!this.editFeeValidate()) return

      this.editFeeInfo.communityId = getCommunityId()

      try {
        const res = await updateFee(this.editFeeInfo)
        if (res.code === 0) {
          this.$message.success(this.$t('common.operationSuccess'))
          this.$emit('success')
          this.handleClose()
        } else {
          this.$message.error(res.msg)
        }
      } catch (error) {
        console.error('请求失败:', error)
        this.$message.error(this.$t('common.operateFail'))
      }
    },
    clearAddFeeConfigInfo() {
      this.editFeeInfo = {
        feeId: '',
        startTime: '',
        endTime: '',
        feeFlag: '',
        maxEndTime: '',
        computingFormula: '',
        rateCycle: '',
        rate: '',
        rateStartTime: ''
      }
    }
  }
}
</script>

<style scoped>
.el-form-item{
  width: 90%;
  text-align: left;
}
.el-select{
  width: 100%;
}
.el-date-editor{  
  width: 100%;
}
</style>