<template>
  <view class="">
    <uni-nav-bar
      fixed="true"
      statusBar="true"
      left-icon="back"
      @clickLeft="back"
      title="系统设置"
    >
    </uni-nav-bar>
    <u-cell-group>
      <picker @change="handlePicker" :range="['是', '否']">
        <u-cell title="记住密码" :isLink="true" arrow-direction="right">
          <text slot="value" class="u-slot-value">{{
            isMemo ? '是' : '否'
          }}</text>
        </u-cell>
      </picker>
      <u-cell
        title="服务器地址"
        :isLink="true"
        arrow-direction="right"
        @click="handleServerSettings"
      >
        <text slot="value" class="u-slot-value" :key="apiAdress">{{
          apiAdress
        }}</text>
      </u-cell>
      <u-cell
        :title="'当前版本 ' + currentVersion"
        :isLink="true"
        arrow-direction="right"
      >
        <text slot="value" class="u-slot-value" @click="handlUpdate">{{
          Nversion == currentVersion ? '' : `发现新版本(${this.Nversion})`
        }}</text>
      </u-cell>
    </u-cell-group>
    <view class="flex-buttom">
      <view class="btn" @click="loginOut"> 退出登录 </view>
      <view class="copyright">
        <view>版权所有© 2019-2021中赢合力</view>
        <view>湘ICP备20015098号</view>
      </view>
    </view>
    <updatePop
      ref="updatePop"
      :versionNum="versionNum"
      :downloadUrl="downloadUrl"
      :sizes="VersionInfo.fileSize"
    ></updatePop>
    <ServerSetting ref="serverSettingRef" @setServerStatus="setServerStatus" />
  </view>
</template>

<script>
import { postJ, get } from '@/utils/api.js'
import updatePop from './components/updatePop.vue'
import { setMemo, isMemo, removeMemo } from '@/utils/passwordMemo.js'
import ServerSetting from '@/components/ServerSetting/index'
export default {
  components: {
    updatePop,
    ServerSetting
  },
  data () {
    return {
      show: false,
      versionNum: '',
      downloadUrl: '',
      currentVersion: '',
      isMemo: isMemo(),
      apiAdress: '',
      Nversion: '',
      VersionInfo: ''
    }
  },
  created () {
    const apiInfo = uni.getStorageSync('apiInfo')

    this.apiAdress =
      apiInfo.hostname &&
      `${apiInfo.protocal || ''}${apiInfo.hostname || ''}:${apiInfo.port || ''}`

    this.currentVersion = plus?.runtime.version
    this.getbb()
  },
  methods: {
    handlePicker (e) {
      this.isMemo = e.detail.value === 0
      if (e.detail.value === 0) {
        setMemo()
      } else {
        removeMemo()
      }
    },
    setServerStatus (val) {
      if (val) {
        const apiInfo = uni.getStorageSync('apiInfo')

        this.apiAdress = `${apiInfo.protocal || ''}${apiInfo.hostname || ''}:${
          apiInfo.port || ''
        }`
      }
    },
    handleServerSettings () {
      this.$refs.serverSettingRef.open()
    },
    loginOut () {

      uni.showModal({
        title: '提示',
        content: '确认退出登录',
        success: res => {
          console.log(res,res.confirm,'-----确认退出登录');
          if (res.confirm) {
          console.log('-----确认退出登录9999999');
          try {
            postJ(this.apiUrl + '/user/logout')
              .then(res => {
                uni.removeStorageSync('token')
                uni.removeStorageSync('userInfo')
				uni.removeStorageSync('treeList')
				
                uni.reLaunch({
                  url: '/pages/login/login'
                })
              })
              .catch(err => {
                uni.showToast({
                  title: '退出失败',
                  icon: 'none'
                })
              })
            
          } catch (error) {
            console.log(error,'error----')
            uni.reLaunch({
                  url: '/pages/login/login'
                })
          }
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    },
    handlUpdate () {
      get(this.apiUrl + '/versioning/getLatestData').then(res => {
        if (res.success) {
          const currentVersion = this.currentVersion
          const Nversion = res.data.version
          this.VersionInfo = res.data
          if (currentVersion !== Nversion) {
            this.$refs.updatePop.open()
            this.versionNum = Nversion
            this.downloadUrl = this.apiUrl + res.data.path
          } else {
            uni.showToast({
              title: '暂无新版本',
              icon: 'none'
            })
          }
        }
      })
    },
    getbb () {
      get(this.apiUrl + '/versioning/getLatestData').then(res => {
        if (res.success) {
          this.Nversion = res.data.version
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.u-slot-value {
  color: #157a2c;
}
.flex-buttom {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  padding: 20rpx;
  box-sizing: border-box;
  .btn {
    color: #157a2c;
    font-size: 28rpx;
    display: flex;
    justify-content: center;
    align-items: center;
    border: 1px solid #157a2c;
    border-radius: 10rpx;
    height: 80rpx;
  }
}
.copyright {
  color: #555;
  font-size: 28rpx;
  text-align: center;
  padding: 20rpx 0;
}
</style>
