<template>
  <div class="AppSpecialPeople">
    <template v-if="hasGridPermit">
      <u-navbar back-icon-color="#000" title="特殊人群" title-color="#000" title-width="300" title-size="32" 
        :title-bold="true" :background="backgroundNavbar" :is-fixed="true" height="44" z-index="999"></u-navbar>
      <div class="header-content-bg">
        <img src="https://cdn.sinoecare.com/i/2024/07/12/6690a1303d423.png" alt="">
      </div>
      <div :class="!tabIndex ? 'top-tabs border-r' : 'top-tabs'" :style="{'top': `${statusBarHeight+50}px`}">
        <u-tabs :list="tabs" :is-scroll="false" :current="tabIndex" height="96" inactive-color="#222"
          active-color="#1D2229" :bar-style="barStyle"  font-size="34" @change="tabClick"></u-tabs>
        <div class="select-content">
          <!-- <div class="search-input">
            <img src="./img/search-icon.png" alt=""/>
            <u-input type="text" placeholder="请输入姓名" placeholder-style="color:#999;font-size:13px;" height="18"
              v-model="search.name" @confirm="getUserList" :clearable="false" maxlength="6"/>
          </div> -->
          <div class="search-input" v-if="tabIndex">
            <u-search placeholder="请输入姓名" v-model="search.name" :show-action="false" bg-color="#F4F5FA" search-icon-color="#666" 
              color="#666" height="72" @search="(page1.current = 1), getList()" ></u-search>
          </div>
          <div class="area-content fill">
            <AiPagePicker type="gird" @select="handleGridSelect">
              <AiMore v-model="gridName" placeholder="选择网格"/>
            </AiPagePicker>
          </div>
        </div>
      </div>
      <!-- <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 v-if="!tabIndex" class="statistic-content">
        <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 v-text="item.value"/>
              <p v-text="item.label"/>
            </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 class="list-content">
        <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" @click="showDetail(e)">
              <div class="user-img">
                <img src="./img/user-img.png" alt=""/>
              </div>
              <div class="user-info">
                <p class="name">{{ e.name }}
                  <span class="btn-icon" v-if="isAdmin || user.id == e.create_user_id">
                  <img src="./img/edit-icon.png" alt="" @click.stop="toEdit(e,item.dictValue)">
                  <img src="./img/del-icon.png" alt="" @click.stop="del(e,item.dictValue)">
                </span>
                </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>
    </template>
    <AiEmpty description="没有网格员权限<br>无法查看特殊人员信息~" v-else style="text-align:center;"/>
  </div>
</template>

<script>
import {mapState} from "vuex";

export default {
  name: 'AppSpecialPeople',
  appName: '特殊人群',
  customNavigation: true,
  data() {
    return {
      areaName: '',
      tabs: [
        {
          name: "统计信息",
        },
        {
          name: "人员信息",
        },
      ],
      tabIndex: 0,
      addressArea: '',
      statisticsList: [],
      statisticsListMon: [],
      userList: [],
      gridName: "",
      search: {name: ''},
      backgroundNavbar: {
        background: 'url(https://cdn.sinoecare.com/i/2024/07/12/6690a1309c7d3.png) no-repeat',
        backgroundSize: 'cover',
      },
      barStyle: {
        'width': '20px',
        'height': '4px',
        'border-radius': '3px',
        'bottom': '-4px',
        'background': '#026AF2'
      },
      statusBarHeight: uni.getSystemInfoSync().statusBarHeight,
    }
  },
  computed: {
    ...mapState(['user']),
    hasGridPermit() {
      return this.user.girdInfos2G && this.user.girdInfos2G.length
    },
    isAdmin() {
      return this.user.girdInfos2G && this.user.girdInfos2G.length
    },
  },
  onLoad() {
    if (this.hasGridPermit) {
      if (!this.search.girdId) {
        this.search.girdId = this.user.girdId
        this.gridName = this.user.girdName
      }
      this.$dict.load('appSpecialTypeFive').then(() => {
        this.getDatas()
      })
    }
  },
  onShow() {
    uni.setNavigationBarColor({
      frontColor: 'black',
      backgroundColor: '#ffffff'
    })
  },
  methods: {
    getDatas() {
      this.getStatistic()
      this.getStatisticMon()
      this.getUserList()
      this.$forceUpdate()
    },
    handleGridSelect(e) {
      this.gridName = e?.girdName
      this.search.girdId = e?.id
      this.getDatas()
    },
    areaSelect(e) {
      this.search.areaId = e
      this.getDatas()
    },
    tabClick(index) {
      this.tabIndex = index
    },
    getStatistic() {
      this.statisticsList = []
      this.$instance.post(`/app/appapplicationinfo/specialStatistic`, null, {
        params: {...this.search, type: 0, range: 0}
      }).then((res) => {
        if (res?.data) {
          this.statisticsList = Object.keys(res.data).map((label, i, arr) => ({label, value: res.data[label]}))
        }
      })
    },
    getStatisticMon() {
      this.statisticsListMon = []
      this.$instance.post(`/app/appapplicationinfo/specialStatistic`, null, {
        params: {...this.search, type: 0, range: 1}
      }).then((res) => {
        if (res?.data) {
          this.statisticsListMon = Object.keys(res.data).map((label, i, arr) => ({label, value: res.data[label]}))
        }
      })
    },
    getUserList() {
      this.userList = []
      this.$instance.post(`/app/appapplicationinfo/queryPeople`, null, {
        params: {...this.search, type: 0, status: 1}
      }).then((res) => {
        if (res?.data) {
          this.userList = Object.keys(res.data).map(e => ({
            label: e,
            value: res.data[e],
            check: false,
          }))
        }
      })
    },
    toAdd() {
      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
      }
    },
    toEdit(row) {
      uni.navigateTo({
        url: `./add?id=${row.id}&appId=${row.appId}`
      })
    },
    del(row) {
      this.$confirm('确定删除该数据?').then(() => {
        uni.showLoading({})
        let {id: ids, appId} = row
        this.$instance.post("/app/appapplicationinfo/delete", null, {
          params: {ids, appId}
        }).then((res) => {
          if (res?.code == 0) {
            this.$u.toast('删除成功！')
            this.getUserList()
          }
          uni.hideLoading()
        })
      }).catch(() => {
      })
    },
    showDetail(e) {
      uni.navigateTo({url: `/mods/AppPeopleList/DetailCard?id=${e.resident_id}`})
    }
  },
}
</script>

