<template>
  <view class="page">
    <view>
      <view class="flex-col group" v-show="deviceList.length > 0">
        <view class="flex-col section_1">
          <view class="flex-row-center-center page__title">监控台</view>
          <view class="flex-col group_5">
            <view class="flex-col group_6">
              <text class="text_3">{{ companyName }}</text>
              <text class="text_4">{{ userName }}</text>
            </view>
            <view class="flex-col section_2">
              <view class="justify-between">
                <view class="flex-row">
                  <text class="text_5">设备数据</text>
                  <view class="flex-row" @click="go2('device-operation', { operation: 'add' })">
                    <text class="text_6">添加</text>
                    <image src="/static/imgs/digital-workshops/add-icon.png" class="image_3" />
                  </view>
                </view>
                <view class="flex-row group_9">
                  <qn-datetime-picker :end="maxTimestamp" :border="false" type="daterange" @change="dateRangeChange">
                    <text>{{ dateRange ? `${dateRange[0]}～${dateRange[1]}` : '全部' }}</text>
                  </qn-datetime-picker>
                  <image v-show="!dateRange" src="/static/imgs/digital-workshops/down-arrow-icon.png" class="image_4" />
                  <image v-show="dateRange" @click="dateRangeChange(null)" src="/static/imgs/general/close-icon.png" class="image_4" />
                </view>
              </view>
              <view class="justify-between group_10">
                <text>设备类型</text>
                <view class="flex-row group_11">
                  <text>设备数量(台)</text>
                  <text class="text_10">设备产量(件)</text>
                </view>
              </view>
              <view class="flex-row group_18" v-for="(item, index) in deviceTypeList" :key="index">
                <view class="flex-row group_19">
                  <view class="section_6" :style="{ backgroundColor: colorList[index] }"></view>
                  <text class="text_20">{{ item.name }}</text>
                </view>
                <text class="text-wrapper_6">{{ item.equipmentNum }}</text>
                <text class="text-wrapper_7">{{ item.produceTotalNum }}</text>
              </view>
            </view>
          </view>
        </view>
        <view class="flex-col group_20">
          <view class="flex-row group_21">
            <text>设备列表(共{{ deviceNum }}台)</text>
            <image @click="toggle" src="/static/imgs/digital-workshops/toggle-icon.png" class="image_5" />
          </view>
          <view class="flex-col section_7" v-show="curTab == 1">
            <view class="flex-row section_8">
              <text>设备名称</text>
              <text class="text_25">状态</text>
              <text class="text_26">产量/件</text>
              <text class="text_27">工作时长/h</text>
            </view>
          </view>
          <scroll-list ref="list" :option="option" @load="upCallback" @refresh="downCallback">
            <view class="flex-col section_9" v-if="curTab == 1">
              <view class="device-group flex-row" v-for="device in deviceList" :key="device.id" @click="go2('device-production-detail', { id: device.id })">
                <text class="text_28">{{ device.name }}</text>
                <view
                  class="flex-col items-center view_1"
                  :class="{ 'text-wrapper_8': device.workingStatus == deviceStatus.WORKING, 'text-wrapper_9': device.workingStatus == deviceStatus.FREE }"
                >
                  <text>{{ device.workingStatus == deviceStatus.WORKING ? '工作中' : '空闲中' }}</text>
                </view>
                <text class="text_30 text_31">{{ device.produceTotalNum }}</text>
                <text class="text_32">{{ device.totalWorkHour | formatNumber }}</text>
              </view>
            </view>
            <view class="grid" v-if="curTab == 0">
              <view class="grid-item flex-col" v-for="device in deviceList" :key="device.id" @click="go2('device-production-detail', { id: device.id })">
                <view class="flex-row">
                  <text class="text_24">{{ device.name }}</text>
                  <view
                    v-if="device.cloudBoxId"
                    class="column flex-col items-center"
                    :class="`right-text-wrapper${device.workingStatus == deviceStatus.WORKING ? '__active' : ''}`"
                  >
                    <text>{{ device.workingStatus == deviceStatus.WORKING ? '工作中' : '空闲中' }}</text>
                  </view>
                </view>
                <image
                  :src="device.cameraId ? '/static/imgs/digital-workshops/camera-tip-bg.png' : '/static/imgs/digital-workshops/no-camera.png'"
                  class="image_6"
                />
                <view v-if="device.cloudBoxId">
                  <view class="bottom-group justify-between">
                    <text>产量：</text>
                    <text>{{ device.produceTotalNum || 0 }}个</text>
                  </view>
                  <view class="bottom-group justify-between">
                    <text>时长：</text>
                    <text>{{ device.totalWorkHour | formatNumber }}h</text>
                  </view>
                </view>
                <view v-else>
                  <view class="bottom-group justify-between">
                    <text>未安装云盒</text>
                  </view>
                </view>
              </view>
            </view>
          </scroll-list>
        </view>
      </view>
      <view v-show="deviceList.length == 0">
        <empty-data></empty-data>
      </view>
    </view>
  </view>
