<template>
  <view class="content">
    <view class="flex-col group">
      <view class="flex-col section_1">
        <view class="justify-between section_2">
          <view class="select-area">
            <qn-select :contentStyle="contentStyle" :options="factoryList" v-model="factoryId" placeholder="请选择工厂"></qn-select>
          </view>
          <text class="text_1" @click="go2('promotion', { id: factoryId, operation: isExample ? 'info' : 'edit' })">推广</text>
        </view>
      </view>
      <view class="flex-col">
        <view class="flex-col section_3">
          <view class="flex-row group_2">
            <view class="text-wrapper" :class="{ 'text-wrapper_selected': curTab === key }" v-for="(value, key) in listObj" :key="key" @click="selectTab(key)">
              <text v-if="key != 'null'">{{ key }}</text>
              <view v-show="curTab === key" class="divider"></view>
            </view>
          </view>
        </view>
        <view class="bottom-group flex-col">
          <view class="flex-row section_5">
            <view class="flex-row group_3" v-for="(value, key) in listObj[curTab].type" :key="key">
              <view class="group_4">
                <text class="text_5">{{ key }}(</text>
                <text class="text_7">{{ value[0] }}</text>
                <text class="text_5">/</text>
                <text class="text_11">{{ value[1] }}</text>
                <text class="text_5">)</text>
              </view>
            </view>
            <view v-if="!listObj['所有设备'].list || listObj['所有设备'].list.length == 0" class="flex-row-center-center empty_1">
              <text>设备列表为空</text>
            </view>
          </view>
          <view class="flex-col section_8" v-if="cameraList && cameraList.length > 0">
            <view class="justify-between group_11">
              <text>摄像机({{ cameraList.length }}台)</text>
              <image src="/static/imgs/digital-workshops/right-arrow-icon.png" class="image_1" @click="go2('camera-list', { id: factoryId })" />
            </view>
            <view class="flex-col items-center group_12">
              <image src="/static/imgs/digital-workshops/camera-tip-bg.png" class="image_2" @click="go2('video-play', { url: cameraList[0].videoUrl })" />
              <text class="text_18">{{ cameraList[0].address }}</text>
            </view>
          </view>
          <view class="flex-col section_9">
            <view class="justify-between">
              <text class="text_19">设备列表</text>
              <view class="flex-row group_14" v-if="!isExample" @click="addDevice">
                <text>新增</text>
                <image src="/static/imgs/digital-workshops/add-icon.png" class="image_4" />
              </view>
            </view>
            <view class="list-area">
              <view v-if="listObj[curTab].list && listObj[curTab].list.length > 0">
                <DeviceItem v-for="item in listObj[curTab].list" :key="item.id" :deviceInfo="item" style="margin-bottom: 20rpx" @click="jump"></DeviceItem>
              </view>
              <view v-else>
                <view class="flex-col group_9">
                  <view class="flex-col items-center group_10">
                    <view class="flex-col items-center image-wrapper">
                      <image src="/static/imgs/digital-workshops/empty-list.png" class="image_5" />
                    </view>
                    <text class="text_10">设备列表为空</text>
                  </view>
                  <view class="flex-col items-center text-wrapper_4" @click="addDevice">
                    <text>添加设备</text>
                  </view>
                </view>
              </view>
            </view>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import { go2, back } from '@/utils/hook.js'
import DeviceItem from './DeviceItem'
import { getDeviceListV2 } from '@/apis/deviceApi'
import { deviceStatus } from '@/enums/index'
import { getCameraListApi, getMyFactoryList } from '@/apis/factoryApi'

