<template>
  <view class="main">
    <view class="hd-v">
      <view class="start-bar"></view>
      <view class="tabs">
        <view class="">{{ $lang.lang.index.index }}</view>
        <view class="igs">
          <view class="index_msg" @click="$request.goPage('../msg/msg')">
            <image src="../../static/img/notice.png" mode=""></image>
            <view v-if="msgHave" class="msg_dian"></view>
          </view>
          <view class="" @click="goPage('../site/site')">
            <image src="../../static/img/site.png" mode=""></image>
          </view>
        </view>
      </view>
      <view class="banner">
        <swiper class="swiper" circular indicator-dots autoplay interval="2000" duration="500">
          <swiper-item v-for="(item, index) in banner" @click="toBanner(item.id)" :key="index">
            <image :src="item.bannerUrlApp" mode=""></image>
          </swiper-item>
        </swiper>
      </view>
    </view>
    <view class="nvs">
      <!--			<view class="index_msg" v-for="(item, index) in nvs" @click="toPage(item.url)" :key="index">-->
      <view
        class="index_msg"
        v-for="(item, index) in nvs"
        @click="toPage(item.url, item.isHttp)"
        :key="index"
      >
        <image :src="item.icon" mode=""></image>
        <view v-if="item.num > 0" class="order_dian">{{ item.num }}</view>
        <text>{{ item.name }}</text>
      </view>
    </view>
    <!-- 搜索 -->
    <!--		<view class="order-tabs">
			<view class="order-tabs-v2">
				<view class="order-tabs-v2s">
					<image class="search" src="../../static/img/search.png" mode=""></image>
					<input type="text" v-model="searchKey" autocomplete="off" :placeholder="$lang.lang.notices.moreKey" placeholder-style="color: #666666">
					<view class="" @click="toMyOrder">{{$lang.lang.order.search}}</view>
				</view>
			</view>
		</view>-->
    <view class="mssgv">
      <view class="mssgv-v" @click="$request.goPage('../notice/notice')">
        <view class="mssgv-v-1">
          <text>{{ $lang.lang.index.system }}</text>
          <text>{{ $lang.lang.index.notice }}</text>
        </view>

        <view class="notice">
          <swiper
            class="swiper-nav"
            :circular="true"
            :vertical="true"
            :autoplay="true"
            :interval="4000"
            :duration="1000"
            style="height: 70px"
          >
            <swiper-item
              style="display: table"
              @touchmove.stop="stopTouchMove"
              v-for="(item, index) in noticeData"
              :key="index"
            >
              <view class="mssgv-v-2">
                <view class="">{{ $lang.locale == 'zh' ? item.titleZh : item.titleEn }}</view>
                <view class="">
                  <image src="../../static/img/time.png" mode=""></image>
                  <text>{{ getDate(item.createTime) }}</text>
                </view>
              </view>
              <image class="rgt" src="../../static/img/rgt.png" mode="widthFix"></image>
            </swiper-item>
          </swiper>
        </view>
      </view>
    </view>
    <view class="actions">
      <view class="title">
        <image src="../../static/img/dongtai.png" mode=""></image>
        <text>{{ $lang.lang.index.dynamic }}</text>
      </view>
      <view class="act-list">
        <view v-if="orderData.length == 0" class="noData">
          <image src="../../static/img/empty.png" mode="widthFix"></image>
          <text>{{ $lang.lang.notices.noData }}~</text>
        </view>
        <template v-else>
          <view
            class="item"
            v-for="(item, index) in orderData"
            :key="item.orderId"
            @click="toOrderInfo(item.orderId)"
          >
            <view class="item-til">
              <image src="../../static/img/sn.png" mode=""></image>
              <text>{{ $lang.lang.index.orderNo }}:{{ item.orderNo }}</text>
              <view class="">{{ $lang.lang.index.marks }}:{{ item.marks }}</view>
            </view>
            <view class="item-body">
              <view class="item-body-v">{{ item.departureName }}</view>
              <view class="item-body-v-2">
                <view class="">{{ getStatusName(item.status) }}</view>
                <image src="../../static/img/jiant1.png" mode="widthFix"></image>
              </view>
              <view class="item-body-v">{{ item.objectiveName }}</view>
            </view>
            <view class="item-times">
              <image src="../../static/img/time.png" mode=""></image>
              <text>{{ item.createTime }}</text>
            </view>
          </view>
        </template>
      </view>
    </view>
    <view class="bom">
      <view class="" @click="$request.goPage('../help/help')">
        <image src="../../static/img/help.png" mode=""></image>
        <text>{{ $lang.lang.index.help }}</text>
      </view>
      <view class="" @click="$request.goPage('../consult/consult')">
        <image src="../../static/img/zixun.png" mode=""></image>
        <text>{{ $lang.lang.index.consult }}</text>
      </view>
    </view>
    <uni-popup ref="adpopup" type="center" :mask-click="false">
      <view class="ad-conten">
        <image @click="closeAd" class="ad-bg" src="../../static/img/index_close.png"></image>
        <view class="ad-title">{{ $lang.locale == 'zh' ? adData.titleZh : adData.titleEn }}</view>
        <view class="ad-info">
          <rich-text
            :nodes="$lang.locale == 'zh' ? adData.htmlAppZh : adData.htmlAppEn"
          ></rich-text>
        </view>
        <view class="ad-btns">
          <view class="ad-btn" @click="closeAd">
            <view>{{ $lang.lang.index.know }}</view>
          </view>
        </view>
      </view>
    </uni-popup>
    <uni-popup ref="choiceTransport" type="center" :mask-click="false">
      <view class="ad-conten type-1">
        <image @click="closeTransport" class="ad-bg1" src="../../static/img/colse1.png"></image>

        <view class="transport-title">
          <span>{{ $lang.lang.notices.transport }}</span>
        </view>
        <image class="type-bg" src="../../static/img/type-1.png"></image>
        <view class="transport-info">
          <view
            v-for="item in transType"
            @click="toCreatOrder(item.value)"
            :class="'type' + item.value"
            >{{ $lang.locale == 'zh' ? item.label : item.labelEn }}</view
          >
        </view>
      </view>
    </uni-popup>
    <uni-popup ref="vepopup" type="center" :mask-click="false">
      <view class="ad-conten">
        <image
          v-if="!forceUpdate"
          @click="closeVe"
          class="ad-bg"
          src="../../static/img/index_close.png"
        ></image>
        <view class="ad-title">V{{ appVersion }}{{ $lang.lang.notices.newVersion }}</view>
        <view class="ad-info">
          <rich-text :nodes="veData"></rich-text>
        </view>
        <view class="ad-btns">
          <view class="ad-btn" @click="toupdate">
            <view>{{ $lang.lang.notices.update }}</view>
          </view>
          <view v-if="!forceUpdate" class="ad-btn" @click="closeVe">
            <view>{{ $lang.lang.notices.close }}</view>
          </view>
        </view>
        <view class="progress" v-if="showdownLine">
          <progress :percent="downloadNum" show-info stroke-width="3" />
        </view>
      </view>
    </uni-popup>
  </view>
