<template>
  <div class="app-container">
    <div class="box-container">
      <div class="contain-title">合作机构列表</div>
    </div>
    <div class="content-container">
      <complex-table :tableData="tableData" :tableItem="tableItem" :operatingList='operatingList' :total='total'
        v-on:sortSingleExpert="sortSingleAgency" :jump="'org'" v-on:current="current"></complex-table>
    </div>
    <agencySort ref="sort" v-on:rearrange="rearrange"></agencySort>
  </div>
</template>

<script>
  import complexTable from '@/components/complexTable'
  import agencySort from './agencySort'
  import { agencyListUrl } from '@/api/cooperationAgency' //  agencyUserUrl
  import { parseTime } from '@/utils/index'
  import ret from '@/utils/comTable'
  import crossDomin from '@/utils/CrossDomain'
  export default {
    data() {
      return {
        operatingList: [ // 在子组件中遍历事件
          {
            text: '排序',
            event: 'sortSingleExpert',
            icon: 'primary'
          }
        ],
        dataList: [], //  拉去专家数据存到dataList这个数组里面
        tableData: [], //  每一页的存储的数据
        total: 0, //  页码总数
        pageSize: 10,
        tableItem: [ // 表头信息
          {
            prop: 'level',
            tit: '机构序号'
          },
          {
            prop: 'name',
            tit: '机构名称',
            active: 'active' // 点击加颜色，加此选项
          },
          {
            prop: 'orgType',
            tit: '机构类型',
            width: '160'
          },
          {
            prop: 'contactNum',
            tit: '联系电话',
            width: '160'
          },
          {
            prop: 'email',
            tit: '联系邮箱',
            width: '160'
          },
          {
            prop: 'assTime',
            tit: '入驻时间',
            width: '160'
          },
          {
            operate: 'edit',
            width: '200'
          }
        ]
      }
    },
    created() {
      this.agencyList()
    },
    components: {
      complexTable,
      agencySort
    },
    methods: {
      agencyList() {
        this.$http.get(agencyListUrl, '', (response) => { //  拉去专家信息列表，总数据存在dataList，tableData存每一页的信息
          if (response.success && response.data) {
            const epData = response.data
            let j = 0
            for (let i = 0; i < epData.length; i++) {
              epData[i].assTime = parseTime(epData[i].assTime).substr(0, 10)
              j++
              crossDomin.getAgencyInfo(epData[i].id, data => {
                j--
                Object.assign(epData[i], data)
                if (j === 0) {
                  this.dataList = epData
                  this.total = epData.length
                  if (this.total >= this.pageSize) {
                    this.tableData = this.dataList.slice(0, this.pageSize)
                  } else {
                    const array = this.dataList
                    ret.gapFilling(array)
                    this.tableData = array
                  }
                }
              })
            }
            if (epData.length === 0) {
              this.dataList = []
              this.tableData = []
              this.total = 0
            }
          } else {
            this.$sort.total = 0
            this.tableData = []
          }
        })
      },
      rearrange() {
        this.agencyList()
      },
      sortSingleAgency(obj) {
        this.$refs.sort.dialogFormVisible = true
        this.$refs.sort.form = { ...obj }
      },
      current(val) { // 点击页码
        if (val <= Math.ceil(this.total / this.pageSize)) {
          this.tableData = this.dataList.slice(this.pageSize * (val - 1), this.pageSize * val)
          if (val === Math.ceil(this.total / this.pageSize)) {
            ret.gapFilling(this.tableData)
          }
        } else {
          const array = this.dataList.slice(this.pageSize * val)
          ret.gapFilling(array)
          this.tableData = array
        }
      }
    }
  }
</script>