<template>
  <div class="detail-container client">
    <head-form-new 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="companyName" slot-scope="scope">
        <router-link :to="scope.row.uipId?`/enterprise/${encodeStr(scope.row.companyId)}`:`/company/${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="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> -->
      <template slot="count" slot-scope="scope">
        <div v-if="scope.row.count>0" class="a-link" style="cursor: pointer;" @click="handleClick($event, scope.row)">{{scope.row.count}}</div>
      </template>
    </tables>
    <client-detail v-if="isDetails" :data="rowData" :title="title" :company-id="companyId" @cancel="isDetails=false" />
  </div>
</template>

<script>
import mixin from '../mixins/mixin';
import { clientPage, getSelect } from '@/api/detail/party-a/dealings';
import ClientDetail from './component/customDetail';
export default {
  name: 'Custom',
  props: ['companyId'],
  mixins: [mixin],
  components: {
    ClientDetail
  },
  data() {
    return {
      title: '',
      queryParams: {
        cid: this.companyId,
        sort: 5,
        pageNum: 1,
        pageSize: 20
      },
      defaultSort: { prop: 'count', 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: 'time', minWidth: '140', sortable: 'custom', descending: '3', ascending: '4' },
        { label: '合作总金额（万元）', prop: 'amount', minWidth: '150', sortable: 'custom', descending: '1', ascending: '2' },
        { label: '合作次数', prop: 'count', minWidth: '150', sortable: 'custom', slot: true, descending: '5', ascending: '6' },
      ],
      formData: [
        { type: 4, fieldName: 'projects', value: '', placeholder: '合作项目类型', options: [], uid: this.getUid() },
        { type: 4, fieldName: 'sources', value: '', placeholder: '数据来源', options: [], uid: this.getUid() },
        { type: 3, fieldName: 'keys', value: '', placeholder: '搜索客户名称', options: [], uid: this.getUid() },
      ],
      //列表
      tableLoading: false,
      tableData: [],
      tableDataTotal: 0,
      isDetails: false,
      rowData: {}
    };
  },
  computed: {
  },
  created() {
    this.handleQuery();
    this.handleOption();
  },
  methods: {
    async handleOption() {
      let res = await getSelect({});
      if (res.code == 200) {
        let data = res.data.projects;
        for (var i = 0; i < data.length; i++) {
          this.formData[0].options.push({
            name: data[i],
            value: data[i],
          });
        }
        let data1 = res.data.sources;
        for (var i = 0; i < data1.length; i++) {
          this.formData[1].options.push({
            name: data1[i],
            value: data1[i],
          });
        }
      }
    },
    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.isSkeleton = false;
        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;
      this.title = '与客户' + data.companyName.replace(/<font color='red'>/g, '').replace(/<\/font>/g, '') + '合作记录';
    },
    handleDetail(row) {
      if (row.sourceUrl) {

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

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