</template>

<script>
import md5 from 'js-md5'

export default {
  data() {
    return {
      nvs: [
        {
          name: this.$lang.lang.index.delivery,
          icon: '../../static/img/express.png',
          url: '../create_order/create_order',
          num: 0
        },
        {
          name: this.$lang.lang.index.order,
          icon: '../../static/img/order.png',
          url: '../order/order',
          num: 0
        },
        // {name: this.$lang.lang.index.consolidation, icon: '../../static/img/order.png', url: '../consolidation/consolidation',num:0},
        // {name: this.$lang.lang.index.coupon, icon: '../../static/img/copue.png', url: '../coupon/coupon',num:0},
        {
          name: this.$lang.lang.index.old_system,
          icon: '../../static/img/copue.png',
          url: '',
          num: 0,
          isHttp: true
        }
        // {name: this.$lang.lang.index.price, icon: '../../static/img/wallet.png', url: '../price_inquiry/price_inquiry',num:0},
      ],
      banner: [],
      orderData: [],
      page: 1,
      isIdcard: false,
      statusData: [],
      name: {},
      adData: {},
      noticeData: [],
      msgHave: false,
      version: '1.0.2',
      appUrl: '',
      veData: '',
      versionCode: 0,
      appVersion: '',
      forceUpdate: 0,
      showdownLine: false,
      downloadNum: 0, //下载百分比
      totalSize: 0, //下载总量
      transType: [],
      preLoginPage: false,
      searchKey: '' //搜索关键词
    }
  },
  onLoad(options) {
    this.getIdcard()
    this.getStatusData()
    this.orderConfig()
    this.getNoticeData()
    this.getVersion()
    this.getAdData()
    this.getMemberSystemStatus()
  },
  onShow() {
    this.getOrder()
    if (this.banner.length == 0) {
      this.getBanner()
    }
    if (this.orderData.length == 0) {
      this.getOrderData()
    }
  },
  methods: {
    toupdate() {
      let that = this
      if (that.showdownLine) return
      // #ifdef APP-PLUS

      let platform = uni.getSystemInfoSync().platform.toLocaleLowerCase()
      if (platform == 'ios') {
        plus.runtime.openURL('https://apps.apple.com/us/app/e-c-logistics/id6466407990')
      } else {
        uni.showLoading({
          title: this.$lang.lang.notices.startDown
        })
        that.showdownLine = true
        var dtask = plus.downloader.createDownload(that.appUrl, {}, (d, status) => {
          uni.hideLoading()
          if (status == 200) {
            that.closeVe()
            that.showdownLine = false
            console.log('下载成功安装: ' + d.filename)
            plus.runtime.install(d.filename)
          } else {
            that.showdownLine = false
            that.closeVe()
            plus.nativeUI.alert(that.$lang.lang.notices.failDown)
          }
        })

        // uni.navigateTo({
        // 	url:'../updateView/updateView?url='+this.appUrl
        // })
        // 下载进度
        dtask.addEventListener('statechanged', function (download, status) {
          if (that.downloadNum == 100) {
            uni.hideLoading()
          }
          switch (download.state) {
            case 2:
              break
            case 3: //进度条百分比 totalSize为总量，baifen为当前下载的百分比
              if (that.totalSize == 0) {
                that.totalSize = parseInt(download.totalSize)
              }
              if (parseInt((download.downloadedSize / that.totalSize) * 100) != that.downloadNum) {
                that.downloadNum = parseInt((download.downloadedSize / that.totalSize) * 100)
              }
              break
            case 4:
              // mui.toast("下载完成");
              uni.hideLoading()
              break
          }
        })
        dtask.start()
      }
      // #endif
    },
    getMemberSystemStatus(){
      this.$request.get('/admin-api/member/score-rule/switch/get').then((res) => {
        if(res.data){
          this.nvs.push({
          name: this.$lang.lang.index.integral,
          icon: '../../static/img/integral.png',
          url: '/pages/integral/integral',
          num: 0
        })
        }
      })
    },
    getVersion() {
      this.version = uni.getStorageSync('appversion')
      this.versionCode = uni.getStorageSync('versionCode')
      this.getVeData()
    },
    old() {
      const code = md5(md5('jiedaoguoji' + this.userInfo.mobile))
      const url =
        'http://backend.groupage.cn/#/pages/login/free_login?phone=' +
        this.userInfo.mobile +
        '&code=' +
        code +
        '&area_code=+' +
        this.userInfo.areaCode
      // #ifdef APP-PLUS
      plus.runtime.openURL(url)
      // #endif
      // #ifdef H5

      window.open(url)
      // #endif
    },
    getUserInfo() {
      let that = this
      that.$request.get('/app-api/member/user/get').then((res) => {
        console.log(res.data)
        if (res.code == 0 && res.data) {
          if (res.data.isSimplePassword) {
            uni.showModal({
              title: that.$lang.lang.notices.notice,
              confirmText: that.$lang.lang.site.editPsd,
              cancelText: that.$lang.lang.index.know,
              content: that.$lang.lang.notices.easyPass,
              success: function (res) {
                if (res.confirm) {
                  uni.navigateTo({
                    url: '../editPsd/editPsd'
                  })
                } else if (!that.preLoginPage) {
                  that.openAd()
                  // #ifdef APP-PLUS
                  if (that.appVersion) {
                    that.openVe()
                  }
                  // #endif
                }
              }
            })
          } else if (!that.preLoginPage) {
            that.openAd()
            // #ifdef APP-PLUS
            if (that.appVersion) {
              that.openVe()
            }
            // #endif
          }
          that.userInfo = res.data
          // 存储信息节点
          const { id, areaCode } = res.data
          that.$store.commit('SET_ID', id)
          that.$store.commit('SET_AREACODE', areaCode)
        }
      })
    },
    getOrder() {
      this.$request.get('/app-api/member/user/angle-mark').then((res) => {
        if (res.code == 0 && res.data) {
          this.nvs[1].num = res.data.allOrderNum
          this.nvs[2].num = res.data.couponNum
          if (res.data.unreadMessageNum && res.data.unreadMessageNum > 0) {
            this.msgHave = true
          } else {
            this.msgHave = false
          }
        }
      })
    },
    getStatusName(status) {
      var statusName = ''
      let that = this
      if (that.statusData.length > 0) {
        var name = that.statusData.filter((item) => item.value == status) || []
        if (name.length > 0) {
          if (that.$lang.locale == 'zh') {
            statusName = name[0].label
          } else {
            statusName = name[0].labelEn
          }
        }
      }
      return statusName
    },
    openAd() {
      console.log(this.adData)
      if (JSON.stringify(this.adData) != '{}') {
        this.$refs.adpopup.open()
      }
    },
    closeAd() {
      this.preLoginPage = false
      this.$refs.adpopup.close()
    },
    closeTransport() {
      this.$refs.choiceTransport.close()
    },
    openTransport() {
      this.$refs.choiceTransport.open()
    },
    openVe() {
      this.$refs.vepopup.open()
    },
    closeVe() {
      this.$refs.vepopup.close()
      // this.getUserInfo()
    },
    getBanner() {
      this.$request
        .get('/app-api/ecw/banner/page', { page: 1, rows: 20, status: 0 })
        .then((res) => {
          if (res.code == 0 && res.data && res.data.list && res.data.list.length > 0) {
            this.banner = res.data.list
          }
        })
    },
    getOrderData() {
      this.$request
        .get('/app-api/my/order/dynamic/page', { page: this.page, rows: 6 })
        .then((res) => {
          if (res.code == 0 && res.data && res.data.list && res.data.list.length > 0) {
            this.orderData = res.data.list
          }
        })
    },
    getNoticeData() {
      this.$request.get('/app-api/system/notice/page', { page: 1, rows: 6 }).then((res) => {
        if (res.code == 0 && res.data && res.data.list && res.data.list.length > 0) {
          this.noticeData = res.data.list
        }
      })
    },
    getDate(time) {
      const date = new Date(time)
      let year = date.getFullYear()
      let month = date.getMonth() + 1
      let day = date.getDate()

      month = month > 9 ? month : '0' + month
      day = day > 9 ? day : '0' + day
      return `${year}-${month}-${day}`
    },
    getAdData() {
      this.$request.get('/app-api/ecw/banner-pop/page', { page: 1, rows: 6 }).then((res) => {
        if (res.code == 0 && res.data && res.data.list && res.data.list.length > 0) {
          this.adData = res.data.list[0]
          let pages = getCurrentPages() // 获取栈实例
          if (pages.length > 1) {
            let prevPage = pages[pages.length - 2]
            if (prevPage && prevPage.route == 'pages/login/login') {
              this.preLoginPage = true
            }
          }
        }
        //#ifdef APP-PLUS
        this.getUserInfo()
        //#endif
        //#ifdef H5
        this.getUserInfo()
        //#endif
      })
    },
    getVeData() {
      let that = this
      var addType = uni.getSystemInfoSync().platform == 'ios' ? 'userIos' : 'userAndroid'
      that.$request
        .get('/app-api/system/version/latestVersion', {
          appType: addType,
          versionCode: that.versionCode
        })
        .then((res) => {
          console.log(res)
          if (res.code == 0 && res.data) {
            that.veData = res.data.desp
            that.appUrl = res.data.appUrl
            that.appVersion = res.data.appVersion
            that.forceUpdate = res.data.forceUpdate
            // that.openVe()
          } else {
            // that.getUserInfo()
          }
        })
    },
    getStatusData() {
      this.$request.getConfig('order_status').then((res) => {
        if (res.code == 0 && res.data && res.data.list && res.data.list.length > 0) {
          this.statusData = res.data.list
        }
      })
    },
    getIdcard() {
      this.$request.get('/app-api/member/user/get-auth-idcard-info').then((res) => {
        if (res.code == 0 && res.data && res.data.status == 2) {
          this.isIdcard = true
        }
      })
    },
    toPage(url, isHttp) {
      if (isHttp) {
        // window.location.href=url;
        this.old()
      } else {
        if (url == '../create_order/create_order') {
          this.openTransport()
        } else {
          uni.navigateTo({
            url: url
          })
        }
      }
    },
    toCreatOrder(id) {
      this.closeTransport()
      uni.navigateTo({
        url: '../create_order/create_order?transportId=' + id
      })
    },
    toBanner(id) {
      uni.navigateTo({
        url: '../banner_info/banner_info?id=' + id
      })
    },
    toOrderInfo(id) {
      uni.navigateTo({
        url: '../orderInfo/orderInfo?id=' + id
      })
    },
    orderConfig() {
      let that = this
      that.$request.getConfig('transport_type').then((res) => {
        if (res.code == 0 && res.data && res.data.list.length > 0) {
          that.transType = res.data.list
        }
      })
    },
    goPage(url) {
      uni.redirectTo({
        url: url
      })
    },
    toMyOrder() {
      uni.navigateTo({
        url: '../order/order?key=' + this.searchKey
      })
    }
  }
}
</script>
<style>
@import url(../../static/css/index.css);
</style>
