<template>
  <div class="detail">
    <div class="tab-select">
      <div class="item" :class="tabIndex == index ? 'active' : ''" v-for="(item, index) in tabs" :key="index" @click="tabClick(index)">{{item}}<span></span></div>
    </div>
    
    <div class="area-select">
      <div class="select-content">
        <div class="area-content">
          <AiAreaPicker :areaId="user.areaId" v-model="areaId" @select="areaSelect" :name.sync="areaName">
            <img src="./img/local-icon.png" alt="">
            <span class="label" v-if="areaName">{{ areaName }}</span>
            <span v-else>请选择</span>
            <u-icon name="arrow-down" color="#666" size="24" />
          </AiAreaPicker>
        </div>
        <div class="search-input" v-if="tabIndex">
          <img src="./img/search-icon.png" alt="">
          <u-input type="text" placeholder="请输入姓名" placeholder-style="color:#999;font-size:13px;" height="18" v-model="name" @confirm="getUserList" :clearable="false" maxlength="6" />
        </div>
      </div>
    </div>

    <div v-if="!tabIndex">
      <div class="info-content">
      <div class="title">特殊人群</div>
      <div class="num-content">
        <div class="num-item" v-for="(item, index) in statisticsList" :key="index">
          <h3>{{item.value}}</h3>
          <p>{{item.label}}</p>
        </div>
        <AiEmpty v-if="!statisticsList.length"/>
      </div>
      </div>
      <div class="info-content">
        <div class="title">本月新增</div>
        <div class="num-content color-org">
          <div class="num-item" v-for="(item, index) in statisticsListMon" :key="index">
          <h3>{{item.value}}</h3>
          <p>{{item.label}}</p>
        </div>
        <AiEmpty v-if="!statisticsListMon.length"/>
        </div>
      </div>
    </div>
    <div v-else>
      <div class="item-content" v-for="(item, index) in userList" :key="index">
        <div class="title" @click="showUserType(index)">
          <h2>{{item.label}}</h2>
          <img src="./img/down-icon.png" alt="" :class="item.check ? 'img-active' : ''">
        </div>
        <div class="user-list" v-if="item.check">
          <div class="user-item" v-for="(e, indexs) in item.value" :key="indexs" v-if="item.value && item.value.length">
            <div class="user-img">
              <img src="./img/user-img.png" alt="">
            </div>
            <div class="user-info">
              <p class="name">{{e.name}}</p>
              <div class="phone">
                <span>{{idNumberInit(e.idNumber)}}</span>
                <span>{{e.phone}}</span>
              </div>
            </div>
          </div>
          <p class="text" v-if="!item.value.length">{{'暂无' + item.label + '信息'}}</p>
        </div>
      </div>
      <div class="footer" @click="toAdd">
        <div class="btn">新增特殊人群信息</div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
export default {

  data() {
    return {
      areaId: '',
      areaName: '',
      tabs: ['统计信息', '人员信息'],
      tabIndex: 0,
      addressArea: '',
      statisticsList: [],
      statisticsListMon: [],
      userList: [],
      name: ''
    }
  },
  computed: { ...mapState(['user']) },
  created() {
    this.areaId = this.user.areaId
    this.areaName = this.user.areaName
    this.getStatistic()
    this.getStatisticMon()
    this.getUserList()
    uni.$on('specialPeopleList', () => {
      this.getStatistic()
      this.getStatisticMon()
      this.getUserList()
    })
  },
  onShow() {
    document.title = '特殊人群管理'
  },

  methods: {
    areaSelect(e) {
      this.areaId = e
      this.getStatistic()
      this.getStatisticMon()
      this.getUserList()
    },
    tabClick(index) {
      this.tabIndex = index
    },
    getStatistic() {
      this.statisticsList = []
      this.$http.post(`/app/appapplicationinfo/specialStatistic?areaId=${this.areaId}&type=0&range=0`).then((res) => {
        if (res.code == 0) {
          for(let i in res.data){
            var obj = {
              label: i,
              value: res.data[i]
            }
            this.statisticsList.push(obj)
          }
        }
      })
    },
    getStatisticMon() {
      this.statisticsListMon = []
      this.$http.post(`/app/appapplicationinfo/specialStatistic?areaId=${this.areaId}&type=0&range=1`).then((res) => {
        if (res.code == 0) {
          for(let i in res.data){
            var obj = {
              label: i,
              value: res.data[i]
            }
            this.statisticsListMon.push(obj)
          }
        }
      })
    },
    getUserList() {
      this.userList = []
      this.$http.post(`/app/appapplicationinfo/queryPeople?areaId=${this.areaId}&type=0&name=${this.name}`).then((res) => {
        if (res.code == 0) {
          for(let i in res.data){
            var obj = {
              label: i,
              value: res.data[i],
              check: false
            }
            this.userList.push(obj)
          }
        }
      })
    },
    toAdd() {
      // this.$emit('change', {
      //   type: 'Add',
      // })
      uni.navigateTo({url: './add'})
    },
    idNumberInit(idNumber) {
      if(!idNumber) {
        idNumber = ''
      }else {
        idNumber = idNumber.substring(0, 5) + '******' + idNumber.substring(12, 15)
      }
      return idNumber
    },
    showUserType(index) {
      if(this.userList[index].check) {
        this.userList[index].check = false
      }else {
        this.userList.map((item) => {
          item.check = false
        })
        this.userList[index].check = true
      }

    }
  }
}
</script>

