<template>
  <!-- 档期陈列 -->
  <SearchList :showSearch="showSearch"
    :queryParams="params"
    @change="getTableList" />
  <CustomTable :tableData="tableData"
    :baseColumns="baseColumns"
    :total="total"
    :params="params"
    :isLoading="isLoading"
    @getTableList="getTableList"
    @updateShowSearch="v => showSearch.value = v" />
</template>

<script setup
  lang="jsx">
  import CustomTable from '../components/Table'
  import SearchList from '../components/SearchList'
  import { getDisplayScheduleDetail, submitDisplayScheduleDetail } from '@/api'
  import { parseTime } from '@/utils'
  import { getScheduleDisConfig } from './data'

  /*************** 表格数据 ***************/
  // 提交变更
  const submitChange = async (row, col) => {
    // 需要特殊处理的
    // 1. 实际主货架-形式，为空时，置空实际主货架-数量
    if (col.prop === 'actualMainShelfType' && !row.actualMainShelfType) {
      row.actualMainShelfQty = 0
    }

    // 关联的公式计算列，需要特殊处理
    for (const str of col.requestKey) {
      const obj = getScheduleDisConfig().flatMap(item => {
        if (item.children) {
          return item.children.filter(child => !child.onlyFill);
        }
        return [];
      }).find(item => item.prop === str)
      if (obj && obj.type === 'formula') {
        obj.func(row)
      }
    }

    await submitDisplayScheduleDetail({
      id: row.sapdId,
      [col.prop]: row[col.prop], // 当前修改列的值
      ...col.requestKey.reduce((acc, key) => ({ ...acc, [key]: row[key] }), {}), // 额外携带影响的列字段值
      // 特殊类型字段处理
      // 端架数量实际
      actualEndCapQty: row.actualEndCapQty || 0,
      // 地堆平米数实际
      actualFloorStackArea: row.actualFloorStackArea || 0,
    })
  }
  // 全部列
  const baseColumns = ref(getScheduleDisConfig(submitChange));

  // 表格数据
  const tableData = ref([])
  const isLoading = ref(true)
  const params = ref({
    pageNum: 1,
    pageSize: 50,
    salesMonth: new Date(),
    deptName: '',
    dealerCN: '',
    lineNameLike: '',
    storeCN: ''
  })
  const total = ref(0)

  // 筛选工具
  const getTableList = async () => {
    isLoading.value = true
    const res = await getDisplayScheduleDetail({
      ...params.value,
      salesMonth: parseTime(params.value.salesMonth, '{y}-{m}')
    })
    res.data.rows.forEach(item => {
      // 计划月份
      item.salesMonth = parseTime(item.salesMonth, '{y}-{m}')
      // 开户日期
      item.openingDate = parseTime(item.openingDate, '{y}-{m}-{d}')
      // 闭户日期
      item.closingDate = parseTime(item.closingDate, '{y}-{m}-{d}')
      // 计划 - 档期开始时间
      item.plannedPromotionStartDate = parseTime(item.plannedPromotionStartDate, '{y}-{m}-{d}')
      // 计划 - 补差开始时间
      item.plannedAdjustmentStartDate = parseTime(item.plannedAdjustmentStartDate, '{y}-{m}-{d}')
      // 计划 - 档期结束时间
      item.plannedPromotionEndDate = parseTime(item.plannedPromotionEndDate, '{y}-{m}-{d}')
      // 计划 - 补差结束时间
      item.plannedAdjustmentEndDate = parseTime(item.plannedAdjustmentEndDate, '{y}-{m}-{d}')
      // 计划 - 档期陈列开始时间
      item.plannedPromotionDisplayStartDate = parseTime(item.plannedPromotionDisplayStartDate, '{y}-{m}-{d}')
      // 计划 - 档期陈列结束时间
      item.plannedPromotionDisplayEndDate = parseTime(item.plannedPromotionDisplayEndDate, '{y}-{m}-{d}')
      // 修改时间
      item.updateTime = parseTime(item.updateTime, '{y}-{m}-{d} {h}:{i}:{s}')

      // 特殊类型处理
      // item.actualEndCapQty = item.actualEndCapQty || ''
      // item.actualFloorStackArea = item.actualFloorStackArea || ''
    })
    tableData.value = res.data.rows
    total.value = res.data.total
    isLoading.value = false
  }
  getTableList()

  const showSearch = ref(true)

</script>