<template>
  <div class="selectType">
    <div class="header-top" v-if="!showType">
      <u-search v-model="keyword" placeholder="请输入姓名" :show-action="false" bg-color="#F5F5F5" search-icon-color="#CCCCCC" placeholder-color="#999999" @search="handerSearch" @clear="handerClear"></u-search>
    </div>

    <div class="header-middle">
      <div class="hint">
        <span :style="{ color: applicationName ? '#3F8DF5' : '' }" @click="back">可选范围</span>
        <span> {{ applicationName ? ' / ' + applicationName : '' }}</span>
      </div>

      <div class="showTypes" v-if="showType">
        <div class="empty-div"></div>

        <div v-if="typeList.length > 0">
          <div class="cards" v-for="(item, i) in typeList" :key="i" @click="toUserSelect(item)">
            <img src="./img/tx@2x.png" alt="" />

            <div class="rightes">
              <div class="applicationNames">{{ item.applicationName }}</div>

              <img src="./img/2.png" alt="" class="imgs" />
            </div>
          </div>
        </div>

        <AiEmpty description="暂无数据" class="emptyWrap" v-else></AiEmpty>
      </div>

      <div class="showUsers" v-else>
        <div v-if="userList.length > 0">
          <div class="cards" v-for="(e, index) in userList" :key="index">
            <div class="imges">
              <img src="./img/xzh.png" alt="" class="imgselect" v-if="e.isChecked" @click="userClick(e, index)" />
              <img src="./img/xz.png" alt="" class="imgselect" v-else @click="userClick(e, index)" />

              <img src="./img/tx@2x.png" alt="" class="avatras" />
            </div>

            <div class="rights">
              <div class="applicationNames">{{ e.name }}</div>
              <div class="idNumbers">{{ e.idNumber && e.idNumber.replace(/^(\w{6})\w{8}(.*)$/, '$1******$2') }}</div>
            </div>
          </div>
        </div>

        <AiEmpty description="暂无数据" class="emptyWrap" v-else></AiEmpty>
      </div>
    </div>

    <div class="subBtn" @click="submit">
      <div>确定选择</div>
    </div>

    <!-- <AiBck /> -->
  </div>
</template>

<script>
export default {
  name: 'selectType',
  components: {},
  props: {},
  data() {
    return {
      typeList: [],
      userList: [],
      keyword: '',
      current: 1,
      showType: true,
      applicationName: '',
      applicationId: '',
      selectUser: {},
    }
  },
  computed: {},
  watch: {},
  onLoad() {
    this.getTypeList()
  },
  onShow() {},
  methods: {
    userClick(row, index) {
      if (this.userList[index].isChecked) {
        this.userList[index].isChecked = false //取消
        this.selectUser = {}
      } else {
        this.userList.map((item) => {
          item.isChecked = false
        })
        this.userList[index].isChecked = true
        this.selectUser = row
      }
    },

    getTypeList() {
      this.userList = []
      this.$http.post(`/app/appapplicationinfo/queryApplicationListByType?type=0&status=1`).then((res) => {
        if (res.code == 0) {
          this.typeList = res.data
        }
      })
    },

    // getUser(data) {
    //   this.applicationId = data.id
    //   this.appId = data.id
    //   this.getUsers()
    // },

    getUsers() {
      uni.showLoading({
        title: '加载中',
      })
      this.$http.post(`/app/appapplicationinfo/list?appId=${this.appId}&current=${this.current}&size=${999}`, { searchParam: this.keyword }).then((res) => {
        if (res.code == 0) {
          res.data.records.map((item) => {
            item.isChecked = false
          })
          uni.hideLoading()
          this.userList = res.data.records
        }
      })
    },

    submit() {
      if (this.selectUser.id != null) {
        uni.$emit('goBack', { selectUser: this.selectUser, applicationName: this.applicationName, applicationId: this.applicationId })
        uni.navigateBack()
      } else {
        return this.$u.toast('请选择人员')
      }
    },

    toUserSelect(item) {
      this.applicationName = item.applicationName
      this.applicationId = item.id
      this.appId = item.id
      this.getUsers()

      this.showType = false
    },

    back() {
      this.keyword = ''
      this.typeList = []
      this.userList = []
      this.applicationName = ''
      this.showType = true
      this.getTypeList()
    },

    handerSearch(e) {
      if (!this.showType) {
        this.keyword = e
        this.current = 1
        // this.getUser()
        this.getUsers()
      }
    },

    handerClear() {
      this.keyword = ''
      this.current = 1
      this.getUsers()
    },
  },
}
</script>

<style scoped lang="scss">
.selectType {
  height: 100%;
  background: #fff;
  .header-top {
    background: #fff;
    padding: 20px 32px;
  }

  .header-middle {
    padding-bottom: 140px;
    .hint {
      padding: 0 20px 0 32px;
      height: 112px;
      line-height: 112px;
      box-shadow: 0px 1px 0px 0px #e4e5e6;
      font-size: 30px;
      font-weight: 500;
    }

    .showTypes {
      .empty-div {
        height: 16px;
        background: #f5f5f5;
      }

      .cards {
        display: flex;
        align-items: center;
        height: 120px;
        line-height: 120px;
        // background: pink;
        padding: 0 0 0 32px;

        img {
          width: 74px;
          height: 74px;
          border-radius: 8px;
        }
        .rightes {
          width: 100%;
          display: flex;
          justify-content: space-between;
          align-items: center;
          margin-left: 32px;
          border-bottom: 1px solid #e4e5e6;
          .applicationNames {
            font-size: 36px;
            font-weight: 500;
            color: #333333;
          }
          .imgs {
            width: 40px;
            height: 40px;
            margin-right: 20px;
          }
        }
      }
    }

    .showUsers {
      .cards {
        display: flex;
        align-items: center;
        height: 120px;
        line-height: 120px;
        // background: pink;
        padding: 0 0 0 32px;

        .imges {
          display: flex;
          align-items: center;
          width: 200px;
          .imgselect {
            width: 48px;
            height: 48px;
          }

          .avatras {
            width: 74px;
            height: 74px;
            border-radius: 8px;
            margin-left: 36px;
          }
        }

        .rights {
          width: 100%;
          display: flex;
          justify-content: space-between;
          align-items: center;
          margin-left: 32px;
          border-bottom: 1px solid #e4e5e6;
          padding-right: 40px;
          .applicationNames {
            font-size: 36px;
            font-weight: 500;
            color: #333333;
          }
          .idNumbers {
            color: #666;
          }
        }
      }
    }
  }

  .subBtn {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 118px;
    background: #f4f8fb;
    div {
      width: 192px;
      height: 80px;
      line-height: 80px;
      text-align: center;
      background: #1365dd;
      border-radius: 4px;
      font-size: 32px;
      color: #fff;
      margin: 20px 34px 0 0;
      float: right;
    }
  }
}
</style>