<style lang="scss" scoped>
.AppSpecialPeople {
  min-height: 100%;
  .header-content-bg {
    width: 100%;
    position: relative;
    img {
      width: 100%;
      height: 592px;
      position: absolute;
      z-index: -1;
    }
  }

  .top-fixed {
    width: 100%;
    position: fixed;
    left: 32px;
    z-index: 999;
  }
  
  .top-tabs {
    width: calc(100% - 64px);
    box-sizing: border-box;
    position: fixed;
    left: 32px;

    .select-content {
      width: 100%;
      padding: 0 32px;
      box-sizing: border-box;
      background-color: #fff;
      .search-input {
        width: 100%;
        padding: 28px 0;
      }
      .area-content {
        width: 100%;
        padding: 26px 0;
        box-sizing: border-box;
        font-family: PingFangSC-Regular;
        font-size: 32px;
        color: #222;
        ::v-deep u-icon, 
        ::v-deep .u-icon {
          width: 100%;
        }
        ::v-deep .u-icon__label {
          display: inline-block;
          width: calc(100% - 32px);
        }
      }

      
    }
  }

  ::v-deep .u-tabs {
    border-bottom: 1px solid #eee;
    border-top-left-radius: 16px;
    border-top-right-radius: 16px;
  }

  .border-r {
    .select-content {
      border-bottom-left-radius: 16px;
      border-bottom-right-radius: 16px;
    }
  }

  .statistic-content {
    padding: 216px 0 32px 0;
  }

  .info-content {
    width: calc(100% - 64px);
    background: #fff;
    border-radius: 32px;
    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;
        }
      }
    }
  }

  .list-content {
    width: calc(100% - 64px);
    // padding: 32px 0;
    margin: 320px 0 188px 32px;
    height: calc(100% - 640px);
    padding-bottom: 32px;
    box-sizing: border-box;
    background-color: #fff;
    border-bottom-left-radius: 16px;
    border-bottom-right-radius: 16px;
  }

  .item-content {
    margin-bottom: 4px;
    border-bottom: 1px solid #eee;

    .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 0.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;

            .btn-icon {
              float: right;

              img {
                width: 48px;
                height: 48px;
                margin-left: 20px;
                vertical-align: baseline;
              }
            }
          }

          .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;
    background-color: #F4F5FA;
    padding: 32px 64px 68px 64px;
    z-index: 9;
    .btn {
      width: calc(100% - 128px);
      line-height: 88px;
      background: #026AF2;
      border-radius: 44px;
      font-family: PingFangSC-Medium;
      font-weight: 500;
      font-size: 34px;
      color: #FFF;
      text-align: center;
    }
  }


}
</style>
