<template>
  <el-dialog
    :title="$t('searchOwner.title')"
    :visible.sync="visible"
    width="80%"
    @close="handleClose"
  >
    <div class="search-container">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            v-model="searchForm.roomName"
            :placeholder="$t('searchOwner.roomPlaceholder')"
            clearable
          ></el-input>
        </el-col>
        <el-col :span="8">
          <el-input
            v-model="searchForm.currentOwnerName"
            :placeholder="$t('searchOwner.namePlaceholder')"
            clearable
          ></el-input>
        </el-col>
        <el-col :span="8">
          <el-button type="primary" @click="searchOwners">{{ $t('common.search') }}</el-button>
          <el-button @click="resetOwners">{{ $t('common.reset') }}</el-button>
        </el-col>
      </el-row>

      <el-table
        :data="tableData"
        border
        style="width: 100%; margin-top: 20px"
      >
        <el-table-column
          prop="memberId"
          :label="$t('searchOwner.memberId')"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="name"
          :label="$t('searchOwner.name')"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="personTypeName"
          :label="$t('searchOwner.personType')"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="personRoleName"
          :label="$t('searchOwner.personRole')"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="idCard"
          :label="$t('searchOwner.idCard')"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="link"
          :label="$t('searchOwner.contact')"
          align="center"
        ></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="chooseOwner(scope.row)"
            >{{ $t('common.select') }}</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        :current-page="pagination.current"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="pagination.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pagination.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      ></el-pagination>
    </div>
  </el-dialog>
</template>

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

export default {
  name: 'SearchOwner',
  data() {
    return {
      visible: false,
      searchForm: {
        roomName: '',
        currentOwnerName: '',
        ownerTypeCd: '1001'
      },
      tableData: [],
      pagination: {
        current: 1,
        size: 10,
        total: 0
      }
    }
  },
  methods: {
    open() {
      this.visible = true
      this.refreshData()
      this.loadAllOwnerInfo()
    },
    handleClose() {
      this.resetOwners()
    },
    async loadAllOwnerInfo() {
      try {
        const params = {
          page: this.pagination.current,
          row: this.pagination.size,
          communityId: getCommunityId(),
          name: this.searchForm.currentOwnerName.trim(),
          roomName: this.searchForm.roomName.trim(),
          ownerTypeCd: this.searchForm.ownerTypeCd
        }
        const res = await queryOwners(params)
        this.tableData = res.data
        this.pagination.total = res.records
      } catch (error) {
        console.error('获取业主列表失败:', error)
      }
    },
    chooseOwner(owner) {
      this.$emit('chooseOwner', owner)
      this.visible = false
    },
    searchOwners() {
      this.pagination.current = 1
      this.loadAllOwnerInfo()
    },
    resetOwners() {
      this.searchForm = {
        roomName: '',
        currentOwnerName: '',
        ownerTypeCd: '1001'
      }
      this.loadAllOwnerInfo()
    },
    refreshData() {
      this.searchForm = {
        roomName: '',
        currentOwnerName: '',
        ownerTypeCd: '1001'
      }
      this.pagination = {
        current: 1,
        size: 10,
        total: 0
      }
    },
    handleSizeChange(val) {
      this.pagination.size = val
      this.loadAllOwnerInfo()
    },
    handleCurrentChange(val) {
      this.pagination.current = val
      this.loadAllOwnerInfo()
    }
  }
}
</script>

<style lang="scss" scoped>
.search-container {
  padding: 20px;

  .el-row {
    margin-bottom: 20px;
  }
}
</style>