<template>
  <div id="big-screen" ref="appRef">
    <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,
      newTransferLength: () => this.transferLength,
      auths: () => this.auths_auth_mixin,
      refreshTime: () => this.refreshTime,
      refreshList: () => this.getTransfer(true)
    }
  },
  data() {
    return {
      transferLength: 0,
      refreshTime: refreshTransferTime / 10000,
      forwardRefreshTime: '',
      forwardRefreshTime1: this.$moment(),
      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 startTime = this.forwardRefreshTime1.format('YYYY-MM-DD HH:mm:ss');
      const endTime = this.$moment().format('YYYY-MM-DD HH:mm:ss');
      const res = (
        (await index({
            table_name: "feedbacks",
            page: 1,
            page_size: 9999,
            filter: [
              {
                key: "created_at",
                op: "range",
                value: `${startTime},${endTime}`
              },
              {
                key: "status",
                op: "eq",
                value: "1"
              }
            ]
          },false)
        ).data);
      this.forwardRefreshTime1 = this.$moment()
      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: "created_at",
            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) {
            if (i.is_adjust && i.to_transfer_id) {
              const pTransfer = transfers1.find(j => j.id === i.to_transfer_id)
              if (pTransfer && (pTransfer.status === i.status)) {
                transfers1.splice(transfers1.indexOf(pTransfer),1,i)
              } else {
                transfers1.unshift(i)
              }
            } else {
              transfers1.unshift(i)
            }
          }
          if (i.status === 5 || i.status === 6) {
            if (i.is_adjust && i.to_transfer_id) {
              const pTransfer = transfers2.find(j => j.id === i.to_transfer_id)
              if (pTransfer && (pTransfer.status === i.status)) {
                transfers2.splice(transfers2.indexOf(pTransfer),1,i)
              } else {
                transfers2.unshift(i)
              }
            } else {
              if (!transfers2.find(j => j.to_transfer_id === i.id)) {
                transfers2.unshift(i)
              }
            }
          }
          if (i.status === 7) {
            transfers3.unshift(i)
          }
        })
        this.transfers1 = transfers1;
        this.transfers2 = transfers2;
        this.transfers3 = transfers3;
      } else {
        const startTime = this.forwardRefreshTime.format('YYYY-MM-DD HH:mm:ss');
        const endTime = this.$moment().format('YYYY-MM-DD HH:mm:ss');
        this.forwardRefreshTime = this.$moment()
        //新的调令
        const res = (
          (await index({
              table_name: "transfers",
              page: 1,
              page_size: 9999,
              filter: [
                {
                  key: "created_at",
                  op: "range",
                  value: `${startTime},${endTime}`
                },
                {
                  key: "status",
                  op: "eq",
                  value: "2"
                }
              ]
            },false)
          ).data);
        this.transferLength = res.length;
        if (res.length > 0) {
          this.$audioPlay(2)
          this.transfers1.unshift(...res)
        }
        //跟新的调令
        const res1 = (await index({
          table_name: "transfers",
          page: 1,
          page_size: 9999,
          filter: [
            {
              key: "updated_at",
              op: "range",
              value: `${startTime},${endTime}`
            },
            {
              key: "status",
              op: "range",
              value: "2,7"
            }
          ]
        })).data
        if (res1.length > 0) {
          let msFlag = false
          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 {
              if (i.status === 2) {
                this.transfers1.unshift(i)
                this.transferLength++
                msFlag = true
              }
            }
          })
          if (msFlag) {
            this.$audioPlay(2)
          }
        }
      }

      this.$forceUpdate()
    }
  },
  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: 1.4rem;
    color: #f00;

    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>
