<script>
export default {
  name: "AppCarouselList",
  label: "分柜监控",
  data() {
    return {
      height: '600px',
      list: [],
      cameras: [],
      storeKeyGoods: [],
      categorySales: [],
      columns: {
        品类销售情况: [
          {label: "品类", prop: "secondCategoryName"},
          {label: "销售额", prop: "currentSaleAmt", width: 70},
          {label: "库存金额", prop: "currentStockAmt", width: 80},
          {label: "同/环比销售额", prop: "compareSaleAmt", width: 70},
          {label: "同/环比库存金额", prop: "compareStockAmt", width: 70},
          {label: "前四周日均销售额", prop: "avg4WeekSaleAmt", width: 70},
        ],
        重点单品情况: [
          {label: "重点单品", prop: "goodsName"},
          {label: "昨日销售数量", prop: "yesterdaySaleNum", width: 70},
          {label: "上周同天销售数量", prop: "lastWeekSaleNum", width: 70},
          {label: "今日销售数量", prop: "saleNum", width: 70},
          {label: "现在库存数量", prop: "stockNum", width: 70},
          {label: "剩余时间预计销售数量", prop: "preSaleNum"},
          // {label: "提醒", custom: 1, width: 70, align: 'center', prop: "remind"},
        ],
        周边库存情况: [
          {label: "门店名称", prop: "storeName"},
          {label: "直线距离", prop: "distance", width: 70},
          {label: "库存数量", prop: "stockNum", width: 70},
          {label: "销售数量", prop: "saleNum", width: 70},
          {label: "剩余时间预计销售数量", prop: "preSaleNum", width: 70},
          {label: "店长姓名/电话", prop: "shopMangerName", format: v => v.shopMangerName ? `${v.shopMangerName}(${v.shopownerPhone})` : ""},
        ]
      },
    }
  },
  computed: {
    search: v => v.$storeBoard.search,
  },
  methods: {
    getData() {
      const {$waitFor, $http} = window
      const {type, compareDate} = this.search
      console.log("筛选条件:", this.search)
      $waitFor($http && (type != 3 || compareDate)).then(() => Promise.all([this.getCameras(), this.getStoreKeyGoods(), this.getCategorySales()]))
          .then(() => {
            this.list = [
              {label: "西点柜", value: "104"},
              {label: "现烤柜", value: "108"},
              {label: "蛋糕柜", value: "109"},
            ].map(({label, value: categoryId}) => {
              const {storeCameraVOList = [], longitude, latitude, storeCode, storeName} = this.cameras.find(e => e.storeCode == this.search.storeCode) || {}
              this.$storeBoard.store = {longitude, latitude, storeCode, storeName}
              const keyGoods = this.storeKeyGoods.filter(e => e.goodsCategoryId == categoryId) || []
              const categorySale = this.categorySales.filter(e => e.secondCategoryId == categoryId) || []
              return {longitude, latitude, categoryId, label, camera: storeCameraVOList.filter(e => e.cameraType == categoryId).map(e => e.cameraUrl), keyGoods, categorySale}
            }) || []
          })
    },
    getCameras() {
      return $http.post("/data-boot/la/screen/multipleStoreBoard/storeCamera", {
        type: "1", ...this.search,
      }).then(res => {
        if (res?.data) {
          this.cameras = res.data?.records || []
        }
      })
    },
    getStoreKeyGoods() {
      return $http.post("/data-boot/la/screen/singleStoreBoard/singStoreKeyGoodsSale", {
        type: "1", ...this.search,
      }).then(res => {
        if (res?.data) {
          this.storeKeyGoods = res.data
          this.$set(this.$storeBoard, 'storeKeyGoods', res.data.filter(e => !/(104|108|109)/.test(e.goodsCategoryId)))
        }
      })
    },
    getCategorySales() {
      return $http.post("/data-boot/la/screen/singleStoreBoard/singStoreCategorySale", {
        type: "1", ...this.search,
      }).then(res => {
        if (res?.data) {
          this.categorySales = res.data
        }
      })
    },
    getTableData(item = {}, tag) {
      const v = this
      const datasource = {
        重点单品情况: item.keyGoods,
        品类销售情况: item.categorySale,
        周边库存情况: v.$storeBoard.aroundStock,
      }
      return {
        headerBGC: 'rgba(13, 48, 99, 0.6)', rowNum: arguments?.[2] || 2,
        oddRowBGC: window.evenRowBGC(), evenRowBGC: "transparent",
        header: v.columns[tag].map(e => e.label),
        columnWidth: v.columns[tag].map(e => e.width || "0;flex:1;min-width:0;"),
        align: v.columns[tag].map(e => e.align || "left"),
        data: datasource[tag]?.map(e => v.columns[tag].map(column => column.custom == 1 ? `<div class="pointer" style="color:${e.preSaleNum > e.stockNum ? 'red' : '#fff'}">周边库存</div>` :
            column.format ? column.format(e) : e[column.prop])) || [],
      }
    },
    openNearbyStores({thirdGoodsCode}, store) {
      const {longitude, latitude} = store
      return $http.post("/data-boot/la/screen/multipleStoreBoard/aroundStock", {
        type: "1", ...this.search, longitude, latitude, thirdGoodsCode,
      }).then(res => {
        if (res?.data) {
          this.$storeBoard.aroundStock = res.data
          this.$nextTick(() => this.$storeBoard.dialog = true)
        }
      })
    },
    getCameraUrl(item) {
      return item.camera?.[0]
    }
  },
  watch: {
    search: {
      immediate: true, deep: true, handler() {
        this.getData()
      }
    }
  },
  mounted() {
    window.addEventListener("click", () => {
      this.$storeBoard.dialog = false
    })
    this.height = `${this.$el.clientHeight}px`
  },
  beforeDestroy() {
    window.removeEventListener("click", () => {
      this.$storeBoard.dialog = false
    })
  }

}
</script>

