<template>
  <div id="big-screen" ref="appRef">
    <div class="refresh-time">
      {{ refreshTime }}
    </div>
    <topBackground></topBackground>
    <Title ref="title" @show-done="$refs['done'].show()" @step-click="stepClick"></Title>
    <toDo ref="toDo"
          @step-click="stepClick"
          @callbackList="({ id }) => {
            $refs['callbackList'].setId(id)
            $refs['callbackList'].show()
          }"
          @callback="e => {
             $refs['callback'].setRow(e)
             $refs['callback'].setId(e.id)
             $refs['callback'].setType('add')
             $refs['callback'].show()
           }"></toDo>
    <doing ref="doing"
           @step-click="stepClick"
           @callbackList="({ id }) => {
            $refs['callbackList'].setId(id)
            $refs['callbackList'].show()
          }"
           @callback="e => {
             $refs['callback'].setRow(e)
             $refs['callback'].setId(e.id)
             $refs['callback'].setType('add')
             $refs['callback'].show()
           }"></doing>
    <done ref="done" @step-click="stepClick"></done>
    <detailTransfer ref="detailTransfer"
                    @start="start"
                    @pause="pause"
                    @refresh="getTransfer(true),$refs['title'].getTomorrow()"></detailTransfer>
    <callback ref="callback" @refresh="getTransfer(true)"></callback>
    <callbackList ref="callbackList" @refresh="getTransfer(true)"></callbackList>
  </div>
</template>