</template>

<script>
import { enterpriseType, deviceStatus } from '@/enums/index.js'
import { go2 } from '@/utils/hook.js'
import { round } from '@/utils/index.js'
import EmptyData from './EmptyData.vue'
import { getDeviceList, getEquipmentStatistics } from '@/apis/deviceApi.js'
const colorList = ['#FA541C', '#EB2F96', '#13C2C2', '#9254DE', '#1890FF', '#4E6EFE', '#FAA90E', '#FF3333', '#64B327']
export default {
  components: {
    EmptyData
  },
  data() {
    return {
      dateRange: null,
      deviceTypeList: [],
      deviceList: [],
      deviceNum: 0,
      colorList: Object.freeze(colorList),
      pagination: {
        pageNum: 0,
        pageSize: 10
      },
      option: {
        size: 10,
        auto: true,
        emptyText: '暂无设备~',
        background: '#F7F8FA',
        fontSize: '40rpx'
      },
      deviceStatus: Object.freeze(deviceStatus),
      curTab: 0,
      maxTimestamp: new Date().getTime()
    }
  },
  computed: {
    userName() {
      return this.$store.state.userInfo.name || this.$store.state.userInfo.mobile
    },
    companyName() {
      return this.$store.state.companyInfo.name
    }
  },
  onShow() {
    let isFactory = this.$store.state.companyInfo.enterpriseType == enterpriseType.PRINT_PACKAGE_FACTORY
    if (isFactory) {
      this.getDeviceList()
      this.getDeviceStatistics()
    }
  },
  methods: {
    go2,
    round,
    // 获取设备统计信息
    getDeviceStatistics() {
      getEquipmentStatistics({
        pageNum: 1,
        pageSize: 100,
        startDate: this.dateRange ? this.dateRange[0] : null,
        endDate: this.dateRange ? this.dateRange[1] : null
      }).then((res) => {
        if (res) {
          this.deviceTypeList = res.records
        }
      })
    },
    selectDateRange() {},
    dateRangeChange(value) {
      if (value) {
        this.dateRange = [...value]
      } else {
        this.dateRange = null
      }
    },
    // 获取设备列表
    getDeviceList() {
      return new Promise((resolve, reject) => {
        let condition = {
          ...this.pagination,
          startDate: this.dateRange ? this.dateRange[0] : '',
          endDate: this.dateRange ? this.dateRange[1] : ''
        }
        getDeviceList(condition)
          .then((res) => {
            if (res) {
              if (res.current <= 1) {
                this.deviceList = res.records
              } else {
                this.deviceList = this.list.concat(res.records)
              }
              this.deviceNum = res.total
              resolve({ list: this.deviceList, total: res.total })
            } else {
              reject()
            }
          })
          .catch((err) => {
            reject(err)
          })
      })
    },
    downCallback() {
      this.pagination.pageNum = 1
      this.getDeviceList()
        .then(({ list, total }) => {
          this.$refs.list.refreshSuccess({ list, total })
        })
        .catch(() => {
          this.$refs.list.refreshFail()
        })
    },
    upCallback() {
      this.pagination.pageNum++
      this.getDeviceList()
        .then(({ list, total }) => {
          this.$refs.list.loadSuccess({ list, total })
        })
        .catch(() => {
          this.$refs.list.loadFail()
        })
    },
    toggle() {
      this.curTab = (this.curTab + 1) % 2
    }
  },
  watch: {
    dateRange() {
      this.getDeviceList()
      this.getDeviceStatistics()
    }
  },
  filters: {
    formatNumber(number) {
      if (number) {
        return round(number, 2)
      }
      return ''
    }
  }
}
</script>

