<template>
  <div class="detail-container">
    <head-form
      title="招标公告"
      :form-data="formData"
      :query-params="queryParams"
      :total="tableDataTotal"
      :isExcel="true"
      @handle-search="handleSearch"
    />

    <tables
      :indexFixed="true"
      :defaultSort="defaultSort"
      :tableLoading="tableLoading"
      :tableData="tableData"
      :forData="forData"
      :tableDataTotal="tableDataTotal"
      :queryParams="queryParams"
      @handle-current-change="handleCurrentChange"
      @sort-change="sortChange"
    >
      <template slot="projectName" slot-scope="scope">
        <router-link :to="`/radar/Tender/details/${scope.row.bid}`" tag="a" class="a-link" v-if="scope.row.bid&&scope.row.projectName " v-html="scope.row.projectName"></router-link>
        <div v-else v-html="scope.row.projectName  || '--'"></div>
      </template>
      <template slot="province" slot-scope="scope">
        {{scope.row.province}}{{`${scope.row.city?'-'+scope.row.city:''}`}}{{`${scope.row.area?'-'+scope.row.area:''}`}}
      </template>
    </tables>

  </div>
</template>

<script>
import mixin from '../mixins/mixin'
import {bidNoticeArea, bidNoticeTenderStage, bidNoticePage} from '@/api/detail/party-a/opport'
export default {
  name: 'Announcement',
  props: ['companyId'],
  mixins: [mixin],
  components: {
  },
  data() {
    return {
      queryParams: {
        cid: this.companyId,
        sort: 3,
        pageNum: 1,
        pageSize: 20
      },
      defaultSort: {prop: 'issueTime', order: 'descending'},
      forData: [
        {label: '项目名称', prop: 'projectName', minWidth: '300', slot: true},
        {label: '发布日期', prop: 'issueTime', sortable: 'custom', descending: '3', ascending: '4', width: '120'},
        {label: '预算金额（万元）', prop: 'projectAmount', sortable: 'custom', descending: '1', ascending: '2', width: '140'},
        {label: '项目地区', prop: 'province', width: '120', slot: true},
        {label: '项目类别', prop: 'tenderStage', width: '90'},
        {label: '招采单位联系人', prop: 'contact', width: '110'},
        {label: '招采单位联系方式', prop: 'contactTel', width: '130'},
        {label: '代理单位', prop: 'agency', minWidth: '170'},
        {label: '代理单位联系人', prop: 'agencyContact', width: '110'},
        {label: '代理单位联系方式', prop: 'agencyContactTel', width: '130'},
        {label: '报名截止日期', prop: 'overTime', width: '100'}
      ],
      formData: [
        { type: 7, fieldName: 'province', value: '',props: {multiple: true}, placeholder: '项目地区', options: []},
        { type: 4, fieldName: 'tenderStage', value: '', placeholder: '项目类型', options: []},
        { type: 3, fieldName: 'keys', value: '', placeholder: '输入关键词查询', options: []}
      ],
      //列表
      tableLoading:false,
      tableData:[],
      tableDataTotal:0
    }
  },
  computed: {
  },
  created() {
    this.handleOption()
    this.handleQuery()
  },
  methods: {
    async handleOption(){
      let [area, tender] = await Promise.all([
        bidNoticeArea({cid: this.companyId}),
        bidNoticeTenderStage({cid: this.companyId})
      ])
      if(area.code==200){
        let region = area.data.map(item => {
          let province = {label:item.province+'('+item.count+')',value:item.provinceId}
          if(item.citys&&item.citys.length>0){
            let city = [], citem = {}
            for(let i=0;i<item.citys.length;i++){
              citem = {label:item.citys[i].city, value:item.citys[i].cityId}
              if(item.citys[i].areas&&item.citys[i].areas.length>0){
                let area = [], aitem = {}
                for(let j=0;j<item.citys[i].areas.length;j++){
                  aitem = {label:item.citys[i].areas[j].area, value:item.citys[i].areas[j].areaId}
                  area.push(aitem)
                  citem.children = area
                }
              }
              city.push(citem)
            }
            city.length>0 ? province.children = city : ''
          }
          return province
        })
        this.setFormData('province', region)
      }
      if(tender.code==200){
        let tenderStage = tender.data.map(item => {
          let it = {name:item.tenderStage+'('+item.count+')',value:item.tenderStage}
          return it
        })
        this.setFormData('tenderStage', tenderStage)
      }
    },
    async handleQuery(params) {
      this.tableLoading = true
      let param = this.getAreaList(params || this.queryParams)
      let res = await bidNoticePage(param)
      this.tableLoading = false
      if(res.code==200){
        this.tableData = res.rows
      }
      this.tableDataTotal = res.total
    },
    getAreaList(params){
      if(params.province&&params.province.length>0){
        let provinceIds = [], cityIds = [], areaIds = []
        for(let i=0;i<params.province.length;i++){
          params.province[i][0]?provinceIds.push(params.province[i][0]):''
          params.province[i][1]?cityIds.push(params.province[i][1]):''
          params.province[i][2]?areaIds.push(params.province[i][2]):''
        }
        delete params.province
        params.provinceIds = provinceIds.filter(function(value,index,self){
          return self.indexOf(value) === index
        })
        params.cityIds = cityIds.filter(function(value,index,self){
          return self.indexOf(value) === index
        })
        params.areaIds = areaIds.filter(function(value,index,self){
          return self.indexOf(value) === index
        })
      }
      return params
    }
  }
}
</script>
<style lang="scss" scoped>
.detail-container{
  background: #ffffff;
  border-radius: 4px;
  padding: 16px;
}
</style>
