<template>
  <el-dialog
    :title="$t('searchRoom.title')"
    :visible.sync="dialogVisible"
    width="80%"
    @close="handleClose"
  >
    <el-form v-if="searchRoomInfo.showSearchCondition" inline>
      <el-form-item :label="$t('searchRoom.buildingNo')">
        <el-input
          v-model="searchRoomInfo._currentFloorNum"
          :placeholder="$t('searchRoom.buildingNoPlaceholder')"
          :readonly="searchRoomInfo.floorNumInputReadonly"
        ></el-input>
      </el-form-item>
      
      <el-form-item :label="$t('searchRoom.roomNo')">
        <el-input
          v-model="searchRoomInfo._currentRoomNum"
          :placeholder="$t('searchRoom.roomNoPlaceholder')"
        ></el-input>
      </el-form-item>
      
      <el-form-item>
        <el-button type="primary" @click="searchRooms">
          <i class="el-icon-search"></i>
          {{ $t('common.search') }}
        </el-button>
        <el-button @click="resetRooms">
          <i class="el-icon-refresh"></i>
          {{ $t('common.reset') }}
        </el-button>
      </el-form-item>
    </el-form>
    
    <el-table :data="searchRoomInfo.rooms" border>
      <el-table-column
        prop="roomId"
        :label="$t('searchRoom.roomId')"
        align="center"
      />
      
      <el-table-column
        prop="floorNum"
        :label="$t('searchRoom.buildingNo')"
        align="center"
      >
        <template slot-scope="scope">
          {{ scope.row.floorNum }}{{ $t('searchRoom.building') }}
        </template>
      </el-table-column>
      
      <el-table-column
        prop="unitNum"
        :label="$t('searchRoom.unitNo')"
        align="center"
      >
        <template slot-scope="scope">
          {{ scope.row.unitNum }}{{ $t('searchRoom.unit') }}
        </template>
      </el-table-column>
      
      <el-table-column
        prop="roomNum"
        :label="$t('searchRoom.roomNo')"
        align="center"
      >
        <template slot-scope="scope">
          {{ scope.row.roomNum }}{{ $t('searchRoom.room') }}
        </template>
      </el-table-column>
      
      <el-table-column
        prop="layer"
        :label="$t('searchRoom.floor')"
        align="center"
      >
        <template slot-scope="scope">
          {{ scope.row.layer }}{{ $t('searchRoom.floorUnit') }}
        </template>
      </el-table-column>
      
      <el-table-column
        :label="$t('common.operation')"
        align="center"
        width="120"
      >
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            @click="chooseRoom(scope.row)"
          >
            {{ $t('common.select') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <div class="pagination-wrapper">
      <el-pagination
        :current-page="pagination.current"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="pagination.size"
        :total="pagination.total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </el-dialog>
</template>

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

export default {
  name: 'SearchRoom',
  props: {
    emitChooseRoom: {
      type: String,
      default: ''
    },
    emitLoadData: {
      type: String,
      default: ''
    },
    roomFlag: {
      type: String,
      default: ''
    },
    showSearchCondition: {
      type: String,
      default: 'true'
    }
  },
  data() {
    return {
      dialogVisible: false,
      searchRoomInfo: {
        rooms: [],
        _currentRoomNum: '',
        _currentFloorNum: '',
        floorNumInputReadonly: false,
        showSearchCondition: this.showSearchCondition === 'true'
      },
      pagination: {
        current: 1,
        size: 10,
        total: 0
      },
      communityId: ''
    }
  },
  created() {
    this.communityId = getCommunityId()
  },
  methods: {
    open() {
      this.dialogVisible = true
      this.refreshSearchRoomData()
      this.loadAllRoomInfo(1, 10)
    },
    chooseRoom(room) {
      this.$emit(this.emitChooseRoom, 'chooseRoom', room)
      this.$emit(this.emitLoadData, 'listRoomData', {
        roomId: room.roomId
      })
      this.dialogVisible = false
    },
    searchRooms() {
      this.loadAllRoomInfo(1, 15, this.searchRoomInfo._currentRoomNum)
    },
    resetRooms() {
      this.searchRoomInfo._currentFloorNum = ''
      this.searchRoomInfo._currentRoomNum = ''
      this.loadAllRoomInfo(1, 15)
    },
    handleSizeChange(val) {
      this.pagination.size = val
      this.loadAllRoomInfo(this.pagination.current, val)
    },
    handleCurrentChange(val) {
      this.pagination.current = val
      this.loadAllRoomInfo(val, this.pagination.size)
    },
    handleClose() {
      this.searchRoomInfo._currentRoomNum = ''
    },
    refreshSearchRoomData() {
      this.searchRoomInfo._currentRoomNum = ''
    },
    loadAllRoomInfo(page, row, roomNum) {
      const params = {
        page,
        row,
        communityId: this.communityId,
        roomNum,
        floorNum: this.searchRoomInfo._currentFloorNum,
        roomFlag: this.roomFlag
      }

      let url = '/room.queryRooms'
      if (this.roomFlag === '1') {
        url = '/room.queryRoomsWithSell'
      } else if (this.roomFlag === '2') {
        url = '/room.queryRoomsWithOutSell'
      }

      queryRooms(params, url)
        .then(response => {
          const roomInfo = response.data
          this.searchRoomInfo.rooms = roomInfo.rooms
          this.pagination.total = roomInfo.records
        })
        .catch(error => {
          console.error('请求失败:', error)
        })
    }
  }
}
</script>

<style scoped>
.pagination-wrapper {
  margin-top: 20px;
  text-align: right;
}
</style>