<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="companyName" slot-scope="scope">
        <router-link :to="scope.row.uipId?`/enterprise/${encodeStr(scope.row.projectUnitId)}`:`/company/${encodeStr(scope.row.projectUnitId)}`" tag="a" class="a-link" v-if="scope.row.projectUnitId&&scope.row.companyName" v-html="scope.row.companyName"></router-link>
        <div v-else v-html="scope.row.companyName || '--'"></div>
      </template>
      <template slot="projectAllName" slot-scope="scope">
        <router-link :to="`/biddetail/${scope.row.projectInfo.projectId}`" tag="a" class="a-link" v-if="scope.row.projectInfo.projectId&&scope.row.projectInfo.projectAllName" >{{ scope.row.projectInfo.projectAllName }}</router-link>
        <div v-else v-html="scope.row.projectInfo&&scope.row.projectInfo.projectAllName || '--'"></div>
        <div v-if="scope.row.count>1" @click="handleClick($event, scope.row)" style="color: #FF7E38;cursor: pointer;">有{{scope.row.count}}个合作项目/工程 ></div>
      </template>
    </tables>
    <client-detail
      v-if="isDetails"
      :data="rowData"
      :title="'合作项目/工程明细'"
      :company-id="companyId"
      @cancel="isDetails=false" />
  </div>
</template>

<script>
import mixin from '../mixins/mixin'
import { clientPage } from '@/api/detail/party-a/dealings'
import ClientDetail from './component/customDetail'
export default {
  name: 'Custom',
  props: ['companyId'],
  mixins: [mixin],
  components: {
    ClientDetail
  },
  data() {
    return {
      queryParams: {
        cid: this.companyId,
        sort: 3,
        pageNum: 1,
        pageSize: 20
      },
      defaultSort: {prop: 'time', order: 'descending'},
      forData: [
        {label: '客户名称', prop: 'companyName', minWidth: '350', slot: true},
        {label: '合作项目/工程名称', prop: 'projectAllName', minWidth: '400', slot: true, sortable: 'custom', descending: '5', ascending: '6'},
        {label: '合作总金额（万元）', prop: 'amount', minWidth: '150', sortable: 'custom', descending: '1', ascending: '2'},
        {label: '最近一次合作时间', prop: 'time', minWidth: '140', sortable: 'custom', descending: '3', ascending: '4'}
      ],
      formData: [
        { type: 3, fieldName: 'keys', value: '', placeholder: '输入企业名称查询', options: []},
      ],
      //列表
      tableLoading:false,
      tableData:[],
      tableDataTotal:0,
      isDetails: false,
      rowData: {}
    }
  },
  computed: {
  },
  created() {
    this.handleQuery()
  },
  methods: {
    async handleQuery(params) {
      this.tableLoading = true
      let param = params?params:this.queryParams
      let res = await clientPage(param)
      this.tableLoading = false
      if(res.code==200){
        this.tableData = res.rows
        if(this.tableData&&this.tableData.length>0){
          this.tableData.forEach(item => {
            typeof item.projectInfo=='string' ? item.projectInfo = JSON.parse(item.projectInfo) : ''
            item.amount ? item.amount = parseFloat(item.amount.toFixed(4)) : ''
          })
        }
      }
      this.tableDataTotal = res.total
    },
    handleClick(e, data) {
      this.rowData = data
      this.isDetails = true
    },
    handleDetail(row){
      if(row.sourceUrl){

        window.open(row.sourceUrl, "_blank")
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.detail-container{
  background: #ffffff;
  border-radius: 4px;
  padding: 16px;
}
</style>
