<template>
  <div>
    <el-form :model="searchForm" label-width="0" inline>
      <el-form-item>
        <el-input v-model="searchForm.signId" placeholder="成交单号" clearable />
      </el-form-item>
      <el-form-item>
        <el-input v-model="searchForm.name" placeholder="线索名称" clearable />
      </el-form-item>
      <el-form-item>
        <el-select v-model="searchForm.signUser" placeholder="登记人" clearable filterable>
          <el-option
            v-for="item in userOptions"
            :key="item.id"
            :label="item.nickname"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="searchForm.dealDate"
          type="daterange"
          format="YYYY-MM-DD"
          value-format="YYYY-MM-DD"
          range-separator="-"
          start-placeholder="成交日期"
          end-placeholder="成交日期"
        />
      </el-form-item>
      <el-form-item>
        <el-select
          v-model="searchForm.productId"
          placeholder="选择成交产品"
          filterable
          clearable
          @change="searchForm.specsId = undefined"
        >
          <el-option
            v-for="item in prodOptions"
            :key="item.productId"
            :label="item.productName"
            :value="item.productId"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select
          v-model="searchForm.specsId"
          placeholder="选择规格"
          filterable
          clearable
          :disabled="!searchForm.productId"
        >
          <el-option
            v-for="item in specsOptions(searchForm.productId)"
            :key="item.specsId"
            :label="item.specsName"
            :value="item.specsId"
          />
        </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>
      <el-table-column prop="signId" label="成交单号" min-width="120px" />
      <el-table-column prop="name" label="线索名称" min-width="120px" />
      <el-table-column prop="phone" label="联系方式" width="120px" />
      <el-table-column prop="signUserName" label="登记人" min-width="90" />
      <el-table-column prop="dealDate" label="成交日期" width="120px" :formatter="dateFormatter" />
      <el-table-column prop="productName" label="成交产品" min-width="150px" />
      <el-table-column prop="specsName" label="产品规格" min-width="150px" />
      <el-table-column prop="signNum" label="销售数量" width="90px" />
      <el-table-column
        prop="createTime"
        label="发货时间"
        width="120px"
        :formatter="dateFormatter"
      />
      <el-table-column prop="batchNo" label="发货批次" min-width="120px" />
      <el-table-column prop="warehouseName" label="发货仓库" min-width="120px" />
      <el-table-column prop="sendNum" label="发货数量" width="90px" />
      <el-table-column label="发货备注">
        <template #default="scope">
          <el-popover
            placement="top"
            width="500px"
            trigger="click"
            v-if="scope.row.warehouseName && scope.row.remark"
          >
            <template #reference>
              <el-button type="primary" style="padding: 0" text>点击查看</el-button>
            </template>
            <div v-dompurify-html="scope.row.remark"></div>
          </el-popover>
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      v-model:limit="searchForm.pageSize"
      v-model:page="searchForm.pageNo"
      :total="total"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="Delivery">
import { getSimpleUserList as getUserOption } from '@/api/system/user'
import { getSimpleProductList } from '@/api/mall/product'
import * as DeliveryApi from '@/api/clue/delivery'
import { removeNullField } from '@/utils'

import { dateFormatter } from '@/utils/formatTime'
// const message = useMessage() // 消息弹窗

const searchForm = ref({
  signId: undefined,
  name: undefined,
  dealDate: [],
  signUser: undefined,
  productId: undefined,
  specsId: undefined,
  pageNo: 1,
  pageSize: 20
})

const userOptions = ref([])

const tableList = ref([])
const total = ref(0)

function handleSearch() {
  searchForm.value.pageNo = 1
  getList()
}

function handleReset() {
  searchForm.value = {
    signId: undefined,
    name: undefined,
    dealDate: [],
    signUser: undefined,
    productId: undefined,
    specsId: undefined,
    pageNo: 1,
    pageSize: 20
  }
}

const loading = ref(false)
async function getList() {
  loading.value = true
  try {
    const data = await DeliveryApi.getDeliveryPage(removeNullField(searchForm.value))
    tableList.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

const prodOptions = ref([])

const specsOptions = computed({
  get() {
    return (prodId) => {
      if (prodId) {
        return prodOptions.value.find((it) => it.productId == prodId).productSpecList
      }
      return []
    }
  }
})
function getOptions() {
  getUserOption().then((data) => {
    userOptions.value = data
  })
  // 产品
  getSimpleProductList().then((data) => {
    prodOptions.value = data
  })
}

onMounted(() => {
  getOptions()
  handleSearch()
})
</script>

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