<template>
  <div class="page">
    <div class="header-content">
      <u-navbar :is-back="false" title="基层治理" title-color="#000" title-width="300" title-size="32" :title-bold="true" :background="backgroundNavbar"></u-navbar>
      <div class="header-bg">
        <!-- <div class="weather-info" v-if="weatherInfo.wea">
          <img :src="weatherInfo.icon" alt="" />
          <p>今天：{{weatherInfo.wea}}{{weatherInfo.tem2}}~{{weatherInfo.tem1}},{{weatherInfo.air_tips}}</p>
        </div> -->
        <div class="swiper-content">
          <u-swiper :list="swiperList" mode="none" height="364" bg-color="none" @click="handleBannerClick"/>
        </div>
        <div class="notice">
          <img :src="`${cdn}/notice-new.png`" alt="">
          <u-notice-bar mode="vertical" color="#4181FF" style="flex: 1;" :volume-icon="false" :is-circular="false"
            duration="5000" speed="5000" :list="noticeList" @click="clickNotice"/>
        </div>
      </div>
    </div>
    <div class="app-list" v-for="(item, index) in appList" :key="index">
      <div class="title-content">
        <h2>{{item.name}}</h2>
        <div @click="toService()">更多<img src="https://cdn.cunwuyun.cn/wechat/wuxi/right-icon-blue.png" alt=""></div>
      </div>
      <div class="banner-flex" v-if="item.bannerApp && item.bannerApp.length">
        <div class="item-banner" v-for="(banner, bannerIndex) in item.bannerApp" :key="bannerIndex" @click="openApp(banner)">
          <img :src="banner.pictureUrl" alt="">
        </div>
      </div>
      <div class="app-content" v-if="item.apps && item.apps.length">
        <div class="item-app" v-for="(app, appIndex) in item.apps" :key="appIndex" @click="openApp(app)">
          <img :src="app.pictureUrl">
          <p>{{app.name}}</p>
        </div>
      </div>
    </div>
    <template v-if="categorys&&categorys.length>0">
      <u-tabs :list="categorys.map(e=>({name:e.categoryName}))" font-size="40" bg-color="transparent"
              inactive-color="#999999" :active-item-style="active"
              :is-scroll="true" :current="index" @change="tabChange"/>
      <div class="list-wrap" v-if="categoryList && categoryList.length>0">
        <div class="list-card" v-for="(category,index) in categoryList" :key="index"
             @click="$linkTo('/mods/AppContent/contentDetail?id='+category.id)">
          <div class="header">{{ category.title }}</div>
          <div class="content-wrap"
               v-if="category.contentType==0 && category.files && category.files.length<3&&category.files.length>0">
            <img class="img" :src="item.url" v-for="(item,index) in category.files.slice(0,1)" :key="index.id">
          </div>
          <div class="content-wrap" v-if="category.contentType==0 && category.files && category.files.length >= 3">
            <img class="min-img" :src="item.url" v-for="(item,index) in  category.files.slice(0,3)" :key="index.id">
          </div>
          <div class="content-wrap" v-if="category.contentType == 1">
            <img class="img" :src="category.pictureUrl" alt=""/>
            <img class="play-icon" :src="`${cdn}/play.png`" alt=""/>
          </div>
          <div class="bottom">
            <div class="left">
              <div class="tag">{{ category.categoryName }}</div>
              {{ category.createTime }}
            </div>
            <div class="right">
              <em>{{ category.viewCount }}</em>
              人看过
            </div>
          </div>
        </div>
      </div>
      <AiEmpty v-else/>
    </template>
    <AiLogin ref="login"/>
  </div>
</template>
<script>
import {mapActions, mapState} from 'vuex'

