<template>
  <!--版本升级-->
  <div id="topDiv">
    <van-nav-bar
        :title="$route.meta.name"
        left-text="返回"
        left-arrow
        @click-left="$router.go(-1)"
    >
    </van-nav-bar>
  </div>
  <div class="Upgrading">
    <img src="/public/assets/logo10.png"/>
    <p>PDA {{ nowVersion }}</p>
    <span>{{this.updatetxt}}</span>
  </div>
  <van-button class="versionButton" @click="versionButton">检测更新</van-button>

  <van-popup class="loginBox" v-model:show="dispy" style="width: 300px;border-radius: 5px;">
    <div>
      <img src="/public/assets/logo10.png"/>
      <h3>版本更新</h3>
    </div>
    <van-cell-group inset>
      <van-cell>本次更新版本号:V{{newVersion}}</van-cell>
      <!--      <van-cell v-for="item in newVersionInfo" :key="item">- {{item}}</van-cell>-->
      <div class="btnBox">
        <van-button @click="cancelButton">稍后再说</van-button>
        <van-button @click="UpdateNow">立即更新</van-button>
      </div>
    </van-cell-group>
  </van-popup>
</template>

<script>
import { Cell, CellGroup, NavBar, Popup, Button, Icon, Notify, Dialog } from "vant";
import {
  getSubSysInfo,
  qrSubSysPatchByTime,
} from "@/api/system/registerConfig";
import { mapState } from "vuex";
import config from "../../../../package.json";
import NativeQRCode from "@/common/nativeJSdk/native";
import request from "@/utils/request";
import {getToken} from "@/utils/auth";

export default {
  name: "versionUpgrade",
  components: {
    [NavBar.name]: NavBar,
    [Popup.name]: Popup,
    [CellGroup.name]: CellGroup,
    [Button.name]: Button,
    [Icon.name]: Icon,
    [Cell.name]: Cell,
  },
  data() {
    return {

      nowVersion: null,
      newVersion: null,
      newVersions:null,
      nowVersions:null,
      updatetxt:null,
      // newVersion: "V2.1",
      dispy: false,
      type: "",
      newVersionInfo: [],
      nowVersionInfo: ["abc", "abc2", "abc2", "abc2", "abc2"],
    };
  },
  computed: {
    ...mapState("sysConf", ["authCode"]),
  },
  mounted() {
    this.getSubSysInfo()
    // this.versionButton();
  },
  methods: {
    // 版本升级
    cancelButton() {
      this.dispy = false;
    },
    // 版本对比
    compareVersion(systemVersion, minVersion) {
      const systemParts = systemVersion.split('.').map(Number);
      const minParts = minVersion.split('.').map(Number);

      for (let i = 0; i < Math.max(systemParts.length, minParts.length); i++) {
        const sysNum = systemParts[i] || 0;
        const minNum = minParts[i] || 0;

        if (sysNum > minNum) {
          return true;
        } else if (sysNum < minNum) {
          return false;
        }
      }

      // 版本号前几位相同，比较长度
      return systemParts.length > minParts.length;
    },
    // 版本升级
    versionButton() {
      // if (this.nowVersion == this.newVersion) {
      // } else this.dispy = true;

      // Notify({ type: 'primary', message: '已经是最新版本' });
      this.nowVersion = config.version
      console.log(this.nowVersion)
      let nowVersion = this.nowVersion
      let newVersion = this.newVersion
      if (this.compareVersion(newVersion,nowVersion)){
        this.dispy = true
        this.updatetxt = '发现新版本'
      }else {
        this.dispy = false
        this.updatetxt = '当前版本最新'
      }


      // let res = this.$nativeQRCode.getNewVersion('http://192.168.31.146/pda/version.json');
      // if (res) {
      //   res = JSON.parse(res);
      //   //赋值并对比最新版本与当前版本
      //   this.newVersion = res.version
      //   this.nowVersion = config.version
      //
      //   this.newVersions = this.newVersion
      //   this.nowVersions = this.nowVersion
      //   this.newVersions = this.newVersions.replace(".","")
      //   this.newVersions = this.newVersions.replace(".","")
      //   this.nowVersions = this.nowVersions.replace(".","")
      //   this.nowVersions = this.nowVersions.replace(".","")
      //   if (this.newVersions > this.nowVersions) {
      //     this.dispy = true
      //     this.updatetxt = '发现新版本'
      //   }else if (this.newVersions == this.nowVersions) {
      //     this.dispy = false
      //     this.updatetxt = '当前版本最新'
      //   }else if (this.newVersions < this.nowVersions){
      //     this.dispy = false
      //     this.updatetxt = '版本号错误'
      //   }
      //
      //   this.newVersionInfo = res.desc.split("|")

      // Dialog.confirm({
      //   title: res.version,
      //   message: this.newVersionInfo,
      //   confirmButtonText: '升级'
      // })
      //     .then(() => {
      //       this.$nativeQRCode.downloadNewVersion("http://192.168.31.146/pda/qrcode1.05.003.apk");
      //     })
      //     .catch(() => {
      //       // on cancel
      //     });
      // console.log(res)
      // this.$nativeQRCode.downloadNewVersion("http://192.168.31.146/pda/qrcode1.05.003.apk");
      // }


    },
    // 立即更新
    UpdateNow(){
      // this.$nativeQRCode.downloadNewVersion("http://192.168.31.146/pda/app-release" + this.newVersion + ".apk");
      let token = getToken();
      NativeQRCode.downloadNewVersion(request.defaults.baseURL+"/file/pda/download?fileName=app-release.apk",token)
    },
    // 获取子系统信息
    getSubSysInfo() {
      getSubSysInfo(this.authCode).then((res) => {
        //this.nowVersion = res.data.version

        this.type = res.data.typeCode;
        this.getSubSysNewVersion();
      });
    },
    // 获取子系统最新版本信息
    getSubSysNewVersion() {
      qrSubSysPatchByTime(this.type).then((res) => {
        if (res.code == 200) {
          //版本
          this.newVersion = res.data.patchNum;
          // 地址
          this.patchUrl = res.data.patchUrl;
        }
      });
    },
    // 下载
    downLoad() {
      this.$nativeQRCode.downloadNewVersion();
    }
  },


};
</script>

