<template>
  <div class="list">
    <div class="area">
      <i>可选范围</i>
      <span class="separat">/</span>
      <!-- <AiArea class="aiArea" v-model="addressAreaId" mode="custom" fullname :areaRange="$areaId" :name.sync="addressArea" @change="reload">
        <div class="label" v-if="addressArea">{{ addressArea }}</div>
        <i v-else>请选择</i>
      </AiArea> -->
      <AiAreaPicker ref="area" class="aiArea" :name.sync="addressArea" :areaId="areaId" mode="custom"
                    @select="onChange">
        <span class="label" v-if="addressArea">{{ addressArea }}</span>
        <i v-else>请选择</i>
      </AiAreaPicker>
    </div>
    <div class="list-wrapper">
      <div class="tab">
        <span @click="changeTab(0)" :class="[currIndex === 0 ? 'active' : '']">已登记监测对象</span>
        <span @click="changeTab(1)" :class="[currIndex === 1 ? 'active' : '']">已消除风险对象</span>
      </div>
      <div class="search">
        <u-search
            placeholder="请输入"
            :show-action="false"
            search-icon-color="#ccc"
            v-model="name"
            @search="reload">
        </u-search>
      </div>
      <div class="list-item__wrapper">
        <div class="list-item" v-for="(item, index) in list" :key="index" @click="toDetail(item.id)">
          <image src="/static/images/avatar.png"/>
          <div class="right">
            <h2>{{ item.name || item.phone }}</h2>
            <p>{{ item.addressArea }}</p>
          </div>
        </div>
      </div>
      <AiEmpty v-if="!list.length && isMore" style="padding-bottom: 20px;"></AiEmpty>
    </div>
    <AiFixedBtn>
      <div class="addBtn iconfont iconfont-iconfangda" @tap="toAdd"></div>
    </AiFixedBtn>
  </div>
</template>

<script>

export default {
  name: 'list',

  data() {
    return {
      currIndex: 0,
      name: '',
      list: [],
      areaId: '',
      addressAreaId: '',
      addressArea: '',
      isMore: false,
      current: 1
    }
  },

  created() {
    uni.showLoading()
    this.areaId = this.$store.state.user.areaId
    this.addressAreaId = this.$store.state.user.areaId
    this.addressArea = this.$store.state.user.areaName

    this.$nextTick(() => {
      this.getList()
    })
    uni.$on('reload', () => {
      if (this.currIndex === 0) {
      } else {
      }
    })
  },

  methods: {
    toDetail(id) {
      uni.navigateTo({
        url: './MonitorDetail?id=' + id
      })
    },

    onChange(e) {
      this.addressAreaId = e.id

      this.$nextTick(() => {
        this.reload()
      })
    },

    toAdd() {
      uni.navigateTo({
        url: './MonitorAdd'
      })
    },

    reload() {
      this.isMore = false
      this.current = 1
      uni.showLoading()

      this.$nextTick(() => {
        this.getList()
      })
    },

    changeTab(index) {
      this.currIndex = index
      this.reload()
    },

    getList() {
      if (this.isMore) return

      this.$http.post(`/app/apppreventionreturntopoverty/list`, null, {
        params: {
          current: this.current,
          size: 15,
          status: this.currIndex === 1 ? '1' : '',
          addressAreaId: this.addressAreaId,
          name: this.name
        }
      }).then(res => {
        if (res.code == 0) {
          if (this.current > 1) {
            this.list = [...this.list, ...res.data.records]
          } else {
            this.list = res.data.records
          }

          uni.hideLoading()

          if (res.data.records.length < 10) {
            this.isMore = true

            return false
          }

          this.current = this.current + 1
        } else {
          uni.hideLoading()
        }
      }).catch(() => {
        uni.hideLoading()
      })
    }
  }
}
</script>

<style lang="scss">
.list {
  padding: 112px 0 120px;

  .addBtn {
    width: 96px;
    height: 96px;
    flex-shrink: 0;
    background: $uni-color-primary;
    box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
    font-size: 48px;
    color: #fff;
    border-radius: 50%;
    justify-content: center;
    align-items: center;
    display: flex;
  }

  .list-wrapper {
    margin-top: 16px;
    background: #fff;

    .list-item {
      display: flex;
      align-items: center;
      padding: 20px 32px;

      image {
        width: 80px;
        height: 80px;
        margin-right: 32px;
      }

      .right {
        flex: 1;
      }

      p {
        color: #999999;
        font-size: 26px;
      }

      h2 {
        margin-bottom: 10px;
        color: #333333;
        font-size: 32px;
      }
    }

    .search {
      margin: 20px 0;
      padding: 0 32px;
    }

    .tab {
      display: flex;
      align-items: center;
      width: 100%;
      height: 96px;
      padding: 0 60px;
      background: #FFFFFF;
      border-bottom: 1px solid #D4D4D4;
      box-sizing: border-box;

      * {
        box-sizing: border-box;
      }

      span {
        position: relative;
        flex: 1;
        height: 96px;
        line-height: 96px;
        color: #000000;
        text-align: center;
        font-size: 32px;

        &::after {
          position: absolute;
          bottom: 0;
          left: 50%;
          width: 192px;
          height: 6px;
          background: transparent;
          transform: translateX(-50%);
          content: ' ';
        }

        &.active {
          color: #1365DD;

          &::after {
            background: #1365DD;
          }
        }
      }
    }
  }

  .area {
    display: flex;
    position: fixed;
    align-items: center;
    top: 0;
    left: 0;
    z-index: 11;
    width: 100%;
    padding: 0 32px;
    height: 112px;
    color: #333333;
    font-size: 30px;
    background: #fff;

    .separat {
      padding: 0 8px;
    }

    i {
      color: #3F8DF5;
      font-size: 30px;
      font-style: normal;
    }

    span {
      color: #333333;
      font-size: 30px;
    }
  }
}
</style>
