<template>
  <ContentWrap>
    <el-form :model="searchForm" label-width="0" inline>
      <el-form-item>
        <el-date-picker
          v-model="searchForm.period"
          type="daterange"
          format="YYYY-MM-DD"
          value-format="YYYY-MM-DD"
          start-placeholder="选择日期"
          end-placeholder="选择日期"
        />
      </el-form-item>
      <el-form-item>
        <el-input v-model="searchForm.nickname" placeholder="销售姓名" clearable />
      </el-form-item>
      <el-form-item>
        <el-tree-select
          v-model="searchForm.sourceId"
          :data="sourceOptions"
          :props="defaultProps"
          check-strictly
          node-key="sourceId"
          placeholder="请选择渠道"
        />
      </el-form-item>
      <el-form-item>
        <el-select v-model="searchForm.cartype" placeholder="选择驾照类型" clearable>
          <el-option
            v-for="item in cartypeOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button @click="handleSearch">查询</el-button>
        <el-button @click="handleReset">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="tableList" border stripe>
      <el-table-column type="index" width="50" fixed="left" />
      <el-table-column prop="nickname" label="姓名" width="80" fixed="left" />
      <el-table-column prop="" sortable min-width="100">
        <template #header>
          <Tooltip message="咨询日期在所选周期内的线索总数" />
          <span>新线索数</span>
        </template>
      </el-table-column>
      <el-table-column label="成交数" prop="" sortable min-width="100">
        <template #header>
          <Tooltip message="成交日期在所选周期内的成交数" />
          <span>成交数</span>
        </template>
      </el-table-column>
      <el-table-column label="成交率" prop="" sortable min-width="100" />
      <el-table-column label="跟进数/日" prop="" sortable min-width="100" />
      <el-table-column label="成交数/日" prop="" sortable min-width="100" />
      <el-table-column label="平均成交周期/天" prop="" sortable min-width="100" />
      <el-table-column prop="" sortable min-width="100">
        <template #header>
          <Tooltip
            message="平均每笔成交订单的公司利润【按照登记，不按审核到账】=总公司利润/总成交数"
          />
          <span>毛利/单</span>
        </template>
      </el-table-column>
      <el-table-column prop="" sortable min-width="100">
        <template #header>
          <Tooltip
            message="平均每笔成交订单的额外支出（返费等）【按照登记，不按审核到账】=总支出/总成交数"
          />
          <span>支出/单</span>
        </template>
      </el-table-column>
      <el-table-column prop="" sortable min-width="100">
        <template #header>
          <Tooltip message="平均每条线索的成本=总线索成本/新线索条数" />
          <span>线索成本/条</span>
        </template>
      </el-table-column>
      <el-table-column prop="" sortable min-width="100">
        <template #header>
          <Tooltip message="=（总公司利润-总支出-总线索成本）/总成交数" />
          <span>净利润/单</span>
        </template>
      </el-table-column>
      <el-table-column label="总线索成本" prop="" sortable min-width="100" />
      <el-table-column label="总支出" prop="" sortable min-width="100" />
      <el-table-column label="总利润" prop="" sortable min-width="100" />
      <el-table-column prop="" sortable min-width="100">
        <template #header>
          <Tooltip message="=总利润/(总成本+总支出)" min-width="100" />
          <span>能效</span>
        </template>
      </el-table-column>
      <el-table-column label="撞单数" prop="" sortable min-width="100" />
      <el-table-column label="撞单成交数" prop="" sortable min-width="100" />
    </el-table>
  </ContentWrap>
</template>

<script setup name="SalesReport">
import * as reportApi from '@/api/home/reportSignRate'
import { removeNullField } from '@/utils'

onMounted(() => {
  handleReset()
  handleSearch()
})

const searchForm = ref({})

function handleReset() {
  searchForm.value = {
    nickname: undefined,
    year: new Date().getFullYear() + ''
  }
}

const loading = ref(false)
const tableList = ref([])
async function handleSearch() {
  loading.value = true
  try {
    const data = await reportApi.getList(removeNullField(searchForm.value))
    tableList.value = data.map((item) => {
      const count = item.monthClueSignRateReportList.reduce(
        (pre, cur) => {
          return {
            signCount: pre.signCount + cur.clueSignNum,
            clueCount: pre.clueCount + cur.followClueNum
          }
        },
        { signCount: 0, clueCount: 0 }
      )
      const rate = count.clueCount > 0 ? ((count.signCount * 100) / count.clueCount).toFixed(2) : 0
      return {
        ...item,
        totalRate: `${rate}%(${count.signCount}/${count.clueCount})`,
        totalRateNum: rate
      }
    })
  } finally {
    loading.value = false
  }
}

// function monthSort(pre, cur, idx) {

//   return (
//     Number(pre.monthClueSignRateReportList[idx].rate) -
//     Number(cur.monthClueSignRateReportList[idx].rate)
//   )
// }
</script>

<style lang="scss" scoped></style>