<style  scoped>
.versionButton {
    width: 400px;
    position: absolute;
    top: 0;
    bottom: -800px;
    left: 0;
    right: 0;
    margin: auto;
    color: rgba(255, 255, 255, 1);
    height: 100px;
    border-radius: 60px;
    background: linear-gradient(-90deg,rgba(0, 105, 83, 1), rgba(33,143,120,0.6));
}
.loginBox>div{
    display: flex;
    flex-wrap: wrap;
    padding-top: 5%;
    margin: 0;
}
.loginBox img{
    width: 30%;
    margin: auto;
}
.loginBox h3{
    text-align: center;
    width: 100%;
    margin-top: 3%;
    margin-bottom: 5%;
    font-size: 35px;
}
.loginBox .van-cell{
  padding: 0;
}
:deep(.van-cell__value){
    font-size: 30px;
    height: 50px;
    line-height: 50px;
    padding-left: 20px;
    color: rgba(67, 67, 67, 1);
}
.van-cell:after {
  border-bottom: none !important;
  border-bottom-style: none !important;
}
.btnBox{
    width: 100%;
    height: 90px;
    margin-top: 10px;
}
.btnBox button{
    width: 50%;
    height: 100%;
    color: rgba(24, 164, 135, 1);
}
.Upgrading{
    display: flex;
    flex-wrap: wrap;
    text-align: center;
}
.Upgrading img{
  margin: 150px auto 60px auto;
}
.Upgrading p{
  width: 100%;
  font-weight: 600;
}
.Upgrading span{
    display: block;
    margin-top: 30px;
    width: 100%;
    font-size: 30px;
    font-weight: 500;
}
</style>
