<template>
  <div class="app-container">
    <div class="title h1" style="margin-top: 10px;">服务企业</div>
    <div class="filter-container">
      <el-input placeholder="公司名字" v-model="listQueryParam.keyword" style="width: 15%; margin-left: 20px; margin-bottom: 20px;"></el-input>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" @click="search">
        搜索
      </el-button>
    </div>

    <div>
      <el-table
        :data="companys"
        border
        fit
        style="width: 100%"
        v-loading="listLoading"
      >
        <el-table-column label="名字" align="center" min-width="25%">
          <template slot-scope="{row}">
            <span>{{ row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="地址" align="center" :show-overflow-tooltip="true" min-width="35%">
          <template slot-scope="{row}">
            <span>{{ row.address }}</span>
          </template>
        </el-table-column>
        <el-table-column label="注册时间" align="center" min-width="20%">
          <template slot-scope="{row}">
            <span>{{ row.applyTime }}</span>
          </template>
        </el-table-column>
        <el-table-column label="发包数（个）" align="center" min-width="20%">
          <template slot-scope="{row}">
            <span>{{ row.projectCount }}</span>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total > 0" :total="total" :page.sync="listQueryParam.pageNo" :limit.sync="listQueryParam.pageSize" @pagination="getList" />
    </div>
  </div>
</template>

<script>
import Http from '@/js/http'
import Api from '@/js/api'
import {notify} from '@/constants'
import Pagination from '@/components/Pagination'

export default {
  name: 'Companys',
  components: { Pagination },
  data: function () {
    return {
      companys: [],
      listLoading: false,
      listQueryParam: {
        pageNo: 1,
        pageSize: 20,
        keyword: ''
      },
      total: 0
    }
  },
  mounted () {
    this.getList()
  },
  methods: {
    search () {
      this.listQueryParam.pageNo = 1
      this.getList()
    },
    getList () {
      this.listLoading = true
      let url = Api.COMPANY.PAGE.replace('{pageNo}', this.listQueryParam.pageNo - 1)
        .replace('{pageSize}', this.listQueryParam.pageSize)
      url = url + '?keyword=' + this.listQueryParam.keyword
      Http.get(url).then((res) => {
        const companyPage = res.data
        this.total = companyPage.totalCount
        this.listQueryParam.pageNo = companyPage.pageNo + 1
        this.listQueryParam.pageSize = companyPage.pageSize
        this.companys.splice(0, this.companys.length)
        companyPage.datas.forEach(company => {
          this.companys.push(company)
        })
      }).catch((error) => {
        console.error(error)
        notify('error', '获取公司数据异常:' + error)
      })
      this.listLoading = false
    }
  }
}
</script>

<style scoped>

</style>