<style lang="scss" scoped>
.page {
  height: 100vh;
  width: 750rpx;
  background-color: #f7f8fa;
  .page__title {
    font-size: 34rpx;
    color: rgb(255, 255, 255);
    margin-top: 56rpx;
  }
}
.group {
  flex: 1 1 auto;
  overflow-y: auto;
  .section_1 {
    padding-top: 4rpx;
    background: url('https://qncloud.oss-cn-shenzhen.aliyuncs.com/print-package-app/digital-workshop-bg.png') no-repeat top/cover;
    .group_5 {
      margin-top: 70rpx;
      .group_6 {
        padding: 0 32rpx;
        .text_3 {
          color: rgb(255, 255, 255);
          font-size: 38rpx;
          font-weight: 500;
          line-height: 53rpx;
          white-space: nowrap;
        }
        .text_4 {
          margin-top: 5rpx;
          color: rgb(255, 255, 255);
          font-size: 34rpx;
          line-height: 48rpx;
          white-space: nowrap;
        }
      }
      .section_2 {
        margin-top: 43rpx;
        padding: 30rpx 32rpx 32rpx;
        background-color: rgb(255, 255, 255);
        border-radius: 20rpx;
        .group_10 {
          margin-top: 16rpx;
          color: rgb(133, 133, 133);
          font-size: 24rpx;
          line-height: 33rpx;
          white-space: nowrap;
          .group_11 {
            margin-right: 64rpx;
            .text_10 {
              margin-left: 72rpx;
            }
          }
        }
        .group_18 {
          margin-top: 20rpx;
          .group_19 {
            width: 260rpx;
            color: rgb(31, 31, 31);
            font-size: 28rpx;
            line-height: 40rpx;
            .section_6 {
              align-self: center;
              background-color: rgb(100, 179, 39);
              border-radius: 2rpx;
              width: 16rpx;
              height: 16rpx;
            }
            .text_20 {
              white-space: nowrap;
              text-overflow: ellipsis;
              overflow: hidden;
              margin-left: 10rpx;
              width: 240rpx;
            }
          }
          .text-wrapper_6 {
            margin-left: 20rpx;
            width: 190rpx;
            color: rgb(51, 51, 51);
            font-size: 28rpx;
            font-weight: 500;
            line-height: 40rpx;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
          }
          .text-wrapper_7 {
            margin-left: 20rpx;
            width: 200rpx;
            color: rgb(51, 51, 51);
            font-size: 28rpx;
            font-weight: 500;
            line-height: 40rpx;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
          }
        }
        .group_9 {
          margin: 8rpx 0;
          color: rgb(133, 133, 133);
          font-size: 24rpx;
          line-height: 33rpx;
          white-space: nowrap;
          .image_4 {
            margin: 0 0 5rpx 16rpx;
            width: 28rpx;
            height: 28rpx;
          }
        }
        .text_5 {
          color: rgb(31, 31, 31);
          font-size: 34rpx;
          font-weight: 500;
          line-height: 48rpx;
          white-space: nowrap;
        }
        .text_6 {
          margin: 4rpx 0 4rpx 24rpx;
          color: rgb(0, 122, 255);
          font-size: 28rpx;
          line-height: 40rpx;
          white-space: nowrap;
        }
        .image_3 {
          margin: 8rpx 0 8rpx 14rpx;
          width: 32rpx;
          height: 32rpx;
        }
      }
    }
    .group_2 {
      padding: 0 5rpx;
      position: relative;
      .image-wrapper {
        position: absolute;
        left: 5rpx;
        top: 50%;
        transform: translateY(-50%);
        width: 107rpx;
        .image {
          width: 92rpx;
          height: 16rpx;
        }
      }
      .group_3 {
        line-height: 26rpx;
        white-space: nowrap;
        .text {
          color: rgb(255, 255, 255);
          font-size: 20rpx;
          line-height: 26rpx;
        }
        .text_1 {
          color: rgb(255, 255, 255);
          font-size: 20rpx;
          line-height: 26rpx;
        }
      }
      .group_4 {
        color: rgb(255, 255, 255);
        font-size: 22rpx;
        line-height: 26rpx;
        white-space: nowrap;
        position: absolute;
        right: 13rpx;
        top: 50%;
        transform: translateY(-50%);
        .image_1 {
          margin: 4rpx 0 3rpx 10rpx;
          width: 43rpx;
          height: 19rpx;
        }
      }
    }
    .image_2 {
      margin-top: 25rpx;
      width: 100vw;
      height: 8vw;
    }
  }
  .group_20 {
    padding: 34rpx 0 30rpx;
    .group_21 {
      padding: 0 32rpx;
      color: rgb(31, 31, 31);
      font-size: 34rpx;
      font-weight: 500;
      line-height: 48rpx;
      white-space: nowrap;
      .image_5 {
        margin-left: 16rpx;
        align-self: center;
        width: 28rpx;
        height: 28rpx;
      }
    }
    .grid {
      padding: 0 32rpx;
      margin-top: 20rpx;
      display: grid;
      grid-template-columns: repeat(2, 1fr);
      grid-row-gap: 22rpx;
      grid-column-gap: 10rpx;
      .grid-item {
        padding: 24rpx 20rpx 20rpx;
        border-radius: 22rpx;
        box-shadow: 0 0 8rpx rgba(0, 0, 0, 0.1);
        background-color: rgb(255, 255, 255);
        .image_6 {
          margin-top: 20rpx;
          width: 296rpx;
          height: 182rpx;
        }
        .bottom-group {
          margin-top: 20rpx;
          color: rgb(31, 31, 31);
          font-size: 22rpx;
          line-height: 30rpx;
          white-space: nowrap;
        }
        .text_24 {
          width: 170rpx;
          overflow: hidden;
          text-overflow: ellipsis;
          color: rgb(31, 31, 31);
          font-size: 28rpx;
          font-weight: 500;
          line-height: 40rpx;
          white-space: nowrap;
        }
        .column {
          margin-left: 14rpx;
          margin-right: 20rpx;
        }
      }
    }
    .right-text-wrapper__active {
      color: rgb(12, 187, 107);
      font-size: 24rpx;
      line-height: 33rpx;
      white-space: nowrap;
      padding: 2rpx 0;
      background-color: rgba(223, 255, 226, 0.6);
      border-radius: 4rpx;
      width: 92rpx;
      height: 40rpx;
      border: solid 2rpx rgba(12, 187, 107, 0.6);
    }
    .right-text-wrapper {
      padding: 2rpx 0;
      color: rgb(197, 49, 37);
      font-size: 24rpx;
      line-height: 33rpx;
      white-space: nowrap;
      background-color: rgba(255, 225, 223, 0.6);
      border-radius: 4rpx;
      width: 92rpx;
      height: 40rpx;
      border: solid 2rpx rgba(197, 49, 37, 0.6);
    }
  }
}
.section_7 {
  background-color: rgb(255, 255, 255);
  border-radius: 20rpx 20rpx 0 0;
  .section_8 {
    padding: 24rpx 32rpx 22rpx;
    color: rgb(133, 133, 133);
    font-size: 24rpx;
    line-height: 33rpx;
    white-space: nowrap;
    background-color: rgb(255, 255, 255);
    border-radius: 20rpx 20rpx 0px 0px;
    border-bottom: solid 2rpx rgba(151, 151, 151, 0.4);
    .text_25 {
      margin-left: 145rpx;
    }
    .text_26 {
      margin-left: 95rpx;
    }
    .text_27 {
      margin-left: 90rpx;
    }
  }
}
.section_9 {
  padding: 0 32rpx;
  background-color: rgb(255, 255, 255);
  .device-group {
    padding: 23rpx 0;
    border-bottom: solid 2rpx rgba(151, 151, 151, 0.4);
    .text_28 {
      width: 220rpx;
      overflow: hidden;
      text-overflow: ellipsis;
      color: rgb(31, 31, 31);
      font-size: 28rpx;
      line-height: 40rpx;
      white-space: nowrap;
    }
    .view_1 {
      margin-left: 20rpx;
    }
    .text_30 {
      width: 150rpx;
      color: rgb(31, 31, 31);
      font-size: 28rpx;
      line-height: 40rpx;
      white-space: nowrap;
    }
    .text_31 {
      margin-left: 50rpx;
    }
    .text_32 {
      margin-left: 20rpx;
      color: rgb(31, 31, 31);
      font-size: 28rpx;
      line-height: 40rpx;
      white-space: nowrap;
    }
    .text_34 {
      color: rgb(31, 31, 31);
      font-size: 28rpx;
      line-height: 40rpx;
      white-space: nowrap;
    }
    .view_3 {
      margin-left: 71rpx;
    }
    .text_36 {
      margin-left: 50rpx;
    }
    .text_41 {
      margin-right: 85rpx;
      color: rgb(31, 31, 31);
      font-size: 28rpx;
      line-height: 40rpx;
      white-space: nowrap;
    }
    .text_42 {
      color: rgb(31, 31, 31);
      font-size: 28rpx;
      line-height: 40rpx;
      white-space: nowrap;
    }
    .group_23 {
      margin-left: 73rpx;
    }
    .text_46 {
      margin-left: 131rpx;
    }
    .text_47 {
      color: rgb(31, 31, 31);
      font-size: 28rpx;
      line-height: 40rpx;
      white-space: nowrap;
    }
    .group_24 {
      margin-left: 101rpx;
    }
    .text_50 {
      margin-left: 131rpx;
    }
    .text_51 {
      color: rgb(31, 31, 31);
      font-size: 28rpx;
      line-height: 40rpx;
      white-space: nowrap;
    }
    .group_25 {
      margin-left: 101rpx;
    }
    .text_54 {
      margin-left: 131rpx;
    }
    .text_38 {
      color: rgb(31, 31, 31);
      font-size: 28rpx;
      line-height: 40rpx;
      white-space: nowrap;
    }
    .view_5 {
      margin-left: 15rpx;
    }
    .text_40 {
      margin-left: 50rpx;
      color: rgb(31, 31, 31);
      font-size: 28rpx;
      line-height: 40rpx;
      white-space: nowrap;
    }
    .text-wrapper_8 {
      color: rgb(12, 187, 107);
      font-size: 24rpx;
      line-height: 33rpx;
      white-space: nowrap;
      padding: 2rpx 0;
      background-color: rgba(223, 255, 226, 0.6);
      border-radius: 4rpx;
      width: 92rpx;
      height: 40rpx;
      border: solid 2rpx rgba(12, 187, 107, 0.6);
    }
    .text_44 {
      margin-left: 50rpx;
      color: rgb(31, 31, 31);
      font-size: 28rpx;
      line-height: 40rpx;
      white-space: nowrap;
    }
  }
  .group_26 {
    padding: 19rpx 0;
    border-bottom: solid 2rpx rgba(151, 151, 151, 0.4);
    .group_27 {
      width: 434rpx;
      .group_28 {
        text-align: left;
        width: 228rpx;
        height: 80rpx;
        .text_55 {
          color: rgb(31, 31, 31);
          font-size: 28rpx;
          line-height: 40rpx;
        }
        .text_56 {
          color: rgb(0, 122, 255);
          font-size: 28rpx;
          line-height: 40rpx;
        }
      }
      .view_12 {
        margin-left: 11rpx;
        align-self: center;
      }
      .text_58 {
        margin-left: 50rpx;
        align-self: center;
        color: rgb(31, 31, 31);
        font-size: 28rpx;
        line-height: 40rpx;
        white-space: nowrap;
      }
    }
    .text_59 {
      margin-right: 85rpx;
      align-self: center;
      color: rgb(31, 31, 31);
      font-size: 28rpx;
      line-height: 40rpx;
      white-space: nowrap;
    }
  }
  .text-wrapper_9 {
    color: rgb(197, 49, 37);
    font-size: 24rpx;
    line-height: 33rpx;
    white-space: nowrap;
    padding: 2rpx 0;
    background-color: rgba(255, 225, 223, 0.6);
    border-radius: 4rpx;
    width: 92rpx;
    height: 40rpx;
    border: solid 2rpx rgba(197, 49, 37, 0.6);
  }
}
</style>
