<template>
  <div class="detail-container">
    <head-form
      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="`/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="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 { supplierPage,getSelect } from '@/api/detail/party-a/dealings'
import ClientDetail from './component/supplierDetail'
export default {
  name: 'Supplier',
  props: ['companyId'],
  mixins: [mixin],
  components: {
    ClientDetail
  },
  data() {
    return {
      title:'',
      queryParams: {
        cid: this.companyId,
        sort: 5,
        pageNum: 1,
        pageSize: 20
      },
      defaultSort: {},
      forData: [
        {label: '供应商', prop: 'companyName', minWidth: '350', slot: true},
        {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: []},
        { type: 4, fieldName: 'sources', value: '', placeholder: '数据来源', options: []},
        { type: 3, fieldName: 'keys', value: '', placeholder: '搜索供应商名称', options: []},
      ],
      //列表
      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 supplierPage(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+'合作记录'
    },
    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>