export default {
  components: {
    DeviceItem
  },
  data() {
    return {
      contentStyle: 'background: none; padding: 0;text-align: right;color: rgb(255, 255, 255);font-size: 34rpx;',
      factoryId: null,
      isExample: false,
      listObj: {
        所有设备: {
          type: {},
          list: []
        }
      },
      curTab: '所有设备',
      factoryList: [],
      deviceList: [],
      workShopList: [
        {
          label: '所有设备',
          value: 0
        },
        {
          label: '1楼车间',
          value: 1
        }
      ],
      deviceTypeList: [
        { id: 1, name: '印刷机', working: 1, free: 1 },
        { id: 2, name: '模切机', working: 1, free: 1 },
        { id: 3, name: '覆膜机', working: 1, free: 1 }
      ],
      cameraList: []
    }
  },
  onShow() {
    this.getFactoryList()
  },
  watch: {
    factoryId(val) {
      if (val === -1) {
        go2('factory-manage')
      } else {
        this.getCameraList()
        this.getList()
        this.isExample = this.factoryList.find((factory) => factory.value == val).isExample
      }
    }
  },
  methods: {
    jump(item) {
      go2('device-production-info', { id: item.id })
    },
    go2,
    back,
    selectTab(index) {
      this.curTab = index
    },
    getList() {
      getDeviceListV2({ factoryId: this.factoryId, pageSize: 1000 }).then((res) => {
        let list = res.records
        let data = { 所有设备: { name: '所有设备', type: {}, list: [] } }
        for (let i = 0; i < list.length; i++) {
          let belongWorkshop = list[i].belongWorkshop
          if (!data[belongWorkshop]) {
            data[belongWorkshop] = {
              name: belongWorkshop,
              list: [],
              type: {}
            }
          }
          data[belongWorkshop].list.push(list[i])
          data['所有设备'].list.push(list[i])
          // 0 工作中 1 空闲中
          if (!data[belongWorkshop].type[list[i].typeName]) {
            data[belongWorkshop].type[list[i].typeName] = [0, 0]
          }
          if (!data['所有设备'].type[list[i].typeName]) {
            data['所有设备'].type[list[i].typeName] = [0, 0]
          }
          if (list[i].workingStatus === deviceStatus.WORKING) {
            data[belongWorkshop].type[list[i].typeName][0]++
            data['所有设备'].type[list[i].typeName][0]++
          }
          if (list[i].workingStatus === deviceStatus.FREE) {
            data[belongWorkshop].type[list[i].typeName][1]++
            data['所有设备'].type[list[i].typeName][1]++
          }
        }
        this.listObj = data
      })
    },
    addDevice() {
      // 先判断是否时演示工厂
      go2('add-device', { id: this.factoryId, operation: 'edit' })
    },
    // 获取摄像头列表
    getCameraList() {
      getCameraListApi({ factoryId: this.factoryId }).then((res) => {
        this.cameraList = res
      })
    },
    // 获取工厂列表
    getFactoryList() {
      getMyFactoryList().then((res) => {
        this.factoryList = res.map((item) => {
          return {
            label: item.name,
            value: item.id,
            isExample: item.name == '演示样板工厂'
          }
        })
        this.factoryId = this.factoryList[0].value
        this.factoryList.push({
          label: '工厂管理',
          value: -1
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.content {
  width: 750rpx;
  height: 100vh;
  overflow-y: auto;
}
.group {
  flex: 1 1 auto;
  overflow-y: auto;
  .section_1 {
    padding-top: 82rpx;
    background-image: url('/static/imgs/digital-workshops/top-bg.png');
    background-position: 0px 0px;
    background-size: 100% 100%;
    background-repeat: no-repeat;
    .section_2 {
      padding: 20rpx 32rpx;
      background-image: linear-gradient(90deg, rgb(72, 155, 250) 0%, rgb(72, 155, 250) 0%, rgb(24, 108, 224) 100%, rgb(24, 108, 224) 100%);
      .group_1 {
        color: rgb(255, 255, 255);
        font-size: 34rpx;
        font-weight: 600;
        line-height: 48rpx;
        white-space: nowrap;
        .image {
          margin-left: 11rpx;
          align-self: center;
          width: 21rpx;
          height: 12rpx;
        }
      }
      .text_1 {
        margin: 4rpx 0;
        color: rgb(255, 255, 255);
        font-size: 28rpx;
        line-height: 40rpx;
        white-space: nowrap;
        text-decoration: underline;
      }
    }
  }
  .section_3 {
    padding-bottom: 3rpx;
    background-color: rgb(255, 255, 255);
    .group_2 {
      padding: 0 40rpx;
      border-bottom: solid 2rpx rgb(221, 221, 221);
      .text-wrapper {
        color: rgb(136, 136, 136);
        font-size: 28rpx;
        height: 86rpx;
        line-height: 86rpx;
        white-space: nowrap;
        position: relative;
        text-align: center;
        margin-right: 40rpx;
        min-width: 80rpx;
      }
      .text-wrapper_selected {
        color: rgb(51, 51, 51);
        font-weight: 500;
      }
      .divider {
        background-color: rgb(0, 122, 255);
        border-radius: 3rpx;
        width: 80rpx;
        height: 6rpx;
        position: absolute;
        bottom: 0;
        left: 50%;
        transform: translateX(-50%);
      }
    }
  }
  .bottom-group {
    padding-bottom: 20rpx;
    .section_5 {
      padding: 12rpx 32rpx 0;
      background-color: rgb(255, 255, 255);
      flex-wrap: wrap;
      .group_3 {
        flex: 0 0 auto;
        align-items: flex-start;
        width: 24%;
        margin-right: 6rpx;
        margin-bottom: 16rpx;
        .group_4 {
          white-space: nowrap;
          height: 40rpx;
          .text_5 {
            color: rgb(31, 31, 31);
            font-size: 28rpx;
            line-height: 40rpx;
          }
          .text_7 {
            color: rgb(82, 196, 26);
            font-size: 28rpx;
            line-height: 40rpx;
          }
          .text_11 {
            color: rgb(255, 72, 73);
            font-size: 28rpx;
            line-height: 40rpx;
          }
        }
      }
      .empty_1 {
        height: 100rpx;
        width: 686rpx;
        color: rgb(0, 0, 0);
        font-size: 28rpx;
        font-weight: 500;
        line-height: 40rpx;
      }
      .grid-item {
        color: rgb(133, 133, 133);
        font-size: 24rpx;
        line-height: 33rpx;
        white-space: nowrap;
        .left-section {
          align-self: center;
          background-color: rgb(82, 196, 26);
          border-radius: 50%;
          width: 8rpx;
          height: 8rpx;
          margin-right: 4rpx;
        }
        .red {
          background-color: rgb(255, 72, 73);
        }
      }
    }
    .section_8 {
      margin-top: 20rpx;
      padding: 0 32rpx 33rpx;
      background-color: rgb(255, 255, 255);
      .group_11 {
        padding: 19rpx 0;
        color: rgb(51, 51, 51);
        font-size: 36rpx;
        font-weight: 500;
        line-height: 50rpx;
        white-space: nowrap;
        .image_1 {
          margin-right: 9rpx;
          align-self: center;
          width: 14rpx;
          height: 24rpx;
        }
      }
      .group_12 {
        color: rgb(255, 255, 255);
        font-size: 28rpx;
        font-weight: 500;
        line-height: 40rpx;
        white-space: nowrap;
        position: relative;
        .image_2 {
          width: 91.5vw;
          height: 51.5vw;
        }
        .text_18 {
          position: absolute;
          left: 24rpx;
          top: 16rpx;
        }
      }
    }
    .section_9 {
      margin-top: 20rpx;
      padding: 19rpx 32rpx 20rpx;
      background-color: rgb(255, 255, 255);
      .text_19 {
        color: rgb(51, 51, 51);
        font-size: 36rpx;
        font-weight: 500;
        line-height: 50rpx;
        white-space: nowrap;
      }
      .group_14 {
        margin: 5rpx 0;
        color: rgb(0, 122, 255);
        font-size: 28rpx;
        font-weight: 500;
        line-height: 40rpx;
        white-space: nowrap;
        .image_4 {
          margin: 4rpx 0 4rpx 10rpx;
          width: 32rpx;
          height: 32rpx;
        }
      }
      .list-area {
        padding-bottom: 40rpx;
        margin-top: 20rpx;
      }
      .group_9 {
        margin-top: 39rpx;
        .group_10 {
          padding-bottom: 28rpx;
          color: rgb(0, 0, 0);
          font-size: 28rpx;
          font-weight: 500;
          line-height: 40rpx;
          white-space: nowrap;
          position: relative;
          .image-wrapper {
            width: 280rpx;
            .image_5 {
              width: 280rpx;
              height: 366rpx;
            }
          }
          .text_10 {
            position: absolute;
            bottom: 8rpx;
            left: 50%;
            transform: translateX(-50%);
          }
        }
        .text-wrapper_4 {
          padding: 16rpx 0;
          align-self: center;
          color: rgb(0, 122, 255);
          font-size: 32rpx;
          font-weight: 500;
          line-height: 45rpx;
          white-space: nowrap;
          border-radius: 40rpx;
          width: 200rpx;
          border: solid 2rpx rgb(0, 122, 255);
        }
      }
    }
  }
}
</style>
