<template>
  <div class="detail-container tender-announcement">
    <head-form-new ref="headForm" title="招标公告" :form-data="formData" :query-params="queryParams" :total="tableDataTotal" :isExcel="true"
      @handle-search="handleSearch" />
    <skeleton style="margin-left:16px;" v-if="isSkeleton"></skeleton>
    <tables v-else :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/Notice/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="agency" slot-scope="scope">
        <router-link :to="`/company/${encodeStr(scope.row.agencyId)}`" tag="a" class="a-link" v-if="scope.row.agencyId&&scope.row.agency" v-html="scope.row.agency"></router-link>
        <div v-else v-html="scope.row.agency || '--'"></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 skeleton from '@/views/project/projectList/component/skeleton';
import { bidNoticeArea, bidNoticeTenderStage, bidNoticeProjectCategory, bidNoticePage } from '@/api/detail/party-a/opport';
export default {
  name: 'Announcement',
  props: ['companyId'],
  mixins: [mixin],
  components: {
    skeleton
  },
  data() {
    return {
      isSkeleton: true,
      queryParams: {
        cid: this.companyId,
        sort: 3,
        pageNum: 1,
        pageSize: 20
      },
      defaultSort: { prop: 'issueTime', order: 'descending' },
      forData: [
        {label: '项目名称', prop: 'projectName', minWidth: '300', slot: true, fixed: 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: 'projectCategory', width: '110'},
        {label: '项目地区', prop: 'province', width: '120', slot: true},
        {label: '招标阶段', prop: 'tenderStage', width: '90'},
        {label: '招采单位联系人', prop: 'contact', width: '120'},
        {label: '招采单位联系方式', prop: 'contactTel', width: '130'},
        {label: '代理单位', prop: 'agency', minWidth: '170', slot: true},
        {label: '代理单位联系人', prop: 'agencyContact', width: '120'},
        {label: '代理单位联系方式', prop: 'agencyContactTel', width: '130'},
        {label: '报名截止日期', prop: 'overTime', width: '110'}
      ],
      formData: [
        { type: 7, fieldName: 'province', value: '',props: {multiple: true}, placeholder: '项目地区', options: [], uid: this.getUid() },
        { type: 4, fieldName: 'projectCategory', value: '', placeholder: '项目类型', options: [], uid: this.getUid() },
        { type: 4, fieldName: 'tenderStage', value: '', placeholder: '招标阶段', options: [], uid: this.getUid() },
        { type: 3, fieldName: 'keys', value: '', placeholder: '输入关键词查询', options: [], uid: this.getUid() }
      ],
      //列表
      tableLoading: false,
      tableData: [],
      tableDataTotal: 0
    };
  },
  computed: {
  },
  created() {
    this.handleOption();
    this.handleQuery();
  },
  methods: {
    async handleOption() {
      let [area, tender, project] = await Promise.all([
        bidNoticeArea({ cid: this.companyId }),
        bidNoticeTenderStage({ cid: this.companyId }),
        bidNoticeProjectCategory({ 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);
      }
      if (project.code == 200) {
        let projectCategory = project.data.map(item => {
          let it = { name: item.projectCategory + '(' + item.count + ')', value: item.projectCategory };
          return it;
        });
        this.setFormData('projectCategory', projectCategory);
      }
    },
    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.isSkeleton = false;
        this.tableData = res.rows;
      }
      this.tableDataTotal = res.total;
    },
    getAreaList(params) {
      if (params.province && params.province.length > 0) {
        let arr = this.$refs.headForm.$refs.cascader[0].getCheckedNodes();
        let provinceIds = [], cityIds = [], areaIds = [];
        for (var i in arr) {
          if (arr[i].parent) {
            if (!arr[i].parent.checked) {
              if (arr[i].hasChildren || arr[i].level == 2) {
                cityIds.push(arr[i].value);
              } else {
                areaIds.push(arr[i].value);
              }
            }
          } else {
            provinceIds.push(arr[i].value);
          }
        }
        delete params.province;
        provinceIds.length > 0 ? params.provinceIds = provinceIds : '';
        cityIds.length > 0 ? params.cityIds = cityIds : '';
        areaIds.length > 0 ? params.areaIds = areaIds : '';
      }
      return params;
    }
  }
}
</script>
<style lang="scss" scoped>
.detail-container {
  background: #ffffff;
  border-radius: 4px;
  padding: 16px;
}

.tender-announcement {
  @import "@/assets/styles/search-common.scss";
}
</style>