export default {
  customNavigation: true,
  name: 'AppHome',
  appName: "首页",
  data() {
    return {
      cdn: "https://cdn.cunwuyun.cn/wxmp",
      swiperList: ['https://cdn.cunwuyun.cn/wechat/biaopin/home/home-swiper1.png'],
      index: 0,
      grids: [],
      activeList: [],
      notices: [],
      categorys: [],
      categoryList: [],
      search: {areaId: ''},
      backgroundNavbar: {
        background: 'url(https://cdn.cunwuyun.cn/wechat/wuxi/wuxi-nav-bg.png) no-repeat',
        backgroundSize: 'cover',
      },
      appList: [], //应用列表
      weatherInfo: {}
    }
  },
  computed: {
    ...mapState(['user', 'token']),
    active() {
      return {
        fontSize: '22px',
        color: '#333333',
      }
    },
    noticeList() {
      let {notices} = this
      return notices?.length > 0 ? notices?.map(e => e.title) || ['暂无公告'] : ['暂无公告']
    }
  },
  onReady() {
    // uni.setNavigationBarTitle({title: "基层治理"})
  },
  onLoad() {
    this.autoLogin().then(() => {
      this.getSwiperList();
      this.getName();
      // this.getGrids();
      this.getActive();
      this.getNotice();
      this.getAppList()
      this.getWeather()
    })
  },
  methods: {
    ...mapActions(['autoLogin', 'authCheck']),
    getName() {
      this.$instance.post("/app/appcontentmoduleinfo/listByName", null, {
        params: {names: "新闻发布"},
        withoutToken: true
      }).then(res => {
        if (res?.data[0]?.categoryList?.length) {
          this.categorys = res.data[0]["categoryList"];
          this.search.moduleId = res.data[0]['id']
          this.search.categoryId = res.data[0]['categoryList'][0]['id']
          this.getCategoryList()
        }
      })
    },
    tabChange(idx) {
      this.index = idx
      this.search.categoryId = this.categorys[idx]['id']
      this.getCategoryList()
    },
    getCategoryList() {
      this.$instance.post("/app/appcontentinfo/list", null, {
        params: {...this.search, size: 100}
      }).then(res => {
        if (res?.data) {
          this.categoryList = res.data.records;
        }
      })
    },
    clickNotice(val) {
      const id = this.notices[val]["id"];
      if (id) {
        uni.navigateTo({
          url: "/mods/AppNotice/AppNotice?id=" + id
        })
      }
    },
    handleActive({type, appId, url}) {
      if (type == 0) {
        uni.navigateToMiniProgram({appId})
      } else if (type == 1) {
        this.$linkTo("/subPages/h5/webview?link=" + url);
      }
    },
    getNotice() {
      this.$instance.post("/app/appmininotice/list", null, {
        params: {size: 999},
        withoutToken: true
      }).then(res => {
        if (res?.data) {
          this.notices = res.data.records;
        }
      })
    },
    getActive() {
      this.$instance.post("/app/appminitopicconfig/list", null, {
        params: {size: 999},
        withoutToken: true
      }).then(res => {
        if (res?.data) {
          this.activeList = res.data.records;
        }
      })
    },
    /**
     * 获取顶部九宫格
     */
    getGrids() {
      this.$instance.post("/app/appminihomeconfig/list", null, {
        params: {picked: 1},
        withoutToken: true
      }).then(res => {
        if (res?.data) {
          this.grids = res.data.records;
        }
      })
    },
    getSwiperList() {
      this.$instance.post('/app/appbanner/listForWx', null, {
        withoutToken: true
      }).then((res) => {
        if (res?.data) {
          this.swiperList = res.data?.map((e) => ({...e, image: e.imgUrl})) || []
        }
      })
    },
    handleClick({type, appId, modulePath, url, checkType, corpId}) {
      //先判读是不是系统应用
      if (type != "0") {
        if (type == "1") {
          uni.navigateToMiniProgram({appId});
        } else if (type == "2") {
          uni.navigateTo({url: "/subPages/h5/webview?link=" + url});
        } else if (type == "3") {
          this.$linkTo(url);
        } else if (type == "4") {
          uni.openCustomerServiceChat({
            extInfo: {url: url},
            corpId: corpId,
            fail: () => {
              this.$u.toast('请使用普通微信打开小程序进行咨询');
            }
          });
        }
      } else if (type && type == "0") {
        uni.showLoading({title: '正在进入应用...'})
        this.authCheck({checkType, modulePath}).finally(() => uni.hideLoading())
      }
    },
    handleBannerClick(index) {
      if (!this.swiperList[index].linkUrl) return

      if (this.swiperList[index].type == '0') {  //0 h5链接； 1 小程序链接； 2外部小程序
        this.$linkTo(`/subPages/h5/webview?link=${this.swiperList?.[index]?.linkUrl}&title=${this.swiperList?.[index]?.title}`)
      } else if (this.swiperList[index].type == '1') {
        this.$linkTo(`${this.swiperList?.[index]?.linkUrl}`)
      } else {
        wx.navigateToMiniProgram({
          appId: this.swiperList[index].linkUrl
        })
      }
    },
    getAppList() {
      this.$instance.get("/biaopin_home.json", null, { withoutToken: true }).then(res => {
        if (res.length) {
          this.appList = res
        }
      })
    },
    toService() {
      uni.switchTab({
        url: '/pages/AppModules/AppModules'
      })
    },
    openApp(row) {
      if(row.type == 1) { //type  0:内置应用, 1:外链， 2：tabbar页面
        uni.navigateToMiniProgram({appId: row.appId});
      }else if(row.type == 2) {
        uni.switchTab({url: row.modulePath})
      }else {
        uni.navigateTo({url: row.modulePath})
      }
    },
    getWeather() {
      this.$instance.post("/app/weather/queryWeather", {cityId: 101041800}, { withoutToken: true }).then(res => {
        if (res.data) {
          this.weatherInfo = res.data
          this.weatherInfo.icon = 'https://cdn.cunwuyun.cn/wuxi/home/weather-'+this.weatherInfo.wea_img+'.png'
        }
      })
    },
  },

  onShareAppMessage() {
    return {
      title: '欢迎使用基层治理~',
      path: `/pages/AppHome/AppHome`
    }
  },
}
</script>

