<template>
  <section class="gridMembers">
    <div class="title">{{ detail.girdName }}</div>
    <div flex v-for="(item, index) in list" :key="index" class="listItem">
      <img class="avatar" :src="item.photo"/>
      <div class="fill">
        <AiOpenData class="memberName" v-if="item.wxUserId" type="userName" :openid="item.wxUserId"/>
        <div class="memberName" v-else v-text="item.name"/>
        <div flex>
          <div class="color-999" v-text="item.userRole"/>
          <div v-if="item.label" class="tag" v-text="item.label"/>
        </div>
      </div>
      <div class="btns">
        <div @click="gotoFamilyList(item)">责任家庭</div>
        <div @click="handleTag(item)">标签设置</div>
      </div>
    </div>
    <div class="btn" v-if="detail.girdRight == 1">
      <span class="del" @click="del" v-if="!!detail.parentGirdId">删除网格</span>
      <span class="edit" @click="edit">编辑网格</span>
    </div>
  </section>
</template>

<script>
export default {
  name: "gridMembers",
  appName: "网格成员",
  computed: {
    list() {
      let {girdMemberManageList, girdMemberList} = this.detail
      return [girdMemberManageList?.map(e => ({...e, userRole: "网格长"})) || [],
        girdMemberList?.map(e => ({
          ...e,
          userRole: "网格员"
        })) || []].flat()
    }
  },
  data() {
    return {
      detail: {}
    }
  },
  methods: {
    getDetail() {
      let {id} = this.$route.query
      this.$http.post("/app/appgirdinfo/queryDetailById", null, {
        params: {id}
      }).then(res => {
        if (res?.data) {
          this.detail = res.data
        }
      })
    },
    del() {
      this.$confirm('删除网格后，会清除网格内网格员和责任家庭信息，如有下级网格，会同步删除下级网格所有数据', `您确认要删除该网格？`).then(() => {
        this.$http.post(`/app/appgirdinfo/delete?ids=${this.detail.id}`).then((res) => {
          if (res?.code == 0) {
            this.$u.toast('删除成功!')
            this.getDetail()
          }
        })
      })
    },
    edit() {
      uni.navigateTo({url: `./AddGird?id=${this.detail.id}&fromType=edit`})
    },
    handleTag({id, label}) {
      uni.showModal({
        title: "设置标签",
        content: label,
        editable: true,
        placeholderText: "请输入标签,最多10字",
        success: res => {
          if (res.confirm) {
            if (res.content && res.content.length <= 10) {
              this.$http.post("/app/appgirdmemberinfo/updateGirdMemberLabelById", null, {
                params: {id, label: res.content}
              }).then(res => {
                if (res?.code == 0) {
                  this.$u.toast("设置成功")
                  this.getDetail()
                } else this.handleTag({id, label})
              }).catch(() => this.handleTag({id, label}))
            } else {
              this.$u.toast("请输入标签,最多10字")
              this.handleTag({id, label})
            }
          }
        }
      })
    },
    gotoFamilyList(item) {
      uni.navigateTo({url: `./FamilyList?id=${item.id}&girdId=${item.girdId}`})
    }
  },
  onShow() {
    this.getDetail()
  },
}
</script>

<style lang="scss" scoped>
.gridMembers {
  padding: 16px 32px 0;

  .color-999 {
    color: #999;
  }

  .avatar {
    width: 96px;
    height: 96px;
    margin-right: 32px;
  }

  .btns {
    margin-left: 32px;
    line-height: 48px;
    color: #26f;
  }

  .title {
    font-size: 36px;
    font-family: PingFang-SC-Heavy, PingFang-SC;
    font-weight: 800;
    color: #333;
    line-height: 50px;
    margin-bottom: 24px;
  }

  .listItem {
    padding: 20px 0;
    border-bottom: 1px solid #ddd;
  }

  .btn {
    width: 100%;
    padding: 32px;
    box-sizing: border-box;
    background-color: #fff;
    display: flex;
    position: fixed;
    bottom: 0;
    left: 0;

    span {
      display: inline-block;
      height: 92px;
      line-height: 90px;
      border-radius: 8px;
      font-size: 34px;
      font-family: PingFangSC-Medium, PingFang SC;
      font-weight: 500;
      box-sizing: border-box;
      text-align: center;
    }

    .del {
      color: #f46;
      border: 1px solid #f46;
      flex: 1;
      margin-right: 32px;
    }

    .edit {
      background-color: #3671EE;
      color: #fff;
      flex: 2;
    }
  }

  .memberName {
    height: 40px;
  }

  .tag {
    margin-left: 32px;
    padding: 4px 8px;
    border-radius: 8px;
    background: #26f;
    color: #fff;
  }
}
</style>