<template>
  <div class="header">
    <div class="left">
      <img class="first" src="../assets/three.png" alt="" />
      <div class="shu"></div>
      <img class="logo" src="../assets/white-logo.png" alt="" />
      <div
        class="ml20"
        style="color: #fff; font-size: 18px; height: 60px; padding-top: 50px"
      >
        version：{{ timelineData[0]?.version }}
      </div>
    </div>
    <div class="right">
      <div
        @click="dialogVisible = true"
        class="pointer"
        style="padding-top: 6px"
      >
        <el-badge
          :hidden="isNewMessage.length == 0"
          :value="isNewMessage.length"
          class="mr30"
        >
          <el-icon :size="size" color="#00a9dc">
            <BellFilled />
          </el-icon>
        </el-badge>
      </div>
      <img class="user-icon" src="../assets/user.png" alt="" />
      <div class="user">{{ userName }}</div>
      <el-popover placement="bottom" trigger="hover" :width="240">
        <div style="width: 100%; height: 80vh; overflow-y: scroll">
          <el-timeline>
            <el-timeline-item
              v-for="item in timelineData"
              center
              :timestamp="item.timer"
              placement="top"
            >
              <div class="log-card">
                <p style="margin-bottom: 10px">Version: {{ item.version }}</p>
                <div
                  style="margin-bottom: 5px; font-size: 12px"
                  v-for="(item1, index) in item.remarks"
                >
                  {{ index + 1 }}. {{ item1.text }}
                </div>
              </div>
            </el-timeline-item>
          </el-timeline>
        </div>

        <template #reference>
          <el-badge value="new">
            <div class="log">新功能日志</div>
          </el-badge>
        </template>
      </el-popover>
      <div class="quit" @click="quit">[退出]</div>
    </div>
    <el-dialog v-model="dialogVisible" title="拍照通知" width="30%">
      <el-table :data="tableData[currentTableIndex - 1]" border>
        <el-table-column align="center" type="index" />
        <el-table-column align="center" property="shipName" label="船名" />
        <el-table-column align="center" property="status" label="状态" />
      </el-table>
      <el-pagination
        style="text-align: right; margin-top: 20px"
        @current-change="pageChange"
        background
        layout="prev, pager, next"
        :total="total"
      >
      </el-pagination>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="dialogVisible = false">
            确定
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script>
import store from "../store";
import router from "../router";
import api from "../apis/fetch";
import { onMounted, ref } from "vue";
import { BellFilled } from "@element-plus/icons";
import _ from "lodash";
import { AnonymousLogin, tcb } from "../apis/cloudLogin";
const db = tcb.database();
const v = db.collection("huihenduo_versions");
const __ = db.command;

export default {
  components: {
    BellFilled,
  },
  setup() {
    let userName = localStorage.staffName;
    function quit() {
      localStorage.removeItem("staffPhone");
      localStorage.removeItem("id");
      localStorage.removeItem("status");
      localStorage.removeItem("userType");
      localStorage.removeItem("staffName");
      store.commit("changeLogin", false);
      router.push({ path: "/login" });
    }
    let dialogVisible = ref(false);
    let isNewMessage = ref([]);
    function isWithinTime(t0 = new Date()) {
      let t1 = _.cloneDeep(t0);
      let t2 = _.cloneDeep(t0);
      t1.setHours(8);
      t1.setMinutes(30);
      t1.setSeconds(0);
      t2.setHours(9);
      t2.setMinutes(30);
      t2.setSeconds(0);
      let t00 = t0.getTime();
      let t11 = t1.getTime();
      let t22 = t2.getTime();
      return t00 > t11 && t00 < t22;
    }
    function spArr(arr, num) {
      //arr是你要分割的数组，num是以几个为一组
      let newArr = []; //首先创建一个新的空数组。用来存放分割好的数组
      for (let i = 0; i < arr.length; ) {
        //注意：这里与for循环不太一样的是，没有i++
        newArr.push(arr.slice(i, (i += num)));
      }
      return newArr;
    }
    let tableData = ref([]);
    let currentTableIndex = ref(1);
    let total = ref(0);
    function pageChange(c) {
      currentTableIndex.value = c;
    }
    async function getUnphotographNotice() {
      let { data } = await api.getUnphotographNotice();
      if (data.status == 0) {
        for (let i of data.result) {
          isNewMessage.value.push({
            shipName: i,
            status: "未拍照",
          });
        }
        total.value = isNewMessage.value.length;
        tableData.value = spArr(isNewMessage.value, 10);
      } else {
        isNewMessage.value = 0;
      }
    }
    onMounted(() => {
      getUnphotographNotice();
      setInterval(async () => {
        if (isWithinTime()) {
          getUnphotographNotice();
        }
      }, 2 * 60 * 1000);
      cloudLogin();
    });

    let vs = [
      {
        version: "1.2.13.0",
        timer: "	2022/2/9",
        remarks: [
          "优化船舶轨迹测算算法",
          "优化船舶预计到港时间测算算法",
          "优化航次状态预测算法",
        ],
      },
      {
        version: "1.2.14.0",
        timer: "	2022/2/11",
        remarks: ["更新保险保单上传接口", "更新装卸单据展示方式"],
      },
      {
        version: "1.3.0.0	",
        timer: "2022/2/11",
        remarks: [
          "发布浙江物产货主版管理平台",
          "发布货损数据采集接口",
          "发布货损智能分析报告",
        ],
      },
      {
        version: "1.3.4.0	",
        timer: "2022/2/28",
        remarks: [
          "发布汽车装货记录功能",
          "发布单据识别算法功能",
          "单据车牌数据、吨位数据拆分功能、生成表报",
          "智能化数据自动匹配、自动修正算法",
        ],
      },
    ];
    let timelineData = ref([]);
    // timelineData.value = vs.reverse();
    async function cloudLogin() {
      await AnonymousLogin();
      getAbledVersions();
    }
    async function getAbledVersions() {
      let res = await v.where({ disabled: false, deleted: __.neq(true) }).get();
      timelineData.value = res.data.reverse();
    }

    const size = 20;
    return {
      size,
      quit,
      userName,
      isNewMessage,
      tableData,
      dialogVisible,
      currentTableIndex,
      total,
      pageChange,
      timelineData,
    };
  },
};
</script>
<style scoped>
.header {
  width: 100%;
  height: 60px;
  background: #212029;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.left,
.right {
  display: flex;
  align-items: center;
}

.first {
  width: 22px;
  height: 20px;
  margin: 0 22px;
}

.shu {
  width: 1px;
  height: 60px;
  background: #101015;
  margin-right: 22px;
}

.logo {
  width: 120px;
  height: 40px;
}

.title {
  font-size: 21px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #ffffff;
  margin-left: 26px;
}

.user-icon {
  width: 18px;
  height: 18px;
  margin-right: 16px;
}

.user,
.quit,
.log {
  font-size: 14px;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 500;
  color: #ffffff;
  cursor: pointer;
  margin-right: 16px;
}

.log {
  margin-right: 4px;
}

.quit {
  margin-left: 26px;
}

.log-card p {
  font-size: 10px;
}
</style>
