<script>
export default {
  name: "AppStoreMonitor",
  label: "市场看板-门店运营监控表",
  data() {
    return {
      tableData: [],
      columns: [
        {label: "课区", prop: "groupName"},
        {label: "课长", prop: "supervisorName"},
        {label: "门店", prop: "storeName", width: 100},
        {label: "昨日门店净收货额", prop: "yesterdayNetAmt"},
        {label: "昨日现烤销售", prop: "yesterdayBakeSaleAmt"},
        {label: "昨日现烤报损", prop: "yesterdayBakeBsAmt"},
        {label: "门店现在现烤库存金额", prop: "bakeStockAmt"},
        {label: "现在现烤销售额", prop: "bakeSaleAmt"},
      ],
      filter: "",
      options: [],
      summary: {}
    }
  },
  computed: {
    search: v => v.$marketBoard.search,
    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.list.map(e => v.columns.map(column => e[column.prop])),
      }
    },
    summaryRow: v => v.columns.map((column, i) => {
      const isNumber = v => /^-?\d+(\.\d+)?%?$/.test(v)
      const style = {textAlign: column.align}
      if (column.width > 0) {
        style.width = `${column.width}px`
      } else {
        style.flex = 1
        style.minWidth = 0
      }
      return {
        style,
        value: isNumber(v.summary[column.prop]) ? v.summary[column.prop] :
            i == 0 ? v.summary.groupName : ""
      }
    })
  },
  methods: {
    getTableData() {
      const {$http, $waitFor} = window
      return $waitFor($http).then(() => $http.post("/data-boot/la/screen/marketBoard/storeMonitor", {
        ...this.search, limit: 999, groupCodeList: [this.filter].filter(Boolean)
      })).then(res => {
        if (res?.data) {
          this.summary = res.data.total
          return this.tableData = res.data?.page?.records || []
        }
      })
    },
    getOptions() {
      const ops = {}
      this.tableData.forEach(e => {
        ops[e.groupCode] = e.supervisorName
      })
      return Object.entries(ops).map(([key, value]) => ({label: value, value: key}))
    }
  },
  watch: {
    search: {
      immediate: true, deep: true, handler() {
        this.getTableData().then(() => {
          if (this.options.length == 0) this.options = this.getOptions()
        })
      }
    }
  }
}
</script>

<template>
  <section class="AppStoreMonitor">
    <app-sub-title text="门店运营监控表">
      <template #right>
        <el-select placeholder="全部" v-model="filter" size="small" clearable class="AppSelect" @change="getTableData">
          <el-option v-for="(op,i) in options" :key="i" v-bind="op"/>
        </el-select>
      </template>
    </app-sub-title>
    <scroll-table :table-data="tableData" :columns="columns"/>
    <!--<dv-scroll-board :config="tableConfig"/>-->
    <div class="summary flex">
      <div class="item" v-for="(col,i) in summaryRow" :key="i" v-text="col.value" :style="col.style"/>
    </div>
  </section>
</template>

<style>
.AppStoreMonitor {
  flex-shrink: 0 !important;
  color: #fff;
  box-sizing: border-box;
  width: 660px;
}

.AppStoreMonitor .dv-scroll-board, .AppStoreMonitor .scrollTable {
  height: calc(100% - 80px) !important;
}

.AppStoreMonitor .el-select {
  width: 120px;
}
</style>
