<template>
  <div class="corePersonnel">
    <head-form
      title="同地区城投"
      :form-data="formData"
      :query-params="queryParams"
      :total="tableDataTotal"
      :isExcel="true"
      @handle-search="handleSearch"
    >
      <template slot="slot">
        <div class="search-box">
          <span class="search-box-t"
                :class="queryParams.uipExecutiveLevel ||
                queryParams.uipBusinessType.length > 0 ||
                queryParams.bratingSubjectLevel.length > 0 ||
                queryParams.shareholderBg.length > 0 ||
                queryParams.equityRelationship.length > 0 ||
                queryParams.platformImportance.length > 0 ||
                queryParams.developmentZone.length > 0 ? 'search-box-ta' : ''"
                @click="handleSearch1">筛选<i :class="searchState ? 'el-icon-caret-top' : 'el-icon-caret-bottom'"></i></span>
          <div v-show="searchState" ref="showContent" class="search-main">
            <div class="item">
              <span class="wrap_label">行政等级</span>
              <div class="item_ckquery">
                <span :class="{color_text:queryParams.uipExecutiveLevel == ''}" @click="changeXZDJ('')">全部</span>
                <template v-for="(item,index) in uipGroupDatalist.uipExecutiveLevel">
                  <span :class="{color_text:item == queryParams.uipExecutiveLevel}" @click="changeXZDJ(item)">{{item}}</span>
                </template>
              </div>
            </div>
            <div class="item">
              <span class="wrap_label">城投业务类型</span>
              <div class="item_ckquery">
                <span :class="{color_text:queryParams.uipBusinessType.length == 0}" @click="changeBeCurrent('','uipBusinessType')">全部</span>
                <template v-for="(item,index) in uipGroupDatalist.uipBusinessType">
                  <span :class="{color_text:queryParams.uipBusinessType.includes(item)}" @click="changeBeCurrent(item,'uipBusinessType')">{{item}}</span>
                </template>
              </div>
            </div>
            <div class="item">
              <span class="wrap_label">主体评级</span>
              <div class="item_ckquery">
                <span :class="{color_text:queryParams.bratingSubjectLevel.length == 0}" @click="changeBeCurrent('','bratingSubjectLevel')">全部</span>
                <template v-for="(item,index) in uipGroupDatalist.bratingSubjectLevel">
                  <span  :class="{color_text:queryParams.bratingSubjectLevel.includes(item)}" @click="changeBeCurrent(item,'bratingSubjectLevel')">{{item}}</span>
                </template>
              </div>
            </div>
            <div class="item">
              <span class="wrap_label">股东背景</span>
              <div class="item_ckquery">
                <span :class="{color_text:queryParams.shareholderBg.length == 0}" @click="changeBeCurrent('','shareholderBg')">全部</span>
                <template v-for="(item,index) in uipGroupDatalist.shareholderBg">
                  <span :class="{color_text:queryParams.shareholderBg.includes(item)}" @click="changeBeCurrent(item,'shareholderBg')">{{item}}</span>
                </template>
              </div>
            </div>
            <div class="item">
              <span class="wrap_label">股权关系</span>
              <div class="item_ckquery">
                <span :class="{color_text:queryParams.equityRelationship.length == 0}" @click="changeBeCurrent('','equityRelationship')">全部</span>
                <template v-for="(item,index) in uipGroupDatalist.equityRelationship">
                  <span :class="{color_text:queryParams.equityRelationship.includes(item)}" @click="changeBeCurrent(item,'equityRelationship')">{{item}}</span>
                </template>
              </div>
            </div>
            <div class="item">
              <span class="wrap_label">平台重要性</span>
              <div class="item_ckquery">
                <span :class="{color_text:queryParams.platformImportance.length == 0}" @click="changeBeCurrent('','platformImportance')">全部</span>
                <template v-for="(item,index) in uipGroupDatalist.platformImportance">
                  <span :class="{color_text:queryParams.platformImportance.includes(item)}" @click="changeBeCurrent(item,'platformImportance')">{{item}}</span>
                </template>
              </div>
            </div>
            <div class="item" v-if="false">
              <span class="wrap_label">开发区类别</span>
              <div class="item_ckquery">
                <span :class="{color_text:queryParams.developmentZone.length == 0}" @click="changeBeCurrent('','developmentZone')">全部</span>
                <template v-for="(item,index) in lfqType">
                  <span :class="{color_text:queryParams.developmentZone.includes(item)}" @click="changeBeCurrent(item,'developmentZone')">{{item}}</span>
                </template>
              </div>
            </div>
          </div>
        </div>
      </template>
    </head-form>

    <tables
      :tableLoading="tableLoading"
      :tableData="tableData"
      :forData="forData"
      :tableDataTotal="tableDataTotal"
      :queryParams="queryParams"
      @handle-current-change="handleCurrentChange"
      @sort-change="sortChange"
    >
      <template slot="companyName" slot-scope="scope">
        <router-link :to="`/enterprise/${encodeStr(scope.row.companyId)}`" tag="a" class="a-link" v-if="scope.row.companyId&&scope.row.companyName " v-html="scope.row.companyName"></router-link>
        <div v-else v-html="scope.row.companyName  || '--'"></div>
      </template>
      <template slot="area" slot-scope="scope">
        <template v-if="scope.row.province || scope.row.city || scope.row.area">
          <span v-if="scope.row.province"> {{ scope.row.province }}</span>
          <span v-if="scope.row.city"> <span v-if="scope.row.province">-</span>{{ scope.row.city }}</span>
          <span v-if="scope.row.area"> <span v-if="scope.row.city">-</span>{{ scope.row.area }}</span>
        </template>
        <template v-else>
          --
        </template>
      </template>
    </tables>

  </div>