<template>
  <section class="AppCarouselList">
    <div class="layout">
      <div class="store" v-for="store in list" :key="store.categoryId">
        <div class="headerTitle" v-text="store.label"/>
        <div class="camera">
          <hls-player :id="`hls_player_${store.categoryId}`" :url="getCameraUrl(store)"/>
        </div>
        <div class="subTitle" v-text="'品类销售情况'"/>
        <scroll-table :table-data="store.categorySale" :columns="columns['品类销售情况']"/>
        <!--<dv-scroll-board :config="getTableData(store, '品类销售情况')"/>-->
        <div class="subTitle" v-text="'重点单品情况'"/>
        <scroll-table :table-data="store.keyGoods" :columns="columns['重点单品情况']" @click="v=>openNearbyStores(v,store)" @click.native.stop/>
        <!--<dv-scroll-board :config="getTableData(store, '重点单品情况')" @click="v=>openNearbyStores(v,store)" @click.native.stop/>-->
      </div>
    </div>
    <div class="dialogTable" v-if="$storeBoard.dialog" @click.stop>
      <scroll-table :table-data="$storeBoard.aroundStock" :columns="columns['周边库存情况']"/>
    </div>
  </section>
</template>

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

.AppCarouselList .dv-scroll-board, .AppCarouselList .scrollTable {
  height: 200px !important;
}

.AppCarouselList .headerTitle {
  height: 48px;
  padding: 8px 0 8px 38px;
  margin-bottom: 24px;
  box-sizing: border-box;
  line-height: 32px;
  background-image: url("http://10.0.97.209/img/kengee/kengee4.png");
  background-repeat: no-repeat;
  background-size: 100% 100%;
}

.AppCarouselList .subTitle {
  line-height: 20px;
  width: fit-content;
  margin: 24px auto 12px;
  background-image: url("http://10.0.97.209/img/kengee/kengee5.png");
  background-repeat: no-repeat;
  background-size: 100% 2px;
  background-position: center bottom;
}

.AppCarouselList .layout {
  display: flex;
  gap: 24px;
  width: 100%;
  overflow-x: auto;
}

.AppCarouselList .store {
  width: calc(33% - 16px);
}

.AppCarouselList .camera {
  height: 250px;
}
</style>
