<template>
  <!-- 零食陈列 -->
  <el-form :inline="true"
    :model="queryParams"
    class="demo-form-inline">
    <el-form-item label="计划月份">
      <el-date-picker v-model="queryParams.salesMonth"
        type="month"
        placeholder="选择计划月份"
        @change="getTableList"
        clearable />
    </el-form-item>
  </el-form>
  <CustomTable :tableData="tableData"
    :baseColumns="baseColumns"
    :isLoading="isLoading"
    :queryParams="queryParams"
    @getTableList="getTableList"
    @updateShowSearch="v => showSearch.value = v" />
</template>

<script setup
  lang="jsx">
  import CustomTable from '../components/Table'
  import { getDisplayScheduleDashboardList, getDisplayScheduleDashboardListArea, getDisplayScheduleDashboardListStore } from '@/api'
  import { parseTime } from '@/utils'
  import { getSnackCofing } from './data.jsx';

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

    // 关联的公式计算列，需要特殊处理
    for (const str of col.requestKey) {
      const obj = getSnackCofing().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 submitSnackPlan({
      id: row.sasdId,
      [col.prop]: row[col.prop], // 当前修改列的值
      ...col.requestKey.reduce((acc, key) => ({ ...acc, [key]: row[key] }), {}), // 额外携带影响的列字段值
    })
    
  }
  // 全部列
  const baseColumns = ref(getSnackCofing(submitChange));

  // 表格数据
  const queryParams = reactive({
    salesMonth: new Date(),
    operation: ''
  })
  const tableData = ref([])
  const isLoading = ref(true)
  const total = ref(0)

  // 筛选列表数据
  const apiObj = {
    '大区战区-分析': getDisplayScheduleDashboardList,
    '城市经理-分析': getDisplayScheduleDashboardListArea,
    '经销商-分析': getDisplayScheduleDashboardListStore,
  }
  const getTableList = async () => {
    isLoading.value = true
    const res = await apiObj[queryParams.operation]({
      ...queryParams,
      salesMonth: parseTime(queryParams.salesMonth, '{y}-{m}')
    })
    if (queryParams.operation === '大区战区-分析') {
      // 合并战区大区结构为扁平化
      const { zq, dq, hz } = res.data
      tableData.value = [...zq, ...dq, ...hz]
    } else if (queryParams.operation === '经销商-分析') {
      // 合并经销商结构为扁平化
      const { dist, hz } = res.data
      tableData.value = [...dist, ...hz]
    } else {
      tableData.value = res.data
    }
    isLoading.value = false
  }

  const { proxy } = getCurrentInstance()
  onMounted(() => {
    const isDaQuZQ = proxy.checkPermi(['promotion:dashboard:list-show'])
    queryParams.operation = isDaQuZQ ? '大区战区-分析' : '城市经理-分析'
    getTableList()
  })
</script>

<style scoped
  lang="scss">
  .container {

    .el-tabs {
      height: 100%;
      display: flex;
      flex-direction: column-reverse;

      .el-tabs__content {
        display: flex;
        flex-direction: column;
        height: 100%;

        .el-tab-pane {
          height: 100%;
          display: flex;
          flex-direction: column;

          .pagination-container {
            margin: 10px;
          }
        }

        .formula-column {
          display: flex;
          justify-content: center;
          align-items: center;

          .el-icon {
            margin-left: 2px;
          }
        }
      }
    }

    .el-form-item {
      align-items: center;
    }

    /* 表格区域 */
    .auto-fit-header-table {

      /* 优化超长文本的显示效果 */
      .cell {
        /* padding: 0 .2133rem; */
      }

      ::v-deep(.el-table__row) {
        .el-table__cell {
          padding: 0;
        }
      }


      ::v-deep(.column-style) {

        .cell-style {
          /* margin: 0 -12px; */


          >div {
            display: flex;
            flex-direction: column;
            align-items: flex-start;

            >span {
              text-align: left;
              text-indent: 5px;
              display: inline-block;
              width: 100%;
              background-color: #e1e2e6;
              border-bottom: 1px solid #ebeef5;
            }
          }

          /* 表格内下拉框 */
          .el-select {
            width: 100% !important;
            padding: 10px;
          }

          .el-input {
            padding: 10px;
          }
        }
      }

    }
  }
</style>