<template>
  <div class="margin-top">
    <div class="flex justify-end">
      <div :span="2" class="padding-right-xs padding-left-xl"></div>
      <div :span="2" class="padding-right-xs padding-right-xl"></div>
      <div :span="8" class="text-right">
        <el-button type="primary" size="small" style="margin-left:10px" @click="_openTempImportContractFeeModal">
          {{ $t('contractDetailFee.temporaryFee') }}
        </el-button>
        <el-button type="primary" size="small" style="margin-left:10px" @click="_openContractCreateFeeAddModal">
          {{ $t('contractDetailFee.createFee') }}
        </el-button>
        <el-button type="primary" size="small" style="margin-left:10px" @click="_toContractOwePayFee">
          {{ $t('contractDetailFee.payArrears') }}
        </el-button>
      </div>
    </div>
    <div class="margin-top">
      <el-table :data="contractDetailFeeInfo.fees" style="width: 100%" border stripe>
        <el-table-column prop="feeName" :label="$t('contractDetailFee.feeItem')" align="center"></el-table-column>
        <el-table-column prop="feeFlagName" :label="$t('contractDetailFee.feeFlag')" align="center"></el-table-column>
        <el-table-column prop="feeTypeCdName" :label="$t('contractDetailFee.feeType')" align="center"></el-table-column>
        <el-table-column prop="amountOwed" :label="$t('contractDetailFee.receivableAmount')"
          align="center"></el-table-column>
        <el-table-column prop="startTime" :label="$t('contractDetailFee.accountingTime')"
          align="center"></el-table-column>
        <el-table-column :label="$t('contractDetailFee.receivablePeriod')" align="center">
          <template slot-scope="scope">
            {{ _getContractEndTime(scope.row) }}~<br />
            {{ _getContractDeadlineTime(scope.row) }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('contractDetailFee.description')" align="center" width="150">
          <template slot-scope="scope">
            <div v-if="scope.row.computingFormula == '5005' || scope.row.computingFormula == '9009'">
              <div><span>{{ $t('contractDetailFee.previousReading') }}:</span>{{ scope.row.preDegrees }}</div>
              <div><span>{{ $t('contractDetailFee.currentReading') }}:</span>{{ scope.row.curDegrees }}</div>
              <div><span>{{ $t('contractDetailFee.unitPrice') }}:</span>{{ scope.row.mwPrice || scope.row.squarePrice }}
              </div>
              <div><span>{{ $t('contractDetailFee.surcharge') }}:</span>{{ scope.row.additionalAmount }}</div>
            </div>
            <div v-else-if="scope.row.computingFormula == '6006'">
              <div><span>{{ $t('contractDetailFee.usage') }}:</span>{{
                _getContractAttrValue(scope.row.feeAttrs, '390006') }}</div>
              <div><span>{{ $t('contractDetailFee.unitPrice') }}:</span>{{ scope.row.squarePrice }}</div>
              <div><span>{{ $t('contractDetailFee.surcharge') }}:</span>{{ scope.row.additionalAmount }}</div>
            </div>
            <div v-else-if="scope.row.feeTypeCd == '888800010017'">
              <div><span>{{ $t('contractDetailFee.algorithm') }}:</span>{{
                _getContractAttrValue(scope.row.feeAttrs, '390005') }}</div>
              <div><span>{{ $t('contractDetailFee.usage') }}:</span>{{
                _getContractAttrValue(scope.row.feeAttrs, '390003') }}</div>
            </div>
            <div v-else-if="scope.row.computingFormula == '4004'">
              <div>{{ $t('contractDetailFee.feeBasedOnActual') }}</div>
            </div>
            <div v-else>
              <div><span>{{ $t('contractDetailFee.unitPrice') }}:</span>{{ scope.row.squarePrice }}</div>
              <div v-if="scope.row.feeFlag == '1003006'"><span>{{ $t('contractDetailFee.surcharge') }}:</span>{{
                scope.row.additionalAmount }}</div>
              <div v-else><span>{{ $t('contractDetailFee.fixedFee') }}:</span>{{ scope.row.additionalAmount }}</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="stateName" :label="$t('contractDetailFee.status')" align="center"></el-table-column>
        <el-table-column :label="$t('contractDetailFee.operation')" align="center">
          <template slot-scope="scope">
            <el-button type="text" v-if="scope.row.state != '2009001' && hasPrivilege('502020082314267912')"
              @click="_payContractFee(scope.row)">
              {{ $t('contractDetailFee.payment') }}
            </el-button>
            <el-button type="text" @click="_payContractFeeHis(scope.row)">
              {{ $t('contractDetailFee.paymentHistory') }}
            </el-button>
            <el-button type="text" v-if="hasPrivilege('502020090604200029')" @click="_deleteContractFee(scope.row)">
              {{ $t('contractDetailFee.cancel') }}
            </el-button>
            <el-button type="text" v-if="scope.row.state != '2009001' && hasPrivilege('502021070488970005')"
              @click="_finishContractFee(scope.row)">
              {{ $t('contractDetailFee.end') }}
            </el-button>
            <el-button type="text" v-if="scope.row.state != '2009001' && hasPrivilege('502020090427190001')"
              @click="_editContractFee(scope.row)">
              {{ $t('contractDetailFee.change') }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-row class="margin-top">
        <el-col :span="4">
          <div>{{ $t('contractDetailFee.note1') }}</div>
          <div>{{ $t('contractDetailFee.note2') }}</div>
        </el-col>
        <el-col :span="2">
          <span>{{ $t('contractDetailFee.arrearsSubtotal') }}:</span>
          {{ contractDetailFeeInfo.totalAmount }}
        </el-col>
        <el-col :span="6" class="text-right">
          <el-pagination @current-change="handlePageChange" :current-page="pagination.currentPage"
            :page-size="pagination.pageSize" layout="total, prev, pager, next"
            :total="pagination.total"></el-pagination>
        </el-col>
      </el-row>
    </div>
    <temp-import-room-fee ref="tempImportRoomFee"></temp-import-room-fee>
  </div>
</template>

<script>
import { listFee } from '@/api/contract/contractDetailFeeApi'
import { getCommunityId } from '@/api/community/communityApi'
import TempImportRoomFee from '@/components/fee/tempImportRoomFee'

export default {
  name: 'ContractDetailFee',
  components: {
    TempImportRoomFee
  },
  data() {
    return {
      contractDetailFeeInfo: {
        rooms: [],
        contractId: '',
        roomNum: '',
        allOweFeeAmount: '0',
        totalAmount: 0,
      },
      pagination: {
        currentPage: 1,
        pageSize: 10,
        total: 0
      }
    }
  },
  methods: {
    open(data) {
      this.contractDetailFeeInfo.contractId = data.contractId
      this._loadContractDetailFeeData(1, this.pagination.pageSize)
    },
    _loadContractDetailFeeData(page, row) {
      const params = {
        page: page || 1,
        row: row || 10,
        communityId: this.communityId,
        payerObjId: this.contractDetailFeeInfo.contractId,
      }

      listFee(params).then(response => {
        this.contractDetailFeeInfo.total = response.total
        this.contractDetailFeeInfo.records = response.records
        this.contractDetailFeeInfo.fees = response.fees
        this.pagination.total = response.records

        let totalAmount = 0.0
        response.fees.forEach(item => {
          totalAmount += parseFloat(item.amountOwed)
        })
        this.contractDetailFeeInfo.totalAmount = totalAmount.toFixed(2)
      }).catch(error => {
        console.error('请求失败:', error)
      })
    },
    _openTempImportContractFeeModal() {
      this.$refs.tempImportRoomFee.open({
        roomId: this.contractDetailFeeInfo.contractId,
        objType: '7777',
        roomName: this.contractDetailFeeInfo.contractName,
        ownerName: this.contractDetailFeeInfo.ownerName
      })
    },
    _payContractFee(fee) {
      this.$router.push(`/property/payFeeOrder?feeId=${fee.feeId}`)
    },
    _editContractFee(fee) {
      this.$emit('edit-fee', fee)
    },
    _payContractFeeHis(fee) {
      fee.builtUpArea = this.contractDetailFeeInfo.builtUpArea
      this.$router.push(`/property/propertyFee?${this.objToGetParam(fee)}`)
    },
    _deleteContractFee(fee) {
      this.$emit('delete-fee', {
        communityId: this.communityId,
        feeId: fee.feeId
      })
    },
    _finishContractFee(fee) {
      this.$emit('finish-fee', {
        communityId: this.communityId,
        feeId: fee.feeId
      })
    },
    _toContractOwePayFee() {
      this.$router.push(`/property/owePayFeeOrder?payObjId=${this.contractDetailFeeInfo.contractId}&payObjType=7777&contractName=${this.contractDetailFeeInfo.contractName}`)
    },
    _openContractCreateFeeAddModal() {
      this.$emit('create-fee-add', {
        isMore: false,
        contract: this.contractDetailFeeInfo
      })
    },
    _getContractAttrValue(attrs, specCd) {
      let value = ""
      attrs.forEach(item => {
        if (item.specCd == specCd) {
          value = item.value
          return
        }
      })
      return value
    },
    _getContractDeadlineTime(fee) {
      if (fee.amountOwed == 0 && fee.endTime == fee.deadlineTime) {
        return "-"
      }
      if (fee.state == '2009001') {
        return "-"
      }
      return this.$options.filters.dateFormat(fee.endTime)
    },
    _getContractEndTime(fee) {
      if (fee.state == '2009001') {
        return "-"
      }
      return this.$options.filters.dateFormat(fee.endTime)
    },
    handlePageChange(currentPage) {
      this.pagination.currentPage = currentPage
      this._loadContractDetailFeeData(currentPage, this.pagination.pageSize)
    },
    objToGetParam(obj) {
      return Object.keys(obj).map(key => `${key}=${obj[key]}`).join('&')
    }
  },
  created() {
    this.communityId = getCommunityId()
  }
}
</script>

<style scoped>
.margin-top {
  margin-top: 20px;
}

.margin-top-lg {
  margin-top: 30px;
}

.padding-right-xs {
  padding-right: 5px;
}

.padding-left-xl {
  padding-left: 20px;
}

.text-right {
  text-align: right;
}
</style>