<style lang="scss" scoped>
.detail {
  .tab-select{
    width: 100%;
    height: 96px;
    line-height: 96px;
    background: #3975C6;
    display: flex;
    .item{
      flex: 1;
      text-align: center;
      font-size: 28px;
      font-family: PingFangSC-Regular, PingFang SC;
      color: #CDDCF0;
    }
    .active{
      font-family: PingFangSC-Medium, PingFang SC;
      font-weight: 500;
      position: relative;
      color: #fff;
      span{
        width: 48px;
        height: 4px;
        background: #FFF;
        position: absolute;
        bottom: 14px;
        left: 50%;
        margin-left: -24px;
      }
    }
  }
  .area-select{
    width: 100%;
    background: #FFF;
    padding-left: 32px;
    box-sizing: border-box;
    .select-content{
      width: 100%;
      padding: 24px 32px 24px 0;
      border-bottom: 1px solid #D8DDE6;
      display: flex;
      justify-content: space-between;
      font-family: PingFangSC-Regular, PingFang SC;
      color: #333;
      line-height: 44px;
      box-sizing: border-box;
      display: flex;
      .area-content{
        width: calc(100% - 402px);
        line-height: 64px;
        img{
          width: 42px;
          vertical-align: middle;
          margin-right: 16px;
        }
        .u-icon{
          margin-left: 6px;
        }
      }
      .search-input{
        width: 402px;
        height: 64px;
        background: #F5F5F5;
        border-radius: 32px;
        padding: 14px 32px 14px 64px;
        box-sizing: border-box;
        position: relative;
        img{
          width: 32px;
          height: 32px;
          margin-right: 12px;
          position: absolute;
          top: 18px;
          left: 24px;
        }
        ::v-deep .u-input{
          display: inline-block;
          width: 240px;
          font-size: 26px;
        }
      }
    }
  }
  .info-content{
    width: calc(100% - 64px);
    background: #FFF;
    border-radius: 8px;
    margin: 24px 32px 0;
    .title{
      padding-left: 32px;
      line-height: 94px;
      border-bottom: 1px solid #ddd;
      font-size: 32px;
      font-family: PingFangSC-Medium, PingFang SC;
      font-weight: 500;
      color: #333;
    }
    .num-content{
      overflow: hidden;
      .num-item{
        float: left;
        width: 33%;
        padding: 40px 0;
        text-align: center;
        h3{
          font-size: 64px;
          font-family: DINAlternate-Bold, DINAlternate;
          font-weight: bold;
          color: #3192F4;
          line-height: 74px;
          margin-bottom: 10px;
        }
        p{
          font-size: 28px;
          font-family: PingFangSC-Medium, PingFang SC;
          font-weight: 500;
          color: #999;
          line-height: 40px;
        }
      }
    }
    .color-org{
      .num-item{
        h3{
          color: #FF8700;
        }
      }
    }
  }
  .item-content{
    margin-bottom: 4px;
    .title{
      width: 100%;
      height: 108px;
      line-height: 108px;
      background: #FFF;
      padding: 0 32px;
      box-sizing: border-box;
      display: flex;
      justify-content: space-between;
      h2{
        font-size: 32px;
        font-family: PingFangSC-Semibold, PingFang SC;
        font-weight: 600;
        color: #333;
      }
      img{
        width: 34px;
        height: 34px;
        margin-top: 38px;
        transition: all .3s ease-in-out;
      }
      .img-active {
        transform: rotate(180deg);
      }
    }
    .user-list{
      background-color: #fff;
      .user-item{
        padding: 24px 32px;
        display: flex;
        .user-img{
          width: 112px;
          img{
            width: 80px;
            height: 80px;
          }
        }
        .user-info{
          width: 100%;
          .name{
            font-size: 32px;
            font-family: PingFangSC-Medium, PingFang SC;
            font-weight: 500;
            color: #333;
            line-height: 44px;
            margin-bottom: 8px;
          }
          .phone{
            width: 100%;
            font-size: 26px;
            font-family: PingFangSC-Regular, PingFang SC;
            color: #999;
            line-height: 36px;
            display: flex;
            justify-content: space-between;
          }
        }
      }
    }
  }
  .text{
    height: 96px;
    line-height: 96px;
    color: #999;
    font-size: 28px;
    text-align: center;
  }
  .footer{
    width: 100%;
    position: fixed;
    bottom: 0;
    left: 0;
  }
  .btn{
    width: 100%;
    height: 112px;
    line-height: 112px;
    text-align: center;
    background: #1365DD;
    font-size: 32px;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    color: #FFF;
  }
}
</style>
