﻿<template>
  <el-card class="box-card">
    <div slot="header" class="flex justify-between">
      <span>{{ $t('contractChangeAssets.title') }}</span>
      <el-button type="primary" size="small" style="float: right" @click="selectRoom">
        <i class="el-icon-plus"></i>
        {{ $t('common.add') }}
      </el-button>
    </div>

    <el-table :data="contractChangeAssetsInfo.rooms" border style="width: 100%">
      <el-table-column prop="roomNum" :label="$t('contractChangeAssets.room')" align="center">
        <template slot-scope="scope">
          {{ scope.row.floorNum }}-{{ scope.row.unitNum }}-{{ scope.row.roomNum }}
        </template>
      </el-table-column>

      <el-table-column prop="ownerName" :label="$t('contractChangeAssets.owner')" align="center" />

      <el-table-column prop="link" :label="$t('contractChangeAssets.phone')" align="center" />

      <el-table-column prop="builtUpArea" :label="$t('contractChangeAssets.area')" align="center">
        <template slot-scope="scope">
          {{ scope.row.builtUpArea }} {{ $t('contractChangeAssets.squareMeters') }}
        </template>
      </el-table-column>

      <el-table-column prop="stateName" :label="$t('contractChangeAssets.status')" align="center" />

      <el-table-column :label="$t('common.operation')" align="center" width="120">
        <template slot-scope="scope">
          <el-button type="danger" size="mini" @click="openDelRoomModel(scope.row)">
            {{ $t('common.delete') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script>
export default {
  name: 'ContractChangeAssets',

  data() {
    return {
      contractChangeAssetsInfo: {
        rooms: [],
        contractId: '',
        planType: '3003'
      }
    }
  },
  watch: {
    contractChangeAssetsInfo: {
      deep: true,
      handler(newVal) {
        this.$emit('changeNotify', newVal)
      }
    }
  },
  methods: {
    selectRoom() {
      this.$emit('openSearchRoom')
    },
    openDelRoomModel(room) {
      this.$confirm(
        this.$t('contractChangeAssets.confirmDelete'),
        this.$t('common.tip'),
        {
          confirmButtonText: this.$t('common.confirm'),
          cancelButtonText: this.$t('common.cancel'),
          type: 'warning'
        }
      ).then(() => {
        this.removeRoom(room)
      })
    },
    removeRoom(room) {
      this.contractChangeAssetsInfo.rooms = this.contractChangeAssetsInfo.rooms.filter(
        item => item.roomId !== room.roomId
      )
    },
    loadContractRooms() {
      // 这里应该调用API加载合同关联的房间数据
      // 示例代码：
      /*
      getContractRooms({ contractId: this.contractChangeAssetsInfo.contractId })
        .then(response => {
          this.contractChangeAssetsInfo.rooms = response.data
        })
      */
    }
  },
  created() {
    this.$on('chooseRoom', room => {
      // 检查是否已存在相同房间
      const exists = this.contractChangeAssetsInfo.rooms.some(
        item => item.roomId === room.roomId
      )
      if (!exists) {
        this.contractChangeAssetsInfo.rooms.push(room)
      }
    })

    this.$on('contractInfo', param => {
      this.contractChangeAssetsInfo.contractId = param.contractId
      this.loadContractRooms()
    })
  }
}
</script>

<style scoped>
.box-card {
  margin-bottom: 20px;
}
</style>