<template>
  <el-dialog
    :title="$t('payFeeUserAccount.title')"
    :visible.sync="dialogVisible"
    width="70%"
    @close="handleClose"
  >
    <el-card v-if="accountList.length > 0">
      <div slot="header" class="clearfix">
        <span>{{ $t('payFeeUserAccount.accountInfo') }}</span>
        <el-button
          type="primary"
          size="small"
          style="float: right;"
          @click="queryPayFeeUserAccount"
        >
          <i class="el-icon-refresh"></i>
          {{ $t('payFeeUserAccount.refresh') }}
        </el-button>
      </div>

      <el-table
        :data="accountList"
        border
        style="width: 100%"
      >
        <el-table-column
          align="center"
          :label="$t('payFeeUserAccount.select')"
          width="80"
        >
          <template slot-scope="scope">
            <el-radio
              v-model="selectedAccount"
              :label="scope.row.acctId"
              @change="computeFeeUserAmount(scope.row.acctId)"
            ></el-radio>
          </template>
        </el-table-column>
        <el-table-column
          prop="acctTypeName"
          align="center"
          :label="$t('payFeeUserAccount.accountType')"
        />
        <el-table-column
          prop="acctName"
          align="center"
          :label="$t('payFeeUserAccount.accountName')"
        />
        <el-table-column
          prop="amount"
          align="center"
          :label="$t('payFeeUserAccount.accountAmount')"
        >
          <template slot-scope="scope">
            {{ scope.row.amount }} {{ $t('payFeeUserAccount.yuan') }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          :label="$t('payFeeUserAccount.operation')"
          width="150"
        >
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              @click="openAddUserAmountModal(scope.row)"
            >
              <i class="el-icon-plus"></i>
              {{ $t('payFeeUserAccount.prestore') }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </el-dialog>
</template>

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

export default {
  name: 'PayFeeUserAccount',
  data() {
    return {
      dialogVisible: false,
      accountList: [],
      feeId: '',
      ownerId: '',
      communityId: '',
      selectedAccount: null
    }
  },
  methods: {
    open(params) {
      this.feeId = params.feeId
      this.ownerId = params.ownerId || ''
      this.listUserAccount()
      this.dialogVisible = true
    },
    close() {
      this.dialogVisible = false
    },
    async listUserAccount() {
      try {
        this.communityId = await getCommunityId()
        const params = {
          page: 1,
          row: 20,
          feeId: this.feeId,
          ownerId: this.ownerId,
          communityId: this.communityId
        }

        const response = await this.$http.get('/account.queryCommunityOwnerAccount', { params })
        this.accountList = response.data.data || []
      } catch (error) {
        console.error('查询用户账户失败:', error)
      }
    },
    queryPayFeeUserAccount() {
      this.listUserAccount()
    },
    openAddUserAmountModal(userAccount) {
      window.open(`/#/views/owner/ownerDetail?ownerId=${userAccount.objId}&currentTab=ownerDetailAccount`)
    },
    computeFeeUserAmount(acctId) {
      let totalUserAmount = 0.0
      let selectAccount = []
      
      this.accountList.forEach(item => {
        if (acctId === item.acctId && item.amount != 0) {
          totalUserAmount += parseFloat(item.amount)
          selectAccount.push(item)
        }
      })

      this.$emit('changeUserAmountPrice', {
        totalUserAmount,
        accountList: this.accountList,
        integralAmount: 0,
        cashAmount: totalUserAmount,
        couponAmount: 0,
        selectAccount
      })
    },
    handleClose() {
      this.accountList = []
      this.selectedAccount = null
      this.feeId = ''
      this.ownerId = ''
    }
  }
}
</script>

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