<template>
  <section class="resident">
    <AiLoading v-if="!custom&&!error" tips="获取居民信息中..."/>
    <AiResult v-else-if="error" status="error" :tips="error"/>
    <template v-else>
      <tag-manage v-if="showTagManage"/>
      <template v-else>
        <AiTopFixed>
          <u-tabs :list="tabs" :is-scroll="false" :current="currentType" font-size="32"
                  bar-width="192" height="96" @change="handleTabClick"/>
        </AiTopFixed>
        <component :is="currentTab.comp"/>
      </template>
    </template>

  </section>
</template>

<script>
import {mapActions} from "vuex";
import Info from "./components/info";
import Document from "./components/document";
import TagManage from "./components/tagManage";

export default {
  name: "resident",
  components: {TagManage, Document, Info},
  provide() {
    return {
      top: this
    }
  },
  computed: {
    tabs() {
      return [
        {name: "居民信息", value: 0, comp: Info},
        {name: "居民档案", value: 1, comp: Document, hide: !this.detail.residentInfo},
      ].filter(e => !e.hide)
    },
    currentTab() {
      return this.tabs.find(e => e.value == this.currentType)
    },
    isNormal() {
      return !!this.$route.query.id;
    }
  },
  data() {
    return {
      currentType: 0,
      detail: {},
      showTagManage: false,
      custom: "",
      error: ""
    }
  },
  methods: {
    ...mapActions(['injectJWeixin', 'wxInvoke']),
    handleTabClick(i) {
      this.currentType = i
    },
    getContact() {
      if (this.isNormal) {
        this.getCustom(this.$route.query.id)
      } else {
        this.injectJWeixin("getCurExternalContact").then(() => {
          this.wxInvoke(['getCurExternalContact', {}, res => {
            if (res?.err_msg == 'getCurExternalContact:ok') {
              this.getCustom(res.userId)
            }
          }])
        }).catch(({errMsg}) => {
          this.error = errMsg
        })
      }
    },
    getCustom(id) {
      this.$http.post("/app/wxcp/wxcustomer/queryCustomerById", null, {
        params: {id}
      }).then(ret => {
        if (ret?.data) {
          this.custom = id
          this.detail = ret.data
          if (this.detail.type == 2) {
            this.error = "只能查看个人微信绑定的居民信息"
          }
        }
      })
    }
  },
  created() {
    this.getContact()
  }
}
</script>

<style lang="scss" scoped>
.resident {
  display: flex;
  flex-direction: column;
  height: 100%;
  background: #F5F5F5;

  .error {
    font-size: 32px;
    color: #666;
    text-align: center;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
  }

  ::v-deep .u-scroll-box {
    border-bottom: 1px solid #D4D4D4;

    .u-tab-bar {
      position: absolute;
      bottom: -6px;
    }
  }

  ::v-deep .card {
    background: #fff;
    margin-bottom: 16px;
    padding: 16px 32px;
  }

  .half {
    width: 50%;
  }

  ::v-deep .tag {
    height: 56px;
    line-height: 56px;
    background: #F3F4F7;
    border-radius: 6px;
    padding: 8px 16px;
    margin-right: 16px;
    margin-bottom: 16px;
    overflow: hidden;
    cursor: default;
  }

}
</style>
