<template>
  <el-dialog :title="$t('contract.selectOwner')" :visible.sync="visible" width="80%" :before-close="handleClose">

    <div class="search-wrapper">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input v-model="searchOwnerInfo.roomName" :placeholder="$t('contract.inputRoomFullNum')">
          </el-input>
        </el-col>
        <el-col :span="8">
          <el-input v-model="searchOwnerInfo._currentOwnerName" :placeholder="$t('contract.inputOwnerName')">
          </el-input>
        </el-col>
        <el-col :span="8">
          <el-button type="primary" @click="searchOwners">
            <i class="el-icon-search"></i>{{ $t('common.search') }}
          </el-button>
          <el-button @click="resetOwners">{{ $t('common.reset') }}</el-button>
        </el-col>
      </el-row>
    </div>

    <el-table :data="searchOwnerInfo.owners" border style="width: 100%" height="400">
      <el-table-column prop="memberId" :label="$t('contract.ownerId')" align="center">
      </el-table-column>
      <el-table-column prop="name" :label="$t('contract.name')" align="center">
      </el-table-column>
      <el-table-column prop="personTypeName" :label="$t('contract.personType')" align="center">
      </el-table-column>
      <el-table-column prop="personRoleName" :label="$t('contract.personRole')" align="center">
      </el-table-column>
      <el-table-column prop="idCard" :label="$t('contract.idCard')" align="center">
      </el-table-column>
      <el-table-column prop="link" :label="$t('contract.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('contract.select') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

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

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

export default {
  name: 'SearchOwner',
  props: {
    emitChooseOwner: String,
    emitLoadData: String
  },
  data() {
    return {
      visible: false,
      searchOwnerInfo: {
        owners: [],
        _currentOwnerName: '',
        roomName: '',
        ownerTypeCd: '1001'
      },
      page: {
        current: 1,
        size: 10,
        total: 0
      }
    }
  },
  methods: {
    open(params = {}) {
      this.visible = true
      this._refreshSearchOwnerData()
      if (params.hasOwnProperty('ownerTypeCd')) {
        this.searchOwnerInfo.ownerTypeCd = params.ownerTypeCd
      }
      this._loadAllOwnerInfo(1, 10)
    },
    handleClose() {
      this.visible = false
    },
    _loadAllOwnerInfo(_page, _row) {
      const params = {
        page: _page,
        row: _row,
        communityId: getCommunityId(),
        name: this.searchOwnerInfo._currentOwnerName.trim(),
        roomName: this.searchOwnerInfo.roomName.trim(),
        ownerTypeCd: this.searchOwnerInfo.ownerTypeCd
      }

      queryOwners(params).then(response => {
        this.searchOwnerInfo.owners = response.data
        this.page.total = response.records
        this.page.current = _page
        this.page.size = _row
      })
    },
    chooseOwner(owner) {
      this.$emit('chooseOwner', owner)
      this.$emit(this.emitLoadData, 'listOwnerData', {
        ownerId: owner.ownerId
      })
      this.visible = false
    },
    searchOwners() {
      this._loadAllOwnerInfo(1, 10)
    },
    resetOwners() {
      this.searchOwnerInfo.roomName = ""
      this.searchOwnerInfo._currentOwnerName = ""
      this._loadAllOwnerInfo(1, 10)
    },
    _refreshSearchOwnerData() {
      this.searchOwnerInfo = {
        owners: [],
        _currentOwnerName: '',
        roomName: '',
        ownerTypeCd: '1001'
      }
    },
    handleSizeChange(val) {
      this.page.size = val
      this._loadAllOwnerInfo(this.page.current, val)
    },
    handleCurrentChange(val) {
      this._loadAllOwnerInfo(val, this.page.size)
    }
  }
}
</script>

<style scoped>
.search-wrapper {
  margin-bottom: 20px;
}

.pagination-wrapper {
  margin-top: 20px;
  text-align: right;
}

.el-button+.el-button {
  margin-left: 10px;
}
</style>