<template>
  <div class="home-container">
    <head-bar :isBack="false">
      <template #title>
        <div style="width: 90%">
          <div class="rightSearch">
            <div class="searchImg" @click="search(undefined)">
              <img src="../../assets/images/home/search.png" alt="" />
            </div>
            <el-input
              v-model="courseName"
              auto-complete="off"
              placeholder="搜索课程..."
              @input="search(undefined)"
            ></el-input>
          </div>
        </div>
        <div class="exit" @click="exitLogin">退出</div>
      </template>
    </head-bar>
    <div class="home-box">
      <class-filter @getAll="search" :zoneClass.sync="zoneClass"> </class-filter>
      <van-pull-refresh v-model="isLoading" @refresh="onRefresh" class="rheight">
        <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad" class="vheight">
          <!-- <course-all :courseList="zoneClass"></course-all> -->
          <van-empty description="暂无课程" v-if="!zoneClass.length" />
          <course-item v-for="(c, cIndex) in zoneClass" :key="cIndex" class="courseItem" :c="c"></course-item>
        </van-list>
      </van-pull-refresh>
    </div>
  </div>
</template>

<script>
import { selectTypeCourse2 } from '@/api/home'
import { loginOut } from '@/api/user'
import { mapGetters } from 'vuex'
import headBar from '@/components/headBar.vue'
import classFilter from './components/classFilter.vue'
import courseItem from './components/courseItem.vue'
// import courseAll from './components/courseAll.vue'
export default {
  components: {
    headBar,
    classFilter,
    courseItem
  },
  data() {
    return {
      courseName: '',
      zoneClass: [],
      typea: '',
      typeb: '',
      typec: '',
      page: 1,
      isLoading: false,
      loading: false,
      finished: true
    }
  },
  computed: {
    ...mapGetters(['userinfo'])
  },
  methods: {
    search(tList) {
      this.page = 1
      if (tList !== undefined) {
        var typeList = Object.values(tList)
        this.typea = typeList[0]
        this.typeb = typeList[2]
        this.typec = typeList[1]
      }
      this.selectAll(true)
    },
    onRefresh() {
      this.page = 1
      this.finished = false
      this.selectAll(true)
    },
    onLoad() {
      this.page++
      this.selectAll()
    },
    async selectAll(isRefresh) {
      const params = {
        uid: this.userinfo.userid,
        oid: this.userinfo.organizeid,
        typea: this.typea !== undefined ? this.typea : '',
        typeb: this.typeb !== undefined ? this.typeb : '',
        typec: '',
        typed: this.typec !== undefined ? this.typec : '',
        typeE: '',
        cu: '',
        cn: this.courseName,
        classid: this.userinfo.classid,
        org: this.userinfo.org,
        page: this.page,
        pageSize: 10
      }
      const res = await selectTypeCourse2(params)
      if (isRefresh) {
        this.isLoading = false
        // 下拉刷新
        this.zoneClass = res[0]
      } else {
        this.loading = false
        // 上拉加载
        this.zoneClass = [...this.zoneClass, ...res[0]]
      }
      this.$emit('update:list', this.zoneClass)
      if (res[0].length === 0) {
        // 证明没有下一页数据了
        this.finished = true
      } else {
        this.finished = false
      }
    },
    exitLogin() {
      this.$dialog({
        message: '是否退出' + this.userinfo.username + '账号',
        showCancelButton: true,
        beforeClose: (action, done) => {
          if (action === 'confirm') {
            loginOut()
              .then(res => {
                this.$toast({
                  message: '退出成功',
                  type: 'success'
                })
                this.$store.dispatch('user/logout')
                window.location.reload()
                done()
              })
              .catch(err => {
                console.error(err)
                done()
              })
          } else {
            // 拦截取消操作
            done()
          }
        }
      })
    }
  },
  mounted() {
    this.selectAll()
  }
}
</script>

<style lang="scss" scoped>
.home-container {
  height: 100%;
  overflow: hidden;
  .home-box {
    height: calc(100% - 55px);
    margin-top: 55px;
    overflow: hidden;
  }
  .rheight {
    height: calc(100% - 40px);
    overflow: auto;
    background: #e8ebf2;

    /deep/.van-pull-refresh__track {
      height: 100%;
    }
  }
  .vheight {
    padding: 10px 0;
    // height: 100%;
    // overflow: auto;
  }

  // .courseBox {
  // background: #e8ebf2;
  // padding: 15px 0;
  .courseItem {
    background: #fff;
    width: 90%;
    margin: 0 auto 15px;
    border-radius: 10px;
    box-shadow: 0 0 10px 1px #dcdfe6;
    padding: 10px;
  }
  .exit {
    color: #fff;
    margin-left: 10px;
    font-size: 15px;
    min-width: 40px;
  }
  // }
}

/deep/.rightSearch {
  display: flex;
  flex-direction: row;
  width: 100%;
  height: 30px;
  position: relative;

  .searchImg {
    width: 20px;
    height: 20px;
    position: absolute;
    z-index: 9;
    top: 5px;
    left: 5px;
    > img {
      width: 100%;
      height: 100%;
    }
  }
  .el-input__inner {
    width: 100%;
    height: 30px;
    text-indent: 15px;
    line-height: 30px;
  }
}
</style>