<style scoped lang="scss">
@import "~dvcp-wui/common";

.page {
  width: 100%;
  min-height: 100%;
  background-color: #F4F5FA;
  position: relative;

  .header-content {
    .header-bg {
      background: url('https://cdn.cunwuyun.cn/wechat/wuxi/wuxi-header-bg.png') center;
      background-size: 100% 100%;
    }
    .weather-info {
      font-family: PingFangSC-Regular;
      font-weight: 400;
      font-size: 28px;
      color: #222;
      padding: 12px 34px;
      display: flex;
      img {
        width: 56px;
        height: 52px;
        margin-right: 12px;
        vertical-align: middle;
      }
      p {
        width: calc(100% - 68px);
      }
    }
    .swiper-content {
      width: 686px;
      height: 364px;
      margin: 0 auto 24px;
    }
    ::v-deep .notice {
      width: 686px;
      height: 88px;
      box-sizing: border-box;
      padding: 0 24px;
      margin: 0 auto;
      background: #FFFFFF;
      box-shadow: 0 0 8px 0 rgba(0, 0, 0, 0.02);
      border-radius: 16px;
      display: flex;
      align-items: center;
      justify-content: center;

      & > img {
        width: 128px;
        height: 52px;
      }

      .u-news-item {
        text-overflow: ellipsis;
        overflow: hidden;
        white-space: nowrap;
      }
    }
  }

  // .app-list {
  //   margin-bottom: 24px;
  // }

  .title-content {
    display: flex;
    justify-content: space-between;
    padding: 32px;
    line-height: 40px;
    h2 {
      font-family: PingFangSC-Medium;
      font-weight: 500;
      font-size: 34px;
      color: #222;
    }
    div {
      font-family: PingFangSC-Regular;
      font-size: 28px;
      color: #687DA6;
      img {
        width: 32px;
        height: 32px;
        margin-left: 6px;
        vertical-align: middle;
      }
    }
  }

  .banner-flex {
    display: flex;
    justify-content: space-between;
    padding: 0 32px;
    .item-banner {
      width: calc(50% - 16px);
      height: 160px;
      position: relative;
      img {
        width: 100%;
        height: 100%;
      }
      .banner-text {
        position: absolute;
        top: 26px;
        left: 28px;
      }
      h2 {
        font-family: PingFangSC-Medium;
        font-weight: 500;
        font-size: 30px;
        line-height: 48px;
      }
      p {
        font-family: PingFangSC-Regular;
        font-size: 22px;
        line-height: 32px;
      }
    }
    .color-3872BC {
      color: #3872BC;
    }
    .color-CE5A5A {
      color: #CE5A5A;
    }
    .color-409681 {
      color: #409681;
    }
  }

  .app-content {
    padding: 48px 16px 8px;
    // display: flex;
    .item-app {
      display: inline-block;
      width: 25%;
      text-align: center;
      margin-bottom: 20px;
      img {
        width: 96px;
        height: 96px;
      }
      p {
        font-family: PingFangSC-Regular;
        font-size: 26px;
        color: #222;
        text-align: center;
        line-height: 36px;
        margin-top: 16px;
      }
    }
    .item-app:nth-of-type(4n) {
      margin-right: 0;
    }
  }

  .banner-content {
    display: flex;
    padding: 0 32px;
    .banner-left {
      width: 322px;
      height: 462px;
      margin-right: 30px;
      position: relative;
      h2 {
        font-family: PingFangSC-SNaNpxibold;
        font-weight: 600;
        font-size: 36px;
        line-height: 48px;
        margin-bottom: 4px;
      }
      p {
        font-family: PingFangSC-Regular;
        font-size: 22px;
        line-height: 40px;
        img {
          width: 20px;
          height: 20px;
          margin-right: 8px;
        }
      }
    }
    .color-4C3FCD {
      color: #4C3FCD;
    }
    .banner-right {
      .item {
        width: 334px;
        height: 216px;
        position: relative;
        h2 {
          font-family: PingFangSC-SNaNpxibold;
          font-weight: 500;
          font-size: 30px;
          line-height: 48px;
          margin-bottom: 4px;
        }
        p {
          font-family: PingFangSC-Regular;
          font-size: 22px;
          line-height: 32px;
        }
      }
      .item:nth-of-type(1) {
        margin-bottom: 30px;
      }
      .color-DF863E {
        color: #DF863E;
      }
      .color-D9606E {
        color: #D9606E;
      }
    }
    .banner-img {
      width: 100%;
      height: 100%;
    }
    .banner-text {
      position: absolute;
      top: 26px;
      left: 28px;
    }
    
  }





  .grid-content {
    box-sizing: border-box;

    .grid-item {
      img {
        width: 108px;
        height: 108px;
        object-fit: fill;
      }

      .grid-text {
        font-size: 26px;
        font-weight: 400;
        color: #333333;
        line-height: 36px;
      }
    }
  }
  .list-wrap {
    box-sizing: border-box;
    padding: 32px;

    .list-card {
      width: 100%;
      min-height: 100px;
      background: #FFFFFF;
      box-shadow: 0 0 8px 0 rgba(0, 0, 0, 0.02);
      border-radius: 16px;
      box-sizing: border-box;
      padding: 32px;
      margin-bottom: 24px;

      .header {
        font-size: 36px;
        font-weight: 600;
        color: #333333;
        line-height: 50px;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 2;
        overflow: hidden;
      }

      .content-wrap {
        display: flex;
        gap: 4px;
        flex-wrap: wrap;
        margin-top: 24px;
        position: relative;

        .img {
          width: 100%;
          height: 350px;
        }

        .min-img {
          width: 204px;
          height: 204px;
        }

        .play-icon {
          width: 80px;
          height: 80px;
          border-radius: 50%;
          position: absolute;
          left: 50%;
          top: 50%;
          transform: translate(-50%, -50%);
        }
      }

      .bottom {
        display: flex;
        align-items: center;
        justify-content: space-between;
        margin-top: 24px;

        .left {
          display: flex;
          align-items: center;
          font-size: 28px;
          font-weight: 400;
          color: #999999;

          .tag {
            width: 144px;
            height: 48px;
            background: #EEEEEE;
            border-radius: 24px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 28px;
            font-weight: 400;
            color: #999999;
            margin-right: 16px;
          }
        }

        .right {
          font-size: 28px;
          font-weight: 400;
          color: #999999;
          display: flex;
          align-items: center;

          em {
            font-style: normal;
            color: #4181FF;
          }
        }
      }
    }
  }

  header {
    font-size: 44px;
    font-weight: 600;
    color: #333333;
    padding: 0 32px;
    margin-bottom: 32px;
  }

  .scroll-wrap {
    box-sizing: border-box;
    padding: 0 32px;
    width: 100%;
    white-space: nowrap;
    margin-bottom: 24px;

    .scroll-card {
      display: inline-block;
      width: 440px;
      height: 240px;
      box-shadow: 0 0 8px 0 rgba(0, 0, 0, 0.02);
      border-radius: 16px;
      margin-right: 32px;

      &:last-child {
        margin-right: 0;
      }
    }
  }
}
</style>
