<template>
  <section class="selectUser">
    <div class="header-middle">
      <div class="userCards" v-for="e in userList" :key="e.userId">
        <div class="imges">
          <div class="imgselect" :class="{ 'checked': e.isChecked }" @click.stop="itemCheck(e)" />
          <img src="./images/tx@2x.png" alt="" class="avatras"/>
        </div>
        <div class="rights fill">
          <div class="applicationNames">{{ e.userName }}</div>
        </div>
      </div>
      <AiEmpty description="暂无数据" v-if="!userList.length"/>
    </div>
    <div class="subBtn" @click="submit">
      <div>确定选择</div>
    </div>
  </section>
</template>

<script>
import { mapState } from 'vuex'

export default {
  // 选择创建人
  name: "selectUser",
  data() {
    return {
      selected: {},
      userList: [],
      current: 1,
    }
  },
  computed: {
    ...mapState(['user']),
    hasData() {
      return this.userList?.length > 0
    }
  },
  onLoad() {
    this.selected = uni.getStorageSync('userSelect') || []
    this.getCreateUserList()
  },
  methods: {
    isSelected(userId) {
      return this.selected.userId===userId
    },
    getCreateUserList() {
      this.userList = []
      this.$http.post(`/app/appmasssendingtask/createUserlist`).then(res => {
        if (res?.data) {
          this.userList = res.data.map(e => ({
            userId: e.create_user_id,
            userName: e.create_user_name, 
            isChecked: this.isSelected(e.create_user_id)}
          ))
        }
      })
    },
    itemCheck(row) {
      this.userList.forEach(e => e.isChecked = false)
      const index = this.userList.findIndex(o => {
        return row.userId===o.userId
      })
      this.userList[index].isChecked = true
      this.selected = this.userList[index]
    },
    submit() {
      if(!this.selected.userId) {
        return this.$u.toast('请选择创建人')
      }
      uni.setStorageSync('userSelect', this.selected)
      uni.navigateBack()
    },
  }
}
</script>

<style lang="scss" scoped>
.selectUser {
  height: 100%;
  background: #fff;

  .header-top {
    background: #fff;
    padding: 20px 32px;
  }

  .header-middle {
    padding-bottom: 140px;

    .hint {
      padding: 28px 20px 28px 32px;
      line-height: 56px;
      box-shadow: 0 1px 0 0 #e4e5e6;
      font-size: 30px;
      font-weight: 500;
      word-break: break-all;
    }

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

    .imges {
      display: flex;
      align-items: center;

      .imgselect {
        width: 48px;
        height: 48px;
        vertical-align: middle;
        background-image: url("./images/xz.png");
        background-position: center;
        background-size: 100% 100%;

        &.checked {
          background-image: url("./images/xzh.png");
        }
      }

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

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


      img {
        width: 74px;
        height: 74px;
        border-radius: 8px;
      }

      .rightes {
        width: calc(100% - 160px);
        display: flex;
        align-items: center;
        margin-left: 32px;
        border-bottom: 1px solid #e4e5e6;

        .applicationNames {
          flex: 1;
          min-width: 0;
          font-size: 36px;
          font-weight: 500;
          color: #333333;
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
        }

        .imgs {
          flex-shrink: 0;
          width: 40px;
          height: 40px;
          margin-right: 20px;
        }
      }
    }

    .userCards {
      display: flex;
      align-items: center;
      height: 120px;
      line-height: 120px;
      padding: 0 0 0 32px;

      .rights {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-left: 32px;
        border-bottom: 1px solid #e4e5e6;
        padding-right: 40px;
        height: inherit;

        .applicationNames {
          font-size: 36px;
          font-weight: 500;
          color: #333333;
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
        }

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

  .color-3F8DF5 {
    color: #3F8DF5;
  }

  .mar-h4 {
    margin: 0 4px;
  }
}
</style>