<script>
import { authMixin } from "@/mixin/authMixin";
import callback from "@/views/bigScreen1/component/callback.vue";
import topBackground from "@/views/bigScreen1/component/topBackground.vue";
import Title from "@/views/bigScreen1/component/title.vue";
import toDo from "@/views/bigScreen1/component/toDo.vue";
import doing from "@/views/bigScreen1/component/doing.vue";
import done from "@/views/bigScreen1/component/done.vue";
import detailTransfer from "@/views/bigScreen1/component/detailTransfer.vue";
import callbackList from "@/views/bigScreen1/component/callbackList.vue";
import { index } from "@/api/system/baseForm";
import { refreshTransferTime } from "@/settings";
export default {
  mixins: [authMixin],
  components: {
    callback,
    topBackground,
    Title,
    toDo,
    doing,
    done,
    detailTransfer,
    callbackList
  },
  provide() {
    return {
      transfers: (status="toDo") => {
        switch (status) {
          case "toDo":
            return this.transfers1;
          case "doing":
            return this.transfers2;
          case "done":
            return this.transfers3;
          default:
            return [];
        }
      },
      nowTime: () => this.time,
      auths: () => this.auths_auth_mixin
    }
  },
  data() {
    return {
      refreshTime: refreshTransferTime / 10000,
      forwardRefreshTime: '',
      transfers1: [],
      transfers2: [],
      transfers3: [],

      time: this.$moment(),
      timer: null,
      transferTimer: null,
      feedbackTimer: null,
    }
  },
  methods: {
    start () {
      this.$refs['toDo'].isPaused = false;
      this.$refs['doing'].isPaused = false;
    },
    pause () {
      this.$refs['toDo'].isPaused = true;
      this.$refs['doing'].isPaused = true;
    },
    stepClick ({ data, isDetail }) {
      this.$refs['detailTransfer'].setData(data);
      this.$refs['detailTransfer'].readonly = isDetail;
      this.$refs['detailTransfer'].show();
    },

    startTime () {
      this.timer = setInterval(() => {
        this.time = this.$moment()
        this.refreshTime--
      },1000)
    },

    setRem () {
      const baseSize = 15;
      const scale = document.documentElement.clientWidth / 1920;
      document.documentElement.style.fontSize = baseSize * Math.min(scale, 2) + "px";

      window.requestAnimationFrame(this.setRem)
    },

    async getFeedback () {
      const res = (
        (await index({
            table_name: "feedbacks",
            page: 1,
            page_size: 9999,
            filter: [
              {
                key: "created_at",
                op: "range",
                value: `${this.forwardRefreshTime.format('YYYY-MM-DD HH:mm:ss')},${this.$moment().format('YYYY-MM-DD HH:mm:ss')}`
              },
              {
                key: "status",
                op: "eq",
                value: "1"
              }
            ]
          },false)
        ).data);
      res.forEach(i => {
        this.$notify({
          title: i.equipment?.name || '调令反馈',
          message: `${ i.feedback_department_id_departments_id_relation?.name ? i.feedback_department_id_departments_id_relation?.name + '-' : '' }${ i.feedback_admin_id_admins_id_relation?.name ? i.feedback_admin_id_admins_id_relation?.name + '-' : '' }${ i.content }`,
          type: 'warning'
        });
      })
    },

    async getTransfer(isFirst = false) {
      this.refreshTime = refreshTransferTime / 1000;
      if (isFirst) {
        this.forwardRefreshTime = this.$moment();
        let transfers1 = [];
        let transfers2 = [];
        let transfers3 = [];
        const res = (
          await index({
            table_name: "transfers",
            page: 1,
            page_size: 9999,
            sort_name: "start_time",
            sort_type: "asc",
            filter: [
              {
                key: 'start_time',
                op: 'range',
                value: `${this.$moment().startOf('days').format('YYYY-MM-DD HH:mm:ss')},${this.$moment().endOf('days').format('YYYY-MM-DD HH:mm:ss')}`
              },
              {
                key: 'status',
                op: 'neq',
                value: 1
              }
            ]
          },false)
        ).data;
        res.forEach(i => {
          if (i.status === 2 || i.status === 3 || i.status === 4) {
            transfers1.push(i)
          }
          if (i.status === 5 || i.status === 6) {
            transfers2.push(i)
          }
          if (i.status === 7) {
            transfers3.push(i)
          }
        })
        this.transfers1 = transfers1;
        this.transfers2 = transfers2;
        this.transfers3 = transfers3;
      } else {
        //新的调令
        const res = (
          (await index({
              table_name: "transfers",
              page: 1,
              page_size: 9999,
              filter: [
                {
                  key: "created_at",
                  op: "range",
                  value: `${this.forwardRefreshTime.format('YYYY-MM-DD HH:mm:ss')},${this.$moment().format('YYYY-MM-DD HH:mm:ss')}`
                },
                {
                  key: "status",
                  op: "eq",
                  value: "2"
                }
              ]
            },false)
          ).data);
        if (res.length > 0) {
          this.$audioPlay(2)
          this.transfers1.push(...res)
        }
        //跟新的调令
        const res1 = (await index({
          table_name: "transfers",
          page: 1,
          page_size: 9999,
          filter: [
            {
              key: "updated_at",
              op: "range",
              value: `${this.forwardRefreshTime.format('YYYY-MM-DD HH:mm:ss')},${this.$moment().format('YYYY-MM-DD HH:mm:ss')}`
            },
            {
              key: "status",
              op: "range",
              value: "2,7"
            }
          ]
        })).data
        if (res1.length > 0) {
          res1.forEach(i => {
            if (this.transfers1.findIndex(j => j.id === i.id) !== -1) {
              this.transfers1.splice(this.transfers1.findIndex(j => j.id === i.id),1,i)
            }
            else if (this.transfers2.findIndex(j => j.id === i.id) !== -1) {
              this.transfers2.splice(this.transfers2.findIndex(j => j.id === i.id),1,i)
            }
            else if (this.transfers3.findIndex(j => j.id === i.id) !== -1) {
              this.transfers3.splice(this.transfers3.findIndex(j => j.id === i.id),1,i)
            }
            else {}
          })
        }
        this.forwardRefreshTime = this.$moment()
      }
    }
  },
  computed: {},
  created() {
    this.setRem();
    window.onresize = this.setRem;
    this.getTransfer(true);
    this.startTime();
    this.transferTimer = setInterval(() => {
      this.getTransfer()
      this.getFeedback()
    },refreshTransferTime)
  },
  mounted() {
  },
  destroyed() {
    window.onscroll = null;
    document.documentElement.style.fontSize = "15px";
    clearInterval(this.timer);
    clearInterval(this.transferTimer);
  }
}
</script>

<style scoped lang="scss">
#big-screen {
  overflow: hidden;
  width: 100vw;
  height: 100vh;

  z-index: 2;
  position: relative;

  .refresh-time {
    font-size: 12px;
    color: #fff;

    z-index: 3;
    position: fixed;
    top: 2px;
    left: 2px;
  }
}
</style>
<style lang="scss">
#app:has(#big-screen) {
  .app-main {
    padding: 0;
  }
  .app-wrapper {
    overflow: hidden!important;
  }
  .main-container {
    margin: 0;
  }
  .sidebar-container,.fixed-header {
    display: none;
  }
}
</style>