</template>

<script>
import mixin from '../mixins/mixin'
import dataRegion from '@/assets/json/dataRegion'
import {
  urbanInvestmentPage,
  uipGroupData
} from '@/api/detail/party-a/urbanLnvestment'
export default {
  name: 'SameRegion',
  mixins: [mixin],
  components: {

  },
  props: ['companyId', 'companyInfo'],
  data() {
    return {
      queryParams: {
        provinceIds: [],
        cityIds: [],
        uipExecutiveLevel: '',
        uipBusinessType: [],
        bratingSubjectLevel: [],
        shareholderBg: [],
        equityRelationship: [],
        platformImportance: [],
        developmentZone: [],
        pageNum: 1,
        pageSize: 20
      },
      forData: [
        {label: '企业名称', prop: 'companyName', width: '369', slot: true},
        {label: '区域', prop: 'area', width: '120', slot: true},
        {label: '招标数量', prop: 'biddingCount', width: '100', sortable: true},
        {label: '城投拿地', prop: 'landInfoCount', width: '100', sortable: true},
        {label: '供应商', prop: 'supplierCount', width: '100', sortable: true},
        {label: '主体评级', prop: 'bratingSubjectLevel', width: '110'},
        {label: '债劵余额（亿元）', prop: 'bondBalance', width: '130'},
        {label: '行政级别', prop: 'uipExecutiveLevel', width: '120'},
        {label: '股东背景', prop: 'shareholderBg', width: '120'},
        {label: '股权关系', prop: 'equityRelationship', width: '120'},
        {label: '平台重要性', prop: 'platformImportance', width: '120'},
        {label: '城投业务类型', prop: 'uipBusinessType', width: '120'},
        {label: '实控人', prop: 'actualController', width: '280'},
        {label: '最新报告期', prop: 'latestReportPeriod', width: '120'},
        {label: '总资产（亿元）', prop: 'totalAssets', width: '120'},
        {label: '归母净资产（亿元）', prop: 'belongNetAssets', width: '140'},
        {label: '货币资金（亿元）', prop: 'monetaryFunds', width: '130'},
        {label: '土地资产（亿元）', prop: 'landAssets', width: '130'},
        {label: '受限资产（亿元）', prop: 'restrictedAssets', width: '130'},
        {label: '应收账款（亿元）', prop: 'accountsReceivable', width: '130'},
        {label: '其他应收款（亿元）', prop: 'otherReceivable', width: '140'},
        {label: '公益性&准公益性主营占比（%）', prop: 'econData001', width: '200'},
        {label: '应收类款项来自政府占比（%）', prop: 'receivableFromGovRatio', width: '200'},
        {label: '政府补助（亿元）', prop: 'govSubsidy', width: '130'},
        {label: '专项应付款（亿元）', prop: 'specialPayable', width: '140'},
        {label: '营业收入（亿元）', prop: 'operatingIncome', width: '130'},
        {label: '所属开发区', prop: 'developmentZone', width: '120'}
      ],
      formData: [
        { type: 0, fieldName: 'penalizeReasonType', value: '', placeholder: '筛选', options: []},
        { type: 3, fieldName: 'keyword', value: '', placeholder: '请输入关键词', options: []},
      ],
      xzdj:['省级','地级市','区县级'],
      selected:[],
      xzdjCalss:'',
      typeList:['土地开发整理','基础设施建设','棚改保障房建设','公用事业','文化旅游','交通建设运营','产投平台'],
      ztpj:['AAA','AA+','AA','A+','A','A-','BBB+','其他'],
      gdbj:['政府','财政','国资委','其他'],
      gqgx:['直接控股','间接控股'],
      pt:['重要平台','主要平台','一般平台'],
      lfqType:['国家级经开','国家级高新区','国家海关监管区域','国家级新区','国家边和区','其他国家级','省级新区','省级开发区'],
      props: {
        value: 'id',
        multiple: true,
      },
      addressList: [],
      addressType: [],
      addresslength: 0,
      searchState:false,
      uipGroupDatalist:[],
      //列表
      tableLoading:false,
      tableData:[],
      tableDataTotal:0,
    }
  },
  watch:{
    searchState:{
      handler(newVal, olVal) {
        if (newVal) {
          setTimeout(() => {
            document.addEventListener('click', this.handleSearch1);
          }, 0);
        } else {
          document.removeEventListener('click', this.handleSearch1);
        }
      }
    }
  },
  created() {
    if(this.companyInfo){
      this.queryParams.provinceIds.push(this.companyInfo.provinceId)
      this.queryParams.cityIds.push(this.companyInfo.cityId)
      this.handleQuery()
      this.getScreen()
    }
  },
  computed: {

  },
  methods: {
    dataRegion() {
      var str = [];
      for (let x = 0; x < 3; x++) {
        for (let i = 0; i < dataRegion.length; i++) {
          if (dataRegion[i].regionLevel == x + 1 && x + 1 == 1) {
            str.push({
              'id': dataRegion[i].id,
              "label": dataRegion[i].regionName,
              "short": dataRegion[i].short,
              "value": dataRegion[i].parentId,
              "children": []
            });
          } else if (dataRegion[i].regionLevel == x + 1 && x + 1 == 2) {
            for (let j = 0; j < str.length; j++) {
              if (str[j].id == dataRegion[i].parentId) {
                str[j].children.push({
                  'id': dataRegion[i].id,
                  "label": dataRegion[i].regionName,
                  "short": dataRegion[i].short,
                  "value": dataRegion[i].parentId,
                  "children": []
                });
              }
            }
          } else if (dataRegion[i].regionLevel == x + 1 && x + 1 == 3) {
            for (let j = 0; j < str.length; j++) {
              for (let k = 0; k < str[j].children.length; k++) {
                if (str[j].children[k].id == dataRegion[i].parentId) {
                  str[j].children[k].children.push({
                    'id': dataRegion[i].id,
                    "label": dataRegion[i].regionName,
                    "short": dataRegion[i].short,
                    "value": dataRegion[i].parentId
                    // "children":[]
                  });
                }
              }
            }
          }
        }
      }
      this.addressList = str;
    },
    // 筛选
    handleSearch1(event){
      // this.searchState=!this.searchState;
      let dom = this.$refs.showContent;
      if (!dom.contains(event.target)) {
        this.searchState = !this.searchState;
        document.removeEventListener('click', this.handleSearch);
      }
    },
    // 请求筛选条件
    getScreen(){
      uipGroupData().then(res => {
        this.uipGroupDatalist = res.data
      })
    },

    changeXZDJ(index) {
      this.queryParams.uipExecutiveLevel = index;
      this.changes()
    },
    changeBeCurrent(index,name) {
      if(index){
        let i = this.queryParams[name].indexOf(index)
        if(i == -1){
          this.queryParams[name].push(index);
        }else{
          this.queryParams[name].splice(i,1);
        }
      }else{
        this.queryParams[name] = []
      }
      this.changes()
      this.$forceUpdate();
    },
    changes(){
      let params = this.formParams()
      params.pageNum = 1
      this.queryParams.pageNum = 1
      params.uipExecutiveLevel = this.queryParams.uipExecutiveLevel
      params.uipBusinessType = this.queryParams.uipBusinessType
      params.bratingSubjectLevel = this.queryParams.bratingSubjectLevel
      params.shareholderBg = this.queryParams.shareholderBg
      params.equityRelationship = this.queryParams.equityRelationship
      params.platformImportance = this.queryParams.platformImportance
      params.developmentZone = this.queryParams.developmentZone
      this.handleQuery(params)
    },
    addressListbtn() {
      let arr = this.$refs.address.getCheckedNodes();
      let provinceCode = [],cityCode = [],countyCode = [];
      for (var i in arr) {
        if (arr[i].parent) {
          if (!arr[i].parent.checked) {
            arr[i].hasChildren && cityCode.push(arr[i].value);
            !arr[i].hasChildren && countyCode.push(arr[i].value);
          }
        } else {
          provinceCode.push(arr[i].value)
        }
      }
      if (provinceCode.length > 0 || cityCode.length > 0 || countyCode.length > 0) {
        this.addresslength = provinceCode.length + cityCode.length + countyCode.length;
      } else {
        this.addresslength = 0;
      }
    },

    handleQuery(params){
      this.tableLoading = true
      let data = params ? params : this.queryParams
      urbanInvestmentPage(data).then(res => {
          this.tableData = res.data.list
          this.tableDataTotal = res.data.totalCount
        this.tableLoading = false
      })
    },
    //排序
    sortChange(e){
      if(e.order=='ascending'){
        this.queryParams.field = e.prop
        this.queryParams.order = 'asc'
      }else if(e.order=='descending'){
        this.queryParams.field = e.prop
        this.queryParams.order = 'desc'
      }else{
        delete this.queryParams.field
        delete this.queryParams.order
      }
      this.handleSearch();
    },
    // attr 参数表示当前点击的是哪一列，传进来的是点击列的标识
    // rev 参数表示的是点击当前列表头之后是升序降序或者null，三种值分别是['ascending', 'descending', null]
    sortFun(attr, rev){
      if(rev === 'ascending') {
        rev = 1
      } else if (rev === 'descending') {
        rev = -1
      } else {
        rev = 0
      }
      return function (x, y) {
        let a = x[attr]
        let b = y[attr]
        if (!a) {
          a = ""
        }
        if (!b) {
          b = ""
        }
        if (a < b) {
          return rev * -1
        }
        if (a > b) {
          return rev * 1
        }
        return 0
      }
    }

  }
}
</script>
<style lang="scss" scoped>
.corePersonnel{
  background: #ffffff;
  border-radius: 4px;
  padding: 16px;
  .query-box{
    margin: 10px 0 20px;
  }
  .search-box{
    display: inline-block;
    /*cursor: pointer;*/
    position: relative;
    .search-box-t{
      font-size: 14px;
      font-weight: 400;
      line-height: 32px;
      color:#232323;
      margin-right: 8px;
      cursor: pointer;
      i{
        color:rgba(35,35,35,0.4);
        margin-left: 5px
      }
    }
    .search-box-ta{
      color: #0081FF;
      i{
        color: #0081FF;
      }
    }
    .search-main{
      background: #ffffff;
      box-shadow: 0px 4px 10px 0px rgba(0,0,0,0.1);
      border-radius: 4px;
      width: 880px;
      //height: 337px;
      padding: 16px;
      position: absolute;
      top: 25px;
      left: 0;
      z-index: 2001;
      .item{
        margin-bottom: 5px;
        display: flex;
        /*align-items: center;*/
        font-size: 14px;
        .wrap_label{
          color: rgba(35,35,35,0.8);
          margin-right: 12px;
          line-height: 30px;
        }
        .item_ckquery{
          position: relative;
          flex: 1;
          span{
            color: #232323;
            padding: 5px 6px;
            display: inline-block;
            cursor: pointer;
            margin-right: 8px;
          }
          .color_text{
            background: #F3F4F5;
            border-radius: 4px;
            color: #0081FF;
          }
          .select-active{
            color: #0081FF;
          }
        }

        .cascader-region {
          position: absolute;
          left: 0;
          top: -6px;
          opacity: 0;
          line-height: 22px;
        }
      }
    }
  }
}
</style>
