<script>
export default {
  name: "AppKeyGoods",
  label: "市场看板-重点单品",
  data() {
    return {
      tableData: [],
      columns: [
        {label: "重点单品", prop: "goodsName"},
        {label: "销售数量", prop: "saleNum", width: 70},
        {label: "库存数量", prop: "stockNum", width: 70},
        {label: "销售目标", prop: "targetSaleNum", width: 70},
        {label: "销售达成", prop: "saleAchieveRate"},
      ],
      goodsCategoryId: ""
    }
  },
  computed: {
    search: v => v.$marketBoard.search,
    dicts: v => window.$dicts || {},
    tableConfig: v => {
      return {
        headerBGC: 'rgba(13, 48, 99, 0.6)',
        // headerBGC: '#003B51',
        oddRowBGC: window.evenRowBGC(), evenRowBGC: "transparent",
        header: v.columns.map(e => e.label),
        columnWidth: v.columns.map(e => e.width || "0;flex:1;min-width:0;"),
        align: v.columns.map(e => e.align || "left"),
        data: v.tableData.map(e => v.columns.map(column => e[column.prop])),
      }
    },
  },
  methods: {
    getTableData() {
      const {$http, $waitFor} = window
      const {goodsCategoryId} = this
      $waitFor($http).then(() => $http.post("/data-boot/la/screen/marketBoard/marketKeyGoods", {
        ...this.search, limit: 999, goodsCategoryId
      })).then(res => {
        if (res?.data) {
          this.tableData = res.data?.records || []
        }
      })
    },
    relevanceMapData(selectGoods) {
      this.$set(this.$marketBoard, 'thirdGoods', selectGoods)
    }
  },
  watch: {
    search: {
      immediate: true, deep: true, handler() {
        this.getTableData()
      }
    }
  },
  mounted() {
    document.addEventListener('click', evt => {
      const mapElement = document.querySelector('.AppMap')
      if (!this.$el.contains(evt.target) && !mapElement.contains(evt.target)) {
        this.relevanceMapData()
      }
    })
  },
  beforeDestroy() {
    document.removeEventListener('click', this.relevanceMapData)
  }
}
</script>

<template>
  <section class="AppKeyGoods">
    <app-sub-title text="重点单品">
      <template #right>
        <el-select placeholder="全部" v-model="goodsCategoryId" size="small" clearable class="AppSelect" @change="getTableData">
          <el-option v-for="(op,i) in dicts.品类" :key="i" v-bind="op"/>
        </el-select>
      </template>
    </app-sub-title>
    <scroll-table :table-data="tableData" :columns="columns" @click="relevanceMapData" @click.native.stop/>
    <!--<dv-scroll-board :config="tableConfig"/>-->
  </section>
</template>

<style>
.AppKeyGoods {
  color: #fff;
  box-sizing: border-box;
}

.AppKeyGoods .dv-scroll-board, .AppKeyGoods .scrollTable {
  height: calc(100% - 60px) !important;
}
</style